1# Copyright (c) 2021-2024 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'ostruct' 15require 'delegate' 16 17module Common 18 module_function 19 20 def plugins 21 @plugins 22 end 23 24 def each_plugin_option(option) 25 @plugins.each do |plugin, plugin_opts| 26 next unless plugin_opts[option] 27 yield plugin_opts[option], plugin, plugin_opts 28 end 29 end 30 31 def each_plugin_suboption(option, suboption) 32 @plugins.each do |plugin, plugin_opts| 33 next unless plugin_opts[option] 34 next unless plugin_opts[option][suboption] 35 yield plugin_opts[option][suboption], plugin, plugin_opts 36 end 37 end 38 39 def include_plugin_files(option, suboption) 40 @plugins 41 .select { |plugin, plugin_opts| plugin_opts[option] && plugin_opts[option][suboption] } 42 .map { |plugin, plugin_opts| "#include \"#{plugin_opts[option][suboption]}\"" } 43 .join "\n" 44 end 45 46 def assign_data_level(cur_hash, key, cur_data) 47 if !cur_data && (cur_data.class == OpenStruct || cur_data.class == Array) 48 return 49 elsif !cur_data 50 #boolean ? 51 cur_data = "false" 52 end 53 54 if cur_data.class == OpenStruct 55 wrap_hash_level(cur_hash, key, cur_data) 56 elsif cur_data.class == Array 57 wrap_array_level(cur_hash, key, cur_data) 58 else 59 if !cur_hash[key] 60 cur_hash[key] = cur_data.to_s 61 elsif cur_hash[key].class == Array 62 cur_hash[key].append(cur_data.to_s) 63 else 64 cur_hash[key] = [cur_hash[key]].append(cur_data.to_s) 65 end 66 end 67 end 68 69 def wrap_array_level(cur_hash, key, cur_data) 70 if !cur_data || cur_data.class != Array 71 return 72 end 73 74 cur_data.each do |val| 75 assign_data_level(cur_hash, key, val) 76 end 77 end 78 79 def wrap_hash_level(cur_hash, key, cur_data) 80 if !cur_data || cur_data.class != OpenStruct 81 return 82 end 83 84 if !cur_hash[key] 85 cur_hash[key] = Hash.new() 86 end 87 88 new_h = cur_data.to_h 89 90 new_h.each do |sub_key, val| 91 assign_data_level(cur_hash[key.to_s], sub_key.to_s, val) 92 end 93 end 94 95 def wrap_data(data) 96 @data = data 97 @plugins = Hash.new() 98 if !data || !data.plugins 99 return 100 end 101 102 data.plugins.each do |plugin| 103 h_plugin = plugin.to_h 104 h_plugin_lang = h_plugin.keys.first.to_s 105 lang_enum = "ark::SourceLanguage::" + h_plugin_lang 106 @plugins[h_plugin_lang] = Hash.new() 107 @plugins[h_plugin_lang]["lang_enum"] = lang_enum 108 assign_data_level(@plugins, h_plugin_lang, h_plugin.values.first) 109 end 110 end 111end 112 113def Gen.on_require(data) 114 Common.wrap_data(data) 115end 116