Skip to content

Commit fdc2c59

Browse files
committed
Add option to purge BuildDirectory on drop
1 parent 26a83d1 commit fdc2c59

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/build.rs

+19
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub(crate) struct CratePatch {
2121
pub struct BuildDirectory {
2222
workspace: Workspace,
2323
name: String,
24+
purge_on_drop: bool,
2425
}
2526

2627
/// Builder for configuring builds in a [`BuildDirectory`](struct.BuildDirectory.html).
@@ -97,6 +98,7 @@ impl BuildDirectory {
9798
Self {
9899
workspace,
99100
name: name.into(),
101+
purge_on_drop: false,
100102
}
101103
}
102104

@@ -172,6 +174,13 @@ impl BuildDirectory {
172174
Ok(())
173175
}
174176

177+
/// Enable or disable purging the build directory when dropped.
178+
///
179+
/// By default, the directory is not dropped.
180+
pub fn purge_on_drop(&mut self, yes: bool) {
181+
self.purge_on_drop = yes;
182+
}
183+
175184
fn build_dir(&self) -> PathBuf {
176185
self.workspace.builds_dir().join(&self.name)
177186
}
@@ -185,6 +194,16 @@ impl BuildDirectory {
185194
}
186195
}
187196

197+
impl Drop for BuildDirectory {
198+
fn drop(&mut self) {
199+
if self.purge_on_drop {
200+
if let Err(err) = self.purge() {
201+
log::error!("failed to delete build directory: {}", err);
202+
}
203+
}
204+
}
205+
}
206+
188207
/// API to interact with a running build.
189208
///
190209
/// This is created from [`BuildDirectory::build`](struct.BuildDirectory.html#method.build)

0 commit comments

Comments
 (0)