runnables 0.1.0

What a folder can run: apps, tests, watchers, simulators, databases and deploys, found from its files. A library, a CLI, and an MCP server for coding agents.
Documentation
// Copyright (c) Pavel Sich.
// Licensed under the MIT License.

//! `runnables [list] [folder]` prints what the folder can run, one command a line.
//! `runnables detect [folder]` prints the same scan as JSON.
//! `runnables mcp` speaks the same scan to an agent.

use std::path::PathBuf;
use std::process::ExitCode;

const USAGE: &str = "Usage: runnables [list] [folder]   what the folder can run
       runnables detect [folder]   the same as JSON
       runnables mcp               an MCP server for coding agents";

fn main() -> ExitCode {
    let mut args = std::env::args().skip(1);
    match args.next().as_deref() {
        None | Some("list") => list(args.next()),
        Some("detect") => detect(args.next()),
        Some("--version" | "-V") => {
            println!("runnables {}", env!("CARGO_PKG_VERSION"));
            ExitCode::SUCCESS
        }
        Some("mcp") => {
            if let Err(error) = runnables::mcp() {
                eprintln!("runnables mcp: {error}");
                return ExitCode::from(1);
            }
            ExitCode::SUCCESS
        }
        Some("help" | "--help" | "-h") => {
            println!("{USAGE}");
            ExitCode::SUCCESS
        }
        Some(folder) if std::path::Path::new(folder).is_dir() => list(Some(folder.to_string())),
        _ => {
            eprintln!("{USAGE}");
            ExitCode::from(2)
        }
    }
}

fn list(folder: Option<String>) -> ExitCode {
    let root = PathBuf::from(folder.unwrap_or_else(|| ".".into()));
    let report = runnables::report(&root);
    if report.groups.is_empty() {
        println!("Nothing to run in {}.", root.display());
        return ExitCode::SUCCESS;
    }
    for group in &report.groups {
        println!("{} ({})", group.title, group.detail);
        for task in &group.tasks {
            let mark = if report.default.as_deref() == Some(task.id.as_str()) { "*" } else { " " };
            println!("  {mark} {:<14} {}", task.label, task.command);
        }
    }
    ExitCode::SUCCESS
}

fn detect(folder: Option<String>) -> ExitCode {
    let root = PathBuf::from(folder.unwrap_or_else(|| ".".into()));
    let report = runnables::report(&root);
    match serde_json::to_string_pretty(&report) {
        Ok(text) => {
            println!("{text}");
            ExitCode::SUCCESS
        }
        Err(error) => {
            eprintln!("runnables detect: {error}");
            ExitCode::from(1)
        }
    }
}