Class: Lono::Importer
- Inherits:
-
Object
- Object
- Lono::Importer
- Defined in:
- lib/lono/importer.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#add_template_definition ⇒ Object
Add template definition to app/definitions/base.rb.
-
#create_params ⇒ Object
Creates starter params/base/.txt file.
- #download_template ⇒ Object
-
#initialize(source, options) ⇒ Importer
constructor
A new instance of Importer.
- #json?(text) ⇒ Boolean
- #params_path ⇒ Object
- #run ⇒ Object
- #show_params_file ⇒ Object
- #summarize ⇒ Object
- #template_name ⇒ Object
- #template_path ⇒ Object
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, ) @source = source @options = @format = 'yml' Lono::ProjectChecker.check end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/lono/importer.rb', line 6 def @options end |
Instance Method Details
#add_template_definition ⇒ Object
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_params ⇒ Object
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_template ⇒ Object
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
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_path ⇒ Object
95 96 97 |
# File 'lib/lono/importer.rb', line 95 def params_path "#{Lono.config.params_path}/base/#{template_name}.txt" end |
#run ⇒ Object
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 [: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 [:summary] summarize show_params_file end |
#show_params_file ⇒ Object
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 |
#summarize ⇒ Object
31 32 33 |
# File 'lib/lono/importer.rb', line 31 def summarize Lono::Inspector::Summary.new(template_name, @options).run end |
#template_name ⇒ Object
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 |