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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
use crate::app;
use std::path::PathBuf;
use walkdir::WalkDir;
use pathdiff::diff_paths;
use regex::{escape, Regex};
use std::fs;
pub fn clean_name(name: &str, dir: &str, return_file: bool) -> String {
let matches = matches(name, dir);
let t = dir.trim_end_matches("s");
if matches.len() == 0 {
println!(
"No matching {} '{}' found, here are the available {}s:",
t, name, t
);
for file in all(dir).iter() {
println!(
" {}",
diff_paths(&file, &app().unwrap().root.join(dir))
.unwrap()
.display()
);
}
} else if matches.len() > 1 {
println!(
"That {} name '{}' is ambiguous, please try again to narrow it down to one of these:",
t, name
);
for file in matches.iter() {
println!(
" {}",
diff_paths(&file, &app().unwrap().root.join(dir))
.unwrap()
.display()
);
}
} else {
if return_file {
return format!("{}", matches[0].display());
} else {
let clean = format!(
"{}",
diff_paths(&matches[0], &app().unwrap().root.join(dir))
.unwrap()
.display()
);
return clean;
}
}
std::process::exit(1);
}
pub fn matches(name: &str, dir: &str) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = Vec::new();
for file in WalkDir::new(format!("{}", app().unwrap().root.join(dir).display())) {
let path = file.unwrap().into_path();
if path.is_file() {
let mut path_str = format!(
"{}",
diff_paths(&path, &app().unwrap().root.join(dir))
.unwrap()
.display()
);
path_str = path_str.replace("\\", "/").replace("//", "/");
if path_str.contains(name) {
files.push(path);
} else {
let re = Regex::new(format!(r#".*{}(\\|/)"#, dir).as_str()).unwrap();
let mut new_name: String = re.replace_all(&name, "").into();
new_name = new_name.replace("\\", "/").replace("//", "/");
if path_str.contains(&new_name) {
files.push(path);
}
}
}
}
if files.len() > 1 {
files = files
.into_iter()
.filter(|path| path.file_name().unwrap().to_str().unwrap() == &format!("{}.py", name))
.collect();
}
files
}
pub fn get(full_paths: bool) -> Option<Vec<String>> {
app()
.unwrap()
.with_config_mut(|config| {
config.refresh();
match config.target.as_ref() {
Some(targets) => Ok(Some(
targets
.iter()
.map(|t| clean_name(t, "targets", full_paths))
.collect::<Vec<String>>()
.clone(),
)),
None => Ok(None),
}
})
.unwrap()
}
pub fn set(targets: Vec<&str>) {
let clean_targets: Vec<String> = targets
.iter()
.map(|t| clean_name(t, "targets", true))
.collect();
set_workspace_array("target", clean_targets)
}
pub fn reset() {
delete_val("target")
}
pub fn add(targets: Vec<&str>) {
let mut current: Vec<String> = app()
.unwrap()
.with_config(|config| {
let c = match &config.target {
Some(targets) => targets.clone(),
None => vec![],
}
.iter()
.map(|t| clean_name(t, "targets", true))
.collect();
Ok(c)
})
.unwrap();
for t in targets.iter() {
let clean_t = clean_name(t, "targets", true);
current.retain(|c| *c != clean_t);
current.push(clean_t);
}
set_workspace_array("target", current);
}
pub fn remove(targets: Vec<&str>) {
let mut current: Vec<String> = app()
.unwrap()
.with_config(|config| {
let c: Vec<String> = match &config.target {
Some(targets) => targets.clone(),
None => vec![],
}
.iter()
.map(|t| clean_name(t, "targets", true))
.collect();
Ok(c)
})
.unwrap();
for t in targets.iter() {
let clean_t = clean_name(t, "targets", true);
current.retain(|c| *c != clean_t);
}
if current.len() == 0 {
println!("All targets were removed. Resetting to the default target.");
reset();
} else {
set_workspace_array("target", current);
}
}
pub fn all(dir: &str) -> Vec<PathBuf> {
let mut files: Vec<PathBuf> = Vec::new();
for file in WalkDir::new(format!("{}", app().unwrap().root.join(dir).display())) {
let path = file.unwrap().into_path();
if path.is_file() {
files.push(path);
}
}
files
}
pub fn set_workspace(key: &str, val: &str) {
ensure_app_dot_toml();
delete_val(key);
add_val(key, val);
}
pub fn set_workspace_array(key: &str, vals: Vec<String>) {
ensure_app_dot_toml();
delete_val(key);
add_val_array(key, vals);
}
pub fn delete_val(key: &str) {
let path = app().unwrap().root.join(".origen").join("application.toml");
let data = fs::read_to_string(path).expect("Unable to read file .origen/application.toml");
let re = Regex::new(format!(r#"{}\s?=.*(\r\n|\n)?"#, escape(key)).as_str()).unwrap();
let new_data: String = re.replace_all(&data, "").into();
fs::write(
app().unwrap().root.join(".origen").join("application.toml"),
new_data,
)
.expect("Unable to write file .origen/application.toml!");
}
fn add_val(key: &str, val: &str) {
let path = app().unwrap().root.join(".origen").join("application.toml");
let data = fs::read_to_string(path).expect("Unable to read file .origen/application.toml");
let new_data = format!("{}\n{} = \"{}\"", data.trim(), key, val);
fs::write(
app().unwrap().root.join(".origen").join("application.toml"),
new_data,
)
.expect("Unable to write file .origen/application.toml!");
}
fn add_val_array(key: &str, vals: Vec<String>) {
let path = app().unwrap().root.join(".origen").join("application.toml");
let data = fs::read_to_string(path).expect("Unable to read file .origen/application.toml");
let new_data = format!(
"{}\n{} = [{}]",
data.trim(),
key,
vals.iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<String>>()
.join(", ")
);
fs::write(
app().unwrap().root.join(".origen").join("application.toml"),
new_data,
)
.expect("Unable to write file .origen/application.toml!");
}
fn ensure_app_dot_toml() {
let path = app().unwrap().root.join(".origen");
if !path.exists() {
fs::create_dir(&path).expect("Unable to create directory .origen!");
}
let path = path.join("application.toml");
if !path.exists() {
let data =
"# This file is generated by Origen and should not be checked into revision control";
fs::write(
app().unwrap().root.join(".origen").join("application.toml"),
data,
)
.expect("Unable to write file .origen/application.toml!");
}
}