Class: Lono::Importer

Inherits:
Object
  • Object
show all
Defined in:
lib/lono/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ Importer

Returns a new instance of Importer.



7
8
9
10
11
12
# File 'lib/lono/importer.rb', line 7

def initialize(source, options)
  @source = source
  @options = options
  @format = 'yml'
  Lono::ProjectChecker.check
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/lono/importer.rb', line 6

def options
  @options
end

Instance Method Details

#add_template_definitionObject

Add template definition to app/definitions/base.rb.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lono/importer.rb', line 64

def add_template_definition
  path = "#{Lono.config.definitions_path}/base.rb"
  lines = File.exist?(path) ? IO.readlines(path) : []
  new_template_definition = %Q|template "#{template_name}"|
  unless lines.detect { |l| l.include?(new_template_definition) }
    lines << ["\n", new_template_definition]
    result = lines.join('')
    IO.write(path, result)
  end
  path
end

#create_paramsObject

Creates starter params/base/.txt file



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/lono/importer.rb', line 77

def create_params
  template = YAML.load_file(template_path)

  result = []
  required_parameters.each do |name, attributes|
    result << "#{name}="
  end
  optional_parameters.each do |name, attributes|
    key = "#{name}=".ljust(20, ' ')
    result << "##{key} # optional"
  end
  content = result.join("\n") + "\n"

  folder = File.dirname(params_path)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  IO.write(params_path, content) unless File.exist?(params_path)
end

#download_templateObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/lono/importer.rb', line 48

def download_template
  template =  open(@source).read

  result = if json?(template)
              # abusing YAML.dump(YAML.load()) to convert json to yaml
              YAML.dump(YAML.load(template))
            else
              template # template is already in YAML format
            end

  folder = File.dirname(template_path)
  FileUtils.mkdir_p(folder) unless File.exist?(folder)
  IO.write(template_path, result)
end

#json?(text) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/lono/importer.rb', line 41

def json?(text)
  JSON.load(text)
  true # if reach here than it's just
rescue JSON::ParserError
  false # not json
end

#params_pathObject



95
96
97
# File 'lib/lono/importer.rb', line 95

def params_path
  "#{Lono.config.params_path}/base/#{template_name}.txt"
end

#runObject



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

def run
  unless options[:noop]
    download_template
    template_definition_path = add_template_definition
    create_params
    puts "=> Imported CloudFormation template and lono-fied it.".colorize(:green)
    puts "Template definition added to #{pretty_path(template_definition_path)}"
    puts "Params file created to #{pretty_path(params_path)}"
  end
  puts "Template downloaded to #{pretty_path(template_path)}" # like having this message at the end

  # at the end display some useful info for the user
  return unless options[:summary]
  summarize
  show_params_file
end

#show_params_fileObject



35
36
37
38
39
# File 'lib/lono/importer.rb', line 35

def show_params_file
  path = "config/params/base/#{template_name}.txt"
  puts "Here are contents of the params #{path.colorize(:green)} file:"
  puts IO.read("#{Lono.root}/#{path}")
end

#summarizeObject



31
32
33
# File 'lib/lono/importer.rb', line 31

def summarize
  Lono::Inspector::Summary.new(template_name, @options).run
end

#template_nameObject



104
105
106
107
108
109
# File 'lib/lono/importer.rb', line 104

def template_name
  return @options[:name] if @options[:name]
  # Else infer name from the original source.
  name = File.basename(@source, ".*")
  @options[:casing] == "camelcase" ? name.camelize : name.underscore.dasherize
end

#template_pathObject



100
101
102
# File 'lib/lono/importer.rb', line 100

def template_path
  "#{Lono.config.templates_path}/#{template_name}.#{@format}"
end