|
1 | 1 | class Exec < Process |
2 | | - VERSION = "0.1.1" |
| 2 | + VERSION = "0.2.0" |
| 3 | + |
| 4 | + class IO |
| 5 | + class MultiWriter < ::IO::MultiWriter |
| 6 | + alias IO = ::IO | ::IO::FileDescriptor | ::String::Builder |
| 7 | + |
| 8 | + def read(slice : Bytes) : NoReturn |
| 9 | + raise ::IO::Error.new("Can't read from IO::MultiWriter") |
| 10 | + end |
| 11 | + end |
| 12 | + end |
3 | 13 |
|
4 | 14 | def self.run(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
5 | | - input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : Process::Status |
6 | | - status = new(command, args, env, clear_env, shell, input, output, error, chdir).wait |
| 15 | + input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : String | {String, String, Process::Status} |
| 16 | + output_strio = String::Builder.new |
| 17 | + error_strio = String::Builder.new |
| 18 | + |
| 19 | + output_writer = if output.is_a?(Redirect) |
| 20 | + output == Redirect::Close ? output : Exec::IO::MultiWriter.new(STDOUT, output_strio) |
| 21 | + else |
| 22 | + Exec::IO::MultiWriter.new(STDOUT, output, output_strio) |
| 23 | + end |
| 24 | + |
| 25 | + error_writer = if error.is_a?(Redirect) |
| 26 | + error == Redirect::Close ? error : Exec::IO::MultiWriter.new(STDERR, error_strio) |
| 27 | + else |
| 28 | + Exec::IO::MultiWriter.new(STDERR, error, error_strio) |
| 29 | + end |
| 30 | + |
| 31 | + status = new(command, args, env, clear_env, shell, input, output_writer, error_writer, chdir).wait |
7 | 32 | $? = status |
8 | | - status |
| 33 | + |
| 34 | + output.close unless output.is_a?(Redirect) |
| 35 | + error.close unless error.is_a?(Redirect) |
| 36 | + output_strio.close |
| 37 | + error_strio.close |
| 38 | + |
| 39 | + case status.success? |
| 40 | + when true |
| 41 | + output_strio.to_s |
| 42 | + else |
| 43 | + {output_strio.to_s, error_strio.to_s, status} |
| 44 | + end |
9 | 45 | end |
10 | 46 |
|
11 | 47 | def self.code(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
|
0 commit comments