Skip to content

Add option to purge BuildDirectory on drop #28

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 19 additions & 0 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) struct CratePatch {
pub struct BuildDirectory {
workspace: Workspace,
name: String,
purge_on_drop: bool,
}

/// Builder for configuring builds in a [`BuildDirectory`](struct.BuildDirectory.html).
Expand Down Expand Up @@ -97,6 +98,7 @@ impl BuildDirectory {
Self {
workspace,
name: name.into(),
purge_on_drop: false,
}
}

Expand Down Expand Up @@ -172,6 +174,13 @@ impl BuildDirectory {
Ok(())
}

/// Enable or disable purging the build directory when dropped.
///
/// By default, the directory is not purged.
pub fn purge_on_drop(&mut self, yes: bool) {
self.purge_on_drop = yes;
}

fn build_dir(&self) -> PathBuf {
self.workspace.builds_dir().join(&self.name)
}
Expand All @@ -185,6 +194,16 @@ impl BuildDirectory {
}
}

impl Drop for BuildDirectory {
fn drop(&mut self) {
if self.purge_on_drop {
if let Err(err) = self.purge() {
log::error!("failed to delete build directory: {}", err);
}
}
}
}

/// API to interact with a running build.
///
/// This is created from [`BuildDirectory::build`](struct.BuildDirectory.html#method.build)
Expand Down