Skip to main content

yog_cli/design/
yui_codegen.rs

1//! `.yui` screen files (the screen designer's format) — regenerating their
2//! code (`.yog-build/generated/**/*.g.rs`, what the code-behind pulls in
3//! with `yog_api::ui_include!`) when one is saved, through `yog ui gen` —
4//! and the same for `.yres` resource files (`resources/<Set>.g.rs`,
5//! through `yog res check`).
6
7use std::path::PathBuf;
8use yog_ids::Format;
9
10/// Runs `yog ui gen <path>` in the file's folder, so a saved screen's
11/// generated `foo.g.rs` is current without a full build (`yog` finds the
12/// project from the `yog.toml` above the file). The result carries what
13/// `yog` found either way — its errors and warnings go to the Error List;
14/// `Err` only when `yog` couldn't run at all.
15pub fn codegen(path: &str) -> Result<Codegen, String> {
16    let file = PathBuf::from(path);
17    let args: &[&str] = match Format::of_path(&file) {
18        Some(Format::Ui) => &["ui", "gen"],
19        // Regenerates `.yog-build/generated/resources/<Set>.g.rs` (and checks the set).
20        Some(Format::Res) => &["res", "check"],
21        // Regenerates `.yog-build/generated/yog-content.g.rs` and its parts (and checks every content file).
22        Some(Format::Block | Format::Item) => &["content", "check"],
23        _ => return Err(format!("not a .yui, .yres, .yblock or .yitem file: {path}")),
24    };
25    let mut command = crate::design::this_cli();
26    command.args(args);
27    if args[0] == "ui" {
28        command.arg(&file);
29    }
30    // `yog res check` / `yog content check` work on the project in the current folder.
31    let project = file
32        .ancestors()
33        .skip(1)
34        .find(|d| crate::design::project::path(d).is_file());
35    let dir = (if args[0] != "ui" { project } else { None })
36        .or(file.parent())
37        .filter(|d| d.is_dir());
38    if let Some(dir) = dir {
39        command.current_dir(dir);
40    }
41    let (output, problems) = crate::diagnostics::collected(&mut command)
42        .map_err(|e| format!("failed to run yog: {e}"))?;
43    let failed = || {
44        let errors: Vec<String> = problems
45            .iter()
46            .filter(|d| d.is_error())
47            .map(ToString::to_string)
48            .collect();
49        if errors.is_empty() {
50            format!("yog {} failed ({})", args.join(" "), output.status)
51        } else {
52            errors.join("\n")
53        }
54    };
55    Ok(Codegen {
56        error: (!output.status.success()).then(failed),
57        root: project
58            .map(|d| d.to_string_lossy().into_owned())
59            .unwrap_or_default(),
60        problems,
61    })
62}
63
64/// One regeneration: the failure's message, and what `yog` found — each
65/// record's file relative to `root`, the project's folder.
66#[derive(yog_data::Schema)]
67pub struct Codegen {
68    pub error: Option<String>,
69    pub problems: Vec<yog_data::Diagnostic>,
70    pub root: String,
71}