|
1 | 1 | class Exec < Process |
2 | | - VERSION = "0.2.2" |
| 2 | + VERSION = "0.2.3" |
3 | 3 |
|
4 | | - # https://github.com/crystal-lang/crystal/blob/1.19.1/src/compiler/crystal/macros/macros.cr#L68 |
5 | | - record Err, stdout : String, stderr : String, status : Process::Status |
| 4 | + class Err < ::Process::Status |
| 5 | + getter stdout : String |
| 6 | + getter stderr : String |
| 7 | + |
| 8 | + {% if flag?(:win32) %} |
| 9 | + # :nodoc: |
| 10 | + def initialize(@exit_status : UInt32, @stdout : String, @stderr : String) |
| 11 | + end |
| 12 | + {% else %} |
| 13 | + # :nodoc: |
| 14 | + def initialize(@exit_status : Int32, @stdout : String, @stderr : String) |
| 15 | + end |
| 16 | + {% end %} |
| 17 | + end |
6 | 18 |
|
7 | 19 | def self.run(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
8 | | - input : Stdio = Redirect::Inherit, output : Stdio = Redirect::Inherit, error : Stdio = Redirect::Inherit, chdir : Path | String? = nil) : String | Err |
| 20 | + input : IO = STDIN, output : IO | Array(IO) = STDOUT, error : IO | Array(IO) = STDERR, chdir : Path | String? = nil) : String | Err |
9 | 21 | output_strio = String::Builder.new |
10 | 22 | error_strio = String::Builder.new |
11 | | - |
12 | | - output_writer = if output.is_a?(Redirect) |
13 | | - output == Redirect::Close ? output : IO::MultiWriter.new(STDOUT, output_strio) |
14 | | - else |
15 | | - output != STDOUT ? IO::MultiWriter.new(STDOUT, output, output_strio) : IO::MultiWriter.new(STDOUT, output_strio) |
16 | | - end |
17 | | - |
18 | | - error_writer = if error.is_a?(Redirect) |
19 | | - error == Redirect::Close ? error : IO::MultiWriter.new(STDERR, error_strio) |
20 | | - else |
21 | | - error != STDERR ? IO::MultiWriter.new(STDERR, error, error_strio) : IO::MultiWriter.new(STDERR, error_strio) |
22 | | - end |
| 23 | + output_writer = output.is_a?(Array) ? IO::MultiWriter.new(output + output_strio) : IO::MultiWriter.new(output, output_strio) |
| 24 | + error_writer = error.is_a?(Array) ? IO::MultiWriter.new(error + error_strio) : IO::MultiWriter.new(error, error_strio) |
23 | 25 |
|
24 | 26 | status = new(command, args, env, clear_env, shell, input, output_writer, error_writer, chdir).wait |
25 | 27 | $? = status |
26 | 28 |
|
27 | | - output.close unless output.is_a?(Redirect) || output == STDOUT |
28 | | - error.close unless error.is_a?(Redirect) || error == STDERR |
29 | | - output_strio.close |
30 | | - error_strio.close |
31 | | - |
32 | | - case status.success? |
33 | | - when true |
34 | | - output_strio.to_s |
35 | | - else |
36 | | - Err.new(output_strio.to_s, error_strio.to_s, status) |
37 | | - end |
| 29 | + output_writer.close |
| 30 | + error_writer.close |
| 31 | + status.success? ? output_strio.to_s : Err.new(status.@exit_status, output_strio.to_s, error_strio.to_s) |
38 | 32 | end |
39 | 33 |
|
40 | 34 | def self.code(command : String, args = nil, env : Env = nil, clear_env : Bool = false, shell : Bool = true, |
|
0 commit comments