origin-kit 0.3.1

The `origin` command: create a new Origin application from the embedded project template, without a checkout of Origin.
Documentation
//! Embeds the project template in the binary.
//!
//! A published crate can only read files that `cargo package` put in it, and `cargo
//! package` skips every directory holding a `Cargo.toml` — which is why the template's
//! own manifests are stored as `Cargo.toml.in` and renamed back when a project is
//! written. Walking the tree here keeps that the only trick: no dependency, no macro,
//! and a build that fails loudly rather than shipping a binary with an empty template.

use std::fmt::Write as _;
use std::path::Path;

/// Directories that may exist in a developer's checkout but are never part of the
/// template: build output and installed packages from running the template in place.
const IGNORED: &[&str] = &["target", "node_modules", "dist", "gen", ".git"];

fn main() {
    let manifest = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR");
    let root = Path::new(&manifest).join("templates");
    println!("cargo::rerun-if-changed={}", root.display());

    let mut files = Vec::new();
    collect(&root, String::new(), &mut files);
    files.sort();

    assert!(
        !files.is_empty(),
        "no template files under {} — the template is missing from this package",
        root.display()
    );

    let mut generated = String::from(
        "// Generated by build.rs from the `templates/` directory. Do not edit.\n\
         pub(crate) static FILES: &[(&str, &[u8])] = &[\n",
    );
    for (relative, absolute) in &files {
        writeln!(
            generated,
            "    ({relative:?}, include_bytes!({absolute:?})),"
        )
        .expect("writing to a String cannot fail");
    }
    generated.push_str("];\n");

    let out = Path::new(&std::env::var("OUT_DIR").expect("OUT_DIR")).join("template.rs");
    std::fs::write(&out, generated).expect("cannot write the embedded template list");
}

/// Collect `(path relative to the template root, absolute path)` for every file.
fn collect(directory: &Path, prefix: String, files: &mut Vec<(String, String)>) {
    let entries = std::fs::read_dir(directory)
        .unwrap_or_else(|error| panic!("cannot read {}: {error}", directory.display()));

    for entry in entries.filter_map(Result::ok) {
        let name = entry.file_name().to_string_lossy().into_owned();
        let path = entry.path();

        // Always `/`, on every platform: the value ends up in generated source and is
        // joined onto the target directory as a relative path.
        let relative = if prefix.is_empty() {
            name.clone()
        } else {
            format!("{prefix}/{name}")
        };

        if path.is_dir() {
            if !IGNORED.contains(&name.as_str()) {
                collect(&path, relative, files);
            }
            continue;
        }

        if name == ".DS_Store" {
            continue;
        }

        files.push((relative, path.to_string_lossy().into_owned()));
    }
}