#![warn(missing_docs)]
use std::io::Write;
use std::path::{Path, PathBuf};
#[path = "commands/build.rs"]
mod build_command;
#[path = "commands/check.rs"]
mod check_command;
#[path = "explain.rs"]
mod explain;
#[path = "grafts.rs"]
mod grafts;
#[path = "commands/new.rs"]
mod new_command;
#[path = "commands/snippets.rs"]
mod snippets_command;
#[path = "commands/studio.rs"]
mod studio_command;
const USAGE: &str = "\
nichlink — NichLink command-line interface
USAGE:
nichlink new <name> [--lib] [--path <workspace> | --git <url>]
nichlink check [path] [--json]
nichlink build [path] [cargo options]
nichlink snippets [path] [--editor vscode|nvim|blink|auto] [--stdout]
nichlink explain <node-id|logical/path> [--path <dir>] [--json]
nichlink explain --overlay [--path <dir>] [--json]
nichlink grafts [path] [--json]
nichlink studio [path]
nichlink mcp
COMMANDS:
new Create a NichLink host project in ./<name>
check Run the registration discovery and validation pass without compiling
build Validate the registration tree, then run cargo build
snippets Inject the face-field editor snippets (VS Code project file, or
the LuaSnip file Neovim loads)
explain Resolve a node id or logical path and report its identity, build
scope, pruning, and the declared graft cuts that name it; with
--overlay, render the static overlay projection of every slot
grafts List every .nichlink/external-grafts/*/graft.plan, with its
selector, target path, graft, full flag, and whether the host
entry declares that slot (read-only)
studio Launch the Studio TUI for the current project, or for `path`
mcp Run the MCP stdio bridge: source and registry queries, plus
authoring writes that preview unless `apply: true`
OPTIONS:
--lib Create a library project instead of a binary
--path <dir> Source nichlink-core/build from a local checkout; for
explain, the host project to inspect (default: .)
--git <url> Source nichlink-core/build from a Git repository
--json Emit one JSON document on stdout instead of human text
(check, explain, grafts); check still exits non-zero on a
failed validation
--overlay With explain, render the static overlay projection of the
build's scope and declared cuts instead of one node
--editor <name> Editor to write snippets for: vscode (default), nvim
(LuaSnip), blink (blink.cmp) or auto (every editor
installed on this machine, in its user-level location;
fuzzy-matching engines need to be named explicitly)
--stdout Print the snippets instead of writing them (any editor)
";
pub fn main() -> Result<(), String> {
run(argv_strings(std::env::args_os())?)
}
pub fn argv_strings(
argv: impl IntoIterator<Item = std::ffi::OsString>,
) -> Result<Vec<String>, String> {
argv.into_iter()
.map(|argument| {
argument
.into_string()
.map_err(|bad| format!("argument {bad:?} is not valid UTF-8"))
})
.collect()
}
pub fn run(argv: impl IntoIterator<Item = String>) -> Result<(), String> {
run_to(argv, &mut std::io::stdout())
}
pub fn run_to(argv: impl IntoIterator<Item = String>, out: &mut dyn Write) -> Result<(), String> {
let mut args = argv.into_iter().skip(1);
match args.next().as_deref() {
None | Some("--help") | Some("-h") | Some("help") => {
write!(out, "{USAGE}").map_err(|error| format!("cannot write usage: {error}"))?;
Ok(())
}
Some("new") => new_command::new(&mut args),
Some("check") => check_command::check(&mut args, out),
Some("build") => build_command::build(&mut args),
Some("snippets") => snippets_command::snippets(&mut args),
Some("explain") => explain::explain(&mut args, out),
Some("grafts") => grafts::grafts(&mut args, out),
Some("studio") => studio_command::studio(&mut args, out),
Some("mcp") => nichlink_mcp::run().map_err(|error| format!("mcp: {error}")),
Some(other) => Err(format!("unknown command '{other}' (see --help)")),
}
}
fn split_build_args(args: &[String]) -> (Option<String>, Vec<String>) {
match args.first() {
Some(first) if !first.starts_with('-') => (Some(first.clone()), args[1..].to_vec()),
_ => (None, args.to_vec()),
}
}
pub(crate) fn resolve_package(directory: &str) -> Result<(PathBuf, String), String> {
let manifest = std::fs::canonicalize(directory)
.map_err(|error| format!("cannot resolve {directory}: {error}"))?;
if !manifest.join("Cargo.toml").is_file() {
return Err(format!("{} has no Cargo.toml", manifest.display()));
}
let package = nichlink_build_method::package_name(&manifest.join("Cargo.toml"))?;
Ok((manifest, package))
}
pub(crate) fn build_out_dir(manifest: &Path) -> PathBuf {
manifest.join("target/nichlink/out")
}
fn registration_check(directory: &str) -> Result<String, String> {
let (manifest, package) = resolve_package(directory)?;
let out_dir = build_out_dir(&manifest);
nichlink_build_method::run_for(&manifest, &out_dir, &package)?;
Ok(package)
}
#[cfg(test)]
#[path = "lib_tests.rs"]
mod tests;