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

Prune versions not referenced in juliaup.json with --prune-orphans #1146

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion src/bin/juliaup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ fn main() -> Result<()> {
Juliaup::Remove { channel } => run_command_remove(&channel, &paths),
Juliaup::Status {} => run_command_status(&paths),
Juliaup::Update { channel } => run_command_update(channel, &paths),
Juliaup::Gc { prune_linked } => run_command_gc(prune_linked, &paths),
Juliaup::Gc {
prune_linked,
prune_orphans,
} => run_command_gc(prune_linked, prune_orphans, &paths),
Juliaup::Link {
channel,
file,
Expand Down
2 changes: 2 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum Juliaup {
Gc {
#[clap(long)]
prune_linked: bool,
#[clap(long)]
prune_orphans: bool,
},
#[clap(subcommand, name = "config")]
/// Juliaup configuration
Expand Down
4 changes: 2 additions & 2 deletions src/command_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::global_paths::GlobalPaths;
use crate::operations::garbage_collect_versions;
use anyhow::{Context, Result};

pub fn run_command_gc(prune_linked: bool, paths: &GlobalPaths) -> Result<()> {
pub fn run_command_gc(prune_linked: bool, prune_orphans: bool, paths: &GlobalPaths) -> Result<()> {
let mut config_file = load_mut_config_db(paths)
.with_context(|| "`gc` command failed to load configuration data.")?;

garbage_collect_versions(prune_linked, &mut config_file.data, paths)?;
garbage_collect_versions(prune_linked, prune_orphans, &mut config_file.data, paths)?;

save_config_db(&mut config_file)
.with_context(|| "`gc` command failed to save configuration db.")?;
Expand Down
4 changes: 2 additions & 2 deletions src/command_remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ pub fn run_command_remove(channel: &str, paths: &GlobalPaths) -> Result<()> {
#[cfg(not(windows))]
remove_symlink(&format!("julia-{}", channel))?;

garbage_collect_versions(false, &mut config_file.data, paths)?;
garbage_collect_versions(false, false, &mut config_file.data, paths)?;

save_config_db(&mut config_file).with_context(|| {
format!(
"Failed to save configuration file from `remove` command after '{}' was installed.",
"Failed to save configuration file from `remove` command after '{}' was removed.",
channel
)
})?;
Expand Down
2 changes: 1 addition & 1 deletion src/command_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub fn run_command_update(channel: Option<String>, paths: &GlobalPaths) -> Resul
}
};

garbage_collect_versions(false, &mut config_file.data, paths)?;
garbage_collect_versions(false, false, &mut config_file.data, paths)?;

save_config_db(&mut config_file)
.with_context(|| "`update` command failed to save configuration db.")?;
Expand Down
83 changes: 67 additions & 16 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use console::style;
use flate2::read::GzDecoder;
use indicatif::{ProgressBar, ProgressStyle};
use indoc::formatdoc;
use itertools::Itertools;
use regex::Regex;
use semver::Version;
#[cfg(not(windows))]
Expand Down Expand Up @@ -687,24 +688,18 @@ pub fn install_non_db_version(

pub fn garbage_collect_versions(
prune_linked: bool,
prune_orphans: bool,
config_data: &mut JuliaupConfig,
paths: &GlobalPaths,
) -> Result<()> {
let mut versions_to_uninstall: Vec<String> = Vec::new();

// GC for SystemChannel channels
for (installed_version, detail) in &config_data.installed_versions {
// Removes installed version if not associated to any installed channel
if config_data.installed_channels.iter().all(|j| match &j.1 {
JuliaupConfigChannel::SystemChannel { version } => version != installed_version,
JuliaupConfigChannel::LinkedChannel {
command: _,
args: _,
} => true,
JuliaupConfigChannel::DirectDownloadChannel {
path: _,
url: _,
local_etag: _,
server_etag: _,
version: _,
} => true,
_ => true,
}) {
let path_to_delete = paths.juliauphome.join(&detail.path);
let display = path_to_delete.display();
Expand All @@ -715,27 +710,83 @@ pub fn garbage_collect_versions(
versions_to_uninstall.push(installed_version.clone());
}
}
for version_to_delete in versions_to_uninstall {
config_data.installed_versions.remove(&version_to_delete);
}

for i in versions_to_uninstall {
config_data.installed_versions.remove(&i);
// GC for DirectDownloadChannel channels
let jl_dirs: Vec<_> = std::fs::read_dir(&paths.juliauphome)?
.into_iter()
.filter_map_ok(|r| {
if r.path().is_dir() {
Some(r.path())
} else {
None
}
})
.filter_map_ok(|r| {
let dirname = r.file_name()?.to_str()?;
if dirname.starts_with("julia-") {
Some(dirname.to_owned())
} else {
None
}
})
.filter(|r| r.is_ok())
.map(|r| r.unwrap()) // This is safe, since we only have the Ok variants
.collect();

if prune_orphans {
for jl_dir in jl_dirs {
if config_data
.installed_channels
.iter()
.all(|(_, detail)| match &detail {
JuliaupConfigChannel::SystemChannel { version } => {
let channel_path = &config_data.installed_versions[version]
.path
.replace("./", "");
*channel_path != jl_dir
}
JuliaupConfigChannel::DirectDownloadChannel {
path,
url: _,
local_etag: _,
server_etag: _,
version: _,
} => {
let channel_path = path.replace("./", "");
channel_path != jl_dir
}
JuliaupConfigChannel::LinkedChannel {
command: _,
args: _,
} => true,
})
{
if std::fs::remove_dir_all(paths.juliauphome.join(&jl_dir)).is_err() {
eprintln!("WARNING: Failed to delete {}. You can try to delete at a later point by running `juliaup gc`.", &jl_dir)
}
}
}
}

if prune_linked {
let mut channels_to_uninstall: Vec<String> = Vec::new();
let mut linked_channels_to_uninstall: Vec<String> = Vec::new();
for (installed_channel, detail) in &config_data.installed_channels {
match &detail {
JuliaupConfigChannel::LinkedChannel {
command: cmd,
args: _,
} => {
if !is_valid_julia_path(&PathBuf::from(cmd)) {
channels_to_uninstall.push(installed_channel.clone());
linked_channels_to_uninstall.push(installed_channel.clone());
}
}
_ => (),
}
}
for channel in channels_to_uninstall {
for channel in linked_channels_to_uninstall {
remove_symlink(&format!("julia-{}", &channel))?;
config_data.installed_channels.remove(&channel);
}
Expand Down
Loading