juliaup/
command_selfuninstall.rs

1#[cfg(feature = "selfupdate")]
2use anyhow::Result;
3
4#[cfg(feature = "selfupdate")]
5pub fn run_command_selfuninstall(paths: &crate::global_paths::GlobalPaths) -> Result<()> {
6    use dialoguer::Confirm;
7
8    use crate::{
9        command_config_backgroundselfupdate::run_command_config_backgroundselfupdate,
10        command_config_modifypath::run_command_config_modifypath,
11        command_config_startupselfupdate::run_command_config_startupselfupdate,
12        command_config_symlinks::run_command_config_symlinks,
13        utils::{print_juliaup_style, JuliaupMessageType},
14    };
15
16    let choice = Confirm::new()
17        .with_prompt("Do you really want to uninstall Julia?")
18        .default(false)
19        .interact()?;
20
21    if !choice {
22        return Ok(());
23    }
24
25    eprint!("Removing background self update task.");
26    match run_command_config_backgroundselfupdate(Some(0), true, paths) {
27        Ok(_) => eprintln!(" Success."),
28        Err(_) => eprintln!(" Failed."),
29    };
30
31    eprint!("Removing startup self update configuration.");
32    match run_command_config_startupselfupdate(Some(0), true, paths) {
33        Ok(_) => eprintln!(" Success."),
34        Err(_) => eprintln!(" Failed."),
35    };
36
37    eprint!("Removing PATH modifications in startup scripts.");
38    match run_command_config_modifypath(Some(false), true, paths) {
39        Ok(_) => eprintln!(" Success."),
40        Err(_) => eprintln!(" Failed."),
41    };
42
43    eprint!("Removing symlinks.");
44    match run_command_config_symlinks(Some(false), true, paths) {
45        Ok(_) => eprintln!(" Success."),
46        Err(_) => eprintln!(" Failed."),
47    };
48
49    eprint!("Deleting Juliaup home folder {:?}.", paths.juliauphome);
50    match std::fs::remove_dir_all(&paths.juliauphome) {
51        Ok(_) => eprintln!(" Success."),
52        Err(_) => eprintln!(" Failed."),
53    };
54
55    if paths.juliauphome != paths.juliaupselfhome {
56        let juliaup_binfolder_path = paths.juliaupselfhome.join("bin");
57        let julia_symlink_path = juliaup_binfolder_path.join("julia");
58        let julialauncher_path = juliaup_binfolder_path.join("julialauncher");
59        let juliaup_path = juliaup_binfolder_path.join("juliaup");
60        let juliaup_config_path = paths.juliaupselfhome.join("juliaupself.json");
61
62        eprint!("Deleting julia symlink {:?}.", julia_symlink_path);
63        match std::fs::remove_file(&julia_symlink_path) {
64            Ok(_) => eprintln!(" Success."),
65            Err(_) => eprintln!(" Failed."),
66        };
67
68        eprint!("Deleting julialauncher binary {:?}.", julialauncher_path);
69        match std::fs::remove_file(&julialauncher_path) {
70            Ok(_) => eprintln!(" Success."),
71            Err(_) => eprintln!(" Failed."),
72        };
73
74        eprint!("Deleting juliaup binary {:?}.", juliaup_path);
75        match std::fs::remove_file(&juliaup_path) {
76            Ok(_) => eprintln!(" Success."),
77            Err(_) => eprintln!(" Failed."),
78        };
79
80        if juliaup_binfolder_path.read_dir()?.next().is_none() {
81            eprint!(
82                "Deleting the Juliaup bin folder {:?}.",
83                juliaup_binfolder_path
84            );
85            match std::fs::remove_dir(&juliaup_binfolder_path) {
86                Ok(_) => {
87                    eprintln!(" Success.");
88
89                    eprint!(
90                        "Deleting the Juliaup configuration file {:?}.",
91                        juliaup_config_path
92                    );
93                    match std::fs::remove_file(&juliaup_config_path) {
94                        Ok(_) => eprintln!(" Success."),
95                        Err(_) => eprintln!(" Failed."),
96                    };
97
98                    if paths.juliaupselfhome.read_dir()?.next().is_none() {
99                        eprint!("Deleting the Juliaup folder {:?}.", paths.juliaupselfhome);
100                        match std::fs::remove_dir(&paths.juliaupselfhome) {
101                            Ok(_) => eprintln!(" Success."),
102                            Err(_) => {
103                                eprintln!(" Failed, skipping removal of the entire Juliaup folder.")
104                            }
105                        };
106                    } else {
107                        eprintln!("The Juliaup folder {:?} is not empty, skipping removal of the entire Juliaup folder.", paths.juliaupselfhome);
108                    }
109                }
110                Err(_) => eprintln!(" Failed, skipping removal of the entire Juliaup folder."),
111            };
112        } else {
113            eprintln!("The Juliaup bin folder {:?} is not empty, skipping removal of the entire Juliaup folder.", juliaup_binfolder_path);
114        }
115    }
116
117    print_juliaup_style(
118        "Remove",
119        "Juliaup successfully removed.",
120        JuliaupMessageType::Success,
121    );
122
123    Ok(())
124}
125
126#[cfg(not(feature = "selfupdate"))]
127use anyhow::Result;
128
129#[cfg(not(feature = "selfupdate"))]
130pub fn run_command_selfuninstall_unavailable() -> Result<()> {
131    eprintln!(
132        "Self uninstall command is unavailable in this variant of Juliaup.
133This software was built with the intention of distributing it
134through a package manager other than cargo or upstream."
135    );
136    Ok(())
137}