-
Notifications
You must be signed in to change notification settings - Fork 63
templates check command: Allow saving rendered samples #5202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
4af582a
72f9a1f
763e236
7a24a22
16ec04e
e5183f6
f633e42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial | ||
| // Please see LICENSE files in the repository root for full details. | ||
|
|
||
| use std::process::ExitCode; | ||
| use std::{collections::BTreeSet, fmt::Write, process::ExitCode}; | ||
|
|
||
| use anyhow::{Context as _, bail}; | ||
| use camino::Utf8PathBuf; | ||
|
|
@@ -83,7 +83,7 @@ impl Options { | |
| tokio::fs::read_dir(&out_dir).await.with_context(|| { | ||
| format!("could not read {out_dir} to check it's empty") | ||
| })?; | ||
| while let Some(x) = read_dir.next_entry().await? { | ||
| if read_dir.next_entry().await?.is_some() { | ||
| bail!("Render directory {out_dir} is not empty, refusing to write."); | ||
| } | ||
| } else { | ||
|
|
@@ -92,19 +92,49 @@ impl Options { | |
| .with_context(|| format!("could not create {out_dir}"))?; | ||
| } | ||
|
|
||
| for (template, template_renders) in &all_renders { | ||
| let template_filename_base = | ||
| template.trim_end_matches(".html").replace('/', "_"); | ||
| for (idx, render) in template_renders.iter().enumerate() { | ||
| let render_path = | ||
| out_dir.join(format!("{template_filename_base}-sample{idx}.html")); | ||
|
|
||
| tokio::fs::write(&render_path, render.as_bytes()) | ||
| .await | ||
| .with_context(|| { | ||
| format!("could not write render to {render_path}") | ||
| })?; | ||
| } | ||
| let all_locales: BTreeSet<&str> = all_renders | ||
| .iter() | ||
| .filter_map(|((_, sample_identifier), _)| { | ||
| sample_identifier.locale.as_deref() | ||
| }) | ||
| .collect(); | ||
| for locale in all_locales { | ||
| let locale_dir = out_dir.join(locale); | ||
| tokio::fs::create_dir(&locale_dir) | ||
| .await | ||
| .with_context(|| format!("could not create {locale_dir}"))?; | ||
| } | ||
|
|
||
| for ((template, sample_identifier), template_render) in &all_renders { | ||
| let (template_filename_base, template_ext) = | ||
| template.rsplit_once('.').unwrap_or((template, "txt")); | ||
| let template_filename_base = template_filename_base.replace('/', "_"); | ||
|
|
||
| // Make a string like: | ||
| // - `-sample1` | ||
| // - `-session2-sample1` | ||
| let sample_suffix = { | ||
| let mut s = String::new(); | ||
| if let Some(session_index) = sample_identifier.session_index { | ||
| write!(s, "-session{session_index}")?; | ||
| } | ||
| write!(s, "-sample{}", sample_identifier.index)?; | ||
| s | ||
| }; | ||
|
|
||
| let locale_dir = if let Some(locale) = &sample_identifier.locale { | ||
| out_dir.join(locale) | ||
|
||
| } else { | ||
| out_dir.clone() | ||
| }; | ||
|
|
||
| let render_path = locale_dir.join(format!( | ||
| "{template_filename_base}{sample_suffix}.{template_ext}" | ||
| )); | ||
|
|
||
| tokio::fs::write(&render_path, template_render.as_bytes()) | ||
| .await | ||
| .with_context(|| format!("could not write render to {render_path}"))?; | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.