Class: ExecuteShell
- Inherits:
-
Object
- Object
- ExecuteShell
- Defined in:
- lib/execute_shell/execute_shell.rb
Overview
Contains methods for returning output from console commands.
Class Method Summary collapse
-
.method_missing(method, *args, &block) ⇒ Object
Allows method calls such as run_success? or run_err_out.
-
.raise_not_implemented(text) ⇒ Object
Raises an error indicating that the class/method/feature is not supported.
-
.respond_to_missing?(method, include_private) ⇒ Boolean
Indicates that this class will respond to calls such as run_success? or run_err_out.
-
.run(command, path = File.expand_path(Dir.getwd)) ⇒ Object
Returns output from a console command.
-
.supported?(os = Platform::IMPL) ⇒ Boolean
Indicates whether the specified operating system is supported.
-
.supported_systems ⇒ Object
Returns an array of the supported systems as returned by Platform::IMPL.
Class Method Details
.method_missing(method, *args, &block) ⇒ Object
Allows method calls such as run_success? or run_err_out.
Input
- method : Symbol
-
The missing method that is being called.
- *args : Array
-
Arguments that were passed to the method.
- &block : Block
-
The block passed to the method.
Output
The output depends on the method called.
This method supports any method that starts with “run_” and combines methods available from a ShellResult object.
For example, the following are all valid calls:
ExecuteShell.run_success? "echo 'hi'"
ExecuteShell.run_success "echo 'hi'"
ExecuteShell.run_success_err_out "echo 'hi'"
ExecuteShell.run_out_to_s_err "echo 'hi'"
If only one method name is provided, that value will be returned alone.
If more than one method name is provided, the results will be returned in an array listing the results in the order specified in the method call.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/execute_shell/execute_shell.rb', line 140 def method_missing(method, *args, &block) return super unless method.to_s =~ /^run_/ # Execute the command. shell_result = run(*args) # Get a list of the requested result values. method_list = method.to_s.sub(/^run_/, '').split('_') results = [] skip = nil # Loop through the methods, building an array of result values. method_list.each_with_index do |item, i| # Skip to the next one if two methods have already been combined. # i.e. 'to_s' if skip skip = false next end item += '?' if item == 'success' if item == 'to' item += "_#{method_list[i + 1]}" skip = true end results << shell_result.send(item.to_sym) end # Return the result(s) in an appropriate way. return case results.length when 0; nil when 1; results[0] else; results end end |
.raise_not_implemented(text) ⇒ Object
Raises an error indicating that the class/method/feature is not supported.
Input
- text : String
-
The class/method/feature that isnot supported.
39 40 41 42 |
# File 'lib/execute_shell/execute_shell.rb', line 39 def raise_not_implemented(text) raise NotImplementedError, "#{text} has not been implemented for #{Platform::IMPL}." end |
.respond_to_missing?(method, include_private) ⇒ Boolean
Indicates that this class will respond to calls such as run_success? or run_err_out.
Input
- method : Symbol
-
The method that is being checked.
- include_private : Boolean
-
Whether to include private methods in the search.
Output
- Boolean
-
Indicates whether the class responds to the specified method.
Examples
a #=> b
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/execute_shell/execute_shell.rb', line 189 def respond_to_missing?(method, include_private) return super unless method.to_s =~ /^run_/ method_list = method.to_s.sub(/^run_/, '').split('_') skip = nil shell_result = ShellResult.new('', '') # Loop through the methods, checking the validity of each. method_list.each_with_index do |item, i| # Skip to the next one if two methods have already been combined. # i.e. 'to_s' if skip skip = false next end item += '?' if item == 'success' if item == 'to' item += "_#{method_list[i + 1]}" skip = true end # Return false if the ShellResult object does not respond to the method. return false unless shell_result.respond_to?(item.to_sym) end return true end |
.run(command, path = File.expand_path(Dir.getwd)) ⇒ Object
Returns output from a console command.
Input
- command : String
-
The command to be run.
- path : String : Dir.getwd
-
The path to use for the command.
Output
- ShellResult
-
A ShellResult object that contains information regarding the output, errors, and success.
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 94 95 |
# File 'lib/execute_shell/execute_shell.rb', line 51 def run(command, path = File.(Dir.getwd)) raise_not_implemented(__method__) unless supported? out = '' err = '' block = case Platform::IMPL when :mingw lambda { # Run the command and wait for it to execute. Open3::popen3('cmd') do |std_in, std_out, std_err, thread| # Set up the command. std_in.puts command # Run it. std_in.close # Get the output. out = std_out.read.strip err = std_err.read.strip end } when :linux, :macosx lambda { # Run the command and wait for it to execute. Open4::popen4('bash') do |pid, std_in, std_out, std_err| # Set up the command. std_in.puts command # Run it. std_in.close # Get the output. out = std_out.read.strip err = std_err.read.strip end } end wrap_path path, block out = ShellResult.cleanup(command, path, out) return ShellResult.new(out, err) end |
.supported?(os = Platform::IMPL) ⇒ Boolean
Indicates whether the specified operating system is supported.
Input
- os : Symbol,String : Platform::IMPL
-
The operating system to check for.
Output
- Boolean
-
A boolean value indicating whether the system is supported.
Examples
ExecuteShell.supported? #=> true
ExecuteShell.supported?(:linux) #=> true
ExecuteShell.supported?(:my_os) #=> false
106 107 108 |
# File 'lib/execute_shell/execute_shell.rb', line 106 def supported?(os = Platform::IMPL) supported_systems.include? os.to_sym end |
.supported_systems ⇒ Object
Returns an array of the supported systems as returned by Platform::IMPL.
Output
- Array
-
The supported operating systems.
Examples
ExecuteShell.supported_systems #=> [:linux, :mingw]
115 116 117 |
# File 'lib/execute_shell/execute_shell.rb', line 115 def supported_systems [:linux, :mingw, :macosx] end |