Class: Lono::Template::PostProcessor

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Blueprint::Root
Defined in:
lib/lono/template/post_processor.rb

Instance Method Summary collapse

Methods included from Blueprint::Root

#bundler_groups, #find_blueprint_root, #require_bundle_gems, #set_blueprint_root

Constructor Details

#initialize(blueprint, options = {}) ⇒ PostProcessor

Returns a new instance of PostProcessor.



6
7
8
9
10
11
# File 'lib/lono/template/post_processor.rb', line 6

def initialize(blueprint, options={})
  @blueprint, @options = blueprint, options
  @template = @options[:template] || @blueprint
  Lono::ProjectChecker.check
  set_blueprint_root(@blueprint)
end

Instance Method Details

#registry_itemsObject

Useful for specs



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

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

#replacementsObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lono/template/post_processor.rb', line 24

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



13
14
15
16
17
18
# File 'lib/lono/template/post_processor.rb', line 13

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

#templateObject



60
61
62
# File 'lib/lono/template/post_processor.rb', line 60

def template
  YAML.load_file(template_path)
end

#template_pathObject



65
66
67
# File 'lib/lono/template/post_processor.rb', line 65

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

#update_template!(hash) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lono/template/post_processor.rb', line 40

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



20
21
22
# File 'lib/lono/template/post_processor.rb', line 20

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