Re: rake rules question



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 Weirich

--
Posted via http://www.ruby-forum.com/.


.



Relevant Pages

  • Re: rake rules question
    ... simple file extenstions, you need to write them in this format: ... task stage1 => stage2 do ... puts File.read ...
    (comp.lang.ruby)
  • Re: grub
    ... My understanding is that stage1 has the disk address of either ... I just thought I could have a backup of mbr and the address it jumps to to ... filesystem, so it can find stage2 by name, not hardware address. ...
    (comp.os.linux.development.apps)
  • Re: grub
    ... know stage1 going into the MBR no problem. ... My understanding is that stage1 has the disk address of either ... stage1.5 or stage2 embedded in it. ... installs stage1 into the MBR, possibly installs stage1.5, *and* puts ...
    (comp.os.linux.development.apps)
  • grub stage2
    ... In grub boot loader, stage1 gets the drive paramaters, does some ... It gets to know the absolute address of stage2. ... This means that stage2 is aware of the file sytem?? ...
    (comp.os.linux.embedded)
  • stage2 GRUB
    ... In grub boot loader, stage1 gets the drive paramaters, does some ... It gets to know the absolute address of stage2. ... This means that stage2 is aware of the file sytem?? ...
    (comp.os.linux.development.system)