Class: Wukong::Deploy::Templater

Inherits:
Object
  • Object
show all
Includes:
FileUtils::Verbose
Defined in:
lib/wukong-deploy/templater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Templater

Returns a new instance of Templater.



16
17
18
19
# File 'lib/wukong-deploy/templater.rb', line 16

def initialize root, options={}
  self.repo    = Repo.new(root)
  self.options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/wukong-deploy/templater.rb', line 12

def options
  @options
end

#repoObject

Returns the value of attribute repo.



11
12
13
# File 'lib/wukong-deploy/templater.rb', line 11

def repo
  @repo
end

Instance Method Details

#createObject



21
22
23
24
25
26
# File 'lib/wukong-deploy/templater.rb', line 21

def create
  create_dirs
  create_templates
  create_gitkeeps
  create_gitignore
end

#create_dirsObject



28
29
30
# File 'lib/wukong-deploy/templater.rb', line 28

def create_dirs
  repo.dirs_to_create.each { |dir| mkdir_p(dir) }
end

#create_gitignoreObject



55
56
57
# File 'lib/wukong-deploy/templater.rb', line 55

def create_gitignore
  create_template(templates_dir.join('gitignore'), repo.root.join('.gitignore'))
end

#create_gitkeepsObject



47
48
49
50
51
52
53
# File 'lib/wukong-deploy/templater.rb', line 47

def create_gitkeeps
  repo.dirs_to_create.each do |dir|
    if Dir[File.join(dir, '*')].empty?
      touch(File.join(dir, '.gitkeep'))
    end
  end
end

#create_template(input_path, output_path, binding = {}) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/wukong-deploy/templater.rb', line 38

def create_template input_path, output_path, binding={}
  input  = File.read(input_path)
  erb    = Erubis::Eruby.new(input)
  output = erb.result(binding)
  action = File.exist?(output_path) ? 'modify' : 'create'
  puts "#{action} #{output_path}"
  File.open(output_path, 'w') { |f| f.puts(output) }
end

#create_templatesObject



32
33
34
35
36
# File 'lib/wukong-deploy/templater.rb', line 32

def create_templates
  repo.files_to_template.each do |repo_path|
    create_template(templates_dir.join("#{repo_path}.erb"), repo.root.join(repo_path))
  end
end

#templates_dirObject



59
60
61
# File 'lib/wukong-deploy/templater.rb', line 59

def templates_dir
  @templates_dir ||= Pathname.new(File.expand_path('../../../templates', __FILE__))
end