1#!/usr/bin/env ruby 2# Copyright (c) 2021 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 15require 'optparse' 16require 'ostruct' 17require 'yaml' 18 19def data_instructions(data) 20 data['groups'].flat_map { |g| g['instructions'] } 21end 22 23options = OpenStruct.new 24optparser = OptionParser.new do |opts| 25 opts.banner = 'Usage: combine.rb [options]' 26 27 opts.on('-d', '--data FILE1,FILE2,...', Array, 'List of source data files in YAML format') 28 opts.on('-o', '--output FILE', 'Output file (default is stdout)') 29 30 opts.on('-h', '--help', 'Prints this help') do 31 puts opts 32 exit 33 end 34end 35optparser.parse!(into: options) 36 37exit unless options.data 38exit if options.data.empty? 39 40# TODO: Unify check with combining loop when we have assigned opcodes 41options.data.drop(1).each do |path| 42 tmp_data = YAML.load_file(File.expand_path(path)) 43 # check that all instructions are prefixed: 44 instructions = data_instructions(tmp_data) 45 raise 'Plugged in instructions must be prefixed' unless instructions.reject { |i| i['prefix'] }.empty? 46end 47 48# TODO: Remove when we have assigned opcodes: 49options.data = options.data.rotate # put plug-in prefixes first 50 51data = YAML.load_file(File.expand_path(options.data.first)) 52options.data.drop(1).each do |path| 53 tmp_data = YAML.load_file(File.expand_path(path)) 54 %w[prefixes groups properties exceptions verification version min_version chapters].each do |attr| 55 if data[attr] 56 data[attr] += tmp_data[attr] if tmp_data[attr] 57 else 58 data[attr] = tmp_data[attr] 59 end 60 end 61end 62 63output = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout 64output.write(data.to_yaml) 65output.close 66