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 mappings;
19pub mod media;
20pub mod recipe_area;
21pub(crate) mod rust_code;
22pub mod serve;
23pub mod yres;
24pub mod yui;
25pub mod yui_component;
26pub mod yui_grid;
27pub mod yui_resources;
28pub mod yui_vm;
29
30/// `path` with `/` between its parts, whatever the system's separator.
31pub fn slash_path(path: &std::path::Path) -> String {
32    path.to_string_lossy().replace('\\', yog_ids::sep::PATH)
33}
34
35/// `path` as a project shows it: relative to its `root`, `/`-separated.
36pub(crate) fn shown_path(root: &std::path::Path, path: &std::path::Path) -> String {
37    slash_path(path.strip_prefix(root).unwrap_or(path))
38}
39
40/// Writes generated `code` to `path` (creating its directory) unless it's
41/// already there — unchanged files keep their mtime, so cargo doesn't
42/// rebuild for nothing.
43pub(crate) fn write_if_changed(
44    path: &std::path::Path,
45    code: impl AsRef<[u8]>,
46) -> Result<(), String> {
47    let code = code.as_ref();
48    if std::fs::read(path).ok().as_deref() == Some(code) {
49        return Ok(());
50    }
51    if let Some(dir) = path.parent() {
52        std::fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?;
53    }
54    std::fs::write(path, code).map_err(|e| format!("{}: {e}", path.display()))
55}