larakiln 0.4.0

Port Laravel PHP apps to a new Laravel runtime (Laravel Rust today) and check nothing was lost: audit, plan, codegen, route parity
mod audit;
mod codegen;
mod components;
mod feasibility;
mod manifest;
mod next;
mod route_parity;

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

use clap::{Parser, Subcommand};

/// Port Laravel PHP apps to a new Laravel runtime (Laravel Rust today) and check nothing was lost.
#[derive(Parser)]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Inventory a Laravel PHP app (read-only) into inventory.json.
    Audit {
        /// Path to the Laravel PHP app.
        app: PathBuf,
        /// Where to write the inventory. Never inside the PHP app.
        #[arg(long, default_value = "inventory.json")]
        out: PathBuf,
        /// Command that runs PHP in the app directory, e.g. "php" or
        /// "docker run --rm -v $PWD:/app -w /app php:8.3-cli php".
        #[arg(long, default_value = "php")]
        php: String,
    },
    /// Score every inventory item green / yellow / red and print the report.
    Feasibility {
        #[arg(default_value = "inventory.json")]
        inventory: PathBuf,
    },
    /// Write PORTING.md: the porting checklist in dependency order (keeps ticked items).
    Manifest {
        #[arg(default_value = "inventory.json")]
        inventory: PathBuf,
        /// Where to write the checklist. Never inside the PHP app.
        #[arg(long, default_value = "PORTING.md")]
        out: PathBuf,
    },
    /// Write models, policies, factories, migrations, input structs, handler stubs, routes and Jinja views into a Laravel Rust app.
    /// Never overwrites; PORT-TODO marks what's left; wiring steps go to larakiln-codegen.md.
    Codegen {
        #[arg(default_value = "inventory.json")]
        inventory: PathBuf,
        /// The Laravel Rust app (from `laravel new <name> --rust`).
        #[arg(long)]
        rust: PathBuf,
    },
    /// Print the first unticked PORTING.md item: its PHP source, its generated Rust stub and PORT-TODOs,
    /// the reference to read, and when it counts as done.
    Next {
        #[arg(default_value = "PORTING.md")]
        porting: PathBuf,
        #[arg(long, default_value = "inventory.json")]
        inventory: PathBuf,
        /// The Laravel Rust app, to find the stub `larakiln codegen` wrote for the item.
        #[arg(long)]
        rust: Option<PathBuf>,
    },
    /// Check every PHP route has a Rust route (method + URI) with the same auth. Exits 1 on gaps.
    RouteParity {
        #[arg(default_value = "inventory.json")]
        inventory: PathBuf,
        /// The Laravel Rust app; runs `<rust-cmd> route:list --json` there.
        #[arg(long, required_unless_present = "rust_routes", conflicts_with = "rust_routes")]
        rust: Option<PathBuf>,
        /// Command that runs the Rust app's console, e.g. "laravel".
        #[arg(long, default_value = "cargo run -q --")]
        rust_cmd: String,
        /// Saved output of the Rust app's `route:list --json`, instead of --rust.
        #[arg(long)]
        rust_routes: Option<PathBuf>,
        /// URI prefix kept in PHP on purpose (path split), e.g. --ignore /admin. Repeatable.
        #[arg(long)]
        ignore: Vec<String>,
    },
}

fn main() -> ExitCode {
    let result = match Cli::parse().command {
        Command::Audit { app, out, php } => audit::run(&app, &out, &php).map(|()| true),
        Command::Feasibility { inventory } => feasibility::run(&inventory).map(|()| true),
        Command::Manifest { inventory, out } => manifest::run(&inventory, &out).map(|()| true),
        Command::Codegen { inventory, rust } => codegen::run(&inventory, &rust).map(|()| true),
        Command::Next { porting, inventory, rust } => next::run(&porting, &inventory, rust.as_deref()).map(|()| true),
        Command::RouteParity { inventory, rust, rust_cmd, rust_routes, ignore } => route_parity::run(
            &inventory,
            route_parity::Rust { dir: rust.as_deref(), cmd: &rust_cmd, routes: rust_routes.as_deref() },
            &ignore,
        ),
    };
    match result {
        Ok(true) => ExitCode::SUCCESS,
        Ok(false) => ExitCode::from(1),
        Err(e) => {
            eprintln!("larakiln: {e}");
            ExitCode::FAILURE
        }
    }
}