• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2# Copyright (c) 2021-2024 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
42if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
43  data = YAML.load_file(File.expand_path(options.data.first))
44else
45  data = YAML.load_file(File.expand_path(options.data.first), aliases: true)
46end
47
48options.data.drop(1).each do |plugin_path|
49  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
50    plugin_data = YAML.load_file(File.expand_path(plugin_path))
51  else
52    plugin_data = YAML.load_file(File.expand_path(plugin_path), aliases: true)
53  end
54  # check that all instructions are prefixed:
55  instructions = data_instructions(plugin_data)
56  raise 'Plugged in instructions must be prefixed' unless instructions.reject { |i| i['prefix'] }.empty?
57
58  plugin_data.each_key do |attr|
59    raise "Uknown data property: #{attr}" unless data.key?(attr)
60
61    if data[attr].is_a? Hash
62      data[attr] = data[attr].merge(plugin_data[attr])
63    else
64      data[attr] += plugin_data[attr]
65    end
66  end
67end
68
69output = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout
70output.write(data.to_yaml)
71output.close
72