easy_install/
env.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::path::PathBuf;

use crud_path::{add_github_path, is_github};

pub const IS_WINDOWS: bool = cfg!(target_os = "windows");

pub fn add_to_path(dir: &str) {
    if crud_path::has_path(dir) {
        return;
    }

    if is_github() {
        add_github_path(dir);
    }

    if let Some(sh) = crud_path::add_path(dir) {
        println!("Successfully added {dir} to {sh}'s $PATH");
    } else {
        println!("You need to add {dir} to your PATH");
    }
}

#[cfg(not(target_os = "windows"))]
pub fn get_install_dir() -> PathBuf {
    use std::str::FromStr;
    let home = PathBuf::from_str(if is_admin::is_admin() {
        "/usr/bin"
    } else {
        "/usr/local/bin"
    })
    .unwrap();

    if !home.exists() {
        std::fs::create_dir(&home).expect("Failed to create_dir home_dir");
    }
    home
}

#[cfg(target_os = "windows")]
pub fn get_install_dir() -> PathBuf {
    let mut home = dirs::home_dir().expect("Failed to get home_dir");
    home.push("easy-install");

    if !home.exists() {
        std::fs::create_dir_all(&home).expect("Failed to create_dir home_dir");
    }
    let home_str = home.to_str().expect("Failed to get home_dir string");
    add_to_path(home_str);
    home
}