Class: LonoParams::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/lono-params/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Generator

Returns a new instance of Generator.



3
4
5
6
7
8
# File 'lib/lono-params/generator.rb', line 3

def initialize(name, options)
  @name = name
  @options = options
  @project_root = options[:project_root] || '.'
  @source_path = options[:path] || "#{@project_root}/params/#{@name}.txt"
end

Instance Method Details

#convert_to_cfn_format(contents, casing = :camel) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lono-params/generator.rb', line 46

def convert_to_cfn_format(contents, casing=:camel)
  lines = parse_contents(contents)
  params = []
  lines.each do |line|
    key,value = line.strip.split("=").map {|x| x.strip}
    param = if value == "use_previous_value"
              {
                ParameterKey: key,
                UsePreviousValue: true
              }
            elsif value
              {
                ParameterKey: key,
                ParameterValue: value
              }
            end
    if param
      param = param.to_snake_keys if casing == :underscore
      params << param
    end
  end
  params
end

#generateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/lono-params/generator.rb', line 10

def generate
  # useful option for lono-cfn
  return if @options[:allow_no_file] && !File.exist?(@source_path)

  if File.exist?(@source_path)
    contents = IO.read(@source_path)
    data = convert_to_cfn_format(contents)
    json = JSON.pretty_generate(data)
    write_output(json)
    puts "Params file generated for #{@name} at #{output_path}" unless @options[:mute]
  else
    puts "#{@source_path} could not be found?  Are you sure it exist?"
    exit 1
  end
end

#output_pathObject



70
71
72
# File 'lib/lono-params/generator.rb', line 70

def output_path
  "#{@project_root}/output/params/#{@name}.json".sub(/\.\//,'')
end

#paramsObject

useful for when calling CloudFormation via the aws-sdk gem



27
28
29
30
31
32
33
# File 'lib/lono-params/generator.rb', line 27

def params
  # useful option for lono-cfn
  return {} if @options[:allow_no_file] && !File.exist?(@source_path)

  contents = IO.read(@source_path)
  convert_to_cfn_format(contents, :underscore)
end

#parse_contents(contents) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/lono-params/generator.rb', line 35

def parse_contents(contents)
  lines = contents.split("\n")
  # remove comment at the end of the line
  lines.map! { |l| l.sub(/#.*/,'').strip }
  # filter out commented lines
  lines = lines.reject { |l| l =~ /(^|\s)#/i }
  # filter out empty lines
  lines = lines.reject { |l| l.strip.empty? }
  lines
end

#write_output(json) ⇒ Object



74
75
76
77
78
# File 'lib/lono-params/generator.rb', line 74

def write_output(json)
  dir = File.dirname(output_path)
  FileUtils.mkdir_p(dir) unless File.exist?(dir)
  IO.write(output_path, json)
end