22//!
33//! This module provides a structured way to execute and manage commands efficiently,
44//! ensuring controlled failure handling and output management.
5- #![ allow( warnings) ]
65
7- use std:: collections:: HashMap ;
86use std:: ffi:: { OsStr , OsString } ;
97use std:: fmt:: { Debug , Formatter } ;
10- use std:: hash:: { Hash , Hasher } ;
8+ use std:: hash:: Hash ;
119use std:: path:: Path ;
1210use std:: process:: { Command , CommandArgs , CommandEnvs , ExitStatus , Output , Stdio } ;
13- use std:: sync:: Mutex ;
1411
1512use build_helper:: ci:: CiEnv ;
1613use build_helper:: drop_bomb:: DropBomb ;
@@ -59,7 +56,7 @@ impl OutputMode {
5956pub struct CommandCacheKey {
6057 program : OsString ,
6158 args : Vec < OsString > ,
62- envs : Vec < ( OsString , OsString ) > ,
59+ envs : Vec < ( OsString , Option < OsString > ) > ,
6360 cwd : Option < PathBuf > ,
6461}
6562
@@ -90,18 +87,14 @@ pub struct BootstrapCommand {
9087impl < ' a > BootstrapCommand {
9188 #[ track_caller]
9289 pub fn new < S : AsRef < OsStr > > ( program : S ) -> Self {
93- Self { should_cache : true , .. Command :: new ( program) . into ( ) }
90+ Command :: new ( program) . into ( )
9491 }
9592 pub fn arg < S : AsRef < OsStr > > ( & mut self , arg : S ) -> & mut Self {
9693 self . command . arg ( arg. as_ref ( ) ) ;
9794 self
9895 }
9996
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 {
10598 self . should_cache = false ;
10699 self
107100 }
@@ -111,9 +104,7 @@ impl<'a> BootstrapCommand {
111104 I : IntoIterator < Item = S > ,
112105 S : AsRef < OsStr > ,
113106 {
114- args. into_iter ( ) . for_each ( |arg| {
115- self . arg ( arg) ;
116- } ) ;
107+ self . command . args ( args) ;
117108 self
118109 }
119110
@@ -207,7 +198,7 @@ impl<'a> BootstrapCommand {
207198 // command will be handled. Caching must also be avoided here, as the inner command could be
208199 // modified externally without us being aware.
209200 self . mark_as_executed ( ) ;
210- self . cache_never ( ) ;
201+ self . do_not_cache ( ) ;
211202 & mut self . command
212203 }
213204
@@ -235,7 +226,19 @@ impl<'a> BootstrapCommand {
235226 }
236227
237228 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+ } )
239242 }
240243}
241244
@@ -251,7 +254,7 @@ impl From<Command> for BootstrapCommand {
251254 fn from ( command : Command ) -> Self {
252255 let program = command. get_program ( ) . to_owned ( ) ;
253256 Self {
254- should_cache : false ,
257+ should_cache : true ,
255258 command,
256259 failure_behavior : BehaviorOnFailure :: Exit ,
257260 run_always : false ,
@@ -260,21 +263,6 @@ impl From<Command> for BootstrapCommand {
260263 }
261264}
262265
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-
278266/// Represents the current status of `BootstrapCommand`.
279267#[ derive( Clone , PartialEq ) ]
280268enum CommandStatus {
0 commit comments