lib/wukong-deploy/templater.rb in wukong-deploy-0.0.1 vs lib/wukong-deploy/templater.rb in wukong-deploy-0.0.2
- old
+ new
@@ -1,55 +1,68 @@
require 'pathname'
require 'fileutils'
require 'erubis'
require_relative('repo')
+require_relative('templater/messaging')
+require_relative('templater/conflict_resolution')
module Wukong
module Deploy
class Templater
attr_accessor :repo
attr_accessor :options
- include FileUtils::Verbose
+ include FileUtils
+ include Messaging
+ include ConflictResolution
def initialize root, options={}
self.repo = Repo.new(root)
self.options = options
end
- def create
+ def dry_run?
+ @options[:dry_run]
+ end
+
+ def run!
+ if dry_run?
+ puts "Would perform the following actions in #{repo.root}"
+ else
+ puts "Within #{repo.root}:"
+ end
create_dirs
create_templates
create_gitkeeps
create_gitignore
end
-
+
def create_dirs
- repo.dirs_to_create.each { |dir| mkdir_p(dir) }
+ repo.dirs_to_create.each do |dir|
+ create_directory(dir)
+ end
end
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
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) }
+ input = File.read(input_path)
+ erb = Erubis::Eruby.new(input)
+ content = erb.result(binding)
+ create_file(content, output_path)
end
def create_gitkeeps
repo.dirs_to_create.each do |dir|
if Dir[File.join(dir, '*')].empty?
- touch(File.join(dir, '.gitkeep'))
+ create_file(empty_file, File.join(dir, '.gitkeep'))
end
end
end
def create_gitignore
@@ -57,9 +70,35 @@
end
def templates_dir
@templates_dir ||= Pathname.new(File.expand_path('../../../templates', __FILE__))
end
+
+ private
+
+ def empty_file
+ ""
+ end
+ def create_file content, path
+ if File.exist?(path)
+ handle_conflict(content, path)
+ else
+ message_create(path)
+ write_file(content, path)
+ end
+ end
+
+ def create_directory(dir)
+ message_create(dir)
+ return if dry_run?
+ mkdir_p(dir)
+ end
+
+ def write_file content, path
+ return if dry_run?
+ File.open(path, 'w') { |f| f.write(content) }
+ end
+
end
end
end