yog_cli/design/
yui_codegen.rs1use std::path::PathBuf;
8use yog_ids::Format;
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, 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#[derive(yog_data::Schema)]
67pub struct Codegen {
68 pub error: Option<String>,
69 pub problems: Vec<yog_data::Diagnostic>,
70 pub root: String,
71}