@@ -484,10 +484,7 @@ fn build_documentation(workspace: &Path, mut args: BuildDocumentationArgs) -> Re
484
484
485
485
for package in args. packages {
486
486
// Not all packages need documentation built:
487
- if matches ! (
488
- package,
489
- Package :: Examples | Package :: HilTest | Package :: QaTest
490
- ) {
487
+ if !package. should_document ( ) {
491
488
continue ;
492
489
}
493
490
@@ -524,15 +521,13 @@ fn build_documentation_index(
524
521
mut args : BuildDocumentationIndexArgs ,
525
522
) -> Result < ( ) > {
526
523
let docs_path = workspace. join ( "docs" ) ;
524
+ let resources_path = workspace. join ( "resources" ) ;
527
525
528
526
args. packages . sort ( ) ;
529
527
530
528
for package in args. packages {
531
529
// Not all packages have documentation built:
532
- if matches ! (
533
- package,
534
- Package :: Examples | Package :: HilTest | Package :: QaTest
535
- ) {
530
+ if !package. should_document ( ) {
536
531
continue ;
537
532
}
538
533
@@ -548,14 +543,17 @@ fn build_documentation_index(
548
543
549
544
// Each path we iterate over should be the directory for a given version of
550
545
// the package's documentation:
551
- for path in fs:: read_dir ( package_docs_path) ? {
552
- let path = path?. path ( ) ;
553
- if path. is_file ( ) {
554
- log:: debug!( "Path is not a directory, skipping: '{}'" , path. display( ) ) ;
546
+ for version_path in fs:: read_dir ( package_docs_path) ? {
547
+ let version_path = version_path?. path ( ) ;
548
+ if version_path. is_file ( ) {
549
+ log:: debug!(
550
+ "Path is not a directory, skipping: '{}'" ,
551
+ version_path. display( )
552
+ ) ;
555
553
continue ;
556
554
}
557
555
558
- for path in fs:: read_dir ( & path ) ? {
556
+ for path in fs:: read_dir ( & version_path ) ? {
559
557
let path = path?. path ( ) ;
560
558
if path. is_dir ( ) {
561
559
device_doc_paths. push ( path) ;
@@ -567,11 +565,11 @@ fn build_documentation_index(
567
565
. map ( |path| {
568
566
let chip = path
569
567
. components ( )
570
- . into_iter ( )
571
568
. last ( )
572
569
. unwrap ( )
573
570
. as_os_str ( )
574
571
. to_string_lossy ( ) ;
572
+
575
573
let chip = Chip :: from_str ( & chip, true ) . unwrap ( ) ;
576
574
577
575
chip
@@ -581,38 +579,87 @@ fn build_documentation_index(
581
579
chips. sort ( ) ;
582
580
583
581
let meta = generate_documentation_meta_for_package ( workspace, package, & chips) ?;
584
- generate_index ( workspace, & path, & meta) ?;
582
+ render_template (
583
+ "package_index.html.jinja" ,
584
+ "index.html" ,
585
+ & version_path,
586
+ & resources_path,
587
+ minijinja:: context! { metadata => meta } ,
588
+ ) ?;
585
589
}
586
590
}
587
591
588
- Ok ( ( ) )
589
- }
590
-
591
- fn generate_index ( workspace : & Path , package_version_path : & Path , meta : & [ Value ] ) -> Result < ( ) > {
592
- let resources = workspace. join ( "resources" ) ;
593
-
594
592
// Copy any additional assets to the documentation's output path:
595
593
fs:: copy (
596
- resources . join ( "esp-rs.svg" ) ,
597
- package_version_path . join ( "esp-rs.svg" ) ,
594
+ resources_path . join ( "esp-rs.svg" ) ,
595
+ docs_path . join ( "esp-rs.svg" ) ,
598
596
)
599
597
. context ( "Failed to copy esp-rs.svg" ) ?;
600
598
601
- // Render the index and write it out to the documentaiton's output path:
602
- let source = fs:: read_to_string ( resources. join ( "index.html.jinja" ) )
603
- . context ( "Failed to read index.html.jinja" ) ?;
599
+ let meta = generate_documentation_meta_for_index ( & workspace) ?;
604
600
605
- let mut env = minijinja:: Environment :: new ( ) ;
606
- env. add_template ( "index" , & source) ?;
601
+ render_template (
602
+ "index.html.jinja" ,
603
+ "index.html" ,
604
+ & docs_path,
605
+ & resources_path,
606
+ minijinja:: context! { metadata => meta } ,
607
+ ) ?;
608
+
609
+ Ok ( ( ) )
610
+ }
611
+
612
+ fn generate_documentation_meta_for_index ( workspace : & Path ) -> Result < Vec < Value > > {
613
+ let mut metadata = Vec :: new ( ) ;
614
+
615
+ for package in Package :: iter ( ) {
616
+ // Not all packages have documentation built:
617
+ if !package. should_document ( ) {
618
+ continue ;
619
+ }
620
+
621
+ let version = xtask:: package_version ( workspace, package) ?;
607
622
608
- let tmpl = env. get_template ( "index" ) ?;
609
- let html = tmpl. render ( minijinja:: context! { metadata => meta } ) ?;
623
+ let url = if package. chip_features_matter ( ) {
624
+ format ! ( "{package}/{version}/index.html" )
625
+ } else {
626
+ let crate_name = package. to_string ( ) . replace ( '-' , "_" ) ;
627
+ format ! ( "{package}/{version}/{crate_name}/index.html" )
628
+ } ;
610
629
611
- let index = package_version_path. join ( "index.html" ) ;
630
+ metadata. push ( minijinja:: context! {
631
+ name => package,
632
+ version => version,
633
+ url => url,
634
+ } ) ;
635
+ }
636
+
637
+ Ok ( metadata)
638
+ }
639
+
640
+ fn render_template < C > (
641
+ template : & str ,
642
+ name : & str ,
643
+ path : & Path ,
644
+ resources : & Path ,
645
+ ctx : C ,
646
+ ) -> Result < ( ) >
647
+ where
648
+ C : serde:: Serialize ,
649
+ {
650
+ let source = fs:: read_to_string ( resources. join ( template) )
651
+ . context ( format ! ( "Failed to read {template}" ) ) ?;
652
+
653
+ let mut env = minijinja:: Environment :: new ( ) ;
654
+ env. add_template ( template, & source) ?;
612
655
613
- fs:: write ( & index, html) . context ( "Failed to write index.html" ) ?;
656
+ let tmpl = env. get_template ( template) ?;
657
+ let html = tmpl. render ( ctx) ?;
614
658
615
- log:: info!( "Created {}" , index. display( ) ) ;
659
+ // Write out the rendered HTML to the desired path:
660
+ let path = path. join ( name) ;
661
+ fs:: write ( & path, html) . context ( format ! ( "Failed to write {name}" ) ) ?;
662
+ log:: info!( "Created {}" , path. display( ) ) ;
616
663
617
664
Ok ( ( ) )
618
665
}
0 commit comments