generators/base_generator.rb in howitzer-1.0.2 vs generators/base_generator.rb in howitzer-1.1.0

- old
+ new

@@ -1,53 +1,47 @@ require 'fileutils' module Howitzer class BaseGenerator - def self.logger=(logger) - @logger = logger + class << self + attr_accessor :logger, :destination end - def self.destination=(destination) - @destination = destination - end - def initialize print_banner manifest.each do |type, list| case type when :files copy_files(list) - #:nocov: when :templates copy_templates(list) else nil - #:nocov: end end end def manifest; end protected def banner; end def logger - BaseGenerator.instance_variable_get(:@logger) || $stdout + BaseGenerator.logger || $stdout end def destination - BaseGenerator.instance_variable_get(:@destination) || Dir.pwd + BaseGenerator.destination || Dir.pwd end def copy_files(list) list.each do |data| source_file = source_path(data[:source]) if File.exists?(source_file) copy_with_path(data) else - print_error("File '#{source_file}' was not found.") + puts_error("File '#{source_file}' was not found.") end end end def copy_templates(list) @@ -57,14 +51,18 @@ def print_banner logger.puts banner unless banner.empty? end def print_info(data) + logger.print " #{data}" + end + + def puts_info(data) logger.puts " #{data}" end - def print_error(data) + def puts_error(data) logger.puts " ERROR: #{data}" end def source_path(file_name) File.expand_path( @@ -78,12 +76,29 @@ def copy_with_path(data) src = source_path(data[:source]) dst = dest_path(data[:destination]) FileUtils.mkdir_p(File.dirname(dst)) - FileUtils.cp(src, dst) - print_info("Added '#{data[:destination]}' file") + if File.exists?(dst) + if FileUtils.identical?(src, dst) + puts_info("Identical '#{data[:destination]}' file") + else + puts_info("Conflict with '#{data[:destination]}' file") + print_info(" Overwrite '#{data[:destination]}' file? [Yn]:") + case gets.strip.downcase + when 'y' + FileUtils.cp(src, dst) + puts_info(" Forced '#{data[:destination]}' file") + when 'n' then nil + puts_info(" Skipped '#{data[:destination]}' file") + else nil + end + end + else + FileUtils.cp(src, dst) + puts_info("Added '#{data[:destination]}' file") + end rescue => e - print_error("Impossible to create '#{data[:destination]}' file. Reason: #{e.message}") + puts_error("Impossible to create '#{data[:destination]}' file. Reason: #{e.message}") end end end \ No newline at end of file