Rake rules question

I am using RMagick to do multi-stage processing of some images.

Can I have two different rules to process .PNG files in two different
directories?

rule stage_0/.png => source/.png do |t|
make_0 t.source, t.name
end

rule stage_1/.png => stage_0/.png do |t|
make_1 t.source, t.name
end

I can’t seem to get this to work, and don’t know if it is supposed to.

Thanks.

Jim W. wrote:
[… Rakefile example elided …]

There are some extra lines of dashes in that Rakefile example. I swear
they weren’t there when I pressed send. Oh well, hopefully that will
get you started.


– Jim W.

itsme213 wrote:

I am using RMagick to do multi-stage processing of some images.

Can I have two different rules to process .PNG files in two different
directories?

rule stage_0/.png => source/.png do |t|
make_0 t.source, t.name
end

rule stage_1/.png => stage_0/.png do |t|
make_1 t.source, t.name
end

I can’t seem to get this to work, and don’t know if it is supposed to.

It will work. The attached Rakefile will demonstrate the principle.
Basically, if you want your rules to trigger on something other than
simple file extenstions, you need to write them in this format:

rule(REGEX => LAMBDA_TRANSFORMER) do
ACTIONS
end

The REGEX should match the thing your are trying to build. The
LAMBDA_TRANSFORMER is just a lambda expression with a single input that
transforms the name of the target (i.e. whatever was matched by the
REGEX) into the target of the task (i.e. the thing the rule is to
produce).

For example, your regex might be %r{stage1/.*.png$} to match a file
that should be in stage 1. Your lambda could be

lambda { |fn| fn.sub(/^stage1/, ‘source’) }

Make appropriate changes for the stage1 to stage2 work.

If you find rules confusing, an alternative is dynamically generate the
tasks in a list. For example:

SRC_FILES = FileList['source/*.png']

SRC_FILES.each do |fn|
  stage1 = fn.sub(/^source/, 'stage1')
  stage2 = fn.sub(/^source/, 'stage2')
  task stage1 => stage2 do
    do_stage_one_transform(fn, stage1)
  end
  task stage2 => stage1 do
    do_stage_two_transform(stage2, stage1)
  end
end

In many ways this is easier than rules and just as flexible.

Anyways, here is the example Rakefile using rules:


#!/usr/bin/env ruby

require ‘rake/clean’

CLOBBER.include ‘stage1’, ‘stage2’

task :default => %w(stage1 stage2 stage2/a.png)

directory ‘stage1’
directory ‘stage2’

rule(%r{^stage1/.*.png$} => lambda { |fn| fn.sub(/^stage1/, ‘source’)
}) do |t|
File.open(t.source) do |ins|
File.open(t.name, “w”) do |outs|
outs.write ins.read
outs.puts “Added to stage one”
end
end
end

rule(%r{^stage2/.*.png$} => lambda { |fn| fn.sub(/^stage2/, ‘stage1’)
}) do |t|
File.open(t.source) do |ins|
File.open(t.name, “w”) do |outs|
outs.write ins.read
outs.puts “Added to stage two”
end
end
end

Other tasks --------------------------------------------------------

task :init => :clobber do
File.open(“source/a.png”, “w”) do |outs|
outs.puts “A PNG FILE”
end
end

task :see do
puts “-- Contents of source/a.png -------------------------------”
puts File.read(“source/a.png”)
puts “-- Contents of stage2/a.png -------------------------------”
puts File.read(“stage1/a.png”)
puts “-- Contents of stage2/a.png -------------------------------”
puts File.read(“stage2/a.png”)
end


– Jim W.

Thanks, Jim!

“Jim W.” [email protected] wrote

Basically, if you want your rules to trigger on something other than
simple file extenstions, you need to write them in this format:

rule(REGEX => LAMBDA_TRANSFORMER) do
ACTIONS
end

Lovely. I will try this out.