file(relative_source, relative_destination, file_options = {}, &block)
public
Copy a file from
source to destination with collision checking.
The file_options hash accepts :chmod and :shebang and :collision options.
:chmod sets the permissions of the destination file:
file 'config/empty.log', 'log/test.log', :chmod => 0664
:shebang sets the #!/usr/bin/ruby line for scripts
file 'bin/generate.rb', 'script/generate', :chmod => 0755, :shebang => '/usr/bin/env ruby'
:collision sets the collision option only for the destination file:
file 'settings/server.yml', 'config/server.yml', :collision => :skip
Collisions are handled by checking whether the destination file exists and
either skipping the file, forcing overwrite, or asking the user what to do.
Show source
def file(relative_source, relative_destination, file_options = {}, &block)
source = source_path(relative_source)
destination = destination_path(relative_destination)
destination_exists = File.exists?(destination)
if destination_exists and identical?(source, destination, &block)
return logger.identical(relative_destination)
end
if destination_exists
choice = case (file_options[:collision] || options[:collision]).to_sym
when :ask then force_file_collision?(relative_destination)
when :force then :force
when :skip then :skip
else raise "Invalid collision option: #{options[:collision].inspect}"
end
case choice
when :force then logger.force(relative_destination)
when :skip then return(logger.skip(relative_destination))
else raise "Invalid collision choice: #{choice}.inspect"
end
else
logger.create relative_destination
end
return if options[:pretend]
File.open(destination, 'wb') do |df|
File.open(source, 'rb') do |sf|
if block_given?
df.write(yield(sf))
else
if file_options[:shebang]
df.puts("#!#{file_options[:shebang]}")
if line = sf.gets
df.puts(line) if line !~ /^#!/
end
end
df.write(sf.read)
end
end
end
if file_options[:chmod]
FileUtils.chmod(file_options[:chmod], destination)
end
system("svn add #{destination}") if options[:svn]
end