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



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

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

#replacementsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lono/template/post_processor.rb', line 14

def replacements
  map = {}

  registry_items.each do |item|
    if item.type == "lambda_layer"
      placeholder = "file://app/files/lambda_layer/#{item.name}"
    elsif item.directory? || item.file?
      placeholder = "file://app/files/file/#{item.name}"
    else
      puts "WARN: PostProcessor replacements Cannot find file: #{item.output_path}"
      next
    end
    map[placeholder] = item.s3_path
  end

  Lono::Configset::S3File::Registry.items.each do |item|
    placeholder = "file://configset/#{item.configset}/#{item.name}"
    # map[placeholder] = "https://s3.amazonaws.com/#{Lono::S3::Bucket.name}/#{item.s3_path}"
    map[placeholder] = item.replacement_value
  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



58
59
60
# File 'lib/lono/template/post_processor.rb', line 58

def template
  YAML.load_file(template_path)
end

#template_pathObject



63
64
65
# File 'lib/lono/template/post_processor.rb', line 63

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

#update_template!(hash) ⇒ Object



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

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