About Books Credits Photos Software Rumblings Travelling Home
side-effects: Part 2

I was talking about the to_target method yesterday, a snippet of code to copy files with a rake task. At the end I was looking for a way of caching the names of the files generated so that they can be used to incrementally scp them.

As always, sleeping on a problem makes finding the solution easier.

Initially I went for caching the target names from within the task created in the to_target method, but this required that I use some kind of member (or global) variable.

Moving the method to a module in my own snippet library (where I put all the snippets I use repeatedly) quickly showed that this approach was flawed (more like inelegant).

I need to include the module and add code that initialises the member variable if it’s not there like this:


    @changed_targets=Array.new unless @changed_targets

    @changed_targets<<tgt

and there is always the danger that I will forget myself and name another member variable the same in some other script.

Instead I went back to the Rake reference pages and found what I was looking for: The targets that change have tasks that are needed, e.g. their file tasks are going to be executed. The following code (from the rakefile for this site) marks which files need to be actually scp’ed. The emphasized line is the answer to yesterday’s question.


RIVA.each{|src|

    targeted=to_target(src,PUBLISH)

    task :riva_site =>targeted

    @changed_files<<targeted if Rake::Task.lookup(targeted).needed?

}

By the way, Task.lookup() returns a new task if one cannot be found, but in this context, we have just created the task we’re looking up, so unexpected behaviour is, well…unexpected.