use clap::{Parser, Subcommand};
use console::style;
pub mod builder;
pub mod context;
pub mod error;
pub mod filter;
pub mod log;
pub mod manifest;
pub mod markdown;
pub mod prelude;
pub mod result;
pub mod server;
pub mod sink;
pub mod utils;
use prelude::*;
use server::Server;
#[derive(Debug, Parser)] #[clap(name = "wahoo")]
#[clap(about, author, version)]
#[clap(
// setting = clap::AppSettings::DeriveDisplayOrder,
)]
struct Args {
#[clap(name = "manifest")]
location: Option<String>,
#[clap(subcommand)]
action: Action,
#[clap(short, long)]
verbose: bool,
}
#[derive(Subcommand, Debug)]
enum Action {
Build {},
Serve {
#[clap(long, default_value = "127.0.0.1")]
host: String,
#[clap(long, default_value = "8080")]
port: u16,
},
Clean {},
Init {},
Publish {},
}
pub async fn async_main() -> Result<()> {
let Args {
location,
action,
verbose,
} = Args::parse();
if verbose {
log::enable_verbose();
}
match action {
Action::Build {} => {
let ctx = Arc::new(Context::create(location, Options::default()).await?);
let build = Arc::new(Builder::new(ctx.clone()));
build.execute().await?;
println!();
}
Action::Clean {} => {
let ctx = Arc::new(Context::create(location, Options::default()).await?);
ctx.clean().await?;
}
Action::Serve { host, port } => {
let sink = Sink::default();
let ctx = {
let ctx = Arc::new(
Context::create(
location.clone(),
Options {
server: true,
verbose: true,
},
)
.await?,
);
let build = Arc::new(Builder::new_with_sink(ctx.clone(), sink.clone()));
build.execute().await?;
ctx
};
let mut watch_targets = ctx.manifest.imports.clone();
watch_targets.extend([ctx.manifest_toml.clone(), ctx.src_folder.clone()]);
if let Some(Settings {
watch: Some(watch), ..
}) = &ctx.manifest.settings
{
for folder in watch.iter() {
let target_folder = ctx.project_folder.join(folder);
let watch_target = target_folder.canonicalize().unwrap_or_else(|err| {
format!(
"Unable to locate watch target `{}`: {err}",
target_folder.display()
)
.into()
});
watch_targets.push(watch_target);
}
}
if let Some(sections) = &ctx.manifest.sections {
for (_name, section) in sections.iter() {
if let Some(SectionSettings {
folder: Some(folder),
..
}) = §ion.settings
{
let target_folder = ctx.project_folder.join(folder);
let watch_target = target_folder.canonicalize().unwrap_or_else(|err| {
format!(
"Unable to locate section folder `{}`: {err}",
target_folder.display()
)
.into()
});
watch_targets.push(watch_target);
}
}
}
log_trace!("Watching", "{watch_targets:#?}");
let server = Server::new(
host,
port,
location,
ctx.project_folder.clone(),
ctx.site_folder.clone(),
&watch_targets,
ctx.settings(),
sink,
);
server.run().await?;
}
Action::Init {} => {
}
Action::Publish {} => {}
}
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
let result = async_main().await;
match &result {
Err(Error::Warning(warn)) => {
println!("\nWarning: {}\n", style(warn).yellow())
}
Err(err) => {
log_error!("{}", err);
println!();
}
Ok(_) => {}
};
if result.is_err() {
std::process::exit(1);
}
Ok(())
}