knpwrs
knpwrs

Reputation: 16456

Rake - Copy only changed files

So I have a rake file like the following:

require 'fileutils'

task :copy do
  FileUtils.cp_r 'src', 'target'
end

How can I:

  1. Only copy the files that have changed?
  2. Make the :copy task have a dependency on the src directory so that it will only launch if it needs to? :copy => 'src' and :copy => FileList['src/*'].to_a don't seem to work.

I can take care of the first question like this:

task :copy do
    sh 'rsync -ru src/* target'
end

I would like to, if reasonably possible, do this with only ruby / rake. This also somewhat takes care of the second question because rsync won't do anything if no files have changed, but I would like for the rake task to not execute at all if possible.

Upvotes: 2

Views: 2263

Answers (2)

peter
peter

Reputation: 42192

Here a solution that is OS independant and pure Ruby

class File
  def self.update(src, dst)
    if File.exist? dst
      # skip if src file modification or creation time is same or earlier than target
      return if [File.ctime(src), File.mtime(src)].max <= [File.ctime(dst), File.mtime(dst)].max
    else
      # create target folder if not present
      FileUtils.mkdir_p(File.dirname(dst)) unless File.exist? File.dirname(dst)
    end
    FileUtils.cp(src, dst)
    puts src
  rescue => e
    puts "#{e}\n#{e.backtrace}"
  end
end

File.update source_file, target_file

Upvotes: 1

Jerry
Jerry

Reputation: 56

To avoid executing the task, Rake needs to know both the target and source and the rule for determining whether to execute the task in order to create the target from the source.

Normally that is rule is "time modified", i.e. if source is newer than target then Rake will run the task. You can modify this by providing a Proc as the source dependency. See http://docs.rubyrake.org/user_guide/chapter03.html under "Advanced Rules" (but it does take some experimentation to get your head around what is happening!).

So your task must depend on the target. If you know the target does not exist it should be easy:

task :copy => 'target/' do
  sh 'rsync -ru src/ target'  # trailing slash is significant; target will be created
done

If target exists already it gets much more complicated. You could define a rule for each and every file but frankly I'd just run the rsync and be done with it. Rsync is very fast on local filesystems, running it everytime is just no big deal.

Upvotes: 4

Related Questions