Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/tty/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +293 to +296
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this statement makes the directory check unreachable. Also, I much prefer to allow copying a file to a directory location rather than providing an error. Wouldn't you agree?


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
Comment on lines +300 to +302
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! However, I think we shouldn't change the destination path. If someone wants to copy a file to an existing directory, we should allow for it and the file collision will handle the situation when the file already exists. For example,

TTY::File.copy_file("foo/bar.rb", "baz/")
# => baz/bar.rb

Also, any changes like this require a test case.


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
Expand All @@ -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 link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test case that confirms this behaviour so that it stays true in future releases.


copy_metadata(source_path, dest_path, verbose: verbose, noop: noop,
color: color)
file_dest
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a test case that confirms this behaviour so that it stays true in future releases.

end
module_function :copy_file

Expand Down
9 changes: 9 additions & 0 deletions lib/tty/file/create_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -59,6 +63,11 @@ def convert_encoded_path(filename)
#
# @api private
def detect_collision
if directory?
notify(:error, :red)
return
end
Comment on lines +66 to +69
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this is necessary if we allow copying a file to a directory destination. This should never be the case unless someone tries to copy a directory to directory, I guess we could test for that case. This would require another test case.


if exist?
if identical?
notify(:identical, :cyan)
Expand Down