-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtask.rs
256 lines (213 loc) · 7.26 KB
/
task.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
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
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::{env, str::FromStr};
use ansi_term::Color::{Green, Red, White, Yellow};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use tracing::{debug, error, info};
use crate::{
command::{get_command, CommandConfig, CommandInterface},
config::{base_config::Command, config_value::ConfigValue, os::Os},
task_runner::TaskRunnerMode,
utils::threads::ThreadPool,
};
use dialoguer::{console::Term, theme::ColorfulTheme, Select};
fn run_command(
command: Box<dyn CommandInterface>,
args: ConfigValue,
mode: &TaskRunnerMode,
config: &CommandConfig,
progress: &ProgressBar,
) -> Result<(), String> {
match mode {
TaskRunnerMode::Install => command.install(args, config, progress),
TaskRunnerMode::Update => command.update(args, config, progress),
TaskRunnerMode::Uninstall => command.uninstall(args, config, progress),
}
}
#[derive(Debug, Clone)]
pub struct Task {
pub name: String,
pub commands: Vec<Command>,
pub os: Vec<Os>,
pub parallel: bool,
}
impl Task {
pub fn run(
&self,
mode: TaskRunnerMode,
config: &CommandConfig,
mp: &MultiProgress,
) -> Result<(), String> {
if should_skip_task(self) {
info!(
"{}",
Yellow.bold().paint(format!(
"Skipping task \"{}\" due to OS condition ...",
self.name
))
);
return Ok(());
}
let commands = &self.commands;
let num_threads = if self.parallel { commands.len() } else { 1 };
if self.parallel {
debug!(
"Executing commands in parallel ({} threads)...",
White.bold().paint(num_threads.to_string())
);
}
let task_name = self.name.clone();
let pb = ProgressBar::new(commands.len().try_into().unwrap()).with_style(
ProgressStyle::default_bar()
.template("[{bar:50.green/white}] {pos}/{len}: {msg}")
.unwrap()
.progress_chars("=> "),
);
let added_pb = mp.add(pb);
let progress_bar = Arc::new(Mutex::new(added_pb));
let has_errors = Arc::new(AtomicBool::new(false));
{
let thread_pool = ThreadPool::new(num_threads);
for command in commands.clone() {
let c = config.clone();
let errors = Arc::clone(&has_errors);
let progress = Arc::clone(&progress_bar);
let task = task_name.clone();
let run = move || {
let p = progress.lock().unwrap();
p.set_message(format!(
"⏳ {}: {}",
&task,
White.bold().paint(&command.name)
));
let resolved_command = get_command(&command.name);
if resolved_command.is_err() {
error!(
"{} {} {}",
Red.paint("Command"),
White.on(Red).paint(format!(" {} ", command.name)),
Red.paint("not found")
);
errors.store(true, Ordering::Relaxed);
p.set_message(format!(
"❌ {}: {}",
Red.paint(&task),
Red.paint(&command.name)
));
p.inc(1);
drop(p);
return;
}
let result = run_command(
resolved_command.unwrap(),
command.args.clone(),
&mode,
&c,
&p,
);
if let Err(err_result) = result {
error!(
"{}: {}",
White.bold().paint(&command.name),
Red.paint("ERROR")
);
err_result.split('\n').for_each(|err| {
error!("{} {}", Red.bold().paint("|>"), Red.paint(err))
});
errors.store(true, Ordering::Relaxed);
}
p.set_message(format!(
"✅ {}: {}",
Green.paint(&task),
Green.paint(&command.name)
));
p.inc(1);
drop(p);
};
thread_pool.execute(run);
}
}
if has_errors.load(Ordering::Relaxed) {
progress_bar.lock().unwrap().finish_with_message(format!(
"❌ {} ➡️ {}",
Red.paint(&task_name),
Red.bold().paint("ERR")
));
Err(format!("{}", Red.paint("Task has errors")))
} else {
progress_bar.lock().unwrap().finish_with_message(format!(
"✅ {} ➡️ {}",
Green.paint(&task_name),
Green.bold().paint("OK")
));
Ok(())
}
}
}
pub fn get_task_names(tasks: &[Task]) -> Vec<String> {
let mut task_names = Vec::new();
for task in tasks {
task_names.push(task.name.clone());
}
task_names
}
pub fn select_task(tasks: &[Task]) -> Option<String> {
println!("\n{}:", White.bold().paint("Select a task"));
let task_names = get_task_names(tasks);
let selection = Select::with_theme(&ColorfulTheme::default())
.items(&task_names)
.default(0)
.interact_on_opt(&Term::stderr());
if let Ok(Some(selected_index)) = selection {
return Some(task_names[selected_index].clone());
}
None
}
pub fn should_skip_task(task: &Task) -> bool {
if task.os.is_empty() {
return false;
}
let current_os = Os::from_str(env::consts::OS).unwrap();
!task.os.contains(¤t_os)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn it_gets_list_of_tasks() {
let tasks = vec![
Task {
name: "task1".to_string(),
commands: vec![],
os: vec![],
parallel: false,
},
Task {
name: "task2".to_string(),
commands: vec![],
os: vec![],
parallel: false,
},
];
let task_names = get_task_names(&tasks);
assert_eq!(task_names, vec!["task1", "task2"]);
}
#[cfg(target_os = "linux")]
#[test]
fn it_only_runs_task_for_specific_os() {
let task_linux = Task {
os: vec![Os::Linux],
name: String::from("my-linux-task"),
commands: vec![],
parallel: false,
};
assert!(!should_skip_task(&task_linux));
let task_win = Task {
os: vec![Os::Windows],
name: String::from("my-linux-task"),
commands: vec![],
parallel: false,
};
assert!(should_skip_task(&task_win));
}
}