• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env ruby
2# Copyright (c) 2022-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 'json'
18require 'optparse'
19require 'set'
20require 'yaml'
21
22def check_option(optparser, options, key)
23  return if options[key]
24
25  puts "Missing option: --#{key}"
26  puts optparser
27  exit false
28end
29
30def check_version
31  major, minor, = RUBY_VERSION.split('.').map(&:to_i)
32  major > 2 || (major == 2 && minor >= 5)
33end
34
35raise "Update your ruby version, #{RUBY_VERSION} is not supported" unless check_version
36
37options = OpenStruct.new
38
39optparser = OptionParser.new do |opts|
40  opts.banner = 'Usage: merge.rb [options]'
41
42  opts.on('-d', '--data foo.yaml,bar.yaml,baz.yaml', Array, 'List of source data in YAML format (required)')
43  opts.on('-o', '--output FILE', 'Output file (default is stdout)')
44
45  opts.on('-h', '--help', 'Prints this help') do
46    puts opts
47    exit
48  end
49end
50optparser.parse!(into: options)
51
52check_option(optparser, options, :data)
53
54# Merge yamls into 'options_hash'
55options_hash = Hash.new("")
56options.data.each do |options_yaml|
57  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
58    data = YAML.load_file(File.expand_path(options_yaml))
59  else
60    data = YAML.load_file(File.expand_path(options_yaml), aliases: true)
61  end
62  data = JSON.parse(data.to_json)
63  data["options"].each do |option|
64    name = option["name"]
65    if !options_hash.has_key?(name)
66      options_hash[name] = option
67      next
68    end
69
70    assert_eq = -> (key) {
71      raise "Option '#{name}' conflicts for key '#{key}'" unless options_hash[name][key] == option[key]
72    }
73
74    merge_arrays = -> (key) {
75      options_hash[name][key] = (Set.new(options_hash[name][key]) + Set.new(option[key])).to_a
76    }
77
78    assert_eq.call("type")
79    assert_eq.call("description")
80
81    if !option["possible_values"].nil?
82      merge_arrays.call("possible_values")
83    end
84
85    if !option["lang"].nil?
86      merge_arrays.call("lang")
87    end
88
89    if !option["default"].nil?
90      if option["type"] == "arg_list_t"
91        merge_arrays.call("default")
92      else
93        assert_eq.call("default")
94      end
95    end
96  end
97end
98
99# Dump resulted 'options_hash' to yaml output
100if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.1.0')
101  data = YAML.load_file(File.expand_path(options.data[0]))
102else
103  data = YAML.load_file(File.expand_path(options.data[0]), aliases: true)
104end
105data = JSON.parse(data.to_json)
106data["options"] = options_hash.values
107output = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout
108output_yaml = YAML.dump(JSON.load(data.to_json))
109output.write(output_yaml)
110output.close
111