Class: Highlander::Compiler::ComponentCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/highlander.compiler.rb

Constant Summary collapse

@@global_extensions_paths =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ ComponentCompiler

Returns a new instance of ComponentCompiler.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/highlander.compiler.rb', line 34

def initialize(component)

  @workdir = ENV['HIGHLANDER_WORKDIR']
  @component = component
  @sub_components = []
  @component_name = component.highlander_dsl.name.downcase
  @cfndsl_compiled = false
  @config_compiled = false
  @cfn_template_paths = []
  @lambdas_processed = false
  @silent_mode = false
  @lambda_src_paths = []

  if @@global_extensions_paths.empty?
    global_extensions_folder = "#{File.dirname(__FILE__)}/../cfndsl_ext"
    Dir["#{global_extensions_folder}/*.rb"].each { |f| @@global_extensions_paths << f }
  end

  @component.highlander_dsl.components.each do |sub_component|
    sub_component_compiler = Highlander::Compiler::ComponentCompiler.new(sub_component.component_loaded)
    sub_component_compiler.component_name = sub_component.name
    @sub_components << sub_component_compiler
  end
end

Instance Attribute Details

#cfn_output_location ⇒ Object

Returns the value of attribute cfn_output_location.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def cfn_output_location
  @cfn_output_location
end

#cfn_template_paths ⇒ Object

Returns the value of attribute cfn_template_paths.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def cfn_template_paths
  @cfn_template_paths
end

#compiled_subcomponents ⇒ Object

Returns the value of attribute compiled_subcomponents.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def compiled_subcomponents
  @compiled_subcomponents
end

#component ⇒ Object

Returns the value of attribute component.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def component
  @component
end

#component_name ⇒ Object

Returns the value of attribute component_name.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def component_name
  @component_name
end

#config_output_location ⇒ Object

Returns the value of attribute config_output_location.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def config_output_location
  @config_output_location
end

#dsl_output_location ⇒ Object

Returns the value of attribute dsl_output_location.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def dsl_output_location
  @dsl_output_location
end

#lambda_src_paths ⇒ Object

Returns the value of attribute lambda_src_paths.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def lambda_src_paths
  @lambda_src_paths
end

#silent_mode ⇒ Object

Returns the value of attribute silent_mode.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def silent_mode
  @silent_mode
end

#workdir ⇒ Object

Returns the value of attribute workdir.



23
24
25
# File 'lib/highlander.compiler.rb', line 23

def workdir
  @workdir
end

Instance Method Details

#compileCfnDsl(out_format) ⇒ Object



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
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/highlander.compiler.rb', line 64

def compileCfnDsl(out_format)
  processLambdas unless @lambdas_processed
  writeConfig unless @config_written
  dsl = @component.highlander_dsl
  component_cfndsl = @component.cfndsl_content

  @component.highlander_dsl.components.each { |sc|
    sc.distribution_format = out_format
  }

  # indent component cfndsl
  component_cfndsl.gsub!("\n", "\n\t")
  component_cfndsl.gsub!("\r\n", "\r\n\t")
  # render cfndsl
  renderer = ERB.new(File.read("#{__dir__}/../templates/cfndsl.component.template.erb"))
  cfn_template = renderer.result(OpenStruct.new({
      'dsl' => dsl,
      'component_cfndsl' => component_cfndsl,
      'component_requires' => (@@global_extensions_paths + @component.cfndsl_ext_files)
  }).instance_eval { binding })

  # write to output file
  output_dir = "#{@workdir}/out/cfndsl"
  @dsl_output_location = output_dir
  output_path = "#{output_dir}/#{@component_name}.compiled.cfndsl.rb"
  FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
  File.write(output_path, cfn_template)
  puts "cfndsl template for #{dsl.name} written to #{output_path}"
  @cfndsl_compiled_path = output_path

  @sub_components.each { |subcomponent_compiler|
    puts "Rendering sub-component cfndsl: #{subcomponent_compiler.component_name}"
    subcomponent_compiler.compileCfnDsl out_format
  }

  @cfndsl_compiled = true

end

#compileCloudFormation(format = 'yaml') ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/highlander.compiler.rb', line 103

def compileCloudFormation(format = 'yaml')


  #compile cfndsl templates first
  compileCfnDsl format unless @cfndsl_compiled

  dsl = @component.highlander_dsl
  component_cfndsl = @component.cfndsl_content

  # create out dir if not there
  @cfn_output_location = "#{@workdir}/out/#{format}"
  output_dir = @cfn_output_location
  FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)

  # write config
  config_yaml_path = writeConfig


  # compile templates
  output_path = "#{output_dir}/#{component_name}.compiled.#{format}"
  @cfn_template_paths << output_path
  # configure cfndsl
  cfndsl_opts = []
  cfndsl_opts.push([:yaml, config_yaml_path])

  # grab cfndsl model
  model = CfnDsl.eval_file_with_extras(@cfndsl_compiled_path, cfndsl_opts, false)

  # write resulting cloud formation template
  if format == 'json'
    output_content = JSON.pretty_generate(model)
  elsif format == 'yaml'
    output_content = JSON.parse(model.to_json).to_yaml
  else
    raise StandardError, "#{format} not supported for cfn generation"
  end

  File.write(output_path, output_content)
  # `cfndsl #{@cfndsl_compiled_path} -p -f #{format} -o #{output_path} --disable-binding`
  puts "CloudFormation #{format.upcase} template for #{dsl.name} written to #{output_path}"

  # compile sub-component templates
  @sub_components.each do |sub_component|
    sub_component.compileCloudFormation format
    @cfn_template_paths += sub_component.cfn_template_paths
    @lambda_src_paths += sub_component.lambda_src_paths
  end

end

#processLambdas ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/highlander.compiler.rb', line 171

def processLambdas()

  @component.highlander_dsl.lambda_functions_keys.each do |lfk|
    resolver = LambdaResolver.new(@component,
        lfk,
        @workdir,
        (not @silent_mode)
    )
    @lambda_src_paths += resolver.generateSourceArchives
    resolver.mergeComponentConfig
  end

  @lambdas_processed = true

end

#writeConfig(write_subcomponents_config = false) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/highlander.compiler.rb', line 153

def writeConfig(write_subcomponents_config = false)
  @config_output_location = "#{@workdir}/out/config"
  config_yaml_path = "#{@config_output_location}/#{@component_name}.config.yaml"
  FileUtils.mkdir_p(@config_output_location) unless Dir.exist?(@config_output_location)

  File.write(config_yaml_path, @component.config.to_yaml)
  puts "Config for #{@component.highlander_dsl.name} written to #{config_yaml_path}"

  if write_subcomponents_config
    # compile sub-component templates
    @sub_components.each do |sub_component|
      sub_component.writeConfig write_subcomponents_config
    end
  end
  @config_written = true
  config_yaml_path
end