Skip to content

Commit

Permalink
Refactor: remove even more unnecessary create_directory (#2428)
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey authored and Keats committed Jun 20, 2024
1 parent 9413d94 commit 96a9dc1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
42 changes: 13 additions & 29 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -647,10 +644,6 @@ impl Site {
site_path.push(component);
}

if write_dirs {
create_directory(&current_path)?;
}

let final_content = if !filename.ends_with("html") || !self.config.minify_html {
content
} else {
Expand Down Expand Up @@ -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, &current_path)?;
Expand Down Expand Up @@ -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(())
}

Expand Down Expand Up @@ -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(())
}

Expand All @@ -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(())
}

Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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(());
}

Expand All @@ -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);
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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(())
}
Expand All @@ -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(&section.lang);
Expand All @@ -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();
Expand Down Expand Up @@ -1107,7 +1093,6 @@ impl Site {
&components,
"index.html",
render_redirect_template(&permalink, &self.tera)?,
create_directories,
)?;

return Ok(());
Expand All @@ -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(())
Expand Down Expand Up @@ -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,
)?;
}

Expand Down
22 changes: 11 additions & 11 deletions components/utils/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())?;
Expand Down Expand Up @@ -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)
}

Expand All @@ -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() {
Expand Down

0 comments on commit 96a9dc1

Please sign in to comment.