From: itsme213 Date: 2006-03-18T03:03:49+09:00 Subject: Re: rake rules question Thanks, Jim! "Jim Weirich" 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. > 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/. > >