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 22module Gen 23 def self.on_require(data); end 24end 25 26require File.expand_path(File.join(File.dirname(__FILE__), 'runtime.rb')) 27 28def create_sandbox 29 # nothing but Ruby core libs and 'required' files 30 binding 31end 32 33def check_option(optparser, options, key) 34 return if options[key] 35 36 puts "Missing option: --#{key}" 37 puts optparser 38 exit false 39end 40 41options = OpenStruct.new 42 43optparser = OptionParser.new do |opts| 44 opts.banner = 'Usage: gen.rb [options]' 45 46 opts.on('-t', '--template FILE', 'Template for file generation (required)') 47 opts.on('-d', '--datafiles FILE1,FILE2,FILE3', Array, 'Source data files in YAML format (required)') 48 opts.on('-o', '--output FILE', 'Output file (required)') 49 50 opts.on('-h', '--help', 'Prints this help') do 51 puts opts 52 exit 53 end 54end 55optparser.parse!(into: options) 56 57check_option(optparser, options, :datafiles) 58check_option(optparser, options, :template) 59check_option(optparser, options, :output) 60 61template_file = File.read(File.expand_path(options.template)) 62output_file = File.open(File.expand_path(options.output), 'w') 63 64output_file.puts('# Autogenerated file -- DO NOT EDIT!') 65output_file.puts('intrinsics:') 66 67options.datafiles.each do |data| 68 data = YAML.load_file(File.expand_path(data)) 69 data = JSON.parse(data.to_json, object_class: OpenStruct) 70 Gen.on_require(data) 71 72 t = ERB.new(template_file, nil, '%-') 73 t.filename = options.template 74 75 output_file.write(t.result(create_sandbox)) 76end 77 78output_file.close 79