Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support removing rake monkey patch #15

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/airbrussh/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
module Airbrussh
class Configuration
attr_accessor :log_file, :monkey_patch_rake, :color, :truncate, :banner,
:command_output
class << self
extend Forwardable
attr_writer :current_formatter
delegate :current_rake_task= => :@current_formatter
end

attr_accessor :log_file, :color, :truncate, :banner, :command_output

def initialize
self.log_file = nil
Expand All @@ -20,6 +25,23 @@ def command_output_stderr?
command_output_include?(:stderr)
end

def monkey_patch_rake=(should_patch)
::Rake::Task.class_eval do
patched = instance_methods(false).include?(:_original_execute_airbrussh)
if should_patch && !patched
define_method(:_original_execute_airbrussh, instance_method(:execute))
def execute(args=nil)
Airbrussh::Configuration.current_rake_task = name
_original_execute_airbrussh(args)
Airbrussh::Configuration.current_rake_task = nil
end
elsif !should_patch && patched
define_method(:execute, instance_method(:_original_execute_airbrussh))
remove_method :_original_execute_airbrussh
end
end
end

private

def command_output_include?(sym)
Expand Down
25 changes: 3 additions & 22 deletions lib/airbrussh/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,12 @@

module Airbrussh
class Formatter < SSHKit::Formatter::Abstract
class << self
attr_accessor :current_rake_task

def monkey_patch_rake_task!
return unless Airbrussh.configuration.monkey_patch_rake
return if @rake_patched

eval(<<-EVAL)
class ::Rake::Task
alias_method :_original_execute_airbrussh, :execute
def execute(args=nil)
#{name}.current_rake_task = name
_original_execute_airbrussh(args)
end
end
EVAL

@rake_patched = true
end
end
attr_writer :current_rake_task

def initialize(io)
super

self.class.monkey_patch_rake_task!
config.class.current_formatter = self

@tasks = {}

Expand Down Expand Up @@ -151,7 +132,7 @@ def print_task_if_changed
end

def current_task_status
task = self.class.current_rake_task.to_s
task = @current_rake_task.to_s
if @tasks[task]
changed = false
else
Expand Down
Loading