4

I'm trying to build a static lib, whose name I only get after I process some files. I have something like this:

task :lib,:config do |t,args|
  # ... do some processing here, get a string representing the 
  # fullpath of the lib. Imaging libname contains a.lib
  file libname => object_files
end

But, of course, since I don't know the name of the dependency when the task gets ran, the code that should build a.lib doesn't get executed. I tried to do like this:

task :lib,:config do |t,args|
  # ... do some processing here, get a string representing the 
  # fullpath of the lib. Imaging libname contains a.lib
  file libname => object_files
  task :lib => [libname]
end

To add this as a dependency, but it doesn't work. I have it like this now, and it works:

task :lib,:config do |t,args|
  # ... do some processing here, get a string representing the 
  # fullpath of the lib. Imaging libname contains a.lib
  file libname => object_files
  Rake.application[libname].invoke
end

but I feel it's too ugly. Is there a better way to declare this dependency?

Geo
  • 93,257
  • 117
  • 344
  • 520

2 Answers2

3

I think this thread already has the best answer: How to run Rake tasks from within Rake tasks?

Community
  • 1
  • 1
Pablo B.
  • 1,823
  • 1
  • 17
  • 27
2
Rake::Task[libname].invoke

That looks slightly nicer to my eyes, i don't think there is a way of excuting a rake task within a rake task other than calling .execute or .invoke.

George
  • 4,273
  • 2
  • 15
  • 10
  • I think they are equivalent. So, probably, it's the only way. – Geo Apr 05 '11 at 13:57
  • Careful, they are not quite equivalent. #execute will *always* execute the task, even if it's already been invoked whereas #invoke will execute it only if it hasn't yet been invoked. Additionally, #execute won't execute prerequisites whereas #invoke will. – Michael Bishop Apr 01 '13 at 18:40