2

I have a rakefile with a task that copies the contents of a directory to somewhere else on disk. When the rakefile is executing, I need this task to run if any of the contents of this directory have changed since the last execution of the rakefile (or if the copy has never occurred). This includes changes to modification dates or file names. (Note that changing the names of files does not change their modification date.)

Put another way, I want something that works the same way as a file task that has a dependency on another file. But rather than it involving individual files, I want the task to generate a directory structure and for its dependency to be another directory structure.

What would be the best method of accomplishing this? I need a cross platform solution as this rakefile is run on Windows, OS X, and Linux.

Bri Bri
  • 2,169
  • 3
  • 19
  • 44
  • 1
    The `guard` gem would be good for this. – max pleaner Aug 17 '16 at 16:22
  • https://github.com/guard/guard-process or https://github.com/guard/guard-yield should work. I believe there is another guard for Ruby scripts, but didn't find it. – B Seven Aug 17 '16 at 17:42
  • One thing I have noticed when running specs is that Guard does not run when a file is added, so you may need to do something to handle that case. – B Seven Aug 17 '16 at 17:43
  • @BSeven @maxpleaner Sorry, I didn't explain my issue properly. I don't want this to happen automatically. I only want the directory copy to happen when the rakefile is executed, so `guard` doesn't help me here. I have edited my original question to better clarify what I'm looking for. – Bri Bri Aug 17 '16 at 18:51

1 Answers1

0

rake is described as

Rake is a Make-like program implemented in Ruby.

Like makeit allows defining dependencies and run tasks only if needed.
See also this RubyTapa.

It boils down to:

file 'target' => 'source' do
  sh "your_transform_command"
end

You can also write general rules:

rule '.html' => '.md' do |file|
  sh "pandoc -o #{file.name} #{file.souce}"
end

and then run this command to transform README.md to README.haml only if README.md is newer than README.html:

rake README.html
Martin M
  • 8,430
  • 2
  • 35
  • 53