From 92acf4cab9b6da380c25726b5670e04cdd10efd9 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Tue, 12 Sep 2023 09:57:19 -0700 Subject: [PATCH] Use Module.prepend instead of alias_method to patch Rake (#152) Libraries like Sentry that also patch Rake conflict with Airbrussh, due to our use of `alias_method`. Fix by using `Module.prepend` instead, when it is available (Ruby >= 2.0). Fixes #151 --- lib/airbrussh/rake/context.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/airbrussh/rake/context.rb b/lib/airbrussh/rake/context.rb index 984dd20..ab7f580 100644 --- a/lib/airbrussh/rake/context.rb +++ b/lib/airbrussh/rake/context.rb @@ -13,6 +13,10 @@ module Rake # for the `position` of a command. # class Context + class << self + attr_accessor :current_task_name + end + def initialize(config=Airbrussh.configuration) @history = [] @enabled = config.monkey_patch_rake @@ -47,10 +51,22 @@ def position(command) history.index(command.to_s) end - class << self - attr_accessor :current_task_name + if Object.respond_to?(:prepend) + module Patch + def execute(args=nil) + ::Airbrussh::Rake::Context.current_task_name = name.to_s + super + end + end - def install_monkey_patch + def self.install_monkey_patch + require "rake" + return if ::Rake::Task.include?(::Airbrussh::Rake::Context::Patch) + + ::Rake::Task.prepend(::Airbrussh::Rake::Context::Patch) + end + else + def self.install_monkey_patch require "rake" return if ::Rake::Task.instance_methods.include?(:_airbrussh_execute)