class AppArchetype::Template::Plan

Plan builds an in memory representation of template output

Attributes

destination_path[R]
files[R]
template[R]
variables[R]

Public Class Methods

new( template, variables, destination_path: nil, overwrite: false ) click to toggle source

Creates a new plan from given source and variables.

@param [AppArchetype::Template::Source] template @param [AppArchetype::Template::VariableManager] variables @param [String] destination_path @param [Boolean] overwrite

# File lib/app_archetype/template/plan.rb, line 18
def initialize(
  template,
  variables,
  destination_path: nil,
  overwrite: false
)
  @template = template
  @destination_path = destination_path
  @files = []
  @variables = variables
  @overwrite = overwrite
end

Public Instance Methods

destination_exist?() click to toggle source

Check for whether the destination exists

@return [Boolean]

# File lib/app_archetype/template/plan.rb, line 66
def destination_exist?
  return false unless @destination_path

  File.exist?(
    File.dirname(@destination_path)
  )
end
devise() click to toggle source

Devise builds an in memory representation of what needs to be done to render the template.

When the destination path does not exist - a RuntimeError is raised - however at this stage we should always have a destination path to render to.

# File lib/app_archetype/template/plan.rb, line 39
def devise
  raise 'destination path does not exist' unless destination_exist?

  @template.files.each do |file|
    @files << OutputFile.new(
      file,
      render_dest_file_path(file)
    )
  end
end
execute() click to toggle source

Execute will render the plan to disk

# File lib/app_archetype/template/plan.rb, line 53
def execute
  renderer = Renderer.new(
    self,
    @overwrite
  )

  renderer.render
end
render_dest_file_path(source_path) click to toggle source

Determines what the destination file path is going to be by taking the source path, subbing the template path and then joining it with the specified destination path.

Calls render path to handle any handlebars moustaches included within the file name.

@param [String] source_path

@return [String]

# File lib/app_archetype/template/plan.rb, line 85
def render_dest_file_path(source_path)
  rel_path = render_path(
    source_path.gsub(@template.path, '')
  )

  File.join(@destination_path, rel_path)
end
render_path(path) click to toggle source

Renders template variables into any moustaches included in the filename

This permits us to have variable file names as well as variable file content.

@param [String] path

@return [String]

# File lib/app_archetype/template/plan.rb, line 103
def render_path(path)
  hbs = Handlebars::Handlebars.new
  hbs.compile(path).call(@variables.to_h)
end