Class: Racker::Processor

Inherits:
Object
  • Object
show all
Includes:
LogSupport
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

Methods included from LogSupport

level=, log4r_level_for, logger, #logger

Constructor Details

#initialize(options) ⇒ Processor

Returns a new instance of Processor.



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

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



73
74
75
76
# File 'lib/racker/processor.rb', line 73

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

Instance Method Details

#capture_templatesObject



78
79
80
81
82
83
84
85
86
# File 'lib/racker/processor.rb', line 78

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

    yield

    return @@last_procs
  end
end

#execute!Object



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
# File 'lib/racker/processor.rb', line 19

def execute!
  # Verify that the templates exist
  @options[:templates].each do |template|
    raise "File does not exist!  (#{template})" unless ::File.exists?(template)
  end

  # Load the templates
  templates = []

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

  # Load the actual templates
  logger.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
  logger.info('Racker template processing complete.')

  # Get the first template and merge each subsequent one on the latest
  logger.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
  logger.info('Compacting racker template...')
  compact_template = current_template.compact(:recurse => true)

  # Pretty-print the JSON template
  JSON.pretty_generate(compact_template.to_packer)
end

#load(templates) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/racker/processor.rb', line 63

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