perk-cli 0.1.0

The `perk` command: create apps, add pages, check them, and run a development server that reloads on save
//! `perk new`: writes a small working application.

use std::fs;
use std::path::{Component, Path, PathBuf};

use crate::{CliError, value};

pub const USAGE: &str = "\
usage: perk new <path> [--name <name>] [--tailwind] [--perk-path <dir> | --git <url>]

Creates a Perk application at <path>: a prerendered home page, a counter
with browser state, a page that loads server data, a layout, a component and
tests. Inside a Cargo workspace it is added to the workspace's members.

  --name          the package name (default: the last part of <path>)
  --tailwind      turn on Tailwind CSS (needs the Tailwind CLI; see the README)
  --perk-path     depend on a local checkout of the Perk repository
  --git           depend on Perk from this git URL

By default the app depends on the crates.io release that matches this
command's version, or on the enclosing Perk checkout when there is one.
";

const FILES: &[(&str, &str)] = &[
    ("Cargo.toml", include_str!("../templates/app/Cargo.toml.template")),
    ("build.rs", include_str!("../templates/app/build.rs")),
    (".gitignore", include_str!("../templates/app/gitignore")),
    ("README.md", include_str!("../templates/app/README.md")),
    ("perk.toml", include_str!("../templates/app/perk.toml")),
    ("src/lib.rs", include_str!("../templates/app/src/lib.rs")),
    ("src/main.rs", include_str!("../templates/app/src/main.rs")),
    ("src/sources.rs", include_str!("../templates/app/src/sources.rs")),
    ("src/lib/Card.perk", include_str!("../templates/app/src/lib/Card.perk")),
    ("src/routes/index.perk", include_str!("../templates/app/src/routes/index.perk")),
    ("src/routes/counter.perk", include_str!("../templates/app/src/routes/counter.perk")),
    ("src/routes/hello.perk", include_str!("../templates/app/src/routes/hello.perk")),
    ("tests/pages.rs", include_str!("../templates/app/tests/pages.rs")),
];
const LAYOUT_PLAIN: &str = include_str!("../templates/app/src/routes/layout-plain.perk");
const LAYOUT_TAILWIND: &str = include_str!("../templates/app/src/routes/layout-tailwind.perk");
const PACKAGE_JSON: &str = "{\n  \"private\": true,\n  \"devDependencies\": {\n    \"@tailwindcss/cli\": \"^4\",\n    \"tailwindcss\": \"^4\"\n  }\n}\n";

enum Dep {
    Path(PathBuf),
    Git(String),
    /// The crates.io release matching this command, such as `"0.1"`.
    Registry,
}

/// The version requirement new apps put on the crates.io releases: the
/// command's own major and minor version, so `perk new` from 0.1.x writes
/// `"0.1"` and the app gets the compatible patch releases.
fn registry_requirement() -> String {
    let mut parts = env!("CARGO_PKG_VERSION").split('.');
    let major = parts.next().unwrap_or("0");
    let minor = parts.next().unwrap_or("0");
    format!("{major}.{minor}")
}

struct Options {
    path: PathBuf,
    name: Option<String>,
    tailwind: bool,
    dep: Option<Dep>,
}

fn parse(args: Vec<String>) -> Result<Options, CliError> {
    let mut args = args.into_iter();
    let mut path = None;
    let mut opts = Options { path: PathBuf::new(), name: None, tailwind: false, dep: None };
    while let Some(arg) = args.next() {
        match arg.as_str() {
            "--name" => opts.name = Some(value(&mut args, "--name")?),
            "--tailwind" => opts.tailwind = true,
            "--perk-path" => opts.dep = Some(Dep::Path(value(&mut args, "--perk-path")?.into())),
            "--git" => opts.dep = Some(Dep::Git(value(&mut args, "--git")?)),
            "-h" | "--help" => return Err(CliError::Help),
            other if other.starts_with('-') => return Err(CliError::Usage(format!("unknown option `{other}`"))),
            _ if path.is_some() => return Err(CliError::Usage(format!("unexpected argument `{arg}`"))),
            _ => path = Some(PathBuf::from(arg)),
        }
    }
    opts.path = path.ok_or_else(|| CliError::Usage("missing <path>".into()))?;
    Ok(opts)
}

pub fn main(args: Vec<String>) -> Result<(), CliError> {
    let opts = parse(args)?;
    let name = match &opts.name {
        Some(n) => n.clone(),
        None => opts.path.file_name().and_then(|n| n.to_str()).ok_or_else(|| "can't take a package name from that path; pass `--name`".to_owned())?.to_owned(),
    };
    check_name(&name)?;
    if opts.path.exists() && fs::read_dir(&opts.path).map_err(|e| e.to_string())?.next().is_some() {
        return Err(CliError::Failed(format!("{} already exists and is not empty", opts.path.display())));
    }
    fs::create_dir_all(&opts.path).map_err(|e| format!("creating {}: {e}", opts.path.display()))?;
    let dir = opts.path.canonicalize().map_err(|e| e.to_string())?;

    let workspace = enclosing_workspace(&dir);
    let dep = match opts.dep {
        Some(Dep::Path(p)) => {
            let p = p.canonicalize().map_err(|e| format!("--perk-path {}: {e}", p.display()))?;
            if !p.join("crates/perk/Cargo.toml").is_file() {
                return Err(CliError::Failed(format!("{} is not a Perk checkout (no crates/perk)", p.display())));
            }
            Dep::Path(p)
        }
        Some(git) => git,
        None => match &workspace {
            Some(root) if root.join("crates/perk/Cargo.toml").is_file() => Dep::Path(root.clone()),
            _ => Dep::Registry,
        },
    };
    let (perk_dep, compiler_dep) = match &dep {
        Dep::Path(p) => {
            let rel = relative(&dir, p);
            let rel = rel.to_string_lossy().replace('\\', "/");
            (format!("{{ path = \"{rel}/crates/perk\" }}"), format!("{{ path = \"{rel}/crates/perk-compiler\" }}"))
        }
        Dep::Git(url) => (format!("{{ git = \"{url}\" }}"), format!("{{ git = \"{url}\" }}")),
        Dep::Registry => {
            let req = format!("\"{}\"", registry_requirement());
            (req.clone(), req)
        }
    };
    let files = app_files(&name, &perk_dep, &compiler_dep, opts.tailwind);
    for (rel, body) in &files {
        let path = dir.join(rel);
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| format!("creating {}: {e}", parent.display()))?;
        }
        fs::write(&path, body).map_err(|e| format!("writing {}: {e}", path.display()))?;
    }

    let mut notes = Vec::new();
    if let Some(root) = &workspace {
        let member = relative(root, &dir).to_string_lossy().replace('\\', "/");
        match add_member(&root.join("Cargo.toml"), &member) {
            Ok(true) => notes.push(format!("added `{member}` to the workspace members in {}", root.join("Cargo.toml").display())),
            Ok(false) => {}
            Err(e) => notes.push(format!("add \"{member}\" to `members` in {}: {e}", root.join("Cargo.toml").display())),
        }
    }

    eprintln!("\x1b[32m     Created\x1b[0m Perk app `{name}` in {}", opts.path.display());
    for n in notes {
        eprintln!("        note: {n}");
    }
    eprintln!("\nNext:\n");
    eprintln!("    cd {}", opts.path.display());
    if opts.tailwind {
        eprintln!("    npm install        # the Tailwind CLI the build uses");
    }
    if workspace.is_some() {
        eprintln!("    perk dev -p {name}");
    } else {
        eprintln!("    perk dev");
    }
    eprintln!("\nThen open http://127.0.0.1:3000.");
    Ok(())
}

/// Every file of a new app, with its placeholders filled.
fn app_files(name: &str, perk_dep: &str, compiler_dep: &str, tailwind: bool) -> Vec<(&'static str, String)> {
    let fill = |s: &str| {
        s.replace("{{name}}", name)
            .replace("{{crate}}", &name.replace('-', "_"))
            .replace("{{perk_dep}}", perk_dep)
            .replace("{{compiler_dep}}", compiler_dep)
            .replace("{{tailwind}}", if tailwind { "true" } else { "false" })
    };
    let mut files: Vec<(&str, String)> = FILES.iter().map(|(p, s)| (*p, fill(s))).collect();
    files.push(("src/routes/layout.perk", fill(if tailwind { LAYOUT_TAILWIND } else { LAYOUT_PLAIN })));
    if tailwind {
        files.push(("package.json", PACKAGE_JSON.into()));
        if let Some((_, ignore)) = files.iter_mut().find(|(p, _)| *p == ".gitignore") {
            ignore.push_str("/node_modules\n");
        }
    }
    files
}

/// Cargo's rules for a package name, plus a crate name that isn't reserved.
fn check_name(name: &str) -> Result<(), String> {
    let first_ok = name.chars().next().is_some_and(|c| c.is_ascii_alphabetic());
    let rest_ok = name.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_');
    if !first_ok || !rest_ok {
        return Err(format!("`{name}` isn't a valid package name: use letters, digits, `-` and `_`, starting with a letter; pass `--name`"));
    }
    let crate_name = name.replace('-', "_");
    if matches!(crate_name.as_str(), "perk" | "perk_compiler" | "test" | "core" | "std" | "alloc" | "proc_macro" | "self" | "super" | "crate")
    {
        return Err(format!("`{name}` would clash with an existing crate; pass `--name`"));
    }
    Ok(())
}

/// The root of the Cargo workspace above `dir`, if there is one.
fn enclosing_workspace(dir: &Path) -> Option<PathBuf> {
    dir.ancestors().skip(1).find(|d| fs::read_to_string(d.join("Cargo.toml")).is_ok_and(|s| s.lines().any(|l| l.trim() == "[workspace]"))).map(Path::to_path_buf)
}

/// Adds `member` to the `members = [...]` array of a workspace manifest.
/// Returns false when it is already listed or covered by a glob.
fn add_member(manifest: &Path, member: &str) -> Result<bool, String> {
    let text = fs::read_to_string(manifest).map_err(|e| e.to_string())?;
    let (updated, added) = insert_member(&text, member)?;
    if added {
        fs::write(manifest, updated).map_err(|e| e.to_string())?;
    }
    Ok(added)
}

fn insert_member(text: &str, member: &str) -> Result<(String, bool), String> {
    let start = text
        .match_indices("members")
        .map(|(i, _)| i)
        .find(|&i| text[..i].rsplit('\n').next().is_some_and(|before| before.trim().is_empty()))
        .ok_or("it has no `members` list")?;
    let open = start + text[start..].find('[').ok_or("can't read its `members` list")?;
    let close = open + text[open..].find(']').ok_or("can't read its `members` list")?;
    let list = &text[open + 1..close];
    let entries: Vec<&str> = list.split(',').map(|e| e.trim().trim_matches('"')).filter(|e| !e.is_empty()).collect();
    let covered = entries.iter().any(|e| {
        *e == member || e.strip_suffix("/*").is_some_and(|prefix| Path::new(member).parent().is_some_and(|p| p == Path::new(prefix)))
    });
    if covered {
        return Ok((text.to_owned(), false));
    }
    let multiline = list.contains('\n');
    let trimmed = list.trim_end();
    let comma = if trimmed.is_empty() || trimmed.ends_with(',') { "" } else { "," };
    let mut out = String::with_capacity(text.len() + member.len() + 8);
    out.push_str(&text[..open + 1]);
    out.push_str(trimmed);
    if multiline {
        out.push_str(&format!("{comma}\n    \"{member}\",\n"));
    } else if trimmed.is_empty() {
        out.push_str(&format!("\"{member}\""));
    } else {
        out.push_str(&format!("{comma} \"{member}\""));
    }
    out.push_str(&text[close..]);
    Ok((out, true))
}

/// `to` relative to `from`. Both are absolute and canonical.
fn relative(from: &Path, to: &Path) -> PathBuf {
    let from: Vec<Component> = from.components().collect();
    let to: Vec<Component> = to.components().collect();
    let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
    // Sharing only the root: an absolute path reads better than `../../..`.
    if common <= 1 {
        return to.iter().collect();
    }
    let mut out = PathBuf::new();
    for _ in common..from.len() {
        out.push("..");
    }
    for c in &to[common..] {
        out.push(c);
    }
    if out.as_os_str().is_empty() { PathBuf::from(".") } else { out }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn the_template_compiles() {
        for tailwind in [false, true] {
            let dir = std::env::temp_dir().join(format!("perk-new-test-{}-{tailwind}", std::process::id()));
            let _ = fs::remove_dir_all(&dir);
            for (rel, body) in app_files("my-app", "{ path = \"x\" }", "{ path = \"y\" }", tailwind) {
                assert!(!body.contains("{{"), "{rel} has an unfilled placeholder");
                let path = dir.join(rel);
                fs::create_dir_all(path.parent().unwrap()).unwrap();
                fs::write(path, body).unwrap();
            }
            let cfg = perk_compiler::Config {
                routes_dir: dir.join("src/routes"),
                out_dir: dir.join("out"),
                state_type: "crate::AppState".into(),
                sources_module: "crate::sources".into(),
                dev: false,
                crate_dir: dir.clone(),
                tailwind: None,
                css_scope: true,
            };
            let routes = perk_compiler::compile_quiet(&cfg).unwrap_or_else(|e| panic!("{e}"));
            let paths: Vec<&str> = routes.iter().map(|r| r.path.as_str()).collect();
            assert_eq!(paths, ["/counter", "/hello", "/"]);
            assert!(fs::read_to_string(dir.join("src/main.rs")).unwrap().contains("my_app::app("));
            let config = perk_compiler::load_file_config(&dir).unwrap_or_else(|e| panic!("{e}"));
            assert_eq!(config.build.tailwind, tailwind);
            assert_eq!(config.sessions.store, perk_compiler::config::StoreKind::File);
            let _ = fs::remove_dir_all(&dir);
        }
    }

    #[test]
    fn inserts_into_a_one_line_members_list() {
        let (out, added) = insert_member("[workspace]\nmembers = [\"a\", \"b\"]\n", "examples/c").unwrap();
        assert!(added);
        assert_eq!(out, "[workspace]\nmembers = [\"a\", \"b\", \"examples/c\"]\n");
    }

    #[test]
    fn inserts_into_a_multiline_members_list() {
        let (out, _) = insert_member("[workspace]\nmembers = [\n    \"a\",\n]\n", "c").unwrap();
        assert_eq!(out, "[workspace]\nmembers = [\n    \"a\",\n    \"c\",\n]\n");
    }

    #[test]
    fn skips_default_members() {
        let (out, _) = insert_member("default-members = [\"a\"]\nmembers = [\"a\"]\n", "b").unwrap();
        assert_eq!(out, "default-members = [\"a\"]\nmembers = [\"a\", \"b\"]\n");
    }

    #[test]
    fn leaves_members_covered_by_a_glob() {
        let (_, added) = insert_member("members = [\"examples/*\"]", "examples/c").unwrap();
        assert!(!added);
        let (_, added) = insert_member("members = [\"examples/c\"]", "examples/c").unwrap();
        assert!(!added);
    }

    #[test]
    fn relative_paths_walk_up_then_down() {
        assert_eq!(relative(Path::new("/a/b/c"), Path::new("/a")), PathBuf::from("../.."));
        assert_eq!(relative(Path::new("/a/b"), Path::new("/a/x/y")), PathBuf::from("../x/y"));
        assert_eq!(relative(Path::new("/tmp/app"), Path::new("/home/perk")), PathBuf::from("/home/perk"));
    }

    #[test]
    fn registry_requirement_is_major_and_minor() {
        let req = registry_requirement();
        assert_eq!(req.split('.').count(), 2, "{req}");
        assert!(env!("CARGO_PKG_VERSION").starts_with(&format!("{req}.")), "{req}");
    }

    #[test]
    fn rejects_bad_names() {
        assert!(check_name("my-app").is_ok());
        assert!(check_name("1app").is_err());
        assert!(check_name("my app").is_err());
        assert!(check_name("perk").is_err());
    }
}