Class: Ufo::DSL

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

Defined Under Namespace

Classes: Helper, Outputter, TaskDefinition

Instance Method Summary collapse

Constructor Details

#initialize(template_definitions_path, options = {}) ⇒ DSL

Returns a new instance of DSL.



5
6
7
8
9
10
# File 'lib/ufo/dsl.rb', line 5

def initialize(template_definitions_path, options={})
  @template_definitions_path = template_definitions_path
  @options = options
  @task_definitions = []
  @outputters = []
end

Instance Method Details

#build_task_definitionsObject



60
61
62
63
64
65
66
67
# File 'lib/ufo/dsl.rb', line 60

def build_task_definitions
  puts "Generating Task Definitions:" unless @options[:quiet]
  clean_existing_task_definitions
  @task_definitions.each do |task|
    erb_result = task.build
    @outputters << Outputter.new(task.task_definition_name, erb_result, @options)
  end
end

#clean_existing_task_definitionsObject



69
70
71
72
73
74
# File 'lib/ufo/dsl.rb', line 69

def clean_existing_task_definitions
  # removing 1 file a a time instead of recursing removing the directory to be safe
  Dir.glob("#{Ufo.root}/.ufo/output/*").each do |path|
    FileUtils.rm_f(path)
  end
end

#evaluate_template_definitionsObject

All we’re doing at this point is saving blocks of code into memory The instance_eval provides the task_definition and helper methods as they are part of this class.



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

def evaluate_template_definitions
  source_code = IO.read(@template_definitions_path)
  begin
    instance_eval(source_code, @template_definitions_path)
  rescue Exception => e
    if e.class == SystemExit # allow exit to happen normally
      raise
    else
      task_definition_error(e)
      puts "\nFull error:"
      raise
    end
  end
end

#helperObject



87
88
89
# File 'lib/ufo/dsl.rb', line 87

def helper
  Helper.new
end

#runObject



12
13
14
15
16
# File 'lib/ufo/dsl.rb', line 12

def run
  evaluate_template_definitions
  build_task_definitions
  write_outputs
end

#task_definition(name, &block) ⇒ Object

methods available in task_definitions



83
84
85
# File 'lib/ufo/dsl.rb', line 83

def task_definition(name, &block)
  @task_definitions << TaskDefinition.new(self, name, @options, &block)
end

#task_definition_error(e) ⇒ Object

Prints out a user friendly task_definition error message



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ufo/dsl.rb', line 37

def task_definition_error(e)
  error_info = e.backtrace.first
  path, line_no, _ = error_info.split(':')
  line_no = line_no.to_i
  puts "Error evaluating #{path}:".color(:red)
  puts e.message
  puts "Here's the line in #{path} with the error:\n\n"

  contents = IO.read(path)
  content_lines = contents.split("\n")
  context = 5 # lines of context
  top, bottom = [line_no-context-1, 0].max, line_no+context-1
  spacing = content_lines.size.to_s.size
  content_lines[top..bottom].each_with_index do |line_content, index|
    line_number = top+index+1
    if line_number == line_no
      printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
    else
      printf("%#{spacing}d %s\n", line_number, line_content)
    end
  end
end

#write_outputsObject



76
77
78
79
80
# File 'lib/ufo/dsl.rb', line 76

def write_outputs
  @outputters.each do |outputter|
    outputter.write
  end
end