class Begin::Template

Represents an installed template on the user's machine.

Constants

CONFIG_NAME

Public Class Methods

new(path) click to toggle source
# File lib/begin/template.rb, line 14
def initialize(path)
  @path = path
  @path.ensure_dir_exists
end

Public Instance Methods

config() click to toggle source
# File lib/begin/template.rb, line 33
def config
  Config.from_file config_path
end
config_path() click to toggle source
# File lib/begin/template.rb, line 29
def config_path
  Path.new CONFIG_NAME, @path, 'Config'
end
ensure_name_not_empty(source_path, expanded_name) click to toggle source
# File lib/begin/template.rb, line 64
def ensure_name_not_empty(source_path, expanded_name)
  return unless expanded_name.empty?
  err = "Mustache evaluation resulted in an empty file name...\n"
  err += "... whilst evaluating: #{source_path}"
  raise err
end
ensure_no_back_references(source_path, expanded_path, target_dir) click to toggle source
# File lib/begin/template.rb, line 44
def ensure_no_back_references(source_path, expanded_path, target_dir)
  return if target_dir.contains? expanded_path
  err = 'Backward-reference detected in expanded ' \
        "template path. Details to follow.\n"
  err += "Source Path:     #{source_path}\n"
  err += "Expanded Path:   #{expanded_path}\n"
  err += "Expected Parent: #{target_dir}\n"
  raise err
end
ensure_no_conflicts(paths, source_path, target_path) click to toggle source
# File lib/begin/template.rb, line 54
def ensure_no_conflicts(paths, source_path, target_path)
  return unless paths.key? target_path
  err = "File path collision detected. Details to follow.\n"
  err += "(1) Source File: #{source_path}\n"
  err += "(1) ..Writes To: #{target_path}\n"
  err += "(2) Source File: #{paths[target_path]}\n"
  err += "(2) ..Writes To: #{target_path}\n"
  raise err
end
process_file(source_path, target_path, context) click to toggle source
# File lib/begin/template.rb, line 99
def process_file(source_path, target_path, context)
  contents = File.read source_path
  File.write target_path, Mustache.render(contents, context)
end
process_files(paths, context) click to toggle source
# File lib/begin/template.rb, line 104
def process_files(paths, context)
  paths.each do |target, source|
    target.make_parent_dirs
    if source.directory?
      target.make_dir
    else
      process_file source, target, context
    end
  end
end
process_path_name(source_path, target_dir, context) click to toggle source
# File lib/begin/template.rb, line 71
def process_path_name(source_path, target_dir, context)
  expanded_name = Mustache.render source_path.basename, context
  ensure_name_not_empty source_path, expanded_name
  expanded_path = Path.new expanded_name, target_dir, 'Target'
  ensure_no_back_references source_path, expanded_path, target_dir
  expanded_path
end
process_path_names(source_dir, target_dir, context) click to toggle source
# File lib/begin/template.rb, line 89
def process_path_names(source_dir, target_dir, context)
  paths = {}
  working_set = [[source_dir, target_dir]]
  until working_set.empty?
    source, target = working_set.pop
    process_path_names_in_dir source, target, paths, working_set, context
  end
  paths
end
process_path_names_in_dir(source, target, paths, working_set, context) click to toggle source
# File lib/begin/template.rb, line 79
def process_path_names_in_dir(source, target, paths, working_set, context)
  source.dir_contents.each do |entry|
    source_path = Path.new entry, '.', 'Source'
    target_path = process_path_name source_path, target, context
    ensure_no_conflicts paths, source_path, target_path
    paths[target_path] = source_path
    working_set.push [source_path, target_path] if source_path.directory?
  end
end
run(target_dir, context) click to toggle source
# File lib/begin/template.rb, line 37
def run(target_dir, context)
  target_dir = Path.new target_dir, '.', 'Directory'
  target_dir.ensure_dir_exists
  paths = process_path_names @path, target_dir, context
  process_files paths, context
end
uninstall() click to toggle source
# File lib/begin/template.rb, line 19
def uninstall
  # Must be implemented in base class
  raise NotImplementedError
end
update() click to toggle source
# File lib/begin/template.rb, line 24
def update
  # Must be implemented in base class
  raise NotImplementedError
end