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.



9
10
11
12
13
14
15
# File 'lib/ufo/dsl.rb', line 9

def initialize(template_definitions_path, options={})
  @template_definitions_path = template_definitions_path
  @options = options
  @project_root = options[:project_root] || '.'
  @task_definitions = []
  @outputters = []
end

Instance Method Details

#build_task_definitionsObject



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

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



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

def clean_existing_task_definitions
  # removing 1 file a a time instead of recursing removing the directory to be safe
  Dir.glob("#{@options[:project_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.



26
27
28
29
30
31
32
33
34
35
# File 'lib/ufo/dsl.rb', line 26

def evaluate_template_definitions
  source_code = IO.read(@template_definitions_path)
  begin
    instance_eval(source_code, @template_definitions_path)
  rescue Exception => e
    task_definition_error(e)
    puts "\nFull error:"
    raise
  end
end

#helperObject



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

def helper
  Helper.new(@options)
end

#runObject



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

def run
  evaluate_template_definitions
  build_task_definitions
  write_outputs
end

#task_definition(name, &block) ⇒ Object

methods available in task_definitions



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

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



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

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}:".colorize(: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".colorize(:red), line_number, line_content)
    else
      printf("%#{spacing}d %s\n", line_number, line_content)
    end
  end
end

#write_outputsObject



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

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