Refactor: remove even more unnecessary create_directory (#2428)
This commit is contained in:
parent
9413d943f1
commit
96a9dc1a28
@ -635,10 +635,7 @@ impl Site {
|
||||
components: &[&str],
|
||||
filename: &str,
|
||||
content: String,
|
||||
create_dirs: bool,
|
||||
) -> Result<PathBuf> {
|
||||
let write_dirs = self.build_mode == BuildMode::Disk || create_dirs;
|
||||
|
||||
let mut site_path = RelativePathBuf::new();
|
||||
let mut current_path = self.output_path.to_path_buf();
|
||||
|
||||
@ -647,10 +644,6 @@ impl Site {
|
||||
site_path.push(component);
|
||||
}
|
||||
|
||||
if write_dirs {
|
||||
create_directory(¤t_path)?;
|
||||
}
|
||||
|
||||
let final_content = if !filename.ends_with("html") || !self.config.minify_html {
|
||||
content
|
||||
} else {
|
||||
@ -696,8 +689,7 @@ impl Site {
|
||||
let output = page.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
|
||||
let content = self.inject_livereload(output);
|
||||
let components: Vec<&str> = page.path.split('/').collect();
|
||||
let current_path =
|
||||
self.write_content(&components, "index.html", content, !page.assets.is_empty())?;
|
||||
let current_path = self.write_content(&components, "index.html", content)?;
|
||||
|
||||
// Copy any asset we found previously into the same directory as the index.html
|
||||
self.copy_assets(page.file.path.parent().unwrap(), &page.assets, ¤t_path)?;
|
||||
@ -849,7 +841,7 @@ impl Site {
|
||||
None => "index.html",
|
||||
};
|
||||
let content = render_redirect_template(permalink, &self.tera)?;
|
||||
self.write_content(&split, page_name, content, false)?;
|
||||
self.write_content(&split, page_name, content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -877,7 +869,7 @@ impl Site {
|
||||
context.insert("lang", &self.config.default_language);
|
||||
let output = render_template("404.html", &self.tera, context, &self.config.theme)?;
|
||||
let content = self.inject_livereload(output);
|
||||
self.write_content(&[], "404.html", content, false)?;
|
||||
self.write_content(&[], "404.html", content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -886,7 +878,7 @@ impl Site {
|
||||
let mut context = Context::new();
|
||||
context.insert("config", &self.config.serialize(&self.config.default_language));
|
||||
let content = render_template("robots.txt", &self.tera, context, &self.config.theme)?;
|
||||
self.write_content(&[], "robots.txt", content, false)?;
|
||||
self.write_content(&[], "robots.txt", content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -917,7 +909,7 @@ impl Site {
|
||||
let list_output =
|
||||
taxonomy.render_all_terms(&self.tera, &self.config, &self.library.read().unwrap())?;
|
||||
let content = self.inject_livereload(list_output);
|
||||
self.write_content(&components, "index.html", content, false)?;
|
||||
self.write_content(&components, "index.html", content)?;
|
||||
|
||||
let library = self.library.read().unwrap();
|
||||
taxonomy
|
||||
@ -942,7 +934,7 @@ impl Site {
|
||||
let single_output =
|
||||
taxonomy.render_term(item, &self.tera, &self.config, &library)?;
|
||||
let content = self.inject_livereload(single_output);
|
||||
self.write_content(&comp, "index.html", content, false)?;
|
||||
self.write_content(&comp, "index.html", content)?;
|
||||
}
|
||||
|
||||
if taxonomy.kind.feed {
|
||||
@ -981,7 +973,7 @@ impl Site {
|
||||
let mut context = Context::new();
|
||||
context.insert("entries", &all_sitemap_entries);
|
||||
let sitemap = render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
|
||||
self.write_content(&[], "sitemap.xml", sitemap, false)?;
|
||||
self.write_content(&[], "sitemap.xml", sitemap)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@ -994,7 +986,7 @@ impl Site {
|
||||
context.insert("entries", &chunk);
|
||||
let sitemap = render_template("sitemap.xml", &self.tera, context, &self.config.theme)?;
|
||||
let file_name = format!("sitemap{}.xml", i + 1);
|
||||
self.write_content(&[], &file_name, sitemap, false)?;
|
||||
self.write_content(&[], &file_name, sitemap)?;
|
||||
let mut sitemap_url = self.config.make_permalink(&file_name);
|
||||
sitemap_url.pop(); // Remove trailing slash
|
||||
sitemap_index.push(sitemap_url);
|
||||
@ -1009,7 +1001,7 @@ impl Site {
|
||||
main_context,
|
||||
&self.config.theme,
|
||||
)?;
|
||||
self.write_content(&[], "sitemap.xml", sitemap, false)?;
|
||||
self.write_content(&[], "sitemap.xml", sitemap)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -1040,10 +1032,9 @@ impl Site {
|
||||
&components.iter().map(|x| x.as_ref()).collect::<Vec<_>>(),
|
||||
feed_filename,
|
||||
feed,
|
||||
false,
|
||||
)?;
|
||||
} else {
|
||||
self.write_content(&[], feed_filename, feed, false)?;
|
||||
self.write_content(&[], feed_filename, feed)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -1052,7 +1043,6 @@ impl Site {
|
||||
pub fn render_section(&self, section: &Section, render_pages: bool) -> Result<()> {
|
||||
let mut output_path = self.output_path.clone();
|
||||
let mut components: Vec<&str> = Vec::new();
|
||||
let create_directories = self.build_mode == BuildMode::Disk || !section.assets.is_empty();
|
||||
|
||||
if section.lang != self.config.default_language {
|
||||
components.push(§ion.lang);
|
||||
@ -1064,10 +1054,6 @@ impl Site {
|
||||
output_path.push(component);
|
||||
}
|
||||
|
||||
if create_directories {
|
||||
create_directory(&output_path)?;
|
||||
}
|
||||
|
||||
if section.meta.generate_feed {
|
||||
let library = &self.library.read().unwrap();
|
||||
let pages = section.pages.iter().map(|k| library.pages.get(k).unwrap()).collect();
|
||||
@ -1107,7 +1093,6 @@ impl Site {
|
||||
&components,
|
||||
"index.html",
|
||||
render_redirect_template(&permalink, &self.tera)?,
|
||||
create_directories,
|
||||
)?;
|
||||
|
||||
return Ok(());
|
||||
@ -1122,7 +1107,7 @@ impl Site {
|
||||
let output =
|
||||
section.render_html(&self.tera, &self.config, &self.library.read().unwrap())?;
|
||||
let content = self.inject_livereload(output);
|
||||
self.write_content(&components, "index.html", content, false)?;
|
||||
self.write_content(&components, "index.html", content)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@ -1174,14 +1159,13 @@ impl Site {
|
||||
let content = self.inject_livereload(output);
|
||||
|
||||
if pager.index > 1 {
|
||||
self.write_content(&pager_components, "index.html", content, false)?;
|
||||
self.write_content(&pager_components, "index.html", content)?;
|
||||
} else {
|
||||
self.write_content(&index_components, "index.html", content, false)?;
|
||||
self.write_content(&index_components, "index.html", content)?;
|
||||
self.write_content(
|
||||
&pager_components,
|
||||
"index.html",
|
||||
render_redirect_template(&paginator.permalink, &self.tera)?,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
|
||||
|
@ -19,8 +19,17 @@ pub fn is_path_in_directory(parent: &Path, path: &Path) -> Result<bool> {
|
||||
Ok(canonical_path.starts_with(canonical_parent))
|
||||
}
|
||||
|
||||
/// Creates the parent of a directory, if needed.
|
||||
fn create_parent(path: &Path) -> Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
create_directory(parent)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a file with the content given
|
||||
pub fn create_file(path: &Path, content: &str) -> Result<()> {
|
||||
create_parent(path)?;
|
||||
let mut file =
|
||||
File::create(path).with_context(|| format!("Failed to create file {}", path.display()))?;
|
||||
file.write_all(content.as_bytes())?;
|
||||
@ -58,12 +67,7 @@ pub fn copy_file(src: &Path, dest: &Path, base_path: &Path, hard_link: bool) ->
|
||||
let relative_path = src.strip_prefix(base_path).unwrap();
|
||||
let target_path = dest.join(relative_path);
|
||||
|
||||
if let Some(parent_directory) = target_path.parent() {
|
||||
create_dir_all(parent_directory).with_context(|| {
|
||||
format!("Failed to create directory {}", parent_directory.display())
|
||||
})?;
|
||||
}
|
||||
|
||||
create_parent(&target_path)?;
|
||||
copy_file_if_needed(src, &target_path, hard_link)
|
||||
}
|
||||
|
||||
@ -72,11 +76,7 @@ pub fn copy_file(src: &Path, dest: &Path, base_path: &Path, hard_link: bool) ->
|
||||
/// 2. Its modification timestamp is identical to that of the src file.
|
||||
/// 3. Its filesize is identical to that of the src file.
|
||||
pub fn copy_file_if_needed(src: &Path, dest: &Path, hard_link: bool) -> Result<()> {
|
||||
if let Some(parent_directory) = dest.parent() {
|
||||
create_dir_all(parent_directory).with_context(|| {
|
||||
format!("Failed to create directory {}", parent_directory.display())
|
||||
})?;
|
||||
}
|
||||
create_parent(&dest)?;
|
||||
|
||||
if hard_link {
|
||||
if dest.exists() {
|
||||
|
Loading…
Reference in New Issue
Block a user