@@ -19,8 +19,8 @@ use env_logger::Builder;
1919use log:: { debug, log_enabled, Level } ;
2020use serde:: Deserialize ;
2121use std:: ffi:: OsString ;
22- use std:: io:: Write ;
23- use std:: process:: { Command , Output } ;
22+ use std:: io:: { BufReader , Write } ;
23+ use std:: process:: { Command , ExitStatus , Stdio } ;
2424
2525#[ derive( Deserialize ) ]
2626pub struct VersionMetadata {
@@ -104,11 +104,16 @@ where
104104 . format_indent ( Some ( 12 ) )
105105 . filter ( None , level_from_usize ( cli. verbose ) . to_level_filter ( ) )
106106 . format ( |f, record| {
107+ let mut is_command_output = false ;
107108 if let Some ( action) = record. key_values ( ) . get ( "action" . into ( ) ) {
108- let mut action_style = f. style ( ) ;
109- action_style. set_color ( Color :: Green ) . set_bold ( true ) ;
110-
111- write ! ( f, "{:>12} " , action_style. value( action. to_str( ) . unwrap( ) ) ) ?;
109+ let action = action. to_str ( ) . unwrap ( ) ;
110+ is_command_output = action == "stdout" || action == "stderr" ;
111+ if !is_command_output {
112+ let mut action_style = f. style ( ) ;
113+ action_style. set_color ( Color :: Green ) . set_bold ( true ) ;
114+
115+ write ! ( f, "{:>12} " , action_style. value( action) ) ?;
116+ }
112117 } else {
113118 let mut level_style = f. default_level_style ( record. level ( ) ) ;
114119 level_style. set_bold ( true ) ;
@@ -120,7 +125,7 @@ where
120125 ) ?;
121126 }
122127
123- if log_enabled ! ( Level :: Debug ) {
128+ if !is_command_output && log_enabled ! ( Level :: Debug ) {
124129 let mut target_style = f. style ( ) ;
125130 target_style. set_color ( Color :: Black ) ;
126131
@@ -171,37 +176,61 @@ fn prettyprint_level(lvl: Level) -> &'static str {
171176pub trait CommandExt {
172177 // The `pipe` function sets the stdout and stderr to properly
173178 // show the command output in the Node.js wrapper.
174- fn pipe ( & mut self ) -> Result < & mut Self > ;
175- fn output_ok ( & mut self ) -> crate :: Result < Output > ;
179+ fn piped ( & mut self ) -> Result < ExitStatus > ;
180+ fn output_ok ( & mut self ) -> crate :: Result < ( ) > ;
176181}
177182
178183impl CommandExt for Command {
179- fn pipe ( & mut self ) -> Result < & mut Self > {
184+ fn piped ( & mut self ) -> crate :: Result < ExitStatus > {
180185 self . stdout ( os_pipe:: dup_stdout ( ) ?) ;
181186 self . stderr ( os_pipe:: dup_stderr ( ) ?) ;
182- Ok ( self )
183- }
187+ let program = self . get_program ( ) . to_string_lossy ( ) . into_owned ( ) ;
188+ debug ! ( action = "Running" ; "Command `{} {}`" , program , self . get_args ( ) . map ( |arg| arg . to_string_lossy ( ) ) . fold ( String :: new ( ) , |acc , arg| format! ( "{} {}" , acc , arg ) ) ) ;
184189
185- fn output_ok ( & mut self ) -> crate :: Result < Output > {
186- debug ! ( action = "Running" ; "Command `{} {}`" , self . get_program ( ) . to_string_lossy ( ) , self . get_args ( ) . map ( |arg| arg . to_string_lossy ( ) ) . fold ( String :: new ( ) , |acc , arg| format! ( "{} {}" , acc , arg ) ) ) ;
190+ self . status ( ) . map_err ( Into :: into )
191+ }
187192
188- let output = self . output ( ) ?;
193+ fn output_ok ( & mut self ) -> crate :: Result < ( ) > {
194+ let program = self . get_program ( ) . to_string_lossy ( ) . into_owned ( ) ;
195+ debug ! ( action = "Running" ; "Command `{} {}`" , program, self . get_args( ) . map( |arg| arg. to_string_lossy( ) ) . fold( String :: new( ) , |acc, arg| format!( "{} {}" , acc, arg) ) ) ;
196+
197+ self . stdout ( Stdio :: piped ( ) ) ;
198+ self . stderr ( Stdio :: piped ( ) ) ;
199+
200+ let mut child = self . spawn ( ) ?;
201+
202+ let mut stdout = child. stdout . take ( ) . map ( BufReader :: new) . unwrap ( ) ;
203+ std:: thread:: spawn ( move || {
204+ let mut buf = Vec :: new ( ) ;
205+ loop {
206+ buf. clear ( ) ;
207+ match tauri_utils:: io:: read_line ( & mut stdout, & mut buf) {
208+ Ok ( s) if s == 0 => break ,
209+ _ => ( ) ,
210+ }
211+ debug ! ( action = "stdout" ; "{}" , String :: from_utf8_lossy( & buf) ) ;
212+ }
213+ } ) ;
214+
215+ let mut stderr = child. stderr . take ( ) . map ( BufReader :: new) . unwrap ( ) ;
216+ std:: thread:: spawn ( move || {
217+ let mut buf = Vec :: new ( ) ;
218+ loop {
219+ buf. clear ( ) ;
220+ match tauri_utils:: io:: read_line ( & mut stderr, & mut buf) {
221+ Ok ( s) if s == 0 => break ,
222+ _ => ( ) ,
223+ }
224+ debug ! ( action = "stderr" ; "{}" , String :: from_utf8_lossy( & buf) ) ;
225+ }
226+ } ) ;
189227
190- let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
191- if !stdout. is_empty ( ) {
192- debug ! ( "Stdout: {}" , stdout) ;
193- }
194- let stderr = String :: from_utf8_lossy ( & output. stderr ) ;
195- if !stderr. is_empty ( ) {
196- debug ! ( "Stderr: {}" , stderr) ;
197- }
228+ let status = child. wait ( ) ?;
198229
199- if output . status . success ( ) {
200- Ok ( output )
230+ if status. success ( ) {
231+ Ok ( ( ) )
201232 } else {
202- Err ( anyhow:: anyhow!(
203- String :: from_utf8_lossy( & output. stderr) . to_string( )
204- ) )
233+ Err ( anyhow:: anyhow!( "failed to run {}" , program) )
205234 }
206235 }
207236}
0 commit comments