Copy static files over to the output directory.

This commit is contained in:
Tom Alexander 2023-12-23 15:45:23 -05:00
parent 397d4ea0bc
commit 138d694b27
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 51 additions and 0 deletions

View File

@ -1,8 +1,11 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use include_dir::include_dir; use include_dir::include_dir;
use include_dir::Dir; use include_dir::Dir;
use tokio::task::JoinHandle;
use walkdir::WalkDir;
use crate::config::Config; use crate::config::Config;
use crate::context::RenderBlogPostPage; use crate::context::RenderBlogPostPage;
@ -196,6 +199,27 @@ impl SiteRenderer {
} }
Ok(()) Ok(())
} }
pub(crate) async fn copy_static_files(&self, config: &Config) -> Result<(), CustomError> {
let static_files_directory = config
.get_root_directory()
.join(config.get_relative_path_to_static_files());
if !static_files_directory.exists() {
return Ok(());
}
let static_files = get_all_files(&static_files_directory)?;
for entry in static_files {
let (path, contents) = entry.await??;
let relative_path = path.strip_prefix(&static_files_directory)?;
let output_path = self.output_directory.join(relative_path);
let parent_directory = output_path
.parent()
.ok_or("Output file should have a containing directory.")?;
tokio::fs::create_dir_all(parent_directory).await?;
tokio::fs::write(output_path, contents).await?;
}
Ok(())
}
} }
fn build_name_contents_pairs<'a>( fn build_name_contents_pairs<'a>(
@ -210,3 +234,25 @@ fn build_name_contents_pairs<'a>(
let contents = std::str::from_utf8(entry.contents())?; let contents = std::str::from_utf8(entry.contents())?;
Ok((name, contents)) Ok((name, contents))
} }
fn get_all_files<P: AsRef<Path>>(
root_dir: P,
) -> Result<impl Iterator<Item = JoinHandle<std::io::Result<(PathBuf, Vec<u8>)>>>, walkdir::Error> {
let files = WalkDir::new(root_dir)
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => dir_entry.file_type().is_file(),
Err(_) => true,
})
.collect::<Result<Vec<_>, _>>()?;
let org_files = files
.into_iter()
.map(walkdir::DirEntry::into_path)
.map(|path| tokio::spawn(read_file(path)));
Ok(org_files)
}
async fn read_file(path: PathBuf) -> std::io::Result<(PathBuf, Vec<u8>)> {
let contents = tokio::fs::read(&path).await?;
Ok((path, contents))
}

View File

@ -25,6 +25,7 @@ pub(crate) async fn build_site(args: BuildArgs) -> Result<(), CustomError> {
renderer.render_blog_posts(&config).await?; renderer.render_blog_posts(&config).await?;
renderer.render_blog_stream(&config).await?; renderer.render_blog_stream(&config).await?;
renderer.render_stylesheets().await?; renderer.render_stylesheets().await?;
renderer.copy_static_files(&config).await?;
Ok(()) Ok(())
} }

View File

@ -88,4 +88,8 @@ impl Config {
.and_then(|stream| stream.entries_per_page) .and_then(|stream| stream.entries_per_page)
.unwrap_or(5) .unwrap_or(5)
} }
pub(crate) fn get_relative_path_to_static_files(&self) -> PathBuf {
Path::new("static").into()
}
} }