diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-02-01 17:41:08 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-02-01 17:41:08 +1300 |
commit | 35a4fc3c3e1871cb0b4a8bf243d5b97f0e827628 (patch) | |
tree | ef5ef9f672a5104b1225e06a98d244c3080881ee /src/main.rs | |
parent | 480bc790bdf4c8e64bd7a3a99884a6bcc1e17055 (diff) | |
download | toaster-35a4fc3c3e1871cb0b4a8bf243d5b97f0e827628.zip |
Set last-modified time of generated files same as source file
When generating a file from a source file, the last-modified time
of the generated file will be set to be the same as the last-modified
time of the source file. This is so that when copying the generated
website to a server with rsync, only modified files will be copied
over, saving considerable time.
This commit also updates vagabond to the latest version which includes
a change where files are only copied if they have been modified or if
they haven't yet been copied. This saves considerable time when
generating the website, if the website contains large static files.
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index 2920917..25d1528 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,6 +9,7 @@ use markdown::*; use vagabond::*; use std::collections::HashSet; +use std::time::SystemTime; const NORMAL: &str = "\x1b[0m"; @@ -95,7 +96,7 @@ fn main() { // Convert document to different formats. if args.html { let html = generate_html(&page.document, page, &website); - write_file(&html, &destination, "html"); + write_file(&html, &destination, "html", page.last_modified); } // Copy original markdown file. destination.add_extension("md"); @@ -115,15 +116,7 @@ fn main() { static_file.source_path, destination)); } - for static_dir in &website.static_dirs { - let mut destination = destination.clone(); - destination.push(&static_dir.full_url); - verbose!("Copying static directory to {destination:?}"); - make_parent_directory(&destination).unwrap(); - copy(&static_dir.source_path, &destination).unwrap_or_else(|_| - error!("Failed to copy static directory {:?} to {:?}", - static_dir.source_path, destination)); - } + // NOTE: Static dir contents are copied as part of all static files. for redirect in &website.redirects { let mut destination = destination.clone(); @@ -132,12 +125,12 @@ fn main() { if args.html { if !path.contains("://") { if let Some(path) = website.has_page(redirect, &path, "html") { - write_file(&generate_html_redirect(&path), &destination, "html"); + write_file(&generate_html_redirect(&path), &destination, "html", redirect.last_modified); } else { warn!("Redirect {:?} links to nonexistent page {path:?}", redirect.name); } } else { - write_file(&generate_html_redirect(&path), &destination, "html"); + write_file(&generate_html_redirect(&path), &destination, "html", redirect.last_modified); } } } @@ -145,7 +138,7 @@ fn main() { -pub fn write_file(text: &str, destination: &PathBuf, ext: &str) { +pub fn write_file(text: &str, destination: &PathBuf, ext: &str, last_modified: Option<SystemTime>) { let mut destination = destination.clone(); destination.add_extension(ext); verbose!("Generating {destination:?}"); @@ -153,6 +146,12 @@ pub fn write_file(text: &str, destination: &PathBuf, ext: &str) { error!("Failed to create parent directories for {destination:?}")); write_to_file(&destination, text).unwrap_or_else(|_| error!("Failed to write generated {ext} file to {destination:?}")); + // Set the last-modified time of the new file to the time provided. + if let Some(time) = last_modified { + if let Ok(dest) = std::fs::File::open(&destination) { + let _ = dest.set_modified(time); + } + } } pub fn make_url_safe(text: &str) -> String { |