1b1994897Sopenharmony_ci#!/usr/bin/env ruby 2b1994897Sopenharmony_ci# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3b1994897Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License"); 4b1994897Sopenharmony_ci# you may not use this file except in compliance with the License. 5b1994897Sopenharmony_ci# You may obtain a copy of the License at 6b1994897Sopenharmony_ci# 7b1994897Sopenharmony_ci# http://www.apache.org/licenses/LICENSE-2.0 8b1994897Sopenharmony_ci# 9b1994897Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software 10b1994897Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS, 11b1994897Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12b1994897Sopenharmony_ci# See the License for the specific language governing permissions and 13b1994897Sopenharmony_ci# limitations under the License. 14b1994897Sopenharmony_ci 15b1994897Sopenharmony_ci# Huawei Technologies Co.,Ltd. 16b1994897Sopenharmony_ci 17b1994897Sopenharmony_cirequire 'optparse' 18b1994897Sopenharmony_cirequire 'ostruct' 19b1994897Sopenharmony_cirequire 'yaml' 20b1994897Sopenharmony_ci 21b1994897Sopenharmony_cidef data_instructions(data) 22b1994897Sopenharmony_ci data['groups'].flat_map { |g| g['instructions'] } 23b1994897Sopenharmony_ciend 24b1994897Sopenharmony_ci 25b1994897Sopenharmony_cioptions = OpenStruct.new 26b1994897Sopenharmony_cioptparser = OptionParser.new do |opts| 27b1994897Sopenharmony_ci opts.banner = 'Usage: combine.rb [options]' 28b1994897Sopenharmony_ci 29b1994897Sopenharmony_ci opts.on('-d', '--data FILE1,FILE2,...', Array, 'List of source data files in YAML format') 30b1994897Sopenharmony_ci opts.on('-o', '--output FILE', 'Output file (default is stdout)') 31b1994897Sopenharmony_ci 32b1994897Sopenharmony_ci opts.on('-h', '--help', 'Prints this help') do 33b1994897Sopenharmony_ci puts opts 34b1994897Sopenharmony_ci exit 35b1994897Sopenharmony_ci end 36b1994897Sopenharmony_ciend 37b1994897Sopenharmony_cioptparser.parse!(into: options) 38b1994897Sopenharmony_ci 39b1994897Sopenharmony_ciexit unless options.data 40b1994897Sopenharmony_ciexit if options.data.empty? 41b1994897Sopenharmony_ci 42b1994897Sopenharmony_cidata = YAML.load_file(File.expand_path(options.data.first)) 43b1994897Sopenharmony_cioptions.data.drop(1).each do |plugin_path| 44b1994897Sopenharmony_ci plugin_data = YAML.load_file(File.expand_path(plugin_path)) 45b1994897Sopenharmony_ci # check that all instructions are prefixed: 46b1994897Sopenharmony_ci instructions = data_instructions(plugin_data) 47b1994897Sopenharmony_ci raise 'Plugged in instructions must be prefixed' unless instructions.reject { |i| i['prefix'] }.empty? 48b1994897Sopenharmony_ci 49b1994897Sopenharmony_ci plugin_data.each_key do |attr| 50b1994897Sopenharmony_ci raise "Uknown data property: #{attr}" unless data.key?(attr) 51b1994897Sopenharmony_ci 52b1994897Sopenharmony_ci data[attr] += plugin_data[attr] 53b1994897Sopenharmony_ci end 54b1994897Sopenharmony_ciend 55b1994897Sopenharmony_ci 56b1994897Sopenharmony_cioutput = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout 57b1994897Sopenharmony_cioutput.write(data.to_yaml) 58b1994897Sopenharmony_cioutput.close 59