Rake: multiple prerequisites in rule?

  • Thread starter Martin Honermeyer
  • Start date
M

Martin Honermeyer

Hello,

I am using Kwartz as a templating engine for my Rails project. Kwartz
compiles an .rhtml (ERB) file from an .html and a .plogic file.

I tried writing a Rake task which should automagically recompile each
template upon changing either the .html or the .plogic file. I've come up
with the following:

desc "Make ERB templates from Kwartz html and plogic files"
SRC = FileList['app/views/**/*.html']
OBJ = SRC.ext('rhtml')
task :templates => [OBJ]

rule '.rhtml' => ['.html'] do |t|
sh "kwartz -Rails -e -p #{t.source.ext('plogic')} --extract=content
#{t.source} > #{t.name}"
end


This works. The problem is that the .rhtml is only regenerated when
the .html changes. I tried adding the .plogic file as another prerequisite:

rule '.rhtml' => ['.html', '.plogic'] do |t|

That makes Rake say

Too many dependents specified in rule .rhtml: [".html", ".plogic"]


Apparently, it's not possible to have multiple prerequisites for a rule.
Does anyone have a hint on this?



Cheers,
Martin
 
S

Stefan Lang

]
I tried writing a Rake task which should automagically recompile
each template upon changing either the .html or the .plogic file.
I've come up with the following:

desc "Make ERB templates from Kwartz html and plogic files"
SRC = FileList['app/views/**/*.html']
OBJ = SRC.ext('rhtml')
task :templates => [OBJ]

rule '.rhtml' => ['.html'] do |t|
sh "kwartz -Rails -e -p #{t.source.ext('plogic')}
--extract=content #{t.source} > #{t.name}"
end


This works. The problem is that the .rhtml is only regenerated when
the .html changes. I tried adding the .plogic file as another
prerequisite:

rule '.rhtml' => ['.html', '.plogic'] do |t|

That makes Rake say

Too many dependents specified in rule .rhtml: [".html", ".plogic"]


Apparently, it's not possible to have multiple prerequisites for a
rule. Does anyone have a hint on this?

Rant allows you to give a block to create the prerequisite name(s)
on the fly. The following works with Rant:

############## Rantfile ############################################
desc "Make ERB templates from Kwartz html and plogic files"
SRC = FileList['app/views/**/*.html']
OBJ = SRC.sub_ext('rhtml')
task :templates => OBJ

rhtml_sources = lambda { |target|
[target.sub_ext("html"), target.sub_ext("plogic")]
}
gen Rule, '.rhtml' => rhtml_sources do |t|
sys "kwartz -Rails -e -p #{t.prerequisites[1]} --extract=content
#{t.prerequisites[0]} > #{t.name}"
end
####################################################################

To convert the Rakefile to an Rantfile I've:
* replaced calls to `ext' with calls to `sub_ext'
* replaced `rule' with `gen Rule, '

HTH,
Stefan
 
M

Martin Honermeyer

Stefan said:
Rant allows you to give a block to create the prerequisite name(s)
on the fly. The following works with Rant:

############## Rantfile ############################################
desc "Make ERB templates from Kwartz html and plogic files"
SRC = FileList['app/views/**/*.html']
OBJ = SRC.sub_ext('rhtml')
task :templates => OBJ

rhtml_sources = lambda { |target|
[target.sub_ext("html"), target.sub_ext("plogic")]
}
gen Rule, '.rhtml' => rhtml_sources do |t|
sys "kwartz -Rails -e -p #{t.prerequisites[1]} --extract=content
#{t.prerequisites[0]} > #{t.name}"
end
####################################################################

To convert the Rakefile to an Rantfile I've:
* replaced calls to `ext' with calls to `sub_ext'
* replaced `rule' with `gen Rule, '

Thanks Stefan!

So there is no solution using Rake? Would have been nice since Rails comes
preinstalled with Rake. That means all devs will have to install Rant, too.
Okay, if there is no other way..


Greetings,
Martin
 
N

Niklas Frykholm

Martin said:
desc "Make ERB templates from Kwartz html and plogic files"
SRC = FileList['app/views/**/*.html']
OBJ = SRC.ext('rhtml')
task :templates => [OBJ]

rule '.rhtml' => ['.html'] do |t|
sh "kwartz -Rails -e -p #{t.source.ext('plogic')} --extract=content
#{t.source} > #{t.name}"
end

This works. The problem is that the .rhtml is only regenerated when
the .html changes. I tried adding the .plogic file as another prerequisite:

rule '.rhtml' => ['.html', '.plogic'] do |t|

That makes Rake say

Too many dependents specified in rule .rhtml: [".html", ".plogic"]

Apparently, it's not possible to have multiple prerequisites for a rule.
Does anyone have a hint on this?
Not as elegant as using a rule perhaps, but you could write

SRC.each {|src|
rhtml = src.ext('rhtml')
plogic = src.ext('plogic')
file src => [rhtml, plogic] do |t|

// Niklas
 
M

Martin Honermeyer

Niklas said:
Apparently, it's not possible to have multiple prerequisites for a rule.
Does anyone have a hint on this?
Not as elegant as using a rule perhaps, but you could write

SRC.each {|src|
rhtml = src.ext('rhtml')
plogic = src.ext('plogic')
file src => [rhtml, plogic] do |t|

Thanks, that does it! So that means there will be one definition (in memory)
for every file. Have to see whether this scales for many templates.


Greetz,
Martin
 
J

Jim Weirich

Apparently, it's not possible to have multiple prerequisites for a rule.
Does anyone have a hint on this?

Rake 0.6.0 (just released) now supports multiple prerequisites in a rule.
 
M

Martin Honermeyer

Jim said:
Rake 0.6.0 (just released) now supports multiple prerequisites in a rule.

Great! Now this works as it should:

rule '.rhtml' => ['.html', '.plogic'] do |t|
sh "kwartz -Rails -e -p #{t.source.ext('plogic')} --extract=content
#{t.source} > #{t.name}"
end



Greetz,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top