Class: Airbrussh::Rake::Context
- Inherits:
-
Object
- Object
- Airbrussh::Rake::Context
- Defined in:
- lib/airbrussh/rake/context.rb
Overview
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.
Defined Under Namespace
Modules: Patch
Class Attribute Summary collapse
-
.current_task_name ⇒ Object
Returns the value of attribute current_task_name.
Class Method Summary collapse
Instance Method Summary collapse
-
#current_task_name ⇒ Object
Returns the name of the currently-executing rake task, if it can be determined.
-
#initialize(config = Airbrussh.configuration) ⇒ Context
constructor
A new instance of Context.
-
#position(command) ⇒ Object
The zero-based position of the specified command in the current rake task.
-
#register_new_command(command) ⇒ Object
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.
Constructor Details
#initialize(config = Airbrussh.configuration) ⇒ Context
Returns a new instance of Context.
20 21 22 23 24 |
# File 'lib/airbrussh/rake/context.rb', line 20 def initialize(config=Airbrussh.configuration) @history = [] @enabled = config.monkey_patch_rake self.class.install_monkey_patch if enabled? end |
Class Attribute Details
.current_task_name ⇒ Object
Returns the value of attribute current_task_name.
17 18 19 |
# File 'lib/airbrussh/rake/context.rb', line 17 def current_task_name @current_task_name end |
Class Method Details
.install_monkey_patch ⇒ Object
62 63 64 65 66 67 |
# File 'lib/airbrussh/rake/context.rb', line 62 def self.install_monkey_patch require "rake" return if ::Rake::Task.include?(::Airbrussh::Rake::Context::Patch) ::Rake::Task.prepend(::Airbrussh::Rake::Context::Patch) end |
Instance Method Details
#current_task_name ⇒ Object
Returns the name of the currently-executing rake task, if it can be determined. If monkey patching is disabled, this will be nil.
28 29 30 31 |
# File 'lib/airbrussh/rake/context.rb', line 28 def current_task_name return nil unless enabled? self.class.current_task_name end |
#position(command) ⇒ Object
The zero-based position of the specified command in the current rake task. May be ‘nil` in certain multi-threaded scenarios, so be careful!
50 51 52 |
# File 'lib/airbrussh/rake/context.rb', line 50 def position(command) history.index(command.to_s) end |
#register_new_command(command) ⇒ Object
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
39 40 41 42 43 44 45 46 |
# File 'lib/airbrussh/rake/context.rb', line 39 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 |