1
1
use std:: env:: consts:: EXE_SUFFIX ;
2
2
use std:: ffi:: { OsStr , OsString } ;
3
- use std:: os:: windows:: ffi:: OsStrExt ;
3
+ use std:: os:: windows:: ffi:: { OsStrExt , OsStringExt } ;
4
4
use std:: path:: Path ;
5
5
use std:: process:: Command ;
6
6
@@ -282,12 +282,26 @@ pub fn do_remove_from_path() -> Result<()> {
282
282
_apply_new_path ( new_path)
283
283
}
284
284
285
- fn do_add_to_programs ( ) -> Result < ( ) > {
285
+ pub fn do_add_to_programs ( ) -> Result < ( ) > {
286
+ use std:: path:: PathBuf ;
287
+
286
288
let key = RegKey :: predef ( HKEY_CURRENT_USER )
287
289
. create_subkey ( r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup" )
288
290
. chain_err ( || ErrorKind :: PermissionDenied ) ?
289
291
. 0 ;
290
292
293
+ // Don't overwrite registry if rustup is already installed
294
+ let prev = key
295
+ . get_raw_value ( "UninstallString" )
296
+ . map ( |val| from_winreg_value ( & val) ) ;
297
+ if let Ok ( Some ( s) ) = prev {
298
+ let mut path = PathBuf :: from ( OsString :: from_wide ( & s) ) ;
299
+ path. pop ( ) ;
300
+ if path. exists ( ) {
301
+ return Ok ( ( ) ) ;
302
+ }
303
+ }
304
+
291
305
let mut path = utils:: cargo_home ( ) ?;
292
306
path. push ( "bin\r ustup.exe" ) ;
293
307
let mut uninstall_cmd = path. into_os_string ( ) ;
@@ -300,15 +314,40 @@ fn do_add_to_programs() -> Result<()> {
300
314
301
315
key. set_raw_value ( "UninstallString" , & reg_value)
302
316
. chain_err ( || ErrorKind :: PermissionDenied ) ?;
303
- key. set_value ( "DisplayName" , & "rustup" )
317
+ key. set_value ( "DisplayName" , & "rustup - Rust toolchain manager " )
304
318
. chain_err ( || ErrorKind :: PermissionDenied ) ?;
305
319
306
320
Ok ( ( ) )
307
321
}
308
322
309
- fn do_remove_from_programs ( ) -> Result < ( ) > {
310
- RegKey :: predef ( HKEY_CURRENT_USER )
311
- . delete_subkey_all ( r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup" )
323
+ pub fn do_remove_from_programs ( ) -> Result < ( ) > {
324
+ use std:: io;
325
+ use std:: path:: PathBuf ;
326
+
327
+ let root = RegKey :: predef ( HKEY_CURRENT_USER ) ;
328
+
329
+ let key = match root. open_subkey ( r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup" )
330
+ {
331
+ Ok ( key) => key,
332
+ Err ( ref e) if e. kind ( ) == io:: ErrorKind :: NotFound => return Ok ( ( ) ) ,
333
+ Err ( e) => return Err ( e) . chain_err ( || ErrorKind :: PermissionDenied ) ,
334
+ } ;
335
+
336
+ // Don't remove uninstall information from another installation
337
+ let cur = key
338
+ . get_raw_value ( "UninstallString" )
339
+ . map ( |val| from_winreg_value ( & val) ) ;
340
+ if let Ok ( Some ( s) ) = cur {
341
+ let mut reg_path = PathBuf :: from ( OsString :: from_wide ( & s) ) ;
342
+ reg_path. pop ( ) ;
343
+ let mut path = utils:: cargo_home ( ) ?;
344
+ path. push ( "bin" ) ;
345
+ if reg_path != path {
346
+ return Ok ( ( ) ) ;
347
+ }
348
+ }
349
+
350
+ root. delete_subkey_all ( r"Software\Microsoft\Windows\CurrentVersion\Uninstall\rustup" )
312
351
. chain_err ( || ErrorKind :: PermissionDenied )
313
352
}
314
353
0 commit comments