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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#[cfg(test)]
#[path = "./runner_test.rs"]
mod runner_test;
use crate::command;
use crate::condition;
use crate::environment;
use crate::execution_plan::create as create_execution_plan;
use crate::installer;
use crate::logger;
use crate::profile;
use crate::scriptengine;
use crate::types::{
CliArgs, Config, EnvInfo, EnvValue, ExecutionPlan, FlowInfo, RunTaskInfo, RunTaskRoutingInfo,
Step, Task,
};
use indexmap::IndexMap;
use std::env;
use std::time::SystemTime;
fn validate_condition(flow_info: &FlowInfo, step: &Step) -> bool {
condition::validate_condition_for_step(&flow_info, &step)
}
fn get_sub_task_name_for_routing_info(
flow_info: &FlowInfo,
routing_info: &Vec<RunTaskRoutingInfo>,
) -> Option<String> {
let mut task_name = None;
for routing_step in routing_info {
let invoke = condition::validate_conditions(
&flow_info,
&routing_step.condition,
&routing_step.condition_script,
None,
);
if invoke {
task_name = Some(routing_step.name.clone());
break;
}
}
task_name
}
fn run_sub_task_and_report(flow_info: &FlowInfo, sub_task: &RunTaskInfo) -> bool {
let mut sub_flow_info = flow_info.clone();
let task_name = match sub_task {
RunTaskInfo::Name(ref name) => Some(name.to_string()),
RunTaskInfo::Routing(ref routing_info) => {
get_sub_task_name_for_routing_info(&flow_info, routing_info)
}
};
if task_name.is_some() {
sub_flow_info.task = task_name.unwrap();
run_flow(&sub_flow_info, true);
true
} else {
false
}
}
fn run_sub_task(flow_info: &FlowInfo, sub_task: &RunTaskInfo) {
run_sub_task_and_report(&flow_info, &sub_task);
}
fn create_watch_task_name(task: &str) -> String {
let mut watch_task_name = "".to_string();
watch_task_name.push_str(&task);
watch_task_name.push_str("-watch");
watch_task_name
}
fn create_watch_step(task: &str) -> Step {
let watch_task = create_watch_task(&task);
let watch_task_name = create_watch_task_name(&task);
Step {
name: watch_task_name,
config: watch_task,
}
}
fn watch_task(flow_info: &FlowInfo, task: &str) {
let step = create_watch_step(&task);
run_task(&flow_info, &step);
}
fn should_watch(task: &Task) -> bool {
match task.watch {
Some(watch_bool) => {
if watch_bool {
let disable_watch_env = environment::get_env("CARGO_MAKE_DISABLE_WATCH", "FALSE");
disable_watch_env != "TRUE"
} else {
false
}
}
None => false,
}
}
fn run_task(flow_info: &FlowInfo, step: &Step) {
info!("Running Task: {}", &step.name);
if validate_condition(&flow_info, &step) {
if !step.config.is_valid() {
error!(
"Invalid task, contains multiple actions.\n{:#?}",
&step.config
);
}
let profile_name = profile::get();
let env = match step.config.env {
Some(ref env) => env.clone(),
None => IndexMap::new(),
};
environment::set_env(env);
profile::set(&profile_name);
let updated_step = environment::expand_env(&step);
let watch = should_watch(&step.config);
if watch {
watch_task(&flow_info, &step.name);
} else {
installer::install(&updated_step.config);
match step.config.run_task {
Some(ref sub_task) => run_sub_task(&flow_info, sub_task),
None => {
let revert_directory = match step.config.cwd {
Some(ref cwd) => {
if cwd.len() > 0 {
let directory =
environment::get_env("CARGO_MAKE_WORKING_DIRECTORY", "");
environment::setup_cwd(Some(cwd));
directory
} else {
"".to_string()
}
}
None => "".to_string(),
};
let cli_arguments = match flow_info.cli_arguments {
Some(ref args) => args.clone(),
None => vec![],
};
let script_runner_done =
scriptengine::invoke(&updated_step.config, &cli_arguments);
if !script_runner_done {
command::run(&updated_step);
};
match step.config.cwd {
Some(_) => environment::setup_cwd(Some(&revert_directory)),
_ => (),
};
}
};
}
} else {
debug!("Task: {} disabled", &step.name);
}
}
fn run_task_flow(flow_info: &FlowInfo, execution_plan: &ExecutionPlan) {
for step in &execution_plan.steps {
run_task(&flow_info, &step);
}
}
fn create_watch_task(task: &str) -> Task {
let mut task_config = create_proxy_task(&task);
let mut env_map = task_config.env.unwrap_or(IndexMap::new());
env_map.insert(
"CARGO_MAKE_DISABLE_WATCH".to_string(),
EnvValue::Value("TRUE".to_string()),
);
task_config.env = Some(env_map);
let make_args = task_config.args.unwrap();
let make_command = &make_args.join(" ");
let watch_args = vec![
"watch".to_string(),
"-q".to_string(),
"-x".to_string(),
make_command.to_string(),
];
task_config.args = Some(watch_args);
task_config
}
fn create_proxy_task(task: &str) -> Task {
let log_level = logger::get_log_level();
let mut log_level_arg = "--loglevel=".to_string();
log_level_arg.push_str(&log_level);
let profile_name = profile::get();
let mut profile_arg = "--profile=\"".to_string();
profile_arg.push_str(&profile_name);
profile_arg.push_str("\"");
let mut args = vec![
"make".to_string(),
"--disable-check-for-updates".to_string(),
"--no-on-error".to_string(),
log_level_arg.to_string(),
profile_arg.to_string(),
];
match env::var("CARGO_MAKE_MAKEFILE_PATH") {
Ok(makefile_path) => {
if makefile_path.len() > 0 {
let mut makefile_arg = "--makefile=".to_string();
makefile_arg.push_str(&makefile_path);
args.push(makefile_arg.to_string());
}
}
_ => {}
};
args.push(task.to_string());
let mut proxy_task = Task::new();
proxy_task.command = Some("cargo".to_string());
proxy_task.args = Some(args);
proxy_task.get_normalized_task()
}
fn run_flow(flow_info: &FlowInfo, allow_private: bool) {
let execution_plan = create_execution_plan(
&flow_info.config,
&flow_info.task,
flow_info.disable_workspace,
allow_private,
);
debug!("Created execution plan: {:#?}", &execution_plan);
run_task_flow(&flow_info, &execution_plan);
}
fn run_protected_flow(flow_info: &FlowInfo) {
let proxy_task = create_proxy_task(&flow_info.task);
let exit_code = command::run_command(&proxy_task.command.unwrap(), &proxy_task.args, false);
if exit_code != 0 {
match flow_info.config.config.on_error_task {
Some(ref on_error_task) => {
let mut error_flow_info = flow_info.clone();
error_flow_info.disable_on_error = true;
error_flow_info.task = on_error_task.clone();
run_flow(&error_flow_info, false);
}
_ => (),
};
error!("Task error detected, exit code: {}", &exit_code);
}
}
pub(crate) fn run(config: Config, task: &str, env_info: EnvInfo, cli_args: &CliArgs) {
let start_time = SystemTime::now();
let flow_info = FlowInfo {
config,
task: task.to_string(),
env_info,
disable_workspace: cli_args.disable_workspace,
disable_on_error: cli_args.disable_on_error,
cli_arguments: cli_args.arguments.clone(),
};
if flow_info.disable_on_error || flow_info.config.config.on_error_task.is_none() {
run_flow(&flow_info, false);
} else {
run_protected_flow(&flow_info);
}
let time_string = match start_time.elapsed() {
Ok(elapsed) => {
let mut string = " in ".to_string();
string.push_str(&elapsed.as_secs().to_string());
string.push_str(" seconds");
string
}
_ => "".to_string(),
};
info!("Build Done {}.", &time_string);
}