xert 0.2.0

A professional Rust project scaffolding tool for Web, CLI, and Embedded projects.
Documentation
// Binary entry point for xert CLI

use anyhow::Result;
use clap::Parser;
use colored::*;
use indicatif::{ProgressBar, ProgressStyle};
use std::time::Duration;
use xert::cli::{Cli, Commands};
use xert::scaffold::{self, ProgressHandler}; // scaffold module and ProgressHandler trait

struct CliProgressHandler {
    pb: ProgressBar,
}

impl CliProgressHandler {
    fn new() -> Self {
        let spinner_style = ProgressStyle::default_spinner()
            .tick_strings(&["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏", ""])
            .template("{spinner:.blue} {msg}")
            .unwrap();

        let pb = ProgressBar::new_spinner();
        pb.set_style(spinner_style);
        pb.enable_steady_tick(Duration::from_millis(80));
        Self { pb }
    }
}

impl ProgressHandler for CliProgressHandler {
    fn start_phase(&self, phase: &str) {
        self.pb.set_message(phase.to_string());
    }

    fn finish_phase(&self, phase: &str) {
        // We can't easily change the message to "Done" and keep it visible as a persistent line
        // with the shared PB without some tricks, or we just print a checkmark message.
        // The original code used `finish_with_message`.
        // But since we reuse the handler, we might want to just print a message or use the PB to print.
        // For faithful reproduction:
        self.pb.println(format!("{} {}", "✓".green(), phase));
    }
}

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    match &cli.command {
        Commands::Create { template, name } => {
            let handler = CliProgressHandler::new();

            // Convert cli::Template to base Template
            let base_template: xert::Template = (*template).into();

            if let Err(e) = scaffold::create_project(&base_template, name, &handler).await {
                eprintln!("\n{} {}", "Error:".red().bold(), e);
                std::process::exit(1);
            }

            // Explicitly finish the PB to clear the spinner if it's still running
            handler.pb.finish_and_clear();

            println!("\nSuccess! Created {} at ./{}", name.green().bold(), name);
            println!("inside that directory, you can run:\n");
            println!(
                "  {}    to start the development server",
                "cargo run".cyan()
            );
            println!("  {}  to check for errors\n", "cargo check".cyan());
            println!("Get started by typing:\n");
            println!("  cd {}", name.cyan());
            println!("  cargo run\n");
        }
    }

    Ok(())
}