Class: RightGit::Shell::Default
- Inherits:
-
Object
- Object
- RightGit::Shell::Default
- Includes:
- Interface, Singleton
- Defined in:
- lib/right_git/shell/default.rb
Overview
Default shell singleton implementation.
Class Method Summary collapse
Instance Method Summary collapse
-
#clear_env_vars(names) { ... } ⇒ TrueClass
Clears (set-to-nil) the given list of environment variables while executing the given block.
-
#configure_executioner(executioner, options) ⇒ Proc
Encapsulates the given executioner with child-process-modifying behavior based on options.
-
#execute(cmd, options = {}) ⇒ Object
Implements execute interface.
-
#output_for(cmd, options = {}) ⇒ Object
Implements output_for interface.
-
#set_env_vars(variables) { ... } ⇒ TrueClass
Sets the given list of environment variables while executing the given block.
Methods included from Interface
Class Method Details
.method_missing(method_sym, *arguments, &block) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/right_git/shell/default.rb', line 42 def self.method_missing(method_sym, *arguments, &block) if instance.respond_to?(method_sym) instance.send(method_sym, *arguments, &block) else super end end |
.respond_to?(*arguments) ⇒ Boolean
38 39 40 |
# File 'lib/right_git/shell/default.rb', line 38 def self.respond_to?(*arguments) instance.respond_to?(*arguments) || super end |
Instance Method Details
#clear_env_vars(names) { ... } ⇒ TrueClass
Clears (set-to-nil) the given list of environment variables while executing the given block.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/right_git/shell/default.rb', line 172 def clear_env_vars(names, &block) save_vars = {} names.each do |k| k = k.to_s save_vars[k] = ENV[k] ENV[k] = nil end begin yield ensure names.each do |k| k = k.to_s ENV[k] = save_vars[k] end end true end |
#configure_executioner(executioner, options) ⇒ Proc
Encapsulates the given executioner with child-process-modifying behavior based on options. Builds the executioner as a series of callbacks.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/right_git/shell/default.rb', line 109 def configure_executioner(executioner, ) # set specific environment variables, if requested. sev = [:set_env_vars] if (sev && !sev.empty?) executioner = lambda do |e| lambda { set_env_vars(sev) { e.call } } end.call(executioner) end # clear specific environment variables, if requested. cev = [:clear_env_vars] if (cev && !cev.empty?) executioner = lambda do |e| lambda { clear_env_vars(cev) { e.call } } end.call(executioner) end # working directory. if directory = [:directory] executioner = lambda do |e, d| lambda { ::Dir.chdir(d) { e.call } } end.call(executioner, directory) end executioner end |
#execute(cmd, options = {}) ⇒ Object
Implements execute interface.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/right_git/shell/default.rb', line 51 def execute(cmd, = {}) = { :directory => nil, :logger => nil, :outstream => nil, :raise_on_failure => true, :set_env_vars => nil, :clear_env_vars => nil, }.merge() logger = [:logger] || default_logger outstream = [:outstream] # build execution block. exitstatus = nil executioner = lambda do logger.info("+ #{cmd}") ::IO.popen("#{cmd} 2>&1", 'r') do |output| output.sync = true done = false while !done begin data = output.readline if outstream outstream << data else logger.info(data.strip) end rescue ::EOFError done = true end end end exitstatus = $?.exitstatus if (!$?.success? && [:raise_on_failure]) raise ShellError, "Execution failed with exitstatus #{exitstatus}" end end # configure and invoke. configure_executioner(executioner, ).call return exitstatus end |
#output_for(cmd, options = {}) ⇒ Object
Implements output_for interface.
96 97 98 99 100 |
# File 'lib/right_git/shell/default.rb', line 96 def output_for(cmd, = {}) output = StringIO.new execute(cmd, .merge(:outstream => output)) output.string end |
#set_env_vars(variables) { ... } ⇒ TrueClass
Sets the given list of environment variables while executing the given block.
Parameters
Yield
Return
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/right_git/shell/default.rb', line 146 def set_env_vars(variables) save_vars = {} variables.each do |k, v| k = k.to_s save_vars[k] = ENV[k] ENV[k] = v.nil? ? v : v.to_s end begin yield ensure variables.each_key do |k| k = k.to_s ENV[k] = save_vars[k] end end true end |