Skip to content

Commit b1a9bf2

Browse files
committed
Add an entry to the installed programs on windows
1 parent 3ac5076 commit b1a9bf2

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/cli/self_update/windows.rs

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use std::env::consts::EXE_SUFFIX;
2+
use std::ffi::{OsStr, OsString};
3+
use std::os::windows::ffi::OsStrExt;
24
use std::path::Path;
35
use std::process::Command;
46

@@ -10,6 +12,9 @@ use crate::process;
1012
use crate::utils::utils;
1113
use crate::utils::Notification;
1214

15+
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
16+
use winreg::{RegKey, RegValue};
17+
1318
pub fn ensure_prompt() -> Result<()> {
1419
writeln!(process().stdout(),)?;
1520
writeln!(process().stdout(), "Press the Enter key to continue.")?;
@@ -42,7 +47,6 @@ pub fn do_msvc_check(opts: &InstallOpts<'_>) -> Result<bool> {
4247

4348
/// Run by rustup-gc-$num.exe to delete CARGO_HOME
4449
pub fn complete_windows_uninstall() -> Result<utils::ExitCode> {
45-
use std::ffi::OsStr;
4650
use std::process::Stdio;
4751

4852
wait_for_parent()?;
@@ -150,8 +154,6 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
150154
use winapi::um::winuser::{
151155
SendMessageTimeoutA, HWND_BROADCAST, SMTO_ABORTIFHUNG, WM_SETTINGCHANGE,
152156
};
153-
use winreg::enums::{RegType, HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
154-
use winreg::{RegKey, RegValue};
155157

156158
let new_path = match new_path {
157159
Some(new_path) => new_path,
@@ -198,8 +200,6 @@ fn _apply_new_path(new_path: Option<Vec<u16>>) -> Result<()> {
198200
// should not mess with it.
199201
fn get_windows_path_var() -> Result<Option<Vec<u16>>> {
200202
use std::io;
201-
use winreg::enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE};
202-
use winreg::RegKey;
203203

204204
let root = RegKey::predef(HKEY_CURRENT_USER);
205205
let environment = root
@@ -270,9 +270,6 @@ fn _with_path_cargo_home_bin<F>(f: F) -> Result<Option<Vec<u16>>>
270270
where
271271
F: FnOnce(Vec<u16>, Vec<u16>) -> Option<Vec<u16>>,
272272
{
273-
use std::ffi::OsString;
274-
use std::os::windows::ffi::OsStrExt;
275-
276273
let windows_path = get_windows_path_var()?;
277274
let mut path_str = utils::cargo_home()?;
278275
path_str.push("bin");
@@ -285,6 +282,36 @@ pub fn do_remove_from_path() -> Result<()> {
285282
_apply_new_path(new_path)
286283
}
287284

285+
fn do_add_to_programs() -> Result<()> {
286+
let key = RegKey::predef(HKEY_CURRENT_USER)
287+
.create_subkey(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
288+
.chain_err(|| ErrorKind::PermissionDenied)?
289+
.0;
290+
291+
let mut path = utils::cargo_home()?;
292+
path.push("bin\rustup.exe");
293+
let mut uninstall_cmd = path.into_os_string();
294+
uninstall_cmd.push(" self uninstall");
295+
296+
let reg_value = RegValue {
297+
bytes: to_winreg_bytes(uninstall_cmd.encode_wide().collect()),
298+
vtype: RegType::REG_SZ,
299+
};
300+
301+
key.set_raw_value("UninstallString", &reg_value)
302+
.chain_err(|| ErrorKind::PermissionDenied)?;
303+
key.set_value("DisplayName", &"rustup")
304+
.chain_err(|| ErrorKind::PermissionDenied)?;
305+
306+
Ok(())
307+
}
308+
309+
fn do_remove_from_programs() -> Result<()> {
310+
RegKey::predef(HKEY_CURRENT_USER)
311+
.delete_subkey_all(r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup")
312+
.chain_err(|| ErrorKind::PermissionDenied)
313+
}
314+
288315
/// Convert a vector UCS-2 chars to a null-terminated UCS-2 string in bytes
289316
pub fn to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
290317
v.push(0);
@@ -296,7 +323,6 @@ pub fn to_winreg_bytes(mut v: Vec<u16>) -> Vec<u8> {
296323
/// does a lossy unicode conversion.
297324
pub fn from_winreg_value(val: &winreg::RegValue) -> Option<Vec<u16>> {
298325
use std::slice;
299-
use winreg::enums::RegType;
300326

301327
match val.vtype {
302328
RegType::REG_SZ | RegType::REG_EXPAND_SZ => {
@@ -364,7 +390,6 @@ pub fn self_replace() -> Result<utils::ExitCode> {
364390
pub fn delete_rustup_and_cargo_home() -> Result<()> {
365391
use std::io;
366392
use std::mem;
367-
use std::os::windows::ffi::OsStrExt;
368393
use std::ptr;
369394
use std::thread;
370395
use std::time::Duration;

0 commit comments

Comments
 (0)