Sha256: ba0551c65000eedf43c2676d992d995ad15a6a2a0cb2a4412a29f6dbbdef705c

Contents?: true

Size: 1.81 KB

Versions: 4

Compression:

Stored size: 1.81 KB

Contents

require "open-uri"
require "json"
require "yaml"

class Lono::Importer
  attr_reader :options
  def initialize(source, options)
    @source = source
    @options = options
    @format = normalize_format(@options[:format])
    @project_root = options[:project_root] || '.'
  end

  def run
    unless options[:noop]
      download_template
      template_definition_path = add_template_definition
      puts "Imported raw CloudFormation template and lono-fied it!"
      puts "Template definition added to #{template_definition_path}."
    end
    puts "Template downloaded to #{dest_path}."
  end

  def download_template
    template =  open(@source).read

    result = if @format == 'yml'
                YAML.dump(YAML.load(template))
              else
                JSON.pretty_generate(JSON.load(template))
              end

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

  # Add template definition to config/templates/base/stacks.rb.
  def add_template_definition
    path = "#{@project_root}/config/templates/base/stacks.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

  def dest_path
    "#{@project_root}/templates/#{template_name}.#{@format}"
  end

  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

private
  def normalize_format(format)
    format == 'yaml' ? 'yml' : format
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lono-3.3.2 lib/lono/importer.rb
lono-3.3.0 lib/lono/importer.rb
lono-3.2.1 lib/lono/importer.rb
lono-3.2.0 lib/lono/importer.rb