yog_cli/design/
yui_codegen.rs1use yog_ids::Format;
8use std::path::PathBuf;
9
10pub 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 Some(Format::Res) => &["res", "check"],
21 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 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 = command
42 .output()
43 .map_err(|e| format!("failed to run yog: {e}"))?;
44 let log = String::from_utf8_lossy(&output.stderr).into_owned();
45 let ok = output.status.success();
46 Ok(Codegen {
47 generated: log
49 .lines()
50 .find_map(|l| l.rsplit_once(" -> ").map(|(_, out)| out.trim().to_string()))
51 .filter(|_| ok)
52 .unwrap_or_default(),
53 error: (!ok).then(|| {
54 let message = log
55 .lines()
56 .map(|l| l.trim_start_matches("yog: error: "))
57 .collect::<Vec<_>>()
58 .join("\n");
59 if message.trim().is_empty() {
60 format!("yog {} failed ({})", args.join(" "), output.status)
61 } else {
62 message.trim().to_string()
63 }
64 }),
65 root: dir.map(|d| d.to_string_lossy().into_owned()).unwrap_or_default(),
66 log,
67 })
68}
69
70#[derive(serde::Serialize)]
74pub struct Codegen {
75 pub generated: String,
76 pub error: Option<String>,
77 pub log: String,
78 pub root: String,
79}