-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathenv.rs
175 lines (160 loc) · 5.58 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::env::var;
use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use crate::normalize_path;
use crate::python_util::{_opt_which_python, get_sys_path};
use crate::style::colors::*;
use crate::style::RESET;
fn fallback_erg_path() -> PathBuf {
#[allow(deprecated)]
std::env::home_dir().map_or(PathBuf::from(".erg"), |path| path.join(".erg"))
}
fn _erg_path() -> PathBuf {
let path = var("ERG_PATH").unwrap_or_else(|_| env!("CARGO_ERG_PATH").to_string());
PathBuf::from(path).canonicalize().unwrap_or_else(|_| {
let fallback = fallback_erg_path();
if !fallback.exists() {
eprintln!("{RED}[ERR] ERG_PATH not found{RESET}");
}
fallback
})
}
fn _erg_std_path() -> PathBuf {
_erg_path()
.join("lib")
.join("std")
.canonicalize()
.unwrap_or_else(|_| {
eprintln!("{RED}[ERR] ERG_PATH/lib/std not found{RESET}");
fallback_erg_path().join("lib/std")
})
}
fn _erg_core_path() -> PathBuf {
_erg_path()
.join("lib")
.join("core")
.canonicalize()
.unwrap_or_else(|_| {
eprintln!("{RED}[ERR] ERG_PATH/lib/core not found{RESET}");
fallback_erg_path().join("lib/core")
})
}
fn _erg_core_decl_path() -> PathBuf {
_erg_path()
.join("lib")
.join("core.d")
.canonicalize()
.unwrap_or_else(|_| {
eprintln!("{RED}[ERR] ERG_PATH/lib/core.d not found {RESET}");
fallback_erg_path().join("lib/core.d")
})
}
fn _erg_pystd_path() -> PathBuf {
_erg_path()
.join("lib")
.join("pystd")
.canonicalize()
.unwrap_or_else(|_| {
eprintln!("{RED}[ERR] ERG_PATH/lib/pystd not found {RESET}");
fallback_erg_path().join("lib/pystd")
})
}
fn _erg_pkgs_path() -> PathBuf {
_erg_path()
.join("lib")
.join("pkgs")
.canonicalize()
.unwrap_or_else(|_| {
// eprintln!("{RED}[ERR] ERG_PATH/lib/pkgs not found {RESET}");
fallback_erg_path().join("lib/pkgs")
})
}
fn _sys_path() -> impl Iterator<Item = PathBuf> {
get_sys_path(None).unwrap_or_default().into_iter().map(|p| {
p.canonicalize().unwrap_or_else(|_| {
// eprintln!("{RED}[ERR] {} not found {RESET}", p.display());
fallback_erg_path().join("lib/pkgs")
})
})
}
fn _python_site_packages() -> impl Iterator<Item = PathBuf> {
let paths = if Path::new("./.venv/lib").is_dir() {
let mut paths = vec![];
for entry in Path::new("./.venv/lib").read_dir().unwrap().flatten() {
if entry.file_type().unwrap().is_dir() {
let mut path = entry.path();
path.push("site-packages");
if path.is_dir() {
paths.push(path);
}
}
}
paths
} else {
get_sys_path(None).unwrap_or_default()
};
paths
.into_iter()
.filter(|p| p.ends_with("site-packages"))
.map(|p| {
p.canonicalize().unwrap_or_else(|_| {
// eprintln!("{RED}[ERR] {} not found {RESET}", p.display());
fallback_erg_path().join("lib/pkgs")
})
})
}
pub static ERG_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_CORE_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_CORE_DECL_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_STD_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_PYSTD_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static ERG_PKGS_PATH: OnceLock<PathBuf> = OnceLock::new();
pub static PYTHON_SYS_PATH: OnceLock<Vec<PathBuf>> = OnceLock::new();
pub static PYTHON_SITE_PACKAGES: OnceLock<Vec<PathBuf>> = OnceLock::new();
pub static PYTHON_PATH: OnceLock<Result<String, String>> = OnceLock::new();
/// == `Path::new("~/.erg")` if ERG_PATH is not set
pub fn erg_path() -> &'static PathBuf {
ERG_PATH.get_or_init(|| normalize_path(_erg_path())) // .with(|s| s.clone())
}
/// == `Path::new("~/.erg/lib/core")` if ERG_PATH is not set
pub fn erg_core_path() -> &'static PathBuf {
ERG_CORE_PATH.get_or_init(|| normalize_path(_erg_core_path()))
}
/// == `Path::new("~/.erg/lib/core.d")` if ERG_PATH is not set
pub fn erg_core_decl_path() -> &'static PathBuf {
ERG_CORE_DECL_PATH.get_or_init(|| normalize_path(_erg_core_decl_path()))
}
/// == `Path::new("~/.erg/lib/std")` if ERG_PATH is not set
pub fn erg_std_path() -> &'static PathBuf {
ERG_STD_PATH.get_or_init(|| normalize_path(_erg_std_path()))
}
/// == `Path::new("~/.erg/lib/pystd")` if ERG_PATH is not set
pub fn erg_pystd_path() -> &'static PathBuf {
ERG_PYSTD_PATH.get_or_init(|| normalize_path(_erg_pystd_path()))
}
/// == `Path::new("~/.erg/lib/pkgs")` if ERG_PATH is not set
pub fn erg_pkgs_path() -> &'static PathBuf {
ERG_PKGS_PATH.get_or_init(|| normalize_path(_erg_pkgs_path()))
}
pub fn python_sys_path() -> &'static Vec<PathBuf> {
PYTHON_SYS_PATH.get_or_init(|| _sys_path().collect())
}
pub fn python_site_packages() -> &'static Vec<PathBuf> {
PYTHON_SITE_PACKAGES.get_or_init(|| _python_site_packages().collect())
}
pub fn opt_which_python() -> Result<&'static String, &'static String> {
PYTHON_PATH.get_or_init(_opt_which_python).as_ref()
}
pub fn is_std_decl_path(path: &Path) -> bool {
path.starts_with(erg_pystd_path().as_path())
}
pub fn is_pystd_main_module(path: &Path) -> bool {
let mut path = PathBuf::from(path);
if path.ends_with("__init__.d.er") {
path.pop();
path.pop();
} else {
path.pop();
}
path == erg_pystd_path().as_path()
}