2
2
//!
3
3
//! This module provides a structured way to execute and manage commands efficiently,
4
4
//! ensuring controlled failure handling and output management.
5
- #![ allow( warnings) ]
6
5
7
- use std:: collections:: HashMap ;
8
6
use std:: ffi:: { OsStr , OsString } ;
9
7
use std:: fmt:: { Debug , Formatter } ;
10
- use std:: hash:: { Hash , Hasher } ;
8
+ use std:: hash:: Hash ;
11
9
use std:: path:: Path ;
12
10
use std:: process:: { Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio } ;
13
- use std:: sync:: Mutex ;
14
11
15
12
use build_helper:: ci:: CiEnv ;
16
13
use build_helper:: drop_bomb:: DropBomb ;
@@ -59,7 +56,7 @@ impl OutputMode {
59
56
pub struct CommandCacheKey {
60
57
program : OsString ,
61
58
args : Vec < OsString > ,
62
- envs : Vec < ( OsString , OsString ) > ,
59
+ envs : Vec < ( OsString , Option < OsString > ) > ,
63
60
cwd : Option < PathBuf > ,
64
61
}
65
62
@@ -90,18 +87,14 @@ pub struct BootstrapCommand {
90
87
impl < ' a > BootstrapCommand {
91
88
#[ track_caller]
92
89
pub fn new < S : AsRef < OsStr > > ( program : S ) -> Self {
93
- Self { should_cache : true , .. Command :: new ( program) . into ( ) }
90
+ Command :: new ( program) . into ( )
94
91
}
95
92
pub fn arg < S : AsRef < OsStr > > ( & mut self , arg : S ) -> & mut Self {
96
93
self . command . arg ( arg. as_ref ( ) ) ;
97
94
self
98
95
}
99
96
100
- pub fn should_cache ( & self ) -> bool {
101
- self . should_cache
102
- }
103
-
104
- pub fn cache_never ( & mut self ) -> & mut Self {
97
+ pub fn do_not_cache ( & mut self ) -> & mut Self {
105
98
self . should_cache = false ;
106
99
self
107
100
}
@@ -111,9 +104,7 @@ impl<'a> BootstrapCommand {
111
104
I : IntoIterator < Item = S > ,
112
105
S : AsRef < OsStr > ,
113
106
{
114
- args. into_iter ( ) . for_each ( |arg| {
115
- self . arg ( arg) ;
116
- } ) ;
107
+ self . command . args ( args) ;
117
108
self
118
109
}
119
110
@@ -207,7 +198,7 @@ impl<'a> BootstrapCommand {
207
198
// command will be handled. Caching must also be avoided here, as the inner command could be
208
199
// modified externally without us being aware.
209
200
self . mark_as_executed ( ) ;
210
- self . cache_never ( ) ;
201
+ self . do_not_cache ( ) ;
211
202
& mut self . command
212
203
}
213
204
@@ -235,7 +226,19 @@ impl<'a> BootstrapCommand {
235
226
}
236
227
237
228
pub fn cache_key ( & self ) -> Option < CommandCacheKey > {
238
- ( !self . should_cache ) . then ( || self . into ( ) )
229
+ if !self . should_cache {
230
+ return None ;
231
+ }
232
+ let command = & self . command ;
233
+ Some ( CommandCacheKey {
234
+ program : command. get_program ( ) . into ( ) ,
235
+ args : command. get_args ( ) . map ( OsStr :: to_os_string) . collect ( ) ,
236
+ envs : command
237
+ . get_envs ( )
238
+ . map ( |( k, v) | ( k. to_os_string ( ) , v. map ( |val| val. to_os_string ( ) ) ) )
239
+ . collect ( ) ,
240
+ cwd : command. get_current_dir ( ) . map ( Path :: to_path_buf) ,
241
+ } )
239
242
}
240
243
}
241
244
@@ -251,7 +254,7 @@ impl From<Command> for BootstrapCommand {
251
254
fn from ( command : Command ) -> Self {
252
255
let program = command. get_program ( ) . to_owned ( ) ;
253
256
Self {
254
- should_cache : false ,
257
+ should_cache : true ,
255
258
command,
256
259
failure_behavior : BehaviorOnFailure :: Exit ,
257
260
run_always : false ,
@@ -260,21 +263,6 @@ impl From<Command> for BootstrapCommand {
260
263
}
261
264
}
262
265
263
- impl From < & BootstrapCommand > for CommandCacheKey {
264
- fn from ( value : & BootstrapCommand ) -> Self {
265
- let command = & value. command ;
266
- CommandCacheKey {
267
- program : command. get_program ( ) . into ( ) ,
268
- args : command. get_args ( ) . map ( OsStr :: to_os_string) . collect ( ) ,
269
- envs : command
270
- . get_envs ( )
271
- . filter_map ( |( k, v_opt) | v_opt. map ( |v| ( k. to_owned ( ) , v. to_owned ( ) ) ) )
272
- . collect ( ) ,
273
- cwd : command. get_current_dir ( ) . map ( Path :: to_path_buf) ,
274
- }
275
- }
276
- }
277
-
278
266
/// Represents the current status of `BootstrapCommand`.
279
267
#[ derive( Clone , PartialEq ) ]
280
268
enum CommandStatus {
0 commit comments