11class 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
313
414 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
732 $? = 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
945 end
1046
1147 def self.code (command : String , args = nil , env : Env = nil , clear_env : Bool = false , shell : Bool = true ,
@@ -26,14 +62,13 @@ class Exec < Process
2662 output
2763 end
2864
29- def self.each_line (command, chomp = false , & block : String - > ) : Nil
65+ def self.each_line (command, chomp = false , & : String - > ) : Nil
3066 process = new(command, shell: true , input: Redirect ::Inherit , output: Redirect ::Pipe , error: Redirect ::Inherit )
31- output = process.output.gets_to_end
32- status = process.wait
33- $? = status
34- output.each_line(chomp) do |line |
67+ process.output.each_line(chomp) do |line |
3568 yield line
3669 end
70+ status = process.wait
71+ $? = status
3772 end
3873
3974 def self.each_line (command, chomp = false )
0 commit comments