Skip to main content

yog_cli/
lib.rs

1//! The `yog` CLI's internals: `yog.toml` parsing, the generated
2//! `.yog-build/` workspace, `.yres` resources, `content/` blocks and items,
3//! `.yui` screens and `#[yog_export]` crates.
4//!
5//! Not for external use — a library only so the `yog` binary, its tests and
6//! its benches share the code. No API stability, not published separately.
7
8pub mod bench;
9pub mod book_page;
10pub mod cargo_toml;
11mod consts;
12pub mod content;
13pub mod design;
14pub mod diagnostics;
15pub mod exports;
16pub mod files;
17pub mod manifest;
18pub mod media;
19pub mod recipe_area;
20pub(crate) mod rust_code;
21pub mod serve;
22pub mod yres;
23pub mod yui;
24pub mod yui_component;
25pub mod yui_grid;
26pub mod yui_resources;
27pub mod yui_vm;
28
29/// `path` with `/` between its parts, whatever the system's separator.
30pub fn slash_path(path: &std::path::Path) -> String {
31    path.to_string_lossy().replace('\\', yog_ids::sep::PATH)
32}
33
34/// `path` as a project shows it: relative to its `root`, `/`-separated.
35pub(crate) fn shown_path(root: &std::path::Path, path: &std::path::Path) -> String {
36    slash_path(path.strip_prefix(root).unwrap_or(path))
37}
38
39/// Writes generated `code` to `path` (creating its directory) unless it's
40/// already there — unchanged files keep their mtime, so cargo doesn't
41/// rebuild for nothing.
42pub(crate) fn write_if_changed(
43    path: &std::path::Path,
44    code: impl AsRef<[u8]>,
45) -> Result<(), String> {
46    let code = code.as_ref();
47    if std::fs::read(path).ok().as_deref() == Some(code) {
48        return Ok(());
49    }
50    if let Some(dir) = path.parent() {
51        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
52    }
53    std::fs::write(path, code).map_err(|e| format!("{}: {e}", path.display()))
54}