Is it possible to know the current rake task within ruby:
# Rakefile
task :install do
MyApp.somemethod(options)
end
# myapp.rb
class MyApp
def somemetod(opts)
## current_task?
end
end
Edit
I'm asking about any enviroment|global variable that can be queried about that, because I wanted to make an app smart about rake, not modify the task itself. I'm thinking of making an app behave differently when it was run by rake.
A better way would be use the block parameter
Rake tasks aren't magical. This is just like any method invocation.
Easiest (and clearest) way to accomplish what you want is to just pass the task into the function as an optional parameter.
This question has been asked a few places, and I didn't think any of the answers were very good... I think the answer is to check
Rake.application.top_level_tasks
, which is a list of tasks that will be run. Rake doesn't necessarily run just one task.So, in this case:
Is it already enough to check
caller
, if it is called from rake, or do you need also which task?I hope, it is ok, when you can modify the rakefile. I have a version which introduce
Rake.application.current_task
.Two remarks on it: