juliaup/
global_paths.rs

1use crate::get_juliaup_target;
2#[cfg(feature = "selfupdate")]
3use anyhow::Context;
4use anyhow::{anyhow, bail, Result};
5use std::path::PathBuf;
6pub struct GlobalPaths {
7    pub juliauphome: PathBuf,
8    pub juliaupconfig: PathBuf,
9    pub lockfile: PathBuf,
10    pub versiondb: PathBuf,
11    #[cfg(feature = "selfupdate")]
12    pub juliaupselfhome: PathBuf,
13    #[cfg(feature = "selfupdate")]
14    pub juliaupselfconfig: PathBuf,
15    #[cfg(feature = "selfupdate")]
16    pub juliaupselfbin: PathBuf,
17}
18
19fn get_juliaup_home_path() -> Result<PathBuf> {
20    match std::env::var("JULIAUP_DEPOT_PATH") {
21        Ok(val) => {
22            let val = val.trim();
23
24            if val.is_empty() {
25                get_default_juliaup_home_path()
26            } else {
27                let path = PathBuf::from(val);
28
29                if !path.is_absolute() {
30                    Err(anyhow!("The current value of '{}' for the environment variable JULIAUP_DEPOT_PATH is not an absolute path.", val))
31                } else {
32                    Ok(PathBuf::from(val).join("juliaup"))
33                }
34            }
35        }
36        Err(_) => get_default_juliaup_home_path(),
37    }
38}
39
40/// Return ~/.julia/juliaup, if such a directory can be found
41fn get_default_juliaup_home_path() -> Result<PathBuf> {
42    let path = dirs::home_dir()
43        .ok_or_else(|| anyhow!("Could not determine the path of the user home directory."))?
44        .join(".julia")
45        .join("juliaup");
46
47    if !path.is_absolute() {
48        bail!(
49            "The system returned an invalid home directory path `{}`.",
50            path.display()
51        );
52    };
53    Ok(path)
54}
55
56pub fn get_paths() -> Result<GlobalPaths> {
57    let juliauphome = get_juliaup_home_path()?;
58
59    #[cfg(feature = "selfupdate")]
60    let my_own_path = std::env::current_exe()
61        .with_context(|| "Could not determine the path of the running exe.")?;
62
63    #[cfg(feature = "selfupdate")]
64    let juliaupselfbin = my_own_path
65        .parent()
66        .ok_or_else(|| anyhow!("Could not determine parent."))?
67        .to_path_buf();
68
69    let juliaupconfig = juliauphome.join("juliaup.json");
70
71    let versiondb = juliauphome.join(format!("versiondb-{}.json", get_juliaup_target()));
72
73    let lockfile = juliauphome.join(".juliaup-lock");
74
75    #[cfg(feature = "selfupdate")]
76    let juliaupselfhome = my_own_path
77        .parent()
78        .ok_or_else(|| anyhow!("Failed to get path of folder of own executable."))?
79        .parent()
80        .ok_or_else(|| anyhow!("Failed to get parent path of folder of own executable."))?
81        .to_path_buf();
82
83    #[cfg(feature = "selfupdate")]
84    let juliaupselfconfig = juliaupselfhome.join("juliaupself.json");
85
86    Ok(GlobalPaths {
87        juliauphome,
88        juliaupconfig,
89        lockfile,
90        versiondb,
91        #[cfg(feature = "selfupdate")]
92        juliaupselfhome,
93        #[cfg(feature = "selfupdate")]
94        juliaupselfconfig,
95        #[cfg(feature = "selfupdate")]
96        juliaupselfbin,
97    })
98}