33//! This module provides the [`ExecutionContext`] type, which holds global configuration
44//! relevant during the execution of commands in bootstrap. This includes dry-run
55//! mode, verbosity level, and behavior on failure.
6- #![ allow( warnings) ]
76use std:: collections:: HashMap ;
87use std:: panic:: Location ;
98use std:: process:: Child ;
@@ -46,10 +45,6 @@ pub struct DeferredCommand<'a> {
4645}
4746
4847impl CommandCache {
49- pub fn new ( ) -> Self {
50- Self { cache : Mutex :: new ( HashMap :: new ( ) ) }
51- }
52-
5348 pub fn get ( & self , key : & CommandCacheKey ) -> Option < CommandOutput > {
5449 self . cache . lock ( ) . unwrap ( ) . get ( key) . cloned ( )
5550 }
@@ -133,7 +128,7 @@ impl ExecutionContext {
133128 {
134129 command. mark_as_executed ( ) ;
135130
136- self . verbose ( || println ! ( "Cache hit: {:?}" , command ) ) ;
131+ self . verbose ( || println ! ( "Cache hit: {command :?}" ) ) ;
137132
138133 return DeferredCommand { state : CommandState :: Cached ( cached_output) } ;
139134 }
@@ -191,29 +186,7 @@ impl ExecutionContext {
191186 stdout : OutputMode ,
192187 stderr : OutputMode ,
193188 ) -> CommandOutput {
194- let cache_key = command. cache_key ( ) ;
195-
196- if let Some ( cached_output) = cache_key. as_ref ( ) . and_then ( |key| self . command_cache . get ( key) )
197- {
198- command. mark_as_executed ( ) ;
199-
200- if self . dry_run ( ) && !command. run_always {
201- return CommandOutput :: default ( ) ;
202- }
203-
204- self . verbose ( || println ! ( "Cache hit: {:?}" , command) ) ;
205- return cached_output;
206- }
207-
208- let output = self . start ( command, stdout, stderr) . wait_for_output ( self ) ;
209-
210- if !self . dry_run ( ) || command. run_always {
211- if let Some ( cache_key) = cache_key {
212- self . command_cache . insert ( cache_key, output. clone ( ) ) ;
213- }
214- }
215-
216- output
189+ self . start ( command, stdout, stderr) . wait_for_output ( self )
217190 }
218191
219192 fn fail ( & self , message : & str , output : CommandOutput ) -> ! {
@@ -247,37 +220,34 @@ impl AsRef<ExecutionContext> for ExecutionContext {
247220}
248221
249222impl < ' a > DeferredCommand < ' a > {
250- pub fn wait_for_output ( mut self , exec_ctx : impl AsRef < ExecutionContext > ) -> CommandOutput {
223+ pub fn wait_for_output ( self , exec_ctx : impl AsRef < ExecutionContext > ) -> CommandOutput {
251224 match self . state {
252225 CommandState :: Cached ( output) => output,
253226 CommandState :: Deferred { process, command, stdout, stderr, executed_at, cache_key } => {
254227 let exec_ctx = exec_ctx. as_ref ( ) ;
255228
256229 let output =
257- Self :: execute_process ( process, command, stdout, stderr, executed_at, exec_ctx) ;
230+ Self :: finish_process ( process, command, stdout, stderr, executed_at, exec_ctx) ;
258231
259232 if ( !exec_ctx. dry_run ( ) || command. run_always )
260- && cache_key. is_some ( )
261- && output. status ( ) . is_some ( )
233+ && let ( Some ( cache_key) , Some ( _) ) = ( & cache_key, output. status ( ) )
262234 {
263- exec_ctx. command_cache . insert ( cache_key. unwrap ( ) , output. clone ( ) ) ;
235+ exec_ctx. command_cache . insert ( cache_key. clone ( ) , output. clone ( ) ) ;
264236 }
265237
266238 output
267239 }
268240 }
269241 }
270242
271- pub fn execute_process (
243+ pub fn finish_process (
272244 mut process : Option < Result < Child , std:: io:: Error > > ,
273245 command : & mut BootstrapCommand ,
274246 stdout : OutputMode ,
275247 stderr : OutputMode ,
276248 executed_at : & ' a std:: panic:: Location < ' a > ,
277249 exec_ctx : & ExecutionContext ,
278250 ) -> CommandOutput {
279- let exec_ctx = exec_ctx. as_ref ( ) ;
280-
281251 let process = match process. take ( ) {
282252 Some ( p) => p,
283253 None => return CommandOutput :: default ( ) ,
@@ -301,11 +271,11 @@ impl<'a> DeferredCommand<'a> {
301271 writeln ! (
302272 message,
303273 r#"
304- Command {:?} did not execute successfully.
274+ Command {command :?} did not execute successfully.
305275Expected success, got {}
306276Created at: {created_at}
307277Executed at: {executed_at}"# ,
308- command , result. status,
278+ result. status,
309279 )
310280 . unwrap ( ) ;
311281
@@ -326,9 +296,8 @@ Executed at: {executed_at}"#,
326296
327297 writeln ! (
328298 message,
329- "\n \n Command {:?} did not execute successfully.\
330- \n It was not possible to execute the command: {e:?}",
331- command
299+ "\n \n Command {command:?} did not execute successfully.\
300+ \n It was not possible to execute the command: {e:?}"
332301 )
333302 . unwrap ( ) ;
334303
@@ -341,9 +310,8 @@ Executed at: {executed_at}"#,
341310
342311 writeln ! (
343312 message,
344- "\n \n Command {:?} did not execute successfully.\
345- \n It was not possible to execute the command: {e:?}",
346- command
313+ "\n \n Command {command:?} did not execute successfully.\
314+ \n It was not possible to execute the command: {e:?}"
347315 )
348316 . unwrap ( ) ;
349317
0 commit comments