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
use directories::{ProjectDirs,UserDirs};
use regex::Regex;
use std::path::{Path, PathBuf};
impl super::Paths {
pub fn home() -> PathBuf {
match UserDirs::new() {
Some(user_dirs) => user_dirs.home_dir().to_path_buf(),
None => std::env::temp_dir(),
}
}
pub fn env(env_name: &str) -> Option<PathBuf> {
match std::env::var(env_name) {
Ok(val) => Some(PathBuf::from(val)),
Err(_) => None,
}
}
pub fn which(name: &str) -> Option<PathBuf> {
let extensions = vec!["", ".exe", ".bat"];
for ext in extensions.iter() {
let exe_name = format!("{}{}", name, ext);
match _which_exact(&exe_name) {
Some(path) => {
return Some(path);
}
None => {}
}
}
None
}
pub fn shebang(path: &Path) -> super::Result<String> {
match std::fs::read_to_string(path) {
Ok(text) => match Regex::new(r"#!\s*([/:\.\w\-]+)") {
Ok(re) => match re.captures(&text) {
Some(caps) => {
if caps.len() > 1 {
Ok(caps[1].to_string())
} else {
Ok("".to_string())
}
}
None => Ok("".to_string()),
},
Err(e) => Err(super::Error::from(e)),
},
Err(e) => Err(super::Error::from(e)),
}
}
pub fn application_path<P: AsRef<Path>>(
qualifier: &str,
organization: &str,
application: &str,
rel_path: P,
) -> PathBuf {
match ProjectDirs::from(qualifier, organization, application) {
Some(project_dirs) => {
let dir = project_dirs.data_dir();
dir.to_path_buf().join(rel_path)
}
None => super::Paths::home()
.join(qualifier)
.join(organization)
.join(application)
.join(rel_path),
}
}
}
fn _which_exact(name: &str) -> Option<PathBuf> {
std::env::var_os("PATH").and_then(|paths| {
std::env::split_paths(&paths)
.filter_map(|dir| {
let full_path = dir.join(&name);
if full_path.is_file() {
Some(full_path)
} else {
None
}
})
.next()
})
}