class ExecuteShell
Contains methods for returning output from console commands.
Public Class Methods
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.
# 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
Raises an error indicating that the class/method/feature is not supported.
Input¶ ↑
- text : String
-
The class/method/feature that isnot supported.
# 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
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
# 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
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.
# File lib/execute_shell/execute_shell.rb, line 51 def run(command, path = File.expand_path(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
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
# File lib/execute_shell/execute_shell.rb, line 106 def supported?(os = Platform::IMPL) supported_systems.include? os.to_sym end
Private Class Methods
Runs a block of code by changing the path first.
Input¶ ↑
- path : String
-
The path to change to prior to running the block.
- block : Proc
-
The code block to execute.
- *args : Array
-
Any other parameters that will be passed on to
block
.
Output¶ ↑
- String
-
Any error messages from trapped errors.
# File lib/execute_shell/execute_shell.rb, line 233 def wrap_path(path, block, *args) path ||= File.expand_path(Dir.getwd) original = File.expand_path(Dir.getwd) begin Dir.chdir path unless path == original block.call(*args) ensure Dir.chdir original unless path == original end end