wip: Render command and site::render stub
This commit is contained in:
parent
200bd42687
commit
962fb39dd2
|
@ -4,11 +4,12 @@ use snafu::{ensure, ResultExt, Snafu};
|
|||
use std::{env::current_dir, path::PathBuf};
|
||||
|
||||
mod init;
|
||||
mod render;
|
||||
mod validate;
|
||||
|
||||
pub use init::InitArgs;
|
||||
|
||||
use self::validate::ValidateArgs;
|
||||
use self::{render::RenderArgs, validate::ValidateArgs};
|
||||
|
||||
/// Error encountered while parsing CLI
|
||||
#[derive(Debug, Snafu)]
|
||||
|
@ -86,6 +87,8 @@ pub enum Command {
|
|||
Init(InitArgs),
|
||||
/// Valiate the structure and configuration of a site
|
||||
Validate(ValidateArgs),
|
||||
/// Generate the site
|
||||
Render(RenderArgs),
|
||||
}
|
||||
|
||||
impl Command {
|
||||
|
@ -94,6 +97,7 @@ impl Command {
|
|||
match self {
|
||||
Command::Init(args) => args.run(globals),
|
||||
Command::Validate(args) => args.run(globals),
|
||||
Command::Render(args) => args.run(globals),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
use std::{
|
||||
fs::{create_dir_all, read_dir},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use clap::Args;
|
||||
use color_eyre::eyre::{bail, ensure, Context, Result};
|
||||
use stranger_site_gen::site::Site;
|
||||
use tracing::{info, warn};
|
||||
|
||||
use super::GlobalArgs;
|
||||
|
||||
/// Arguments for the render subcommand
|
||||
#[derive(Args, Debug)]
|
||||
pub struct RenderArgs {
|
||||
/// Output directory to use
|
||||
#[arg(long, short)]
|
||||
pub output_directory: Option<PathBuf>,
|
||||
/// Overrite the output if needed
|
||||
#[arg(long, short)]
|
||||
pub force: bool,
|
||||
}
|
||||
|
||||
impl RenderArgs {
|
||||
/// Run the render command
|
||||
#[tracing::instrument]
|
||||
pub fn run(self, globals: GlobalArgs) -> Result<()> {
|
||||
// Get the site directory
|
||||
let site_dir = globals.site_path().context("Failed getting site dir")?;
|
||||
|
||||
// Read in the site state
|
||||
let site = Site::read(&site_dir).context("Failed to load site state")?;
|
||||
|
||||
// Figure out where we are putting the output
|
||||
let output_dir = self
|
||||
.output_directory
|
||||
.unwrap_or_else(|| site_dir.join("output"));
|
||||
info!(?output_dir);
|
||||
|
||||
// Make sure the output path is valid, meaning one of the following:
|
||||
// - Non existant path, in which case we create a directory
|
||||
// - Empty directory
|
||||
if output_dir
|
||||
.try_exists()
|
||||
.context("Failed to check for existance of ouput directory")?
|
||||
{
|
||||
ensure!(output_dir.is_dir(), "Output path was not a directory");
|
||||
let mut children = Vec::new();
|
||||
for child in
|
||||
read_dir(&output_dir).context("Failed to check output directory for emptyness")?
|
||||
{
|
||||
let child = child.context("Failed to check output directory for emptynesss")?;
|
||||
children.push(child.path());
|
||||
}
|
||||
if !children.is_empty() {
|
||||
if self.force {
|
||||
warn!(
|
||||
?children,
|
||||
"Output directory was non-empty, but force was requested, continuing anyway"
|
||||
);
|
||||
} else {
|
||||
bail!("Output directory was non-empty: {:?}", children);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
info!(?output_dir, "Creating output directory");
|
||||
create_dir_all(&output_dir).context("Failed to create output directory")?;
|
||||
}
|
||||
|
||||
// Pass off to `Site` to render the site
|
||||
site.render(&site_dir, &output_dir)
|
||||
.context("Failed to render site")?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
19
src/site.rs
19
src/site.rs
|
@ -285,6 +285,25 @@ impl Site {
|
|||
styles,
|
||||
})
|
||||
}
|
||||
|
||||
/// Render the site to the provided output directory
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Will return an error if one of the djot files is invalid, or if any IO errors occur during
|
||||
/// rendering
|
||||
pub fn render(
|
||||
&self,
|
||||
site_dir: impl AsRef<Path>,
|
||||
output_dir: impl AsRef<Path>,
|
||||
) -> Result<(), SiteError> {
|
||||
// Get dirs and setup logging
|
||||
let site_dir = site_dir.as_ref();
|
||||
let output_dir = output_dir.as_ref();
|
||||
let span = info_span!("Site::render", ?site_dir, ?output_dir);
|
||||
let _enter = span.enter();
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
/// Error encountered interacting with a [`Site`]
|
||||
#[derive(Snafu, Debug)]
|
||||
|
|
Loading…
Reference in New Issue