class Airbrussh::Rake::Context
Maintains information about what Rake task is currently being invoked, in order to be able to decorate SSHKit commands with additional context-sensitive information. Works via a monkey patch to Rake::Task, which can be disabled via by setting Airbrussh.configuration.monkey_patch_rake = false.
Note that this class is not thread-safe. Normally this is not a problem, but some Capistrano users are known to use `invoke` to switch the Rake task in the middle of an SSHKit thread, which causes Context to get very confused. It such scenarios Context is not reliable and may return `nil` for the `position` of a command.
Attributes
Public Class Methods
# File lib/airbrussh/rake/context.rb, line 60 def execute(args=nil) # rubocop:disable Lint/NestedMethodDefinition ::Airbrussh::Rake::Context.current_task_name = name.to_s _airbrussh_execute(args) end
# File lib/airbrussh/rake/context.rb, line 55 def install_monkey_patch return if ::Rake::Task.instance_methods.include?(:_airbrussh_execute) ::Rake::Task.class_exec do alias_method :_airbrussh_execute, :execute def execute(args=nil) # rubocop:disable Lint/NestedMethodDefinition ::Airbrussh::Rake::Context.current_task_name = name.to_s _airbrussh_execute(args) end end end
# File lib/airbrussh/rake/context.rb, line 18 def initialize(config=Airbrussh.configuration) @history = [] @enabled = config.monkey_patch_rake self.class.install_monkey_patch if enabled? end
Public Instance Methods
Returns the name of the currently-executing rake task, if it can be determined. If monkey patching is disabled, this will be nil.
# File lib/airbrussh/rake/context.rb, line 26 def current_task_name return nil unless enabled? self.class.current_task_name end
The zero-based position of the specified command in the current rake task. May be `nil` in certain multi-threaded scenarios, so be careful!
# File lib/airbrussh/rake/context.rb, line 48 def position(command) history.index(command.to_s) end
Update the context when a new command starts by:
-
Clearing the command history if the rake task has changed
-
Recording the command in the history
Returns whether or not this command was the first execution of this command in the current rake task
# File lib/airbrussh/rake/context.rb, line 37 def register_new_command(command) reset_history_if_task_changed first_execution = !history.include?(command.to_s) history << command.to_s history.uniq! first_execution end
Private Instance Methods
# File lib/airbrussh/rake/context.rb, line 78 def enabled? @enabled end
# File lib/airbrussh/rake/context.rb, line 73 def reset_history_if_task_changed history.clear if last_task_name != current_task_name self.last_task_name = current_task_name end