Created
December 16, 2017 02:38
-
-
Save murakmii/0e8a47f7c9a6db5932eec53732c7ee03 to your computer and use it in GitHub Desktop.
Monkey patch for rake to save result of task that is depended
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# namespace :result do | |
# task :foo do | |
# "Result of foo" | |
# end | |
# | |
# task bar: :foo do | |
# "Result of bar" | |
# end | |
# end | |
# | |
# task test: :"result:bar" do |task| | |
# puts task.dependency_result # => "Result of bar" | |
# puts task.dependency_result("result:foo") # => "Result of foo" | |
# end | |
module Rake | |
class Task | |
attr_reader :result | |
alias_method :old_reenable, :reenable | |
alias_method :old_enhance, :enhance | |
def enhance(deps=nil, &original_action) | |
wrapped_action = -> (task, args) { | |
@result = original_action.call(task, args) | |
} | |
old_enhance(deps, &wrapped_action) | |
end | |
def reenable | |
old_reenable | |
@result = nil | |
end | |
def dependency_result(name = nil) | |
task = if name | |
all_prerequisite_tasks.find { |t| t.to_s == name } | |
else | |
prerequisite_tasks.first | |
end | |
task&.result | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment