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



24
25
26
27
28
29
30
31
32
33
# File 'lib/ufo/dsl/task_definition.rb', line 24

def build
  copy_instance_variables
  begin
    instance_eval(&@block)
  rescue Exception => e
    build_error_info(e)
    raise
  end
  RenderMePretty.result(source_path, context: template_scope)
end

#build_error_info(e) ⇒ Object

Provide a slightly better error message to user when the task definition code block is not evaluated successfully.



37
38
39
40
41
42
43
44
# File 'lib/ufo/dsl/task_definition.rb', line 37

def build_error_info(e)
  puts "ERROR: evaluating block for task_definition #{@task_definition_name}".color(:red)
  # The first line of the backtrace has the info of the file name. Example:
  # ./.ufo/task_definitions.rb:24:in `block in evaluate_template_definitions'
  info = e.backtrace[0]
  filename = info.split(':')[0..1].join(':')
  puts "Filename: #{filename}".color(:red)
end

#check_source_path(path) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/ufo/dsl/task_definition.rb', line 96

def check_source_path(path)
  unless File.exist?(path)
    friendly_path = path.sub("#{Ufo.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

#copy_instance_variablesObject

Copy the instance variables from TemplateScope to TaskDefinition so that config/variables are available in the task_definition blocks also. Example:

task_definition "my-app" do
  # make config/variables available here also
end

This allows possible collision but think it is worth it to have variables available.



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

def copy_instance_variables
  template_scope.instance_variables.each do |var|
    val = template_scope.instance_variable_get(var)
    instance_variable_set(var, val)
  end
end

#helperObject

delegate helper method back up to dsl



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

def helper
  @dsl.helper
end

#source(name) ⇒ Object

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



65
66
67
# File 'lib/ufo/dsl/task_definition.rb', line 65

def source(name)
  @source = name
end

#source_pathObject



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

def source_path
  if @source # this means that source has been called
    path = "#{Ufo.root}/.ufo/templates/#{@source}.json.erb"
    check_source_path(path)
  else
    # default source path
    path = File.expand_path("../../default/templates/main.json.erb", __FILE__)
    puts "#{task_definition_name} template definition using default template: #{path}" unless @options[:mute]
  end
  path
end

#template_scopeObject



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

def template_scope
  @template_scope ||= Ufo::TemplateScope.new(helper, @task_definition_name)
end

#template_scope_instance_variable?(var) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/ufo/dsl/task_definition.rb', line 80

def template_scope_instance_variable?(var)
  template_scope.instance_variables.include?("@#{var}".to_sym)
end

#variables(vars = {}) ⇒ Object



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

def variables(vars={})
  vars.each do |var,value|
    # Warn when variable collides with internal variable, but dont warn
    # template_scope variables collision.
    if instance_variable_defined?("@#{var}") && !template_scope_instance_variable?(var)
      puts "WARNING: The instance variable @#{var} is already used internally with ufo.  Please name you variable another name!"
    end
    template_scope.instance_variable_set("@#{var}", value)
  end
end