Class: Begin::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/begin/template.rb

Overview

Represents an installed template on the user’s machine.

Direct Known Subclasses

GitTemplate, SymlinkTemplate

Constant Summary collapse

CONFIG_NAME =
'.begin.yml'

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Template

Returns a new instance of Template.



16
17
18
19
# File 'lib/begin/template.rb', line 16

def initialize(path)
  @path = path
  @path.ensure_dir_exists
end

Instance Method Details

#configObject



35
36
37
# File 'lib/begin/template.rb', line 35

def config
  Config.from_file config_path
end

#config_pathObject



31
32
33
# File 'lib/begin/template.rb', line 31

def config_path
  Path.new CONFIG_NAME, @path, 'Config'
end

#ensure_name_not_empty(source_path, expanded_name) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/begin/template.rb', line 68

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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/begin/template.rb', line 46

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) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/begin/template.rb', line 57

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) ⇒ Object



104
105
106
107
# File 'lib/begin/template.rb', line 104

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) ⇒ Object



109
110
111
112
113
114
115
116
117
118
# File 'lib/begin/template.rb', line 109

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) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/begin/template.rb', line 76

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) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/begin/template.rb', line 94

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) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/begin/template.rb', line 84

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) ⇒ Object



39
40
41
42
43
44
# File 'lib/begin/template.rb', line 39

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

#uninstallObject

Raises:

  • (NotImplementedError)


21
22
23
24
# File 'lib/begin/template.rb', line 21

def uninstall
  # Must be implemented in base class
  raise NotImplementedError
end

#updateObject

Raises:

  • (NotImplementedError)


26
27
28
29
# File 'lib/begin/template.rb', line 26

def update
  # Must be implemented in base class
  raise NotImplementedError
end