Skip to content

Commit 8a728a6

Browse files
committed
Clean workspace registry of disassociated workspaces
1 parent d4bba75 commit 8a728a6

1 file changed

Lines changed: 54 additions & 2 deletions

File tree

crates/pixi_cli/src/clean.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pixi_config;
1+
use pixi_config::{self, Config};
22
use pixi_consts::consts;
33
use pixi_core::WorkspaceLocator;
44
use pixi_manifest::EnvironmentName;
@@ -47,6 +47,10 @@ pub struct Args {
4747
/// Only remove the pixi-build cache
4848
#[arg(long)]
4949
pub build: bool,
50+
51+
/// Only remove disassociated workspace registries
52+
#[arg(long)]
53+
pub workspaces_registry: bool,
5054
}
5155

5256
/// Clean the cache of your system which are touched by pixi.
@@ -131,7 +135,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
131135
explicit_env.name().fancy_display()
132136
);
133137
}
134-
} else if !args.activation_cache & !args.build {
138+
} else if !args.activation_cache & !args.build & !args.workspaces_registry {
135139
// Remove all pixi related work from the workspace.
136140
if !workspace
137141
.environments_dir()
@@ -151,6 +155,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
151155
false,
152156
)
153157
.await?;
158+
clean_workspaces().await?;
154159
} else {
155160
if args.activation_cache {
156161
remove_folder_with_progress(workspace.activation_env_cache_folder(), true).await?;
@@ -162,6 +167,9 @@ pub async fn execute(args: Args) -> miette::Result<()> {
162167
)
163168
.await?;
164169
}
170+
if args.workspaces_registry {
171+
clean_workspaces().await?;
172+
}
165173
}
166174
Ok(())
167175
}
@@ -221,6 +229,49 @@ async fn clean_cache(args: CacheArgs) -> miette::Result<()> {
221229
Ok(())
222230
}
223231

232+
fn global_config_write_path() -> miette::Result<PathBuf> {
233+
let mut global_locations = pixi_config::config_path_global();
234+
let mut to = global_locations
235+
.pop()
236+
.expect("should have at least one global config path");
237+
238+
for p in global_locations {
239+
if p.exists() {
240+
to = p;
241+
break;
242+
}
243+
}
244+
Ok(to)
245+
}
246+
247+
/// Clean disassociated workspaces from the workspace registry
248+
async fn clean_workspaces() -> miette::Result<()> {
249+
let mut config = Config::load_global();
250+
let to = global_config_write_path()?;
251+
let mut workspaces = config.named_workspaces.clone();
252+
253+
workspaces.retain(|key, val| {
254+
if val.exists() {
255+
true
256+
} else {
257+
eprintln!(
258+
"{} {}",
259+
console::style("removed workspace").green(),
260+
key
261+
);
262+
false
263+
}
264+
});
265+
config.named_workspaces = workspaces;
266+
config.save(&to)?;
267+
eprintln!(
268+
"{} {}",
269+
console::style(console::Emoji("✔ ", "")).green(),
270+
"Workspace registry cleaned"
271+
);
272+
Ok(())
273+
}
274+
224275
async fn remove_folder_with_progress(
225276
folder: PathBuf,
226277
warning_non_existent: bool,
@@ -257,6 +308,7 @@ async fn remove_folder_with_progress(
257308
Ok(())
258309
}
259310

311+
260312
async fn remove_file(file: PathBuf, warning_non_existent: bool) -> miette::Result<()> {
261313
if !file.exists() {
262314
if warning_non_existent {

0 commit comments

Comments
 (0)