Class: Ufo::DSL::TaskDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/dsl/task_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={}, &block)
  @dsl = dsl
  @task_definition_name = task_definition_name
  @block = block
  @options = options
  @project_root = @options[:project_root] || '.'
end

Instance Attribute Details

#task_definition_nameObject (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

#buildObject



21
22
23
24
# File 'lib/ufo/dsl/task_definition.rb', line 21

def build
  instance_eval(&@block)
  erb_result(source_path)
end

#check_source_path(path) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/ufo/dsl/task_definition.rb', line 83

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



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ufo/dsl/task_definition.rb', line 26

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.message.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

#helperObject

delegate helper method back up to dsl



17
18
19
# File 'lib/ufo/dsl/task_definition.rb', line 17

def helper
  @dsl.helper
end

#source(name) ⇒ Object

at this point instance_eval has been called and source has possibly been called



58
59
60
# File 'lib/ufo/dsl/task_definition.rb', line 58

def source(name)
  @source = name
end

#source_pathObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ufo/dsl/task_definition.rb', line 71

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.expand_path("../../templates/default.json.erb", __FILE__)
    puts "#{task_definition_name} template definition using default template: #{path}" unless @options[:mute]
  end
  path
end

#variables(vars = {}) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/ufo/dsl/task_definition.rb', line 62

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