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