-
Notifications
You must be signed in to change notification settings - Fork 124
/
execution_plan.rs
554 lines (480 loc) · 17.5 KB
/
execution_plan.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! # execution_plan
//!
//! Creates execution plan for a given task and makefile.
//!
#[cfg(test)]
#[path = "execution_plan_test.rs"]
mod execution_plan_test;
use crate::environment;
use crate::error::CargoMakeError;
use crate::logger;
use crate::profile;
use crate::proxy_task::create_proxy_task;
use crate::types::{
Config, CrateInfo, EnvValue, ExecutionPlan, ScriptValue, Step, Task, TaskIdentifier, Workspace,
};
use fsio::path::{get_basename, get_parent_directory};
use glob::Pattern;
use indexmap::IndexMap;
use regex::Regex;
use std::collections::HashSet;
use std::env;
use std::path::Path;
/// Resolve aliases recursively until a task without alias is found.
fn get_task_name_recursive(
config: &Config,
name: &str,
seen: &mut Vec<String>,
) -> Result<String, CargoMakeError> {
seen.push(name.to_string());
match config.tasks.get(name) {
Some(task_config) => {
let alias = task_config.get_alias();
match alias {
Some(ref alias) if seen.contains(alias) => {
let chain = seen.join(" -> ");
error!("Detected cycle while resolving alias {}: {}", &name, chain);
return Err(CargoMakeError::AliasCycle(name.to_string(), chain));
}
Some(ref alias) => get_task_name_recursive(config, alias, seen),
None => Ok(name.to_string()),
}
}
None => Err(CargoMakeError::NotFound(format!(
"Task {:#?} not found",
name
))),
}
}
/// Returns the actual task name to invoke as tasks may have aliases.
///
/// # Panics
/// if there is a cycle in the alias chain.
pub(crate) fn get_actual_task_name(config: &Config, name: &str) -> Result<String, CargoMakeError> {
let mut seen = Vec::new();
get_task_name_recursive(config, name, &mut seen)
}
/// Resolves alias and normalizes task.
///
/// # Panics
/// if task is not found or there is a cycle in the alias chain.
pub(crate) fn get_normalized_task(
config: &Config,
name: &str,
support_alias: bool,
) -> Result<Task, CargoMakeError> {
get_optional_normalized_task(config, name, support_alias)
}
/// Resolves alias and normalizes task.
///
/// # Panics
/// if there is a cycle in the alias chain.
fn get_optional_normalized_task(
config: &Config,
name: &str,
support_alias: bool,
) -> Result<Task, CargoMakeError> {
let actual_task_name = if support_alias {
get_actual_task_name(config, name)
} else {
Ok(name.to_string())
}?;
match config.tasks.get(&actual_task_name) {
Some(task_config) => {
let mut clone_task = task_config.clone();
let mut normalized_task = clone_task.get_normalized_task();
normalized_task = match normalized_task.extend {
Some(ref extended_task_name) => {
let mut extended_task =
get_normalized_task(config, extended_task_name, support_alias)?;
if let Some(ref env) = normalized_task.env {
if env.len() == 2
&& env.contains_key("CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE")
&& env
.contains_key("CARGO_MAKE_CURRENT_TASK_INITIAL_MAKEFILE_DIRECTORY")
{
normalized_task.env = None;
}
}
extended_task.extend(&normalized_task);
extended_task
}
None => normalized_task,
};
Ok(normalized_task)
}
None => Err(CargoMakeError::NotFound(format!(
"Task {:#?} not found",
name
))),
}
}
fn get_workspace_members_config(members_config: String) -> HashSet<String> {
let mut members = HashSet::new();
let members_list: Vec<&str> = members_config.split(';').collect();
for member in members_list.iter() {
if member.len() > 0 {
members.insert(member.to_string());
}
}
members
}
fn is_workspace_member_found(member: &str, members_map: &HashSet<String>) -> bool {
if members_map.contains(member) {
true
} else {
// search for globs
let mut found = false;
for member_iter in members_map {
if member_iter.contains("*") {
found = match Pattern::new(member_iter) {
Ok(pattern) => pattern.matches(&member),
_ => false,
};
if found {
break;
}
}
}
found
}
}
fn should_skip_workspace_member(member: &str, skipped_members: &HashSet<String>) -> bool {
is_workspace_member_found(member, skipped_members)
}
fn should_include_workspace_member(member: &str, include_members: &HashSet<String>) -> bool {
if include_members.is_empty() {
true
} else {
is_workspace_member_found(member, include_members)
}
}
fn filter_workspace_members(members: &Vec<String>) -> Vec<String> {
let skip_members_config = envmnt::get_or("CARGO_MAKE_WORKSPACE_SKIP_MEMBERS", "");
let skip_members = get_workspace_members_config(skip_members_config);
let include_members_config = envmnt::get_or("CARGO_MAKE_WORKSPACE_INCLUDE_MEMBERS", "");
let include_members = get_workspace_members_config(include_members_config);
let mut filtered_members = vec![];
for member in members {
if !should_skip_workspace_member(&member, &skip_members)
&& should_include_workspace_member(&member, &include_members)
{
filtered_members.push(member.to_string());
} else {
debug!("Skipping Member: {}.", &member);
}
}
filtered_members
}
fn create_workspace_task(crate_info: &CrateInfo, task: &str) -> Task {
let set_workspace_emulation = crate_info.workspace.is_none()
&& envmnt::is("CARGO_MAKE_WORKSPACE_EMULATION")
&& !envmnt::exists("CARGO_MAKE_WORKSPACE_EMULATION_ROOT_DIRECTORY");
if set_workspace_emulation {
environment::search_and_set_workspace_cwd();
let root_directory = envmnt::get_or_panic("CARGO_MAKE_WORKSPACE_WORKING_DIRECTORY");
envmnt::set(
"CARGO_MAKE_WORKSPACE_EMULATION_ROOT_DIRECTORY",
&root_directory,
);
}
let members = if crate_info.workspace.is_some() {
let workspace_clone = crate_info.workspace.clone();
let workspace = workspace_clone.unwrap_or(Workspace::new());
workspace.members.unwrap_or(vec![])
} else {
envmnt::get_list("CARGO_MAKE_CRATE_WORKSPACE_MEMBERS").unwrap_or(vec![])
};
let log_level = logger::get_log_level();
let profile_name = if envmnt::is_or("CARGO_MAKE_USE_WORKSPACE_PROFILE", true) {
profile::get()
} else {
profile::default_profile()
};
let filtered_members = filter_workspace_members(&members);
let mut script_lines = vec![];
if !filtered_members.is_empty() {
let cargo_make_command = "cargo make";
script_lines.push("workspace_directory = pwd".to_string());
for member in &filtered_members {
let mut cd_line = "cd ./".to_string();
cd_line.push_str(&member.replace("\\", "/"));
script_lines.push(cd_line);
//get member name
let member_name = match Path::new(&member).file_name() {
Some(name) => String::from(name.to_string_lossy()),
None => member.clone(),
};
debug!("Adding Member: {} Path: {}", &member_name, &member);
let mut make_line = "exec --fail-on-error ".to_string();
make_line.push_str(&cargo_make_command);
make_line
.push_str(" --disable-check-for-updates --allow-private --no-on-error --loglevel=");
make_line.push_str(&log_level);
make_line.push_str(" --env CARGO_MAKE_CRATE_CURRENT_WORKSPACE_MEMBER=");
make_line.push_str(&member_name);
make_line.push_str(" --profile ");
make_line.push_str(&profile_name);
make_line.push_str(" -- ");
make_line.push_str(&task);
if let Some(args) = envmnt::get_list("CARGO_MAKE_TASK_ARGS") {
for arg in args {
make_line.push_str(" ");
make_line.push_str(&arg);
}
}
script_lines.push(make_line);
script_lines.push("cd ${workspace_directory}".to_string());
}
}
//only if environment variable is set
let task_env = if envmnt::is_or("CARGO_MAKE_EXTEND_WORKSPACE_MAKEFILE", false) {
match env::var("CARGO_MAKE_MAKEFILE_PATH") {
Ok(makefile) => {
let mut env_map = IndexMap::new();
env_map.insert(
"CARGO_MAKE_WORKSPACE_MAKEFILE".to_string(),
EnvValue::Value(makefile.to_string()),
);
Some(env_map)
}
_ => None,
}
} else {
None
};
debug!("Workspace Task Script: {:#?}", &script_lines);
let mut workspace_task = Task::new();
workspace_task.script_runner = Some("@duckscript".to_string());
workspace_task.script = Some(ScriptValue::Text(script_lines));
workspace_task.env = task_env;
workspace_task
}
fn is_workspace_flow(
config: &Config,
task: &str,
disable_workspace: bool,
crate_info: &CrateInfo,
sub_flow: bool,
) -> bool {
// determine if workspace flow is explicitly set and enabled in the requested task
let (task_set_workspace, task_enable_workspace) =
match get_optional_normalized_task(config, task, true) {
Ok(normalized_task) => match normalized_task.workspace {
Some(enable_workspace) => (true, enable_workspace),
None => (false, false),
},
Err(_) => (false, false),
};
// if project is not a workspace or if workspace is disabled via cli, return no workspace flow
if disable_workspace
|| (sub_flow && !task_enable_workspace)
|| (crate_info.workspace.is_none() && !envmnt::is("CARGO_MAKE_WORKSPACE_EMULATION"))
|| envmnt::exists("CARGO_MAKE_CRATE_CURRENT_WORKSPACE_MEMBER")
{
false
} else {
// project is a workspace and wasn't disabled via cli, need to check requested task
// use requested task's workspace flag if set
if task_set_workspace {
task_enable_workspace
} else {
// use configured default workspace flag if set
config.config.default_to_workspace.unwrap_or(true)
}
}
}
/// Creates an execution plan for the given step based on existing execution plan data
fn create_for_step(
config: &Config,
task: &TaskIdentifier,
steps: &mut Vec<Step>,
task_names: &mut HashSet<String>,
root: bool,
allow_private: bool,
skip_tasks_pattern: Option<&Regex>,
) -> Result<(), CargoMakeError> {
if let Some(skip_tasks_pattern_regex) = skip_tasks_pattern {
if skip_tasks_pattern_regex.is_match(&task.name) {
debug!("Skipping task: {} due to skip pattern.", &task.name);
return Ok(());
}
}
if let Some(path) = &task.path {
// this is referring to a task in another file
// so we create a proxy task to invoke it
let proxy_name = format!("{}_proxy", task.name);
let path_obj = Path::new(path);
let (working_directory, makefile) = if path_obj.is_file() {
let filename = get_basename(&path_obj);
let working_directory = get_parent_directory(&path_obj);
(working_directory, filename)
} else {
(Some(path.to_string()), Some("Makefile.toml".to_string()))
};
let mut proxy_task = create_proxy_task(&task.name, true, false, makefile, None);
proxy_task.cwd = working_directory;
let step = Step {
name: proxy_name,
config: proxy_task,
};
debug!("Created external dependency step: {:#?}", &step);
steps.push(step);
task_names.insert(task.to_string());
return Ok(());
}
let task_config = get_normalized_task(config, &task.name, true)?;
debug!("Normalized Task: {} config: {:#?}", &task, &task_config);
let is_private = match task_config.private {
Some(value) => value,
None => false,
};
if allow_private || !is_private {
let add = !task_config.disabled.unwrap_or(false);
if add {
match task_config.dependencies {
Some(ref dependencies) => {
for dependency in dependencies {
create_for_step(
&config,
&dependency.to_owned().into(),
steps,
task_names,
false,
true,
skip_tasks_pattern,
)?;
}
}
_ => debug!("No dependencies found for task: {}", &task),
};
if !task_names.contains(&task.name) {
steps.push(Step {
name: task.to_string(),
config: task_config,
});
task_names.insert(task.to_string());
} else if root {
return Err(CargoMakeError::CircularReference(format!("{}", task)));
}
}
Ok(())
} else {
error!("Task {} is private", &task);
return Err(CargoMakeError::TaskIs(format!("{}", task), "private"));
}
}
fn add_predefined_step(
config: &Config,
task: &str,
steps: &mut Vec<Step>,
) -> Result<(), CargoMakeError> {
let task_config = get_normalized_task(config, task, false)?;
let add = !task_config.disabled.unwrap_or(false);
if add {
steps.push(Step {
name: task.to_string(),
config: task_config,
});
}
Ok(())
}
#[derive(Clone, Debug)]
pub(crate) struct ExecutionPlanBuilder<'a> {
pub config: &'a Config,
pub task: &'a str,
pub crate_info: Option<&'a CrateInfo>,
pub disable_workspace: bool,
pub allow_private: bool,
pub sub_flow: bool,
pub skip_tasks_pattern: Option<&'a Regex>,
pub skip_init_end_tasks: bool,
}
impl<'a> ExecutionPlanBuilder<'a> {
pub fn new(config: &'a Config, task: &'a str) -> Self {
Self {
config,
task,
crate_info: None,
disable_workspace: false,
allow_private: false,
sub_flow: false,
skip_tasks_pattern: None,
skip_init_end_tasks: false,
}
}
pub fn build(&self) -> Result<ExecutionPlan, CargoMakeError> {
let Self {
config,
task,
crate_info,
disable_workspace,
allow_private,
sub_flow,
skip_tasks_pattern,
skip_init_end_tasks,
} = *self;
let mut task_names = HashSet::new();
let mut steps = Vec::new();
let default_crate_info = CrateInfo::new();
let crate_info = crate_info.unwrap_or(&default_crate_info);
let skip_init_end_tasks = skip_init_end_tasks || sub_flow;
if !skip_init_end_tasks {
match config.config.legacy_migration_task {
Some(ref task) => add_predefined_step(config, task, &mut steps)?,
None => {
return Err(CargoMakeError::NotFound(String::from(
"Legacy migration task not defined.",
)))
}
};
match config.config.init_task {
Some(ref task) => add_predefined_step(config, task, &mut steps)?,
None => {
return Err(CargoMakeError::NotFound(String::from(
"Init task not defined.",
)))
}
};
}
let skip = match skip_tasks_pattern {
Some(pattern) => pattern.is_match(task),
None => false,
};
if !skip {
let workspace_flow =
is_workspace_flow(&config, &task, disable_workspace, &crate_info, sub_flow);
if workspace_flow {
let workspace_task = create_workspace_task(crate_info, task);
steps.push(Step {
name: "workspace".to_string(),
config: workspace_task,
});
} else {
create_for_step(
&config,
&TaskIdentifier::from_name(task),
&mut steps,
&mut task_names,
true,
allow_private,
skip_tasks_pattern,
)?;
}
} else {
debug!("Skipping task: {} due to skip pattern.", &task);
}
if !skip_init_end_tasks {
// always add end task even if already executed due to some dependency
match config.config.end_task {
Some(ref task) => add_predefined_step(config, task, &mut steps)?,
None => {
return Err(CargoMakeError::NotFound(String::from(
"Ent task not defined.",
)))
}
};
}
Ok(ExecutionPlan { steps })
}
}