Skip to content

Commit 5ad9f84

Browse files
flxztKeats
authored andcommitted
fix zola serve not respecting preserve_dotfiles_in_output (#2113)
* fix `zola serve` not respecting `preserve_dotfiles_in_output`
1 parent 64feaeb commit 5ad9f84

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

components/site/src/lib.rs

+4-32
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub mod tpls;
77

88
use std::borrow::Cow;
99
use std::collections::{HashMap, HashSet};
10-
use std::fs::{remove_dir_all, remove_file};
1110
use std::path::{Path, PathBuf};
1211
use std::sync::{Arc, Mutex, RwLock};
1312

@@ -18,13 +17,13 @@ use libs::walkdir::{DirEntry, WalkDir};
1817

1918
use config::{get_config, Config, IndexFormat};
2019
use content::{Library, Page, Paginator, Section, Taxonomy};
21-
use errors::{anyhow, bail, Context as ErrorContext, Result};
20+
use errors::{anyhow, bail, Result};
2221
use libs::relative_path::RelativePathBuf;
2322
use std::time::Instant;
2423
use templates::{load_tera, render_redirect_template};
2524
use utils::fs::{
26-
copy_directory, copy_file_if_needed, create_directory, create_file, ensure_directory_exists,
27-
is_dotfile,
25+
clean_site_output_folder, copy_directory, copy_file_if_needed, create_directory, create_file,
26+
ensure_directory_exists,
2827
};
2928
use utils::net::{get_available_port, is_external_link};
3029
use utils::templates::{render_template, ShortcodeDefinition};
@@ -613,34 +612,7 @@ impl Site {
613612
/// Deletes the `public` directory if it exists and the `preserve_dotfiles_in_output` option is set to false,
614613
/// or if set to true: its contents except for the dotfiles at the root level.
615614
pub fn clean(&self) -> Result<()> {
616-
if self.output_path.exists() {
617-
if !self.config.preserve_dotfiles_in_output {
618-
return remove_dir_all(&self.output_path)
619-
.context("Couldn't delete output directory");
620-
}
621-
622-
for entry in self.output_path.read_dir().context(format!(
623-
"Couldn't read output directory `{}`",
624-
self.output_path.display()
625-
))? {
626-
let entry = entry.context("Couldn't read entry in output directory")?.path();
627-
628-
// Skip dotfiles if the preserve_dotfiles_in_output configuration option is set
629-
if is_dotfile(&entry) {
630-
continue;
631-
}
632-
633-
if entry.is_dir() {
634-
remove_dir_all(entry)
635-
.context("Couldn't delete folder while cleaning the output directory")?;
636-
} else {
637-
remove_file(entry)
638-
.context("Couldn't delete file while cleaning the output directory")?;
639-
}
640-
}
641-
}
642-
643-
Ok(())
615+
clean_site_output_folder(&self.output_path, self.config.preserve_dotfiles_in_output)
644616
}
645617

646618
/// Handles whether to write to disk or to memory

components/utils/src/fs.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use libs::filetime::{set_file_mtime, FileTime};
22
use libs::walkdir::WalkDir;
3-
use std::fs::{copy, create_dir_all, metadata, File};
3+
use std::fs::{copy, create_dir_all, metadata, remove_dir_all, remove_file, File};
44
use std::io::prelude::*;
55
use std::path::Path;
66
use std::time::SystemTime;
@@ -201,6 +201,41 @@ pub fn is_temp_file(path: &Path) -> bool {
201201
}
202202
}
203203

204+
/// Deletes the `output_path` directory if it exists and `preserve_dotfiles_in_output` is set to false,
205+
/// or if set to true: its contents except for the dotfiles at the root level.
206+
pub fn clean_site_output_folder(
207+
output_path: &Path,
208+
preserve_dotfiles_in_output: bool,
209+
) -> Result<()> {
210+
if output_path.exists() {
211+
if !preserve_dotfiles_in_output {
212+
return remove_dir_all(output_path).context("Couldn't delete output directory");
213+
}
214+
215+
for entry in output_path
216+
.read_dir()
217+
.context(format!("Couldn't read output directory `{}`", output_path.display()))?
218+
{
219+
let entry = entry.context("Couldn't read entry in output directory")?.path();
220+
221+
// Skip dotfiles if the preserve_dotfiles_in_output configuration option is set
222+
if is_dotfile(&entry) {
223+
continue;
224+
}
225+
226+
if entry.is_dir() {
227+
remove_dir_all(entry)
228+
.context("Couldn't delete folder while cleaning the output directory")?;
229+
} else {
230+
remove_file(entry)
231+
.context("Couldn't delete file while cleaning the output directory")?;
232+
}
233+
}
234+
}
235+
236+
Ok(())
237+
}
238+
204239
#[cfg(test)]
205240
mod tests {
206241
use std::fs::{metadata, read_to_string, File};

src/cmd/serve.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2222
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2323

24-
use std::fs::{read_dir, remove_dir_all};
24+
use std::fs::read_dir;
2525
use std::net::{SocketAddrV4, TcpListener};
2626
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
2727
use std::sync::mpsc::channel;
@@ -47,7 +47,7 @@ use errors::{anyhow, Context, Result};
4747
use pathdiff::diff_paths;
4848
use site::sass::compile_sass;
4949
use site::{Site, SITE_CONTENT};
50-
use utils::fs::{copy_file, is_temp_file};
50+
use utils::fs::{clean_site_output_folder, copy_file, is_temp_file};
5151

5252
use crate::messages;
5353
use std::ffi::OsStr;
@@ -441,12 +441,14 @@ pub fn serve(
441441
watchers.join(",")
442442
);
443443

444+
let preserve_dotfiles_in_output = site.config.preserve_dotfiles_in_output;
445+
444446
println!("Press Ctrl+C to stop\n");
445-
// Delete the output folder on ctrl+C
447+
// Clean the output folder on ctrl+C
446448
ctrlc::set_handler(move || {
447-
match remove_dir_all(&output_path) {
449+
match clean_site_output_folder(&output_path, preserve_dotfiles_in_output) {
448450
Ok(()) => (),
449-
Err(e) => println!("Errored while deleting output folder: {}", e),
451+
Err(e) => println!("Errored while cleaning output folder: {}", e),
450452
}
451453
::std::process::exit(0);
452454
})

0 commit comments

Comments
 (0)