Class: Ufo::DSL::TaskDefinition
- Inherits:
-
Object
- Object
- Ufo::DSL::TaskDefinition
- Defined in:
- lib/ufo/dsl/task_definition.rb
Instance Attribute Summary collapse
-
#task_definition_name ⇒ Object
readonly
Returns the value of attribute task_definition_name.
Instance Method Summary collapse
- #build ⇒ Object
- #check_source_path(path) ⇒ Object
- #erb_result(path) ⇒ Object
-
#helper ⇒ Object
delegate helper method back up to dsl.
-
#initialize(dsl, task_definition_name, options = {}, &block) ⇒ TaskDefinition
constructor
A new instance of TaskDefinition.
- #load_variables ⇒ Object
-
#load_variables_file(filename) ⇒ Object
Load the variables defined in ufo/variables/* to make available in the template blocks in ufo/templates/*.
-
#source(name) ⇒ Object
at this point instance_eval has been called and source has possibly been called.
- #source_path ⇒ Object
- #variables(vars = {}) ⇒ Object
Constructor Details
#initialize(dsl, task_definition_name, options = {}, &block) ⇒ TaskDefinition
Returns a new instance of TaskDefinition.
8 9 10 11 12 13 14 |
# File 'lib/ufo/dsl/task_definition.rb', line 8 def initialize(dsl, task_definition_name, ={}, &block) @dsl = dsl @task_definition_name = task_definition_name @block = block @options = @project_root = @options[:project_root] || '.' end |
Instance Attribute Details
#task_definition_name ⇒ Object (readonly)
Returns the value of attribute task_definition_name.
7 8 9 |
# File 'lib/ufo/dsl/task_definition.rb', line 7 def task_definition_name @task_definition_name end |
Instance Method Details
#build ⇒ Object
21 22 23 24 25 |
# File 'lib/ufo/dsl/task_definition.rb', line 21 def build load_variables instance_eval(&@block) erb_result(source_path) end |
#check_source_path(path) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ufo/dsl/task_definition.rb', line 114 def check_source_path(path) unless File.exist?(path) friendly_path = path.sub("#{@project_root}/", '') puts "ERROR: Could not find the #{friendly_path} template. Are sure it exists? Check where you called source in ufo/task_definitions.rb" exit 1 else puts "#{task_definition_name} template definition using project template: #{path}" unless @options[:mute] end path end |
#erb_result(path) ⇒ Object
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 |
# File 'lib/ufo/dsl/task_definition.rb', line 57 def erb_result(path) template = IO.read(path) begin ERB.new(template, nil, "-").result(binding) rescue Exception => e puts e # how to know where ERB stopped? - https://www.ruby-forum.com/topic/182051 # syntax errors have the (erb):xxx info in e.message # undefined variables have (erb):xxx info in e.backtrac error_info = e..split("\n").grep(/\(erb\)/)[0] error_info ||= e.backtrace.grep(/\(erb\)/)[0] raise unless error_info # unable to find the (erb):xxx: error line line = error_info.split(':')[1].to_i puts "Error evaluating ERB template on line #{line.to_s.colorize(:red)} of: #{path.sub(/^\.\//, '')}" template_lines = template.split("\n") context = 5 # lines of context top, bottom = [line-context-1, 0].max, line+context-1 spacing = template_lines.size.to_s.size template_lines[top..bottom].each_with_index do |line_content, index| line_number = top+index+1 if line_number == line printf("%#{spacing}d %s\n".colorize(:red), line_number, line_content) else printf("%#{spacing}d %s\n", line_number, line_content) end end exit 1 unless ENV['TEST'] end end |
#helper ⇒ Object
delegate helper method back up to dsl
17 18 19 |
# File 'lib/ufo/dsl/task_definition.rb', line 17 def helper @dsl.helper end |
#load_variables ⇒ Object
27 28 29 30 |
# File 'lib/ufo/dsl/task_definition.rb', line 27 def load_variables load_variables_file("base") load_variables_file(UFO_ENV) end |
#load_variables_file(filename) ⇒ Object
Load the variables defined in ufo/variables/* to make available in the template blocks in ufo/templates/*.
Example:
`ufo/variables/base.rb`:
@name = "docker-process-name"
@image = "docker-image-name"
`ufo/templates/main.json.erb`:
{
"containerDefinitions": [
{
"name": "<%= @name %>",
"image": "<%= @image %>",
....
}
NOTE: Only able to make instance variables avaialble with instance_eval
Wasnt able to make local variables available.
52 53 54 55 |
# File 'lib/ufo/dsl/task_definition.rb', line 52 def load_variables_file(filename) path = "#{@project_root}/ufo/variables/#{filename}.rb" instance_eval(IO.read(path)) if File.exist?(path) end |
#source(name) ⇒ Object
at this point instance_eval has been called and source has possibly been called
89 90 91 |
# File 'lib/ufo/dsl/task_definition.rb', line 89 def source(name) @source = name end |
#source_path ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ufo/dsl/task_definition.rb', line 102 def source_path if @source # this means that source has been called path = "#{@project_root}/ufo/templates/#{@source}.json.erb" check_source_path(path) else # default source path path = File.("../../templates/default.json.erb", __FILE__) puts "#{task_definition_name} template definition using default template: #{path}" unless @options[:mute] end path end |
#variables(vars = {}) ⇒ Object
93 94 95 96 97 98 99 100 |
# File 'lib/ufo/dsl/task_definition.rb', line 93 def variables(vars={}) vars.each do |var,value| if instance_variable_defined?("@#{var}") puts "WARNING: The instance variable @#{var} is already used internally with ufo. Please name you variable another name!" end instance_variable_set("@#{var}", value) end end |