Forum: Ruby Rake Rule name problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
068a366d75adbde5644ef48c040f883a?d=identicon&s=25 Jim Boooo (jimflip)
on 2008-10-20 11:42
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.
068a366d75adbde5644ef48c040f883a?d=identicon&s=25 Jim Boooo (jimflip)
on 2008-10-20 11:45
That should read
>In my rake file a rule does not seem to produce a correct name.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-10-20 14:35
(Received via mailing list)
Try:
rule '.o' => ['.c'] do |t|
  p t.source
  p t.name
end

--wpd
068a366d75adbde5644ef48c040f883a?d=identicon&s=25 Jim Boooo (jimflip)
on 2008-10-21 15:57
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.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-10-21 23:03
(Received via mailing list)
>   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
068a366d75adbde5644ef48c040f883a?d=identicon&s=25 Jim Boooo (jimflip)
on 2008-10-22 12:51
> 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.
Ed437e52d8d6720308720e7e678f3e6d?d=identicon&s=25 Patrick Doyle (Guest)
on 2008-10-22 15:51
(Received via mailing list)
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
068a366d75adbde5644ef48c040f883a?d=identicon&s=25 Jim Boooo (jimflip)
on 2008-10-23 13:34
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.
This topic is locked and can not be replied to.