Skip to content

Commit 027d398

Browse files
committed
crates: add the Crate::purge_from_cache method
1 parent 28b4c98 commit 027d398

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- New method `WorkspaceBuilder::fetch_registry_index_during_builds` to enable
1212
or disable fetching the registry's index during each build. The method is
1313
only available when the `unstable` rustwide feature is enabled.
14+
- New method `Crate::purge_from_cache` to remove the cached copy of a crate.
1415

1516
### Changed
1617

src/crates/cratesio.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ impl CrateTrait for CratesIOCrate {
5959
Ok(())
6060
}
6161

62+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
63+
let path = self.cache_path(workspace);
64+
if path.exists() {
65+
std::fs::remove_file(&path)?;
66+
}
67+
Ok(())
68+
}
69+
6270
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
6371
let cached = self.cache_path(workspace);
6472
let mut file = File::open(cached)?;

src/crates/git.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ impl CrateTrait for GitRepo {
118118
}
119119
}
120120

121+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
122+
let path = self.cached_path(workspace);
123+
if path.exists() {
124+
remove_dir_all::remove_dir_all(&path)?;
125+
}
126+
Ok(())
127+
}
128+
121129
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error> {
122130
Command::new(workspace, "git")
123131
.args(&["clone"])

src/crates/local.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ impl CrateTrait for Local {
2121
Ok(())
2222
}
2323

24+
fn purge_from_cache(&self, _workspace: &Workspace) -> Result<(), Error> {
25+
// There is no cache to purge for a local crate.
26+
Ok(())
27+
}
28+
2429
fn copy_source_to(&self, _workspace: &Workspace, dest: &Path) -> Result<(), Error> {
2530
info!(
2631
"copying local crate from {} to {}",

src/crates/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::path::Path;
1010

1111
trait CrateTrait: std::fmt::Display {
1212
fn fetch(&self, workspace: &Workspace) -> Result<(), Error>;
13+
fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error>;
1314
fn copy_source_to(&self, workspace: &Workspace, dest: &Path) -> Result<(), Error>;
1415
}
1516

@@ -47,6 +48,11 @@ impl Crate {
4748
self.as_trait().fetch(workspace)
4849
}
4950

51+
/// Remove the cached copy of this crate. The method will do nothing if the crate isn't cached.
52+
pub fn purge_from_cache(&self, workspace: &Workspace) -> Result<(), Error> {
53+
self.as_trait().purge_from_cache(workspace)
54+
}
55+
5056
/// Get this crate's git commit. This method is best-effort, and currently works just for git
5157
/// crates. If the commit can't be retrieved `None` will be returned.
5258
pub fn git_commit(&self, workspace: &Workspace) -> Option<String> {

0 commit comments

Comments
 (0)