Skip to content

Commit

Permalink
Merge pull request #1035 from ghyatzo/patch-improveupdatechannel
Browse files Browse the repository at this point in the history
Better messages when updating existing links
  • Loading branch information
davidanthoff authored Sep 6, 2024
2 parents ab16ed1 + b6f8e11 commit 96aa395
Showing 1 changed file with 58 additions and 25 deletions.
83 changes: 58 additions & 25 deletions src/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,14 +661,16 @@ pub fn garbage_collect_versions(
Ok(())
}

fn _remove_symlink(symlink_path: &Path) -> Result<()> {
fn _remove_symlink(symlink_path: &Path) -> Result<Option<PathBuf>> {
std::fs::create_dir_all(symlink_path.parent().unwrap())?;

if symlink_path.exists() {
let prev_target = std::fs::read_link(&symlink_path)?;
std::fs::remove_file(symlink_path)?;
return Ok(Some(prev_target));
}

Ok(())
Ok(None)
}

pub fn remove_symlink(symlink_name: &String) -> Result<()> {
Expand Down Expand Up @@ -698,20 +700,29 @@ pub fn create_symlink(

let symlink_path = symlink_folder.join(symlink_name);

_remove_symlink(&symlink_path)?;
let updating = _remove_symlink(&symlink_path)?;

match channel {
JuliaupConfigChannel::SystemChannel { version } => {
let child_target_foldername = format!("julia-{}", version);

let target_path = paths.juliauphome.join(&child_target_foldername);

eprintln!(
"{} {} for Julia {}.",
style("Creating symlink").cyan().bold(),
symlink_name,
version
);
if let Some(ref prev_target) = updating {
eprintln!(
"{} symlink {} ( {} -> {} )",
style("Updating").cyan().bold(),
symlink_name,
prev_target.to_string_lossy(),
version
);
} else {
eprintln!(
"{} {} for Julia {}.",
style("Creating symlink").cyan().bold(),
symlink_name,
version
);
}

std::os::unix::fs::symlink(target_path.join("bin").join("julia"), &symlink_path)
.with_context(|| {
Expand All @@ -730,12 +741,22 @@ pub fn create_symlink(
} => {
let target_path = paths.juliauphome.join(path);

eprintln!(
"{} {} for Julia {}.",
style("Creating symlink").cyan().bold(),
symlink_name,
version
);
if let Some(ref prev_target) = updating {
eprintln!(
"{} symlink {} ( {} -> {} )",
style("Updating").cyan().bold(),
symlink_name,
prev_target.to_string_lossy(),
version
);
} else {
eprintln!(
"{} {} for Julia {}.",
style("Creating symlink").cyan().bold(),
symlink_name,
version
);
}

std::os::unix::fs::symlink(target_path.join("bin").join("julia"), &symlink_path)
.with_context(|| {
Expand All @@ -751,12 +772,22 @@ pub fn create_symlink(
None => command.clone(),
};

eprintln!(
"{} {} for `{}`",
style("Creating shim").cyan().bold(),
symlink_name,
formatted_command
);
if let Some(ref prev_target) = updating {
eprintln!(
"{} shim {} ( {} -> {} )",
style("Updating").cyan().bold(),
symlink_name,
prev_target.to_string_lossy(),
formatted_command
);
} else {
eprintln!(
"{} {} for {}.",
style("Creating shim").cyan().bold(),
symlink_name,
formatted_command
);
}

std::fs::write(
&symlink_path,
Expand Down Expand Up @@ -785,12 +816,14 @@ pub fn create_symlink(
}
};

if let Ok(path) = std::env::var("PATH") {
if !path.split(':').any(|p| Path::new(p) == symlink_folder) {
eprintln!(
if updating.is_none() {
if let Ok(path) = std::env::var("PATH") {
if !path.split(':').any(|p| Path::new(p) == symlink_folder) {
eprintln!(
"Symlink {} added in {}. Add this directory to the system PATH to make the command available in your shell.",
&symlink_name, symlink_folder.display(),
);
}
}
}

Expand Down

0 comments on commit 96aa395

Please sign in to comment.