44// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
55// Please see LICENSE files in the repository root for full details.
66
7- use std:: process:: ExitCode ;
7+ use std:: { collections :: BTreeSet , fmt :: Write , process:: ExitCode } ;
88
99use anyhow:: { Context as _, bail} ;
1010use camino:: Utf8PathBuf ;
@@ -83,7 +83,7 @@ impl Options {
8383 tokio:: fs:: read_dir ( & out_dir) . await . with_context ( || {
8484 format ! ( "could not read {out_dir} to check it's empty" )
8585 } ) ?;
86- while let Some ( x ) = read_dir. next_entry ( ) . await ? {
86+ if read_dir. next_entry ( ) . await ?. is_some ( ) {
8787 bail ! ( "Render directory {out_dir} is not empty, refusing to write." ) ;
8888 }
8989 } else {
@@ -92,19 +92,49 @@ impl Options {
9292 . with_context ( || format ! ( "could not create {out_dir}" ) ) ?;
9393 }
9494
95- for ( template, template_renders) in & all_renders {
96- let template_filename_base =
97- template. trim_end_matches ( ".html" ) . replace ( '/' , "_" ) ;
98- for ( idx, render) in template_renders. iter ( ) . enumerate ( ) {
99- let render_path =
100- out_dir. join ( format ! ( "{template_filename_base}-sample{idx}.html" ) ) ;
101-
102- tokio:: fs:: write ( & render_path, render. as_bytes ( ) )
103- . await
104- . with_context ( || {
105- format ! ( "could not write render to {render_path}" )
106- } ) ?;
107- }
95+ let all_locales: BTreeSet < & str > = all_renders
96+ . iter ( )
97+ . filter_map ( |( ( _, sample_identifier) , _) | {
98+ sample_identifier. locale . as_deref ( )
99+ } )
100+ . collect ( ) ;
101+ for locale in all_locales {
102+ let locale_dir = out_dir. join ( locale) ;
103+ tokio:: fs:: create_dir ( & locale_dir)
104+ . await
105+ . with_context ( || format ! ( "could not create {locale_dir}" ) ) ?;
106+ }
107+
108+ for ( ( template, sample_identifier) , template_render) in & all_renders {
109+ let ( template_filename_base, template_ext) =
110+ template. rsplit_once ( '.' ) . unwrap_or ( ( template, "txt" ) ) ;
111+ let template_filename_base = template_filename_base. replace ( '/' , "_" ) ;
112+
113+ // Make a string like:
114+ // - `-sample1`
115+ // - `-session2-sample1`
116+ let sample_suffix = {
117+ let mut s = String :: new ( ) ;
118+ if let Some ( session_index) = sample_identifier. session_index {
119+ write ! ( s, "-session{session_index}" ) ?;
120+ }
121+ write ! ( s, "-sample{}" , sample_identifier. index) ?;
122+ s
123+ } ;
124+
125+ let locale_dir = if let Some ( locale) = & sample_identifier. locale {
126+ out_dir. join ( locale)
127+ } else {
128+ out_dir. clone ( )
129+ } ;
130+
131+ let render_path = locale_dir. join ( format ! (
132+ "{template_filename_base}{sample_suffix}.{template_ext}"
133+ ) ) ;
134+
135+ tokio:: fs:: write ( & render_path, template_render. as_bytes ( ) )
136+ . await
137+ . with_context ( || format ! ( "could not write render to {render_path}" ) ) ?;
108138 }
109139 }
110140
0 commit comments