1#!/usr/bin/env ruby 2# Copyright (c) 2021-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 '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 FILE1,FILE2,...', Array, 'Source data files in YAML format') 82 opts.on('-q', '--api FILE1,FILE2,...', Array, 'Ruby files providing api for data in YAML format') 83 opts.on('-o', '--output FILE', 'Output file (default is stdout)') 84 opts.on('-a', '--assert FILE', 'Go through assertions on data provided and exit') 85 opts.on('-r', '--require foo,bar,baz', Array, 'List of additional Ruby files to be required for generation') 86 87 opts.on('-h', '--help', 'Prints this help') do 88 puts opts 89 exit 90 end 91end 92optparser.parse!(into: options) 93 94check_option(optparser, options, :data) 95check_option(optparser, options, :api) 96raise "'api' and 'data' must be of equal length" if options.api.length != options.data.length 97options.api.zip(options.data).each do |api_file, data_file| 98 data = YAML.load_file(File.expand_path(data_file)) 99 data = JSON.parse(data.to_json, object_class: OpenStruct).freeze 100 require File.expand_path(api_file) 101 Gen.on_require(data) 102end 103options.require&.each { |r| require File.expand_path(r) } 104 105if options.assert 106 require options.assert 107 exit 108end 109 110check_option(optparser, options, :template) 111template = File.read(File.expand_path(options.template)) 112output = options.output ? File.open(File.expand_path(options.output), 'w') : $stdout 113t = erb_new(template, trim_mode: '%-') 114t.filename = options.template 115output.write(t.result(create_sandbox)) 116output.close 117