From 0a8cfbee37256dcd4c62c0d0ea5101551cb26ff5 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 5 Feb 2021 02:44:08 +0100 Subject: [PATCH] Add return value to copy_file(). Correct source is directory, source does not exist, destination is a directory corner cases. #13 --- lib/tty/file.rb | 15 +++++++++++++-- lib/tty/file/create_file.rb | 9 +++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/tty/file.rb b/lib/tty/file.rb index 0b0caee..8d6400a 100644 --- a/lib/tty/file.rb +++ b/lib/tty/file.rb @@ -288,16 +288,26 @@ def create_file(relative_path, *args, context: nil, force: false, skip: false, # @api public def copy_file(source_path, *args, context: nil, force: false, skip: false, verbose: true, color: :green, noop: false, preserve: nil, &block) + source_path = source_path.to_s + unless ::File.file? source_path + log_status(:error, source_path, verbose: verbose, color: :red) + return + end + dest_path = (args.first || source_path).to_s.sub(/\.erb$/, "") + if ::File.directory? dest_path + dest_path = ::File.join(dest_path, ::File.basename(source_path)) + end + ctx = if context context.instance_eval("binding") else instance_eval("binding") end - create_file(dest_path, context: context, force: force, skip: skip, + file_dest = create_file(dest_path, context: context, force: force, skip: skip, verbose: verbose, color: color, noop: noop) do version = ERB.version.scan(/\d+\.\d+\.\d+/)[0] template = if version.to_f >= 2.2 @@ -309,10 +319,11 @@ def copy_file(source_path, *args, context: nil, force: false, skip: false, content = block[content] if block content end - return unless preserve + return file_dest unless preserve copy_metadata(source_path, dest_path, verbose: verbose, noop: noop, color: color) + file_dest end module_function :copy_file diff --git a/lib/tty/file/create_file.rb b/lib/tty/file/create_file.rb index 578cd8c..0858820 100644 --- a/lib/tty/file/create_file.rb +++ b/lib/tty/file/create_file.rb @@ -27,6 +27,10 @@ def exist? ::File.exist?(relative_path) end + def directory? + ::File.directory?(relative_path) + end + def identical? ::File.binread(relative_path) == content end @@ -59,6 +63,11 @@ def convert_encoded_path(filename) # # @api private def detect_collision + if directory? + notify(:error, :red) + return + end + if exist? if identical? notify(:identical, :cyan)