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 'yaml'
19b1994897Sopenharmony_cirequire 'json'
20b1994897Sopenharmony_cirequire 'erb'
21b1994897Sopenharmony_ci
22b1994897Sopenharmony_ci# Extend Module to implement a decorator for ISAPI methods
23b1994897Sopenharmony_ciclass Module
24b1994897Sopenharmony_ci  def cached(method_name)
25b1994897Sopenharmony_ci    definer = instance_methods.include?(method_name) ? :define_method : :define_singleton_method
26b1994897Sopenharmony_ci    noncached_method = instance_method(method_name)
27b1994897Sopenharmony_ci    send(definer, method_name) do
28b1994897Sopenharmony_ci      unless instance_variable_defined? "@#{method_name}"
29b1994897Sopenharmony_ci        instance_variable_set("@#{method_name}", noncached_method.bind(self).call)
30b1994897Sopenharmony_ci      end
31b1994897Sopenharmony_ci      instance_variable_get("@#{method_name}").freeze
32b1994897Sopenharmony_ci    end
33b1994897Sopenharmony_ci  end
34b1994897Sopenharmony_ciend
35b1994897Sopenharmony_ci
36b1994897Sopenharmony_ci# Gen.on_require will be called after requiring scripts.
37b1994897Sopenharmony_ci# May (or even should) be redefined in scripts.
38b1994897Sopenharmony_cimodule Gen
39b1994897Sopenharmony_ci  def self.on_require(data); end
40b1994897Sopenharmony_ciend
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_cidef create_sandbox
43b1994897Sopenharmony_ci  # nothing but Ruby core libs and 'required' files
44b1994897Sopenharmony_ci  binding
45b1994897Sopenharmony_ciend
46b1994897Sopenharmony_ci
47b1994897Sopenharmony_cidef check_option(optparser, options, key)
48b1994897Sopenharmony_ci  return if options[key]
49b1994897Sopenharmony_ci
50b1994897Sopenharmony_ci  puts "Missing option: --#{key}"
51b1994897Sopenharmony_ci  puts optparser
52b1994897Sopenharmony_ci  exit false
53b1994897Sopenharmony_ciend
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_cidef check_version
56b1994897Sopenharmony_ci  major, minor, = RUBY_VERSION.split('.').map(&:to_i)
57b1994897Sopenharmony_ci  major > 2 || (major == 2 && minor >= 5)
58b1994897Sopenharmony_ciend
59b1994897Sopenharmony_ci
60b1994897Sopenharmony_ciraise "Update your ruby version, #{RUBY_VERSION} is not supported" unless check_version
61b1994897Sopenharmony_ci
62b1994897Sopenharmony_cioptions = OpenStruct.new
63b1994897Sopenharmony_ci
64b1994897Sopenharmony_cioptparser = OptionParser.new do |opts|
65b1994897Sopenharmony_ci  opts.banner = 'Usage: gen.rb [options]'
66b1994897Sopenharmony_ci
67b1994897Sopenharmony_ci  opts.on('-t', '--template FILE', 'Template for file generation (required)')
68b1994897Sopenharmony_ci  opts.on('-d', '--data FILE', 'Source data in YAML format (required)')
69b1994897Sopenharmony_ci  opts.on('-o', '--output FILE', 'Output file (default is stdout)')
70b1994897Sopenharmony_ci  opts.on('-a', '--assert FILE', 'Go through assertions on data provided and exit')
71b1994897Sopenharmony_ci  opts.on('-r', '--require foo,bar,baz', Array, 'List of files to be required for generation')
72b1994897Sopenharmony_ci
73b1994897Sopenharmony_ci  opts.on('-h', '--help', 'Prints this help') do
74b1994897Sopenharmony_ci    puts opts
75b1994897Sopenharmony_ci    exit
76b1994897Sopenharmony_ci  end
77b1994897Sopenharmony_ciend
78b1994897Sopenharmony_cioptparser.parse!(into: options)
79b1994897Sopenharmony_ci
80b1994897Sopenharmony_cicheck_option(optparser, options, :data)
81b1994897Sopenharmony_cidata = YAML.load_file(File.expand_path(options.data))
82b1994897Sopenharmony_cidata = JSON.parse(data.to_json, object_class: OpenStruct).freeze
83b1994897Sopenharmony_ci
84b1994897Sopenharmony_cioptions&.require&.each { |r| require File.expand_path(r) } if options.require
85b1994897Sopenharmony_ciGen.on_require(data)
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_ciif options.assert
88b1994897Sopenharmony_ci  require options.assert
89b1994897Sopenharmony_ci  exit
90b1994897Sopenharmony_ciend
91b1994897Sopenharmony_ci
92b1994897Sopenharmony_cicheck_option(optparser, options, :template)
93b1994897Sopenharmony_citemplate = File.read(File.expand_path(options.template))
94b1994897Sopenharmony_cioutput = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout
95b1994897Sopenharmony_cit = ERB.new(template, nil, '%-')
96b1994897Sopenharmony_cit.filename = options.template
97b1994897Sopenharmony_cioutput.write(t.result(create_sandbox))
98b1994897Sopenharmony_cioutput.close
99