3
3
//! This module provides the [`ExecutionContext`] type, which holds global configuration
4
4
//! relevant during the execution of commands in bootstrap. This includes dry-run
5
5
//! mode, verbosity level, and behavior on failure.
6
- #![ allow( warnings) ]
7
6
use std:: collections:: HashMap ;
8
7
use std:: panic:: Location ;
9
8
use std:: process:: Child ;
@@ -46,10 +45,6 @@ pub struct DeferredCommand<'a> {
46
45
}
47
46
48
47
impl CommandCache {
49
- pub fn new ( ) -> Self {
50
- Self { cache : Mutex :: new ( HashMap :: new ( ) ) }
51
- }
52
-
53
48
pub fn get ( & self , key : & CommandCacheKey ) -> Option < CommandOutput > {
54
49
self . cache . lock ( ) . unwrap ( ) . get ( key) . cloned ( )
55
50
}
@@ -133,7 +128,7 @@ impl ExecutionContext {
133
128
{
134
129
command. mark_as_executed ( ) ;
135
130
136
- self . verbose ( || println ! ( "Cache hit: {:?}" , command ) ) ;
131
+ self . verbose ( || println ! ( "Cache hit: {command :?}" ) ) ;
137
132
138
133
return DeferredCommand { state : CommandState :: Cached ( cached_output) } ;
139
134
}
@@ -191,29 +186,7 @@ impl ExecutionContext {
191
186
stdout : OutputMode ,
192
187
stderr : OutputMode ,
193
188
) -> 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 )
217
190
}
218
191
219
192
fn fail ( & self , message : & str , output : CommandOutput ) -> ! {
@@ -247,7 +220,7 @@ impl AsRef<ExecutionContext> for ExecutionContext {
247
220
}
248
221
249
222
impl < ' 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 {
251
224
match self . state {
252
225
CommandState :: Cached ( output) => output,
253
226
CommandState :: Deferred { process, command, stdout, stderr, executed_at, cache_key } => {
@@ -256,11 +229,10 @@ impl<'a> DeferredCommand<'a> {
256
229
let output =
257
230
Self :: execute_process ( process, command, stdout, stderr, executed_at, exec_ctx) ;
258
231
259
- if ( !exec_ctx. dry_run ( ) || command. run_always )
260
- && cache_key. is_some ( )
261
- && output. status ( ) . is_some ( )
262
- {
263
- exec_ctx. command_cache . insert ( cache_key. unwrap ( ) , output. clone ( ) ) ;
232
+ if !exec_ctx. dry_run ( ) || command. run_always {
233
+ if let ( Some ( cache_key) , Some ( _) ) = ( & cache_key, output. status ( ) ) {
234
+ exec_ctx. command_cache . insert ( cache_key. clone ( ) , output. clone ( ) ) ;
235
+ }
264
236
}
265
237
266
238
output
@@ -276,8 +248,6 @@ impl<'a> DeferredCommand<'a> {
276
248
executed_at : & ' a std:: panic:: Location < ' a > ,
277
249
exec_ctx : & ExecutionContext ,
278
250
) -> CommandOutput {
279
- let exec_ctx = exec_ctx. as_ref ( ) ;
280
-
281
251
let process = match process. take ( ) {
282
252
Some ( p) => p,
283
253
None => return CommandOutput :: default ( ) ,
@@ -301,11 +271,11 @@ impl<'a> DeferredCommand<'a> {
301
271
writeln ! (
302
272
message,
303
273
r#"
304
- Command {:?} did not execute successfully.
274
+ Command {command :?} did not execute successfully.
305
275
Expected success, got {}
306
276
Created at: {created_at}
307
277
Executed at: {executed_at}"# ,
308
- command , result. status,
278
+ result. status,
309
279
)
310
280
. unwrap ( ) ;
311
281
@@ -326,9 +296,8 @@ Executed at: {executed_at}"#,
326
296
327
297
writeln ! (
328
298
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:?}"
332
301
)
333
302
. unwrap ( ) ;
334
303
0 commit comments