Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perfect some aspect of directory-based profiles #94

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/tutorial/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ In this way, you can detect from inside the pipeline if you are in a profile or
This is useful if you want to keep the outputs of different profiles separate,
for instance.

> Profiles work on directories too! If you specify a directory as a target
> of a profile, Kerblam! will move the whole directory to the new location.

### File modification times when using profiles
`make` tracks file creation times to determine if it has to re-run pipelines again.
This means that if you move files around, like Kerblam! does when it applies
Expand Down
18 changes: 12 additions & 6 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::cache::{check_last_profile, delete_last_profile, get_cache};
use crate::execution::{setup_ctrlc_hook, Executor, FileMover};
use crate::options::KerblamTomlOptions;
use crate::options::Pipe;
use crate::utils::update_timestamps;

use anyhow::{anyhow, bail, Result};
use filetime::{set_file_mtime, FileTime};

/// Push a bit of a string to the end of this path
///
Expand Down Expand Up @@ -75,10 +75,16 @@ fn extract_profile_paths(
.map(|file| {
let f = &root_dir.join(file);
log::debug!("Checking if {f:?} exists...");
if !f.exists() {
bail!("\t- {:?} does not exists!", file)
};
Ok(())
match f.try_exists() {
Ok(i) => {
if i {
Ok(())
} else {
bail!("\t - {file:?} does not exist!")
}
}
Err(e) => bail!("\t- {file:?} - {e:?}"),
}
})
.filter_map(|x| x.err())
.collect();
Expand Down Expand Up @@ -228,7 +234,7 @@ pub fn kerblam_run_project(

for mover in profile_paths {
log::debug!("Touching {:?}", &mover.clone().get_from());
set_file_mtime(&mover.get_from(), FileTime::now())?;
update_timestamps(&mover.clone().get_from())?
}

// We are done. We can delete the last profile.
Expand Down
5 changes: 2 additions & 3 deletions src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ use std::path::{Path, PathBuf};
use std::process::{Child, Command, ExitStatus, Stdio};

use crate::options::KerblamTomlOptions;
use crate::utils::update_timestamps;

use anyhow::{anyhow, bail, Context, Result};
use crossbeam_channel::{bounded, Receiver};

use filetime::{set_file_mtime, FileTime};

// TODO: I think we can add all cleanup code to `Drop`, so that a lot of these
// functions can be simplified a lot.
// E.g. make a "cleanup: Option<Vec<PathBuf>>" to the Executor and remove
Expand Down Expand Up @@ -405,7 +404,7 @@ impl FileMover {
log::debug!("Moving {:?} to {:?}", self.from, self.to);
fs::rename(&self.from, &self.to)?;
if update_time {
set_file_mtime(&self.to, FileTime::now())?;
update_timestamps(&self.to)?;
}

Ok(self.invert())
Expand Down
22 changes: 22 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, bail, Result};
use filetime::{set_file_mtime, FileTime};
use flate2::Compression;
use std::env::current_dir;
use std::ffi::{OsStr, OsString};
Expand Down Expand Up @@ -475,3 +476,24 @@ pub fn gunzip_file(input: impl AsRef<Path>, output: impl AsRef<Path>) -> Result<

Ok(unzipped)
}

pub fn update_timestamps(path: &PathBuf) -> anyhow::Result<()> {
let mut files_touched = 0;
if path.is_file() {
files_touched += 1;
set_file_mtime(path, FileTime::now())?
} else if path.is_dir() {
for entry in walkdir::WalkDir::new(path)
.follow_links(false)
.into_iter()
.filter_map(|x| x.ok())
{
files_touched += 1;
if entry.path().is_file() {
set_file_mtime(entry.path(), FileTime::now())?;
}
}
}
log::debug!("Re-touched {files_touched} files.");
Ok(())
}
1 change: 1 addition & 0 deletions tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use filetime::{set_file_mtime, FileTime};
use similar::{ChangeTag, TextDiff};
use std::env::set_var;
use std::fmt::{Debug, Write};
Expand Down
Loading