Class: Racker::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/racker/processor.rb

Overview

This class handles command line options.

Constant Summary collapse

CONFIGURE_MUTEX =
Mutex.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Processor

Returns a new instance of Processor.



14
15
16
# File 'lib/racker/processor.rb', line 14

def initialize(options)
  @options = options
end

Class Method Details

.register_template(version = '1', &block) ⇒ Object

This is a class method so the templates can load it



86
87
88
89
# File 'lib/racker/processor.rb', line 86

def self.register_template(version='1',&block)
  @@last_procs ||= []
  @@last_procs << [version, block]
end

Instance Method Details

#capture_templatesObject



91
92
93
94
95
96
97
98
99
# File 'lib/racker/processor.rb', line 91

def capture_templates
  CONFIGURE_MUTEX.synchronize do
    @@last_procs = []

    yield

    return @@last_procs
  end
end

#execute!Object



18
19
20
21
22
23
24
25
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/racker/processor.rb', line 18

def execute!
  # Get the global logger
  log = Log4r::Logger['racker']
  
  # Verify that the templates exist
  @options[:templates].each do |template|
    raise "File does not exist!  (#{template})" unless ::File.exists?(template)
  end

  # Check that the output directory exists
  output_dir = File.dirname(File.expand_path(@options[:output]))

  # If the output directory doesnt exist
  log.info('Creating the output directory if it does not exist...')
  FileUtils.mkdir_p output_dir unless File.exists? output_dir

  # Load the templates
  templates = []

  # Load the template procs
  log.info('Loading racker templates...')
  template_procs = load(@options[:templates])

  # Load the actual templates
  log.info('Processing racker templates...')
  template_procs.each do |version,proc|
    # Create the new template
    template = Racker::Template.new

    # Run the block with the template
    proc.call(template)

    # Store the template
    templates << template
  end
  log.info('Racker template processing complete.')

  # Get the first template and merge each subsequent one on the latest
  log.info('Merging racker templates...')
  current_template = templates.shift

  # Overlay the templates
  templates.each do |template|
    current_template = current_template.deep_merge!(template, {:knockout_prefix => @options[:knockout]})
  end
  
  # Compact the residual template to remove nils
  log.info('Compacting racker template...')
  compact_template = current_template.compact(:recurse => true)

  # Write the compact template out to file
  File.open(@options[:output], 'w') do |file|
    log.info('Writing packer template...') 
    file.write(JSON.pretty_generate(compact_template.to_packer))
    log.info('Writing packer template complete.') 
  end
end

#load(templates) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/racker/processor.rb', line 76

def load(templates)
  return capture_templates do
    templates.each do |template|
      puts "Loading template file: #{template}" unless @options[:quiet]
      Kernel.load template
    end
  end
end