[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rake Rule name problem

Jim flip

10/20/2008 9:42:00 AM


I my rake file a rule does not seem to produce a correct name.

i.e.

rule '.c' do |t|
p t.name #prints out '.c'
end

Should this not be the name of the file it is going to produce i.e.
blah.c

Thanks,
Jim.
--
Posted via http://www.ruby-....

7 Answers

Jim flip

10/20/2008 9:44:00 AM

0

That should read
>In my rake file a rule does not seem to produce a correct name.


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

Patrick Doyle

10/20/2008 12:33:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

Try:
rule '.o' => ['.c'] do |t|
p t.source
p t.name
end

--wpd


On Mon, Oct 20, 2008 at 5:44 AM, Jim Boooo <james@dimsum.tv> wrote:

> That should read
> >In my rake file a rule does not seem to produce a correct name.
>
>
> --
> Posted via http://www.ruby-....
>
>

Jim flip

10/21/2008 1:56:00 PM

0


for the script:

----------------------------------------------------------------
task :default do
@name = "do some stuff here to get path and name"
Rake::Task[:go].invoke
end

task :go =>["#{@name}.c"]

rule '.c' => ['.xml'] do |t|
p t.name
p t.source
end

rule '.xml' do
#create the xml file in here
end

-------------------------------------------------------------------

I get the following results:

".c"
".c.xml"

Any help as to why I don't get the name of the file I'm creating or any
other advice will be much appreciated.

Thanks,
Jim.
--
Posted via http://www.ruby-....

Patrick Doyle

10/21/2008 5:43:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

>
> task :default do
> @name = "do some stuff here to get path and name"
> Rake::Task[:go].invoke
> end
>
> task :go =>["#{@name}.c"]
>
> rule '.c' => ['.xml'] do |t|
> p t.name
> p t.source
> end
>
> rule '.xml' do
> #create the xml file in here
> end
>
> The misunderstanding is in how rakefiles get interpreted. A rakefile is
simply a bunch of Ruby code. When you invoke rake at the command line, it
effectively loads a few libraries, which define some handy methods for you,
and then hands your rakefile off to the Ruby interpreter.

One of those "handy methods" I mentioned was one called "task" which takes
takes an argument (or two, or three) and a block. It records the first
argument as the name of the task (and records things like the prerequisites
and argument names if you pass more parameters) and it records the block to
be executed if/when that named task gets invoked.

By default, after the Ruby interpreter interprets all of the Ruby code in
the makefile, the rake utility invokes the task named :default.

So, in your case, when the interpreter hits the first "task" method call, it
records the name, :default, and some code to be executed if/when the
:default task is executed. Notice that the code inside the "do" block is
not executed at this point.

When the interpreter hits the second "task" method call, it records the
name, :go, and says that that task has a prerequisite of, hmmm... at that
point, the interpreter doesn't know anything about @name, so the :go task
has a prerequisite of ".c".

At least, that's what I think is going on. Try replacing the line that
reads:

task :go =>["#{@name}.c"]

with a line that reads:

task :go =>["blah.c"]

and see if that gives you more insight into what's going on.

I'm not sure I have any good suggestions for accomplishing what you want to
do. One idea might be to recognize that a Rakefile is simply ruby code, and
instead of defining a '.c' => ['.xml'] rule, you could define a method:

def c_to_xml(source)
sh blah blah blah
end

and invoke #c_to_xml from your :default task.

hth

--wpd

Jim flip

10/22/2008 10:50:00 AM

0


> task :go =>["blah.c"]

Maybe it will work like that but it's not useful. I don't know until
run time the name of the file I want to create.

> def c_to_xml(source)
> sh blah blah blah
> end
>

Would that not then defeat the purpose of rake, I could just use a
standard ruby script and I would not get any of the features of rules
and file tasks?

the :go task might well be the problem though, I might have to create
that task programatticaly then invoke it.

Many thanks for helping,
Jim.
--
Posted via http://www.ruby-....

Patrick Doyle

10/22/2008 1:49:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Wed, Oct 22, 2008 at 6:49 AM, Jim Boooo <james@dimsum.tv> wrote:

>
> > task :go =>["blah.c"]
>
> Maybe it will work like that but it's not useful. I don't know until
> run time the name of the file I want to create.

Sorry, I just meant that to be used as an example to show what was going
on. From my understanding of your problem, I didn't think it would be
useful.

Would something like this solve your problem?

def figure_out_names
# This should return a list of names
end

# assuming that figure_out_names() returns the names w/o the ".c" extension
list_of_names = figure_out_names()
list_of_names.each do |name|
task name + ".c" => name + ".xml"
end

task :default => list_of_names.map {|n| n + ".c"}

rule '.c' => ['.xml'] do |t|
p t.name
p t.source
end

--wpd

Jim flip

10/23/2008 11:33:00 AM

0


ok I changed the previous script to this

----------------------------------------------------------------
task :default do
@name = "do some stuff here to get path and name"

go_task = task :go => ["{#name}.c"]
go_task.invoke
end

rule '.c' => ['.xml'] do |t|
p t.name
p t.source
end

rule '.xml' do
#create the xml file in here
end

-------------------------------------------------------------------
This now works a treat, Patrick was largely right about the names of
tasks getting assigned before the variable was getting a value.
The solution was to programatticaly create the task inside my default
task.

Many thanks for the help,
Jim.
--
Posted via http://www.ruby-....