@@ -7,12 +7,14 @@ use crate::{
7
7
utils:: { apply_sccache_if_possible, project_root} ,
8
8
Result ,
9
9
} ;
10
- use anyhow:: anyhow;
10
+ use anyhow:: { anyhow, Context } ;
11
+ use cargo_metadata:: Message ;
11
12
use indexmap:: map:: IndexMap ;
12
13
use log:: { info, warn} ;
13
14
use std:: {
14
15
env,
15
16
ffi:: { OsStr , OsString } ,
17
+ io:: Cursor ,
16
18
path:: Path ,
17
19
process:: { Command , Output , Stdio } ,
18
20
time:: Instant ,
@@ -207,12 +209,10 @@ impl Cargo {
207
209
}
208
210
209
211
/// Runs this command, capturing the standard output into a `Vec<u8>`.
210
- /// No logging/timing will be displayed as the result of this call from x.
211
- #[ allow( dead_code) ]
212
+ /// Standard error is forwarded.
212
213
pub fn run_with_output ( & mut self ) -> Result < Vec < u8 > > {
213
214
self . inner . stderr ( Stdio :: inherit ( ) ) ;
214
- // Since system out hijacked don't log for this command
215
- self . do_run ( false ) . map ( |o| o. stdout )
215
+ self . do_run ( true ) . map ( |o| o. stdout )
216
216
}
217
217
218
218
/// Internal run command, where the magic happens.
@@ -332,14 +332,39 @@ impl<'a> CargoCommand<'a> {
332
332
return Ok ( ( ) ) ;
333
333
}
334
334
335
+ let mut cargo = self . prepare_cargo ( packages) ;
336
+ cargo. run ( )
337
+ }
338
+
339
+ /// Runs this command on the selected packages, returning the standard output as a bytestring.
340
+ pub fn run_capture_messages (
341
+ & self ,
342
+ packages : & SelectedPackages < ' _ > ,
343
+ ) -> Result < impl Iterator < Item = Result < Message > > > {
344
+ // Early return if we have no packages to run.
345
+ let output = if !packages. should_invoke ( ) {
346
+ info ! ( "no packages to {}: exiting early" , self . as_str( ) ) ;
347
+ vec ! [ ]
348
+ } else {
349
+ let mut cargo = self . prepare_cargo ( packages) ;
350
+ cargo. args ( & [ "--message-format" , "json" ] ) ;
351
+ cargo. run_with_output ( ) ?
352
+ } ;
353
+
354
+ Ok ( Message :: parse_stream ( Cursor :: new ( output) )
355
+ . map ( |message| message. context ( "error while parsing message from Cargo" ) ) )
356
+ }
357
+
358
+ fn prepare_cargo ( & self , packages : & SelectedPackages < ' _ > ) -> Cargo {
335
359
let mut cargo = Cargo :: new ( self . cargo_config ( ) , self . as_str ( ) , true ) ;
336
360
cargo
337
361
. current_dir ( project_root ( ) )
338
362
. args ( self . direct_args ( ) )
339
363
. packages ( packages)
340
364
. pass_through ( self . pass_through_args ( ) )
341
- . envs ( self . get_extra_env ( ) . to_owned ( ) )
342
- . run ( )
365
+ . envs ( self . get_extra_env ( ) . to_owned ( ) ) ;
366
+
367
+ cargo
343
368
}
344
369
345
370
pub fn as_str ( & self ) -> & ' static str {
0 commit comments