Class: Tap::Tasks::Copy

Inherits:
FileTask
  • Object
show all
Defined in:
lib/tap/tasks/copy.rb

Overview

:startdoc::manifest copies files

Copies a list of files to the specified directory. The files will be copied using the relative filepath from Dir.pwd, or the file basename if the filepath is not relative to Dir.pwd. For example: when copying to ‘/target_dir’ from Dir.pwd = ‘/dir’:

source path              target path
/dir/path/to/file.txt    /target_dir/path/to/file.txt
/path/to/file.txt        /target_dir/file.txt

Existing files are backed up as ‘<file>before<timestamp>’ into the standard backup directory. Up-to-date files are not copied. Raises an error for non-existing and non-file input files, as well as a non-directory target_dir.

Instance Method Summary collapse

Instance Method Details

#backup_filepath(path) ⇒ Object

Determines a backup filepath by adding a timestamp to the input path.



30
31
32
33
# File 'lib/tap/tasks/copy.rb', line 30

def backup_filepath(path)
  extname = File.extname(path)
  File.expand_path("#{path.chomp(extname)}_before_#{Time.now.strftime(timestamp)}#{extname}")
end

#copy_filepath(target_dir, path) ⇒ Object

Determines the copy filepath using the target_dir and the relative filepath from Dir.pwd to path. Uses the basename of path if path is not relative to Dir.pwd.



24
25
26
27
# File 'lib/tap/tasks/copy.rb', line 24

def copy_filepath(target_dir, path)
  relative_path = Root.relative_filepath(Dir.pwd, path) || File.basename(path)
  File.join(target_dir, relative_path)
end

#process(target_dir, *filepaths) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/tap/tasks/copy.rb', line 35

def process(target_dir, *filepaths)
  filepaths.collect do |filepath|
    target = copy_filepath(target_dir, filepath)
    
    if uptodate?(target, filepath)
      log_basename :skip, filepath, Logger::DEBUG
    else
      prepare target
  
      log_basename :cp, filepath
      FileUtils.cp(filepath, target) 
    end

    target
  end
end