Skip to content

Commit

Permalink
Enhance and use code to add an entry to the installed programs on win…
Browse files Browse the repository at this point in the history
…dows
  • Loading branch information
kellda committed Feb 24, 2021
1 parent b1a9bf2 commit 41c5f1b
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ pub fn install(

if !opts.no_modify_path {
do_add_to_path()?;
#[cfg(windows)]
do_add_to_programs()?;
}
utils::create_rustup_home()?;
maybe_install_rust(
Expand Down Expand Up @@ -854,6 +856,8 @@ pub fn uninstall(no_prompt: bool) -> Result<utils::ExitCode> {

// Remove CARGO_HOME/bin from PATH
do_remove_from_path()?;
#[cfg(windows)]
do_remove_from_programs()?;

// Delete everything in CARGO_HOME *except* the rustup bin

Expand Down
48 changes: 42 additions & 6 deletions src/cli/self_update/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env::consts::EXE_SUFFIX;
use std::ffi::{OsStr, OsString};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -282,12 +282,26 @@ pub fn do_remove_from_path() -> Result<()> {
_apply_new_path(new_path)
}

fn do_add_to_programs() -> Result<()> {
pub fn do_add_to_programs() -> Result<()> {
use std::path::PathBuf;

let key = RegKey::predef(HKEY_CURRENT_USER)
.create_subkey(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
.chain_err(|| ErrorKind::PermissionDenied)?
.0;

// Don't overwrite registry if rustup is already installed
let prev = key
.get_raw_value("UninstallString")
.map(|val| from_winreg_value(&val));
if let Ok(Some(s)) = prev {
let mut path = PathBuf::from(OsString::from_wide(&s));
path.pop();
if path.exists() {
return Ok(());
}
}

let mut path = utils::cargo_home()?;
path.push("bin\rustup.exe");
let mut uninstall_cmd = path.into_os_string();
Expand All @@ -300,15 +314,37 @@ fn do_add_to_programs() -> Result<()> {

key.set_raw_value("UninstallString", &reg_value)
.chain_err(|| ErrorKind::PermissionDenied)?;
key.set_value("DisplayName", &"rustup")
key.set_value("DisplayName", &"rustup - Rust toolchain manager")
.chain_err(|| ErrorKind::PermissionDenied)?;

Ok(())
}

fn do_remove_from_programs() -> Result<()> {
RegKey::predef(HKEY_CURRENT_USER)
.delete_subkey_all(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
pub fn do_remove_from_programs() -> Result<()> {
let root = RegKey::predef(HKEY_CURRENT_USER);

let key = match root.open_subkey(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
{
Ok(key) => key,
Err(ref e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(e).chain_err(|| ErrorKind::PermissionDenied),
};

// Don't remove uninstall information from another installation
let cur = key
.get_raw_value("UninstallString")
.map(|val| from_winreg_value(&val));
if let Ok(Some(s)) = cur {
let mut reg_path = PathBuf::from(OsString::from_wide(&s));
reg_path.pop();
let mut path = utils::cargo_home()?;
path.push("bin");
if reg_path != path {
return Ok(());
}
}

root.delete_subkey_all(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
.chain_err(|| ErrorKind::PermissionDenied)
}

Expand Down

0 comments on commit 41c5f1b

Please sign in to comment.