use anyhow::Result;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use crate::plugins::PluginRegistry;
use crate::templates::TemplateRegistry;
#[derive(Parser)]
#[command(about = "A static site generator for Markdown content")]
#[command(version = env!("CARGO_PKG_VERSION"))]
struct CliArgs {
#[command(subcommand)]
command: Commands,
#[arg(short = 'i', long = "input", default_value = "content", global = true)]
input: PathBuf,
#[arg(short = 'o', long = "output", default_value = "dist", global = true)]
output: PathBuf,
}
#[derive(Subcommand)]
enum Commands {
Generate,
Dev {
#[arg(short = 'p', long = "port", default_value = "3000")]
port: u16,
},
}
pub struct Sherwood {
plugin_registry: Option<PluginRegistry>,
template_registry: Option<TemplateRegistry>,
}
impl Default for Sherwood {
fn default() -> Self {
Self::new()
}
}
impl Sherwood {
pub fn new() -> Self {
Self {
plugin_registry: None,
template_registry: None,
}
}
pub fn with_plugins(mut self, registry: PluginRegistry) -> Self {
self.plugin_registry = Some(registry);
self
}
pub fn with_templates(mut self, registry: TemplateRegistry) -> Self {
self.template_registry = Some(registry);
self
}
pub async fn run(self) -> Result<()> {
let args = CliArgs::parse();
match args.command {
Commands::Generate => {
crate::generate_site_with_plugins_and_templates(
&args.input,
&args.output,
self.plugin_registry,
self.template_registry,
)
.await
}
Commands::Dev { port } => {
crate::run_dev_server_with_plugins_and_templates(
&args.input,
&args.output,
port,
self.plugin_registry,
self.template_registry,
)
.await
}
}
}
}