@@ -14,7 +14,8 @@ use args::Args;
14
14
use clap:: Parser ;
15
15
use fontbakery_bridge:: FontbakeryBridge ;
16
16
use fontspector_checkapi:: {
17
- Check , CheckResult , Context , FixResult , Plugin , Registry , TestableCollection , TestableType ,
17
+ Check , CheckResult , Context , FixResult , HotfixFunction , Plugin , Registry , Status , StatusCode ,
18
+ Testable , TestableCollection , TestableType ,
18
19
} ;
19
20
use itertools:: Either ;
20
21
use profile_googlefonts:: GoogleFonts ;
@@ -187,7 +188,7 @@ fn main() {
187
188
) ;
188
189
// }
189
190
190
- // Run all the things! Check all the fonts! Fix all the binaries! Fix all the sources!
191
+ // Run all the things! Check all the fonts!
191
192
192
193
// Do this in parallel for release, serial for debug
193
194
#[ cfg( debug_assertions) ]
@@ -203,21 +204,22 @@ fn main() {
203
204
Either :: Right ( checkorder. par_iter ( ) )
204
205
} ;
205
206
206
- #[ allow( clippy:: unwrap_used) ] // We check for is_some before unwrapping
207
- let results: RunResults = checkorder_iterator
207
+ let mut results: RunResults = checkorder_iterator
208
208
. map ( |( sectionname, testable, check, context) | {
209
209
(
210
210
testable,
211
211
check,
212
212
check. run ( testable, context, Some ( sectionname) ) ,
213
213
)
214
214
} )
215
- . filter_map ( |( testable, check, result) | {
216
- result. map ( |result| apply_fixes ( testable, check, result, & args) )
217
- } )
215
+ . filter_map ( |( _, _, result) | result)
218
216
. collect :: < Vec < CheckResult > > ( )
219
217
. into ( ) ;
220
218
219
+ if args. hotfix || args. fix_sources {
220
+ try_fixing_stuff ( & mut results, & args, & registry) ;
221
+ }
222
+
221
223
let worst_status = results. worst_status ( ) ;
222
224
223
225
let mut reporters: Vec < Box < dyn Reporter > > = vec ! [ ] ;
@@ -330,37 +332,88 @@ fn load_configuration(args: &Args) -> Map<String, serde_json::Value> {
330
332
. unwrap_or_default ( )
331
333
}
332
334
333
- fn apply_fixes (
334
- testable : & & TestableType ,
335
- check : & & Check ,
336
- mut result : CheckResult ,
337
- args : & Args ,
338
- ) -> CheckResult {
339
- if let TestableType :: Single ( testable) = testable {
340
- if args. hotfix {
341
- if let Some ( fix) = check. hotfix {
342
- result. hotfix_result = match fix ( testable) {
343
- Ok ( _) => Some ( FixResult :: Fixed ) ,
344
- Err ( e) => Some ( FixResult :: FixError ( e) ) ,
345
- }
346
- } else {
347
- result. hotfix_result = Some ( FixResult :: Unfixable ) ;
348
- }
349
- } else if check. hotfix . is_some ( ) {
350
- result. hotfix_result = Some ( FixResult :: Available ) ;
335
+ // fn apply_fixes(
336
+ // testable: &mut TestableType,
337
+ // check: &&Check,
338
+ // mut result: CheckResult,
339
+ // args: &Args,
340
+ // ) -> CheckResult {
341
+ // if let TestableType::Single(testable) = testable {
342
+ // if args.hotfix {
343
+ // if let Some(fix) = check.hotfix {
344
+ // result.hotfix_result = match fix(testable) {
345
+ // Ok(_) => Some(FixResult::Fixed),
346
+ // Err(e) => Some(FixResult::FixError(e)),
347
+ // }
348
+ // } else {
349
+ // result.hotfix_result = Some(FixResult::Unfixable);
350
+ // }
351
+ // } else if check.hotfix.is_some() {
352
+ // result.hotfix_result = Some(FixResult::Available);
353
+ // }
354
+ // if args.fix_sources {
355
+ // if let Some(fix) = check.fix_source {
356
+ // result.sourcefix_result = match fix(testable) {
357
+ // Ok(_) => Some(FixResult::Fixed),
358
+ // Err(e) => Some(FixResult::FixError(e)),
359
+ // }
360
+ // } else {
361
+ // result.sourcefix_result = Some(FixResult::Unfixable);
362
+ // }
363
+ // } else if check.fix_source.is_some() {
364
+ // result.sourcefix_result = Some(FixResult::Available);
365
+ // }
366
+ // }
367
+ // result
368
+ // }
369
+
370
+ fn try_fixing_stuff ( results : & mut RunResults , args : & Args , registry : & Registry ) {
371
+ let failed_checks = results
372
+ . iter_mut ( )
373
+ . filter ( |x| x. worst_status ( ) >= StatusCode :: Fail )
374
+ . collect :: < Vec < _ > > ( ) ;
375
+ // Group the fixes by filename because we want to provide testables
376
+ // // let mut fix_sources = HashMap::new();
377
+ let mut fix_binaries: HashMap < String , Vec < ( & HotfixFunction , & mut CheckResult ) > > =
378
+ HashMap :: new ( ) ;
379
+ for result in failed_checks. into_iter ( ) {
380
+ let Some ( check) = registry. checks . get ( & result. check_id ) else {
381
+ log:: warn!(
382
+ "A check called {} just mysteriously vanished" ,
383
+ result. check_id
384
+ ) ;
385
+ continue ;
386
+ } ;
387
+ if args. hotfix && result. filename . is_some ( ) && check. hotfix . is_some ( ) {
388
+ #[ allow( clippy:: unwrap_used) ] // We know this is Some
389
+ fix_binaries
390
+ . entry ( result. filename . clone ( ) . unwrap ( ) )
391
+ . or_default ( )
392
+ . push ( ( check. hotfix . unwrap ( ) , result) ) ;
351
393
}
352
- if args. fix_sources {
353
- if let Some ( fix) = check. fix_source {
354
- result. sourcefix_result = match fix ( testable) {
355
- Ok ( _) => Some ( FixResult :: Fixed ) ,
356
- Err ( e) => Some ( FixResult :: FixError ( e) ) ,
394
+ }
395
+
396
+ for ( file, fixes) in fix_binaries. into_iter ( ) {
397
+ let mut testable = Testable :: new ( & file) . unwrap_or_else ( |e| {
398
+ log:: error!( "Could not load files from {:?}: {:}" , file, e) ;
399
+ std:: process:: exit ( 1 )
400
+ } ) ;
401
+ let mut modified = false ;
402
+ for ( fix, result) in fixes. into_iter ( ) {
403
+ result. hotfix_result = match fix ( & mut testable) {
404
+ Ok ( hotfix_behaviour) => {
405
+ modified |= hotfix_behaviour;
406
+ Some ( FixResult :: Fixed )
357
407
}
358
- } else {
359
- result. sourcefix_result = Some ( FixResult :: Unfixable ) ;
408
+ Err ( e) => Some ( FixResult :: FixError ( e) ) ,
360
409
}
361
- } else if check. fix_source . is_some ( ) {
362
- result. sourcefix_result = Some ( FixResult :: Available ) ;
410
+ }
411
+ if modified {
412
+ // save it
413
+ testable. save ( ) . unwrap_or_else ( |e| {
414
+ log:: error!( "Could not save file {:?}: {:}" , file, e) ;
415
+ std:: process:: exit ( 1 )
416
+ } ) ;
363
417
}
364
418
}
365
- result
366
419
}
0 commit comments