1
- use serde_json:: json;
2
- use clap:: ValueEnum ;
3
- use serde:: Deserialize ;
4
1
use crate :: logging:: ask_value;
5
2
use crate :: mod_file:: { PlatformName , ToGeodeString } ;
6
3
use crate :: util:: mod_file:: DependencyImportance ;
@@ -14,22 +11,25 @@ use crate::{
14
11
} ,
15
12
} ;
16
13
use clap:: Subcommand ;
14
+ use clap:: ValueEnum ;
17
15
use edit_distance:: edit_distance;
18
16
use semver:: Version ;
17
+ use serde:: Deserialize ;
18
+ use serde_json:: json;
19
+ use serde_json:: Value ;
19
20
use std:: env;
20
21
use std:: {
21
22
collections:: HashMap ,
22
23
fs,
23
24
path:: { Path , PathBuf } ,
24
25
} ;
25
- use serde_json:: Value ;
26
26
27
27
#[ derive( Debug , Deserialize , Hash , PartialEq , Eq , Clone , Copy , ValueEnum ) ]
28
28
#[ serde( rename_all = "lowercase" ) ]
29
29
pub enum ResourceType {
30
30
Sprite ,
31
31
Font ,
32
- File
32
+ File ,
33
33
}
34
34
35
35
#[ derive( Subcommand , Debug ) ]
@@ -69,8 +69,8 @@ pub enum Project {
69
69
Add {
70
70
/// Type of resource to add
71
71
resource : ResourceType ,
72
- files : Vec < PathBuf >
73
- }
72
+ files : Vec < PathBuf > ,
73
+ } ,
74
74
}
75
75
76
76
fn find_build_directory ( root : & Path ) -> Option < PathBuf > {
@@ -534,75 +534,121 @@ pub fn check_dependencies(
534
534
}
535
535
536
536
fn add_resource ( dir : & Path , resource : ResourceType , files : Vec < PathBuf > ) {
537
- let mut mod_json: HashMap < String , Value > = serde_json:: from_reader ( fs:: File :: open ( dir. join ( "mod.json" ) ) . ok ( ) . nice_unwrap ( "Must be inside a project with a mod.json" ) )
538
- . nice_unwrap ( "Unable to read mod.json" ) ;
539
-
537
+ let mut mod_json: HashMap < String , Value > = serde_json:: from_reader (
538
+ fs:: File :: open ( dir. join ( "mod.json" ) )
539
+ . ok ( )
540
+ . nice_unwrap ( "Must be inside a project with a mod.json" ) ,
541
+ )
542
+ . nice_unwrap ( "Unable to read mod.json" ) ;
540
543
541
544
let mut do_thing = |name : & str , othername : & str | {
542
- let resource = mod_json. get ( "resources" )
545
+ let resource = mod_json
546
+ . get ( "resources" )
543
547
. and_then ( |x| x. get ( name) )
544
548
. and_then ( |x| x. as_array ( ) )
545
549
. unwrap_or ( & vec ! [ ] )
546
550
. clone ( ) ;
547
551
548
- let mut new_resource: Vec < Value > = resource. into_iter ( ) . chain ( files. clone ( ) . into_iter ( ) . filter_map ( |x| {
549
- if !x. exists ( ) {
550
- warn ! ( "{} {} does not exist" , othername, x. display( ) ) ;
551
- None
552
- } else {
553
- Some ( Value :: String ( x. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) )
554
- }
555
- } ) ) . collect ( ) ;
552
+ let mut new_resource: Vec < Value > = resource
553
+ . into_iter ( )
554
+ . chain ( files. clone ( ) . into_iter ( ) . filter_map ( |x| {
555
+ if !x. exists ( ) {
556
+ warn ! ( "{} {} does not exist" , othername, x. display( ) ) ;
557
+ None
558
+ } else {
559
+ Some ( Value :: String ( x. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) )
560
+ }
561
+ } ) )
562
+ . collect ( ) ;
556
563
557
- let mut duplicates: Vec < _ > = new_resource. iter ( ) . filter ( |x| new_resource. iter ( ) . filter ( |y| y == x) . count ( ) > 1 ) . collect ( ) ;
564
+ let mut duplicates: Vec < _ > = new_resource
565
+ . iter ( )
566
+ . filter ( |x| new_resource. iter ( ) . filter ( |y| y == x) . count ( ) > 1 )
567
+ . collect ( ) ;
558
568
duplicates. dedup ( ) ;
559
- duplicates. into_iter ( ) . for_each ( |x| warn ! ( "Duplicate {}: {}" , othername, x) ) ;
569
+ duplicates
570
+ . into_iter ( )
571
+ . for_each ( |x| warn ! ( "Duplicate {}: {}" , othername, x) ) ;
560
572
561
573
new_resource. dedup ( ) ;
562
574
563
- * mod_json. entry ( "resources" . to_string ( ) ) . or_insert ( json ! ( { } ) )
564
- . as_object_mut ( ) . nice_unwrap ( "resources is not an object" )
565
- . entry ( name. to_string ( ) ) . or_insert ( json ! ( [ ] ) )
566
- . as_array_mut ( ) . nice_unwrap ( & format ! ( "{} is not an array" , name) ) = new_resource;
575
+ * mod_json
576
+ . entry ( "resources" . to_string ( ) )
577
+ . or_insert ( json ! ( { } ) )
578
+ . as_object_mut ( )
579
+ . nice_unwrap ( "resources is not an object" )
580
+ . entry ( name. to_string ( ) )
581
+ . or_insert ( json ! ( [ ] ) )
582
+ . as_array_mut ( )
583
+ . nice_unwrap ( & format ! ( "{} is not an array" , name) ) = new_resource;
567
584
} ;
568
585
569
586
match resource {
570
587
ResourceType :: Sprite => do_thing ( "sprites" , "Sprite" ) ,
571
588
ResourceType :: File => do_thing ( "files" , "File" ) ,
572
589
573
590
ResourceType :: Font => {
574
- let fonts = mod_json. get ( "resources" )
591
+ let fonts = mod_json
592
+ . get ( "resources" )
575
593
. and_then ( |x| x. get ( "fonts" ) )
576
594
. and_then ( |x| x. as_array ( ) )
577
595
. unwrap_or ( & vec ! [ ] )
578
596
. clone ( ) ;
579
597
580
- let mut new_fonts: Vec < Value > = fonts. into_iter ( ) . chain ( files. into_iter ( ) . filter_map ( |x| {
581
- if !x. exists ( ) {
582
- warn ! ( "Font {} does not exist" , x. display( ) ) ;
583
- None
584
- } else {
585
- Some ( json ! ( {
586
- "path" : x. as_os_str( ) . to_str( ) . unwrap( ) . to_string( ) ,
587
- "size" : ask_value( "Font Size" , None , true ) . parse:: <u32 >( ) . ok( ) . nice_unwrap( "Invalid font size!" )
588
- } ) )
589
- }
590
- } ) ) . collect ( ) ;
591
-
592
- let mut duplicates: Vec < _ > = new_fonts. iter ( ) . filter_map ( |x| x. get ( "path" ) ) . filter ( |x| new_fonts. iter ( ) . filter ( |y| y. get ( "path" ) . map ( |y| y == * x) . unwrap_or ( false ) ) . count ( ) > 1 ) . collect ( ) ;
598
+ let mut new_fonts: Vec < Value > = fonts
599
+ . into_iter ( )
600
+ . chain ( files. into_iter ( ) . filter_map ( |x| {
601
+ if !x. exists ( ) {
602
+ warn ! ( "Font {} does not exist" , x. display( ) ) ;
603
+ None
604
+ } else {
605
+ let size = ask_value ( "Font Size" , None , true )
606
+ . parse :: < u32 > ( )
607
+ . ok ( )
608
+ . nice_unwrap ( "Invalid font size!" ) ;
609
+
610
+ Some ( json ! ( {
611
+ "path" : x. as_os_str( ) . to_str( ) . unwrap( ) . to_string( ) ,
612
+ "size" : size
613
+ } ) )
614
+ }
615
+ } ) )
616
+ . collect ( ) ;
617
+
618
+ let mut duplicates: Vec < _ > = new_fonts
619
+ . iter ( )
620
+ . filter_map ( |x| x. get ( "path" ) )
621
+ . filter ( |x| {
622
+ new_fonts
623
+ . iter ( )
624
+ . filter ( |y| y. get ( "path" ) . map ( |y| y == * x) . unwrap_or ( false ) )
625
+ . count ( ) > 1
626
+ } )
627
+ . collect ( ) ;
593
628
duplicates. dedup ( ) ;
594
- duplicates. into_iter ( ) . for_each ( |x| warn ! ( "Duplicate Font: {}" , x) ) ;
629
+ duplicates
630
+ . into_iter ( )
631
+ . for_each ( |x| warn ! ( "Duplicate Font: {}" , x) ) ;
595
632
596
633
new_fonts. dedup ( ) ;
597
634
598
- * mod_json. entry ( "resources" . to_string ( ) ) . or_insert ( json ! ( { } ) )
599
- . as_object_mut ( ) . nice_unwrap ( "resources is not an object" )
600
- . entry ( "fonts" . to_string ( ) ) . or_insert ( json ! ( [ ] ) )
601
- . as_array_mut ( ) . nice_unwrap ( "fonts is not an array" ) = new_fonts;
635
+ * mod_json
636
+ . entry ( "resources" . to_string ( ) )
637
+ . or_insert ( json ! ( { } ) )
638
+ . as_object_mut ( )
639
+ . nice_unwrap ( "resources is not an object" )
640
+ . entry ( "fonts" . to_string ( ) )
641
+ . or_insert ( json ! ( [ ] ) )
642
+ . as_array_mut ( )
643
+ . nice_unwrap ( "fonts is not an array" ) = new_fonts;
602
644
}
603
- } ;
645
+ } ;
604
646
605
- fs:: write ( dir. join ( "mod.json" ) , serde_json:: to_string_pretty ( & mod_json) . unwrap ( ) ) . nice_unwrap ( "Failed to save mod.json" ) ;
647
+ fs:: write (
648
+ dir. join ( "mod.json" ) ,
649
+ serde_json:: to_string_pretty ( & mod_json) . unwrap ( ) ,
650
+ )
651
+ . nice_unwrap ( "Failed to save mod.json" ) ;
606
652
607
653
done ! ( "Resource added to mod.json" ) ;
608
654
}
@@ -621,10 +667,8 @@ pub fn subcommand(cmd: Project) {
621
667
platform,
622
668
externals,
623
669
) ,
624
- Project :: Add { resource, files } => add_resource (
625
- & std:: env:: current_dir ( ) . unwrap ( ) ,
626
- resource,
627
- files
628
- ) ,
670
+ Project :: Add { resource, files } => {
671
+ add_resource ( & std:: env:: current_dir ( ) . unwrap ( ) , resource, files)
672
+ }
629
673
}
630
674
}
0 commit comments