Class: Lono::Template::PostProcessor

Inherits:
AbstractBase show all
Defined in:
lib/lono/template/post_processor.rb

Instance Method Summary collapse

Methods inherited from AbstractBase

#initialize, #reinitialize

Methods included from Blueprint::Root

#find_blueprint_root, #set_blueprint_root

Constructor Details

This class inherits a constructor from Lono::AbstractBase

Instance Method Details

#registry_itemsObject

Useful for specs



46
47
48
# File 'lib/lono/template/post_processor.rb', line 46

def registry_items
  Lono::AppFile::Registry.items
end

#replacementsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lono/template/post_processor.rb', line 14

def replacements
  map = {}
  registry_items.each do |item|
    if item.directory? || item.file?
      replacement = item.s3_path
    else
      puts "WARN: PostProcessor replacements Cannot find file: #{item.path}"
      next
    end

    placeholder = "file://app/files/#{item.name}"
    map[placeholder] = replacement
  end
  map
end

#runObject



3
4
5
6
7
8
# File 'lib/lono/template/post_processor.rb', line 3

def run
  replacements.each do |placeholder, replacement|
    update_template!(template)
  end
  write_template!
end

#templateObject



50
51
52
# File 'lib/lono/template/post_processor.rb', line 50

def template
  YAML.load_file(template_path)
end

#template_pathObject



55
56
57
# File 'lib/lono/template/post_processor.rb', line 55

def template_path
  "#{Lono.config.output_path}/#{@blueprint}/templates/#{@template}.yml"
end

#update_template!(hash) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lono/template/post_processor.rb', line 30

def update_template!(hash)
  hash.each do |k, v|
    if v.is_a?(String)
      if v =~ %r{^file://}
        v.replace(replacements[v]) # replace the placeholder
      end
    elsif v.is_a?(Hash)
      update_template!(v) # recurse
    elsif v.is_a?(Array)
      v.each { |x| update_template!(x) if x.is_a?(Hash) }
    end
  end
  hash
end

#write_template!Object



10
11
12
# File 'lib/lono/template/post_processor.rb', line 10

def write_template!
  IO.write(template_path, YAML.dump(template)) # unless ENV['LONO_TEST'] # additional safeguard for testing
end