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