Class: Proteus::Templates::Partial
- Inherits:
-
TemplateBinding
- Object
- TemplateBinding
- Proteus::Templates::Partial
- Includes:
- Helpers::PathHelpers, Thor::Shell
- Defined in:
- lib/proteus/templates/partial.rb
Instance Method Summary collapse
-
#initialize(name:, context:, environment:, module_name:, data:, data_context: nil, force_rendering: true, deep_merge: false, terraform_variables:, scope_resources: nil) ⇒ Partial
constructor
A new instance of Partial.
- #partial_data? ⇒ Boolean
- #render ⇒ Object
- #scope_resources(manifest:, scope:) ⇒ Object
Methods included from Helpers::PathHelpers
#config_dir, #config_path, #context_path, #context_temp_directory, #contexts_path, #environments_path, #module_config_path, #module_data_path, #module_hooks_path, #module_io_file, #module_path, #module_templates_path, #modules_path, #plan_file, #root_path, #state_file, #var_file
Methods inherited from TemplateBinding
#default_true, #get_binding, #render_defaults, #render_partial, #set
Methods included from Helpers::StringHelpers
Constructor Details
#initialize(name:, context:, environment:, module_name:, data:, data_context: nil, force_rendering: true, deep_merge: false, terraform_variables:, scope_resources: nil) ⇒ Partial
Returns a new instance of Partial.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/proteus/templates/partial.rb', line 8 def initialize(name:, context:, environment:, module_name:, data:, data_context: nil, force_rendering: true, deep_merge: false, terraform_variables:, scope_resources: nil) @name = name @context = context @environment = environment @module_name = module_name @data = data @data_context = data_context @force_rendering = force_rendering @deep_merge = deep_merge @terraform_variables = terraform_variables @scope_resources = scope_resources @defaults = {} set(@name.split('/').last, {}) if data.dig('partials', @name) @partial_data_present = true @data['partials'][@name].delete('render') set(@name, @data['partials'][@name]) else set(@name.split('/').last, ActiveSupport::HashWithIndifferentAccess.new) end end |
Instance Method Details
#partial_data? ⇒ Boolean
90 91 92 |
# File 'lib/proteus/templates/partial.rb', line 90 def partial_data? @partial_data_present end |
#render ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/proteus/templates/partial.rb', line 33 def render if partial_data? || @force_rendering defaults_file = File.join(module_templates_path(@context, @module_name), 'defaults', "#{@name}.yaml") default_partial_path = File.join(module_templates_path(@context, @module_name), "_#{@name}.tf.erb") sub_directory_partial_path = File.join(module_templates_path(@context, @module_name), "#{@name}.tf.erb") if File.file?(default_partial_path) partial_file = default_partial_path else partial_file = sub_directory_partial_path end if File.file?(partial_file) partial_template = File.read(partial_file) if File.file?(defaults_file) @defaults = YAML.load_file(defaults_file, {}).with_indifferent_access partial_data = instance_variable_get("@#{@name}") if @deep_merge == false @defaults.each do |key, value| if !partial_data.key?(key) partial_data[key] = value end end elsif @deep_merge == :each merged_data = [] partial_data.each do |item| merged_data << @defaults.deep_merge(item) end instance_variable_set("@#{@name.split('/').last}", merged_data) else instance_variable_set("@#{@name.split('/').last}", @defaults.deep_merge(partial_data)) end end begin if @scope_resources return scope_resources(manifest: Erubis::Eruby.new(partial_template).result(get_binding), scope: @scope_resources) else return Erubis::Eruby.new(partial_template).result(get_binding) end rescue Exception => e say "Error in partial: #{partial_file}", :magenta e.backtrace.each { |line| say line, :magenta } say e., :magenta exit 1 end end end end |
#scope_resources(manifest:, scope:) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/proteus/templates/partial.rb', line 94 def scope_resources(manifest:, scope:) scoped = [] manifest.each_line do |line| if line.include?('proteus:noscope') say "NOT SCOPING: #{line}", :green scoped << line.gsub(/( *)#( *)proteus:noscope/, '') next end if matches = line.match(/(?<rd>resource|data)( +)"(?<resource_type>[a-z0-9_]+)"( +)"(?<resource_name>[a-zA-Z0-9_\-]+)"( *)(\{)/) matches = matches.named_captures.with_indifferent_access say "MATCHED RESOURCE: #{matches[:rd].upcase}", :green scoped << "#{matches[:rd]} \"#{matches[:resource_type]}\" \"#{scope}_#{matches[:resource_name]}\" {" say "CHANGED TO: #{scoped.last}", :green elsif matches = line.match(/^(?<pre>\s+)?(?<left>[a-z_]+)(?<eq>[= ]+)(?<lbracket>\[)?(?<lparen>"\${)?(?<funcbeg>([a-z]+\()*)?(?<data>data\.)?(?<resource_type>[a-z_0-9]+)\.(?<resource_name>[a-z_0-9-]+)\.(?<wildcard>\*.)?(?<resource_attribute>[a-z_]+)(?<rparen>}")?(?<post>.*)?/) matches = matches.named_captures.with_indifferent_access #if !matches[:data] say "MATCHED REFERENCE: #{line}", :green scoped << "#{matches[:pre]}#{matches[:left]}#{matches[:eq]}#{matches[:lbracket]}#{matches[:lparen]}#{matches[:funcbeg]}#{matches[:data]}#{matches[:resource_type]}.#{scope}_#{matches[:resource_name]}.#{matches[:wildcard]}#{matches[:resource_attribute]}#{matches[:rparen]}#{matches[:post]}" say "CHANGED TO: #{scoped.last}", :green elsif matches = line.match(/(?<resource>(?<data>data\.)?(?<resource_type>(?<provider>aws|helm|local)[a-z_0-9]+)\.(?<resource_name>[a-z\-_0-9]+)\.(?<resource_attribute>[a-z_]+))/) say "MATCHED RESOURCE REFERENCE: #{line}", :green scoped << line.gsub(matches[:resource], "#{matches[:data]}#{matches[:resource_type]}.#{scope}_#{matches[:resource_name]}.#{matches[:resource_attribute]}") say "CHANGED TO: #{scoped.last}", :green else scoped << line end end scoped.map!(&:chomp).join("\n") end |