Refactor: create_directory responsibly (#2407)
* Remove ensure_directory_exists since it's identical to create_directory, and misleading * Don't create directories unless needed; rely on create_dir_all instead of manually iterating over components
This commit is contained in:
parent
9890df7d64
commit
7fb0a70477
@ -177,7 +177,7 @@ impl Processor {
|
|||||||
/// Run the enqueued image operations
|
/// Run the enqueued image operations
|
||||||
pub fn do_process(&mut self) -> Result<()> {
|
pub fn do_process(&mut self) -> Result<()> {
|
||||||
if !self.img_ops.is_empty() {
|
if !self.img_ops.is_empty() {
|
||||||
ufs::ensure_directory_exists(&self.output_dir)?;
|
ufs::create_directory(&self.output_dir)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.img_ops
|
self.img_ops
|
||||||
@ -197,7 +197,7 @@ impl Processor {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
ufs::ensure_directory_exists(&self.output_dir)?;
|
ufs::create_directory(&self.output_dir)?;
|
||||||
let output_paths: HashSet<_> = self
|
let output_paths: HashSet<_> = self
|
||||||
.img_ops
|
.img_ops
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -23,7 +23,6 @@ use std::time::Instant;
|
|||||||
use templates::{load_tera, render_redirect_template};
|
use templates::{load_tera, render_redirect_template};
|
||||||
use utils::fs::{
|
use utils::fs::{
|
||||||
clean_site_output_folder, copy_directory, copy_file_if_needed, create_directory, create_file,
|
clean_site_output_folder, copy_directory, copy_file_if_needed, create_directory, create_file,
|
||||||
ensure_directory_exists,
|
|
||||||
};
|
};
|
||||||
use utils::net::{get_available_port, is_external_link};
|
use utils::net::{get_available_port, is_external_link};
|
||||||
use utils::templates::{render_template, ShortcodeDefinition};
|
use utils::templates::{render_template, ShortcodeDefinition};
|
||||||
@ -639,7 +638,6 @@ impl Site {
|
|||||||
create_dirs: bool,
|
create_dirs: bool,
|
||||||
) -> Result<PathBuf> {
|
) -> Result<PathBuf> {
|
||||||
let write_dirs = self.build_mode == BuildMode::Disk || create_dirs;
|
let write_dirs = self.build_mode == BuildMode::Disk || create_dirs;
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
|
|
||||||
let mut site_path = RelativePathBuf::new();
|
let mut site_path = RelativePathBuf::new();
|
||||||
let mut current_path = self.output_path.to_path_buf();
|
let mut current_path = self.output_path.to_path_buf();
|
||||||
@ -647,10 +645,6 @@ impl Site {
|
|||||||
for component in components {
|
for component in components {
|
||||||
current_path.push(component);
|
current_path.push(component);
|
||||||
site_path.push(component);
|
site_path.push(component);
|
||||||
|
|
||||||
if !current_path.exists() && write_dirs {
|
|
||||||
create_directory(¤t_path)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if write_dirs {
|
if write_dirs {
|
||||||
@ -788,9 +782,13 @@ impl Site {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn render_themes_css(&self) -> Result<()> {
|
pub fn render_themes_css(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.static_path)?;
|
let themes = &self.config.markdown.highlight_themes_css;
|
||||||
|
|
||||||
for t in &self.config.markdown.highlight_themes_css {
|
if !themes.is_empty() {
|
||||||
|
create_directory(&self.static_path)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for t in themes {
|
||||||
let p = self.static_path.join(&t.filename);
|
let p = self.static_path.join(&t.filename);
|
||||||
if !p.exists() {
|
if !p.exists() {
|
||||||
let content = &self.config.markdown.export_theme_css(&t.theme)?;
|
let content = &self.config.markdown.export_theme_css(&t.theme)?;
|
||||||
@ -818,7 +816,7 @@ impl Site {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn build_search_index(&self) -> Result<()> {
|
pub fn build_search_index(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
create_directory(&self.output_path)?;
|
||||||
// TODO: add those to the SITE_CONTENT map
|
// TODO: add those to the SITE_CONTENT map
|
||||||
|
|
||||||
// index first
|
// index first
|
||||||
@ -857,7 +855,6 @@ impl Site {
|
|||||||
/// Renders all the aliases for each page/section: a magic HTML template that redirects to
|
/// Renders all the aliases for each page/section: a magic HTML template that redirects to
|
||||||
/// the canonical one
|
/// the canonical one
|
||||||
pub fn render_aliases(&self) -> Result<()> {
|
pub fn render_aliases(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
let library = self.library.read().unwrap();
|
let library = self.library.read().unwrap();
|
||||||
for (_, page) in &library.pages {
|
for (_, page) in &library.pages {
|
||||||
for alias in &page.meta.aliases {
|
for alias in &page.meta.aliases {
|
||||||
@ -874,7 +871,6 @@ impl Site {
|
|||||||
|
|
||||||
/// Renders 404.html
|
/// Renders 404.html
|
||||||
pub fn render_404(&self) -> Result<()> {
|
pub fn render_404(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.insert("config", &self.config.serialize(&self.config.default_language));
|
context.insert("config", &self.config.serialize(&self.config.default_language));
|
||||||
context.insert("lang", &self.config.default_language);
|
context.insert("lang", &self.config.default_language);
|
||||||
@ -886,7 +882,6 @@ impl Site {
|
|||||||
|
|
||||||
/// Renders robots.txt
|
/// Renders robots.txt
|
||||||
pub fn render_robots(&self) -> Result<()> {
|
pub fn render_robots(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
let mut context = Context::new();
|
let mut context = Context::new();
|
||||||
context.insert("config", &self.config.serialize(&self.config.default_language));
|
context.insert("config", &self.config.serialize(&self.config.default_language));
|
||||||
let content = render_template("robots.txt", &self.tera, context, &self.config.theme)?;
|
let content = render_template("robots.txt", &self.tera, context, &self.config.theme)?;
|
||||||
@ -911,8 +906,6 @@ impl Site {
|
|||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
|
|
||||||
let mut components = Vec::new();
|
let mut components = Vec::new();
|
||||||
if taxonomy.lang != self.config.default_language {
|
if taxonomy.lang != self.config.default_language {
|
||||||
components.push(taxonomy.lang.as_ref());
|
components.push(taxonomy.lang.as_ref());
|
||||||
@ -977,8 +970,6 @@ impl Site {
|
|||||||
|
|
||||||
/// What it says on the tin
|
/// What it says on the tin
|
||||||
pub fn render_sitemap(&self) -> Result<()> {
|
pub fn render_sitemap(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
|
|
||||||
let library = self.library.read().unwrap();
|
let library = self.library.read().unwrap();
|
||||||
let all_sitemap_entries =
|
let all_sitemap_entries =
|
||||||
{ sitemap::find_entries(&library, &self.taxonomies[..], &self.config) };
|
{ sitemap::find_entries(&library, &self.taxonomies[..], &self.config) };
|
||||||
@ -1032,8 +1023,6 @@ impl Site {
|
|||||||
lang: &str,
|
lang: &str,
|
||||||
additional_context_fn: impl Fn(Context) -> Context,
|
additional_context_fn: impl Fn(Context) -> Context,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
|
|
||||||
let feed = match feed::render_feed(self, all_pages, lang, base_path, additional_context_fn)?
|
let feed = match feed::render_feed(self, all_pages, lang, base_path, additional_context_fn)?
|
||||||
{
|
{
|
||||||
Some(v) => v,
|
Some(v) => v,
|
||||||
@ -1060,7 +1049,6 @@ impl Site {
|
|||||||
|
|
||||||
/// Renders a single section
|
/// Renders a single section
|
||||||
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
|
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
let mut output_path = self.output_path.clone();
|
let mut output_path = self.output_path.clone();
|
||||||
let mut components: Vec<&str> = Vec::new();
|
let mut components: Vec<&str> = Vec::new();
|
||||||
let create_directories = self.build_mode == BuildMode::Disk || !section.assets.is_empty();
|
let create_directories = self.build_mode == BuildMode::Disk || !section.assets.is_empty();
|
||||||
@ -1068,19 +1056,15 @@ impl Site {
|
|||||||
if section.lang != self.config.default_language {
|
if section.lang != self.config.default_language {
|
||||||
components.push(§ion.lang);
|
components.push(§ion.lang);
|
||||||
output_path.push(§ion.lang);
|
output_path.push(§ion.lang);
|
||||||
|
|
||||||
if !output_path.exists() && create_directories {
|
|
||||||
create_directory(&output_path)?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for component in §ion.file.components {
|
for component in §ion.file.components {
|
||||||
components.push(component);
|
components.push(component);
|
||||||
output_path.push(component);
|
output_path.push(component);
|
||||||
|
}
|
||||||
|
|
||||||
if !output_path.exists() && create_directories {
|
if create_directories {
|
||||||
create_directory(&output_path)?;
|
create_directory(&output_path)?;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if section.meta.generate_feed {
|
if section.meta.generate_feed {
|
||||||
@ -1166,7 +1150,6 @@ impl Site {
|
|||||||
|
|
||||||
/// Renders all pages that do not belong to any sections
|
/// Renders all pages that do not belong to any sections
|
||||||
pub fn render_orphan_pages(&self) -> Result<()> {
|
pub fn render_orphan_pages(&self) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
let library = self.library.read().unwrap();
|
let library = self.library.read().unwrap();
|
||||||
for page in library.get_all_orphan_pages() {
|
for page in library.get_all_orphan_pages() {
|
||||||
self.render_page(page)?;
|
self.render_page(page)?;
|
||||||
@ -1181,8 +1164,6 @@ impl Site {
|
|||||||
components: Vec<&'a str>,
|
components: Vec<&'a str>,
|
||||||
paginator: &'a Paginator,
|
paginator: &'a Paginator,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
ensure_directory_exists(&self.output_path)?;
|
|
||||||
|
|
||||||
let index_components = components.clone();
|
let index_components = components.clone();
|
||||||
|
|
||||||
paginator
|
paginator
|
||||||
|
@ -7,10 +7,10 @@ use libs::walkdir::{DirEntry, WalkDir};
|
|||||||
|
|
||||||
use crate::anyhow;
|
use crate::anyhow;
|
||||||
use errors::{bail, Result};
|
use errors::{bail, Result};
|
||||||
use utils::fs::{create_file, ensure_directory_exists};
|
use utils::fs::{create_directory, create_file};
|
||||||
|
|
||||||
pub fn compile_sass(base_path: &Path, output_path: &Path) -> Result<()> {
|
pub fn compile_sass(base_path: &Path, output_path: &Path) -> Result<()> {
|
||||||
ensure_directory_exists(output_path)?;
|
create_directory(output_path)?;
|
||||||
|
|
||||||
let sass_path = {
|
let sass_path = {
|
||||||
let mut sass_path = PathBuf::from(base_path);
|
let mut sass_path = PathBuf::from(base_path);
|
||||||
|
@ -27,14 +27,6 @@ pub fn create_file(path: &Path, content: &str) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a directory at the given path if it doesn't exist already
|
|
||||||
pub fn ensure_directory_exists(path: &Path) -> Result<()> {
|
|
||||||
if !path.exists() {
|
|
||||||
create_directory(path)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Very similar to `create_dir` from the std except it checks if the folder
|
/// Very similar to `create_dir` from the std except it checks if the folder
|
||||||
/// exists before creating it
|
/// exists before creating it
|
||||||
pub fn create_directory(path: &Path) -> Result<()> {
|
pub fn create_directory(path: &Path) -> Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user