Is there a way to know the current rake task?
rake, ruby
Solution
I'm thinking of making an app behave different when runned by rake.
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`.
# Rakefile
require 'rake'
module Rake
class Application
attr_accessor :current_task
end
class Task
alias :old_execute :execute
def execute(args=nil)
Rake.application.current_task = @name
old_execute(args)
end
end #class Task
end #module Rake
task :start => :install do; end
task :install => :install2 do
MyApp.new.some_method()
end
task :install2 do; end
# myapp.rb
class MyApp
def some_method(opts={})
## current_task? -> Rake.application.current_task
puts "#{self.class}##{__method__} called from task #{Rake.application.current_task}"
end
end
Two remarks on it:
- you may add the rake-modifications in a file and require it in your rakefile.
- the tasks start and install are test tasks to test, if there are more then one task.
- I made only small tests on side effects. I could imagine there are problems in a real productive situation.
Problem
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.