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 major_minor_version 56 major, minor, = RUBY_VERSION.split('.').map(&:to_i) 57 [major, minor] 58end 59 60def check_version(min_major, min_minor) 61 major, minor = major_minor_version 62 major > min_major || (major == min_major && minor >= min_minor) 63end 64 65def erb_new(str, trim_mode: nil) 66 if check_version(2, 6) 67 ERB.new(str, trim_mode: trim_mode) 68 else 69 ERB.new(str, nil, trim_mode) 70 end 71end 72 73raise "Update your ruby version, #{RUBY_VERSION} is not supported" unless check_version(2, 5) 74 75options = OpenStruct.new 76 77optparser = OptionParser.new do |opts| 78 opts.banner = 'Usage: gen.rb [options]' 79 80 opts.on('-t', '--template FILE', 'Template for file generation (required)') 81 opts.on('-d', '--data FILE', 'Source data in YAML format (required)') 82 opts.on('-o', '--output FILE', 'Output file (default is stdout)') 83 opts.on('-a', '--assert FILE', 'Go through assertions on data provided and exit') 84 opts.on('-r', '--require foo,bar,baz', Array, 'List of files to be required for generation') 85 86 opts.on('-h', '--help', 'Prints this help') do 87 puts opts 88 exit 89 end 90end 91optparser.parse!(into: options) 92 93check_option(optparser, options, :data) 94data = YAML.load_file(File.expand_path(options.data)) 95data = JSON.parse(data.to_json, object_class: OpenStruct).freeze 96 97options&.require&.each { |r| require File.expand_path(r) } if options.require 98Gen.on_require(data) 99 100if options.assert 101 require options.assert 102 exit 103end 104 105check_option(optparser, options, :template) 106template = File.read(File.expand_path(options.template)) 107output = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout 108t = erb_new(template, trim_mode: '%-') 109t.filename = options.template 110output.write(t.result(create_sandbox)) 111output.close 112