Module: ExecuteShell
- Defined in:
- lib/execute_shell/execute_shell.rb
Overview
Contains methods for returning output from console commands.
Constant Summary collapse
- ExecuteShellMode =
Indicates the mode that ExecuteShell is running in.
StateManager.new(:production)
Instance Method Summary collapse
-
#shell(command, path = Dir.getwd) ⇒ Object
Returns output from a console command.
Instance Method Details
#shell(command, 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
- Boolean
-
Whether the shell execution was successful.
This is determined in two ways.
The first is if anything is sent to stderr.
The second is whether any exceptions occurred.
- String
-
The output from the shell command.
This will include any messages sent to stderr, as well as those sent to stdout. Any exceptions are included in this return value, as well.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 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 134 135 136 |
# File 'lib/execute_shell/execute_shell.rb', line 56 def shell(command, path = Dir.getwd) raise_not_implemented(__method__) unless [:linux, :mingw].include?(Platform::IMPL) out = '' err = '' success = nil path ||= Dir.getwd begin STDOUT.puts command if ExecuteShellMode.development block = case Platform::IMPL when :mingw lambda { # Run the command and wait for it to execute. result = 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 lambda { # Run the command and wait for it to execute. result = 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_success, wrap_out = wrap_path(path, block) # Success is determined by lack of error text. success = err.empty? # Remove all the extra stuff that the cmd prompt adds. if Platform::IMPL == :mingw out.gsub!(/\n\n#{path.gsub(%r[/], '\\\\\\')}>\Z/, '') # replace contains the command line prompt # and the command up to the first space. replace = path.gsub(%r[/], '\\\\\\') replace += '>' replace += command[0..(command.index(/ /) || 0) - 1] # Remove the header portion of the text. # This includes the Microsoft 'banner' text # that consumes the first two lines. out = out.gsub(/\A.+#{replace}.*?$/m, '').strip end # Set the error flag and notify the user if anything went wrong. out = (out + "\n" + err).strip unless success out += wrap_out.strip unless wrap_success # Include the wrapper's success flag in the resulting success flag. success = success && wrap_success rescue Exception => exc # Format exception messages. success = false out += format_error(exc) end return success, out end |