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_cioptions = OpenStruct.new
22b1994897Sopenharmony_cioptparser = OptionParser.new do |opts|
23b1994897Sopenharmony_ci  opts.banner = 'Usage: gen_ruby.rb [options]'
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ci  opts.on('-d', '--data FILE', 'Logger data file in YAML format')
26b1994897Sopenharmony_ci  opts.on('-p', '--plugins FILE', 'Plugins data file in YAML format')
27b1994897Sopenharmony_ci  opts.on('-o', '--output FILE', 'Output file (default is stdout)')
28b1994897Sopenharmony_ci
29b1994897Sopenharmony_ci  opts.on('-h', '--help', 'Prints this help') do
30b1994897Sopenharmony_ci    puts opts
31b1994897Sopenharmony_ci    exit
32b1994897Sopenharmony_ci  end
33b1994897Sopenharmony_ciend
34b1994897Sopenharmony_cioptparser.parse!(into: options)
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_ciexit unless options.data
37b1994897Sopenharmony_ciexit unless options.plugins
38b1994897Sopenharmony_ci
39b1994897Sopenharmony_cidata = YAML.load_file(File.expand_path(options.data))
40b1994897Sopenharmony_ciplugins_data = YAML.load_file(File.expand_path(options.plugins))
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_ciif plugins_data['plugins']
43b1994897Sopenharmony_ci  plugins_data['plugins'].each do |plugins|
44b1994897Sopenharmony_ci    name = plugins.keys.first
45b1994897Sopenharmony_ci    plugin_data = plugins[name]['logger']
46b1994897Sopenharmony_ci    data['components'] += plugin_data['components'] if plugin_data
47b1994897Sopenharmony_ci  end
48b1994897Sopenharmony_ciend
49b1994897Sopenharmony_ci
50b1994897Sopenharmony_cioutput = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout
51b1994897Sopenharmony_cioutput.write(data.to_yaml)
52b1994897Sopenharmony_cioutput.close
53