1
- use crate :: util:: { process_error, read2, CargoResult , CargoResultExt } ;
2
- use anyhow:: bail;
1
+ use crate :: process_error:: ProcessError ;
2
+ use crate :: read2;
3
+ use anyhow:: { bail, Context , Result } ;
3
4
use jobserver:: Client ;
4
5
use shell_escape:: escape;
5
6
use std:: collections:: BTreeMap ;
@@ -10,7 +11,7 @@ use std::iter::once;
10
11
use std:: path:: Path ;
11
12
use std:: process:: { Command , Output , Stdio } ;
12
13
13
- /// A builder object for an external process, similar to `std::process::Command`.
14
+ /// A builder object for an external process, similar to [ `std::process::Command`] .
14
15
#[ derive( Clone , Debug ) ]
15
16
pub struct ProcessBuilder {
16
17
/// The program to execute.
@@ -21,10 +22,10 @@ pub struct ProcessBuilder {
21
22
env : BTreeMap < String , Option < OsString > > ,
22
23
/// The directory to run the program from.
23
24
cwd : Option < OsString > ,
24
- /// The `make` jobserver. See the [jobserver crate][jobserver_docs] for
25
+ /// The `make` jobserver. See the [jobserver crate] for
25
26
/// more information.
26
27
///
27
- /// [jobserver_docs ]: https://docs.rs/jobserver/0.1.6 /jobserver/
28
+ /// [jobserver crate ]: https://docs.rs/jobserver/
28
29
jobserver : Option < Client > ,
29
30
/// `true` to include environment variable in display.
30
31
display_env_vars : bool ,
@@ -58,6 +59,18 @@ impl fmt::Display for ProcessBuilder {
58
59
}
59
60
60
61
impl ProcessBuilder {
62
+ /// Creates a new [`ProcessBuilder`] with the given executable path.
63
+ pub fn new < T : AsRef < OsStr > > ( cmd : T ) -> ProcessBuilder {
64
+ ProcessBuilder {
65
+ program : cmd. as_ref ( ) . to_os_string ( ) ,
66
+ args : Vec :: new ( ) ,
67
+ cwd : None ,
68
+ env : BTreeMap :: new ( ) ,
69
+ jobserver : None ,
70
+ display_env_vars : false ,
71
+ }
72
+ }
73
+
61
74
/// (chainable) Sets the executable for the process.
62
75
pub fn program < T : AsRef < OsStr > > ( & mut self , program : T ) -> & mut ProcessBuilder {
63
76
self . program = program. as_ref ( ) . to_os_string ( ) ;
@@ -149,16 +162,16 @@ impl ProcessBuilder {
149
162
}
150
163
151
164
/// Runs the process, waiting for completion, and mapping non-success exit codes to an error.
152
- pub fn exec ( & self ) -> CargoResult < ( ) > {
165
+ pub fn exec ( & self ) -> Result < ( ) > {
153
166
let mut command = self . build_command ( ) ;
154
- let exit = command. status ( ) . chain_err ( || {
155
- process_error ( & format ! ( "could not execute process {}" , self ) , None , None )
167
+ let exit = command. status ( ) . with_context ( || {
168
+ ProcessError :: new ( & format ! ( "could not execute process {}" , self ) , None , None )
156
169
} ) ?;
157
170
158
171
if exit. success ( ) {
159
172
Ok ( ( ) )
160
173
} else {
161
- Err ( process_error (
174
+ Err ( ProcessError :: new (
162
175
& format ! ( "process didn't exit successfully: {}" , self ) ,
163
176
Some ( exit) ,
164
177
None ,
@@ -182,22 +195,22 @@ impl ProcessBuilder {
182
195
/// include our child process. If the child terminates then we'll reap them in Cargo
183
196
/// pretty quickly, and if the child handles the signal then we won't terminate
184
197
/// (and we shouldn't!) until the process itself later exits.
185
- pub fn exec_replace ( & self ) -> CargoResult < ( ) > {
198
+ pub fn exec_replace ( & self ) -> Result < ( ) > {
186
199
imp:: exec_replace ( self )
187
200
}
188
201
189
202
/// Executes the process, returning the stdio output, or an error if non-zero exit status.
190
- pub fn exec_with_output ( & self ) -> CargoResult < Output > {
203
+ pub fn exec_with_output ( & self ) -> Result < Output > {
191
204
let mut command = self . build_command ( ) ;
192
205
193
- let output = command. output ( ) . chain_err ( || {
194
- process_error ( & format ! ( "could not execute process {}" , self ) , None , None )
206
+ let output = command. output ( ) . with_context ( || {
207
+ ProcessError :: new ( & format ! ( "could not execute process {}" , self ) , None , None )
195
208
} ) ?;
196
209
197
210
if output. status . success ( ) {
198
211
Ok ( output)
199
212
} else {
200
- Err ( process_error (
213
+ Err ( ProcessError :: new (
201
214
& format ! ( "process didn't exit successfully: {}" , self ) ,
202
215
Some ( output. status ) ,
203
216
Some ( & output) ,
@@ -217,10 +230,10 @@ impl ProcessBuilder {
217
230
/// output.
218
231
pub fn exec_with_streaming (
219
232
& self ,
220
- on_stdout_line : & mut dyn FnMut ( & str ) -> CargoResult < ( ) > ,
221
- on_stderr_line : & mut dyn FnMut ( & str ) -> CargoResult < ( ) > ,
233
+ on_stdout_line : & mut dyn FnMut ( & str ) -> Result < ( ) > ,
234
+ on_stderr_line : & mut dyn FnMut ( & str ) -> Result < ( ) > ,
222
235
capture_output : bool ,
223
- ) -> CargoResult < Output > {
236
+ ) -> Result < Output > {
224
237
let mut stdout = Vec :: new ( ) ;
225
238
let mut stderr = Vec :: new ( ) ;
226
239
@@ -274,7 +287,9 @@ impl ProcessBuilder {
274
287
} ) ?;
275
288
child. wait ( )
276
289
} ) ( )
277
- . chain_err ( || process_error ( & format ! ( "could not execute process {}" , self ) , None , None ) ) ?;
290
+ . with_context ( || {
291
+ ProcessError :: new ( & format ! ( "could not execute process {}" , self ) , None , None )
292
+ } ) ?;
278
293
let output = Output {
279
294
status,
280
295
stdout,
@@ -284,14 +299,14 @@ impl ProcessBuilder {
284
299
{
285
300
let to_print = if capture_output { Some ( & output) } else { None } ;
286
301
if let Some ( e) = callback_error {
287
- let cx = process_error (
302
+ let cx = ProcessError :: new (
288
303
& format ! ( "failed to parse process output: {}" , self ) ,
289
304
Some ( output. status ) ,
290
305
to_print,
291
306
) ;
292
307
bail ! ( anyhow:: Error :: new( cx) . context( e) ) ;
293
308
} else if !output. status . success ( ) {
294
- bail ! ( process_error (
309
+ bail ! ( ProcessError :: new (
295
310
& format!( "process didn't exit successfully: {}" , self ) ,
296
311
Some ( output. status) ,
297
312
to_print,
@@ -333,9 +348,9 @@ impl ProcessBuilder {
333
348
/// # Examples
334
349
///
335
350
/// ```rust
336
- /// use cargo::util::{ ProcessBuilder, process} ;
351
+ /// use cargo_util:: ProcessBuilder;
337
352
/// // Running this would execute `rustc`
338
- /// let cmd: ProcessBuilder = process ("rustc");
353
+ /// let cmd = ProcessBuilder::new ("rustc");
339
354
///
340
355
/// // Running this will execute `sccache rustc`
341
356
/// let cmd = cmd.wrapped(Some("sccache"));
@@ -360,28 +375,16 @@ impl ProcessBuilder {
360
375
}
361
376
}
362
377
363
- /// A helper function to create a `ProcessBuilder`.
364
- pub fn process < T : AsRef < OsStr > > ( cmd : T ) -> ProcessBuilder {
365
- ProcessBuilder {
366
- program : cmd. as_ref ( ) . to_os_string ( ) ,
367
- args : Vec :: new ( ) ,
368
- cwd : None ,
369
- env : BTreeMap :: new ( ) ,
370
- jobserver : None ,
371
- display_env_vars : false ,
372
- }
373
- }
374
-
375
378
#[ cfg( unix) ]
376
379
mod imp {
377
- use crate :: util :: { process_error , ProcessBuilder } ;
378
- use crate :: CargoResult ;
380
+ use super :: { ProcessBuilder , ProcessError } ;
381
+ use anyhow :: Result ;
379
382
use std:: os:: unix:: process:: CommandExt ;
380
383
381
- pub fn exec_replace ( process_builder : & ProcessBuilder ) -> CargoResult < ( ) > {
384
+ pub fn exec_replace ( process_builder : & ProcessBuilder ) -> Result < ( ) > {
382
385
let mut command = process_builder. build_command ( ) ;
383
386
let error = command. exec ( ) ;
384
- Err ( anyhow:: Error :: from ( error) . context ( process_error (
387
+ Err ( anyhow:: Error :: from ( error) . context ( ProcessError :: new (
385
388
& format ! ( "could not execute process {}" , process_builder) ,
386
389
None ,
387
390
None ,
@@ -391,8 +394,8 @@ mod imp {
391
394
392
395
#[ cfg( windows) ]
393
396
mod imp {
394
- use crate :: util :: { process_error , ProcessBuilder } ;
395
- use crate :: CargoResult ;
397
+ use super :: { ProcessBuilder , ProcessError } ;
398
+ use anyhow :: Result ;
396
399
use winapi:: shared:: minwindef:: { BOOL , DWORD , FALSE , TRUE } ;
397
400
use winapi:: um:: consoleapi:: SetConsoleCtrlHandler ;
398
401
@@ -401,10 +404,10 @@ mod imp {
401
404
TRUE
402
405
}
403
406
404
- pub fn exec_replace ( process_builder : & ProcessBuilder ) -> CargoResult < ( ) > {
407
+ pub fn exec_replace ( process_builder : & ProcessBuilder ) -> Result < ( ) > {
405
408
unsafe {
406
409
if SetConsoleCtrlHandler ( Some ( ctrlc_handler) , TRUE ) == FALSE {
407
- return Err ( process_error ( "Could not set Ctrl-C handler." , None , None ) . into ( ) ) ;
410
+ return Err ( ProcessError :: new ( "Could not set Ctrl-C handler." , None , None ) . into ( ) ) ;
408
411
}
409
412
}
410
413
0 commit comments