-
Notifications
You must be signed in to change notification settings - Fork 130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add before_each_task
and after_each_task
support
#1127
base: master
Are you sure you want to change the base?
Changes from 4 commits
8ec041b
41cf1ce
8b7594e
32eaded
6538cde
335f702
2e826c0
5f28cc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ use fsio::path::{get_basename, get_parent_directory}; | |
use glob::Pattern; | ||
use indexmap::IndexMap; | ||
use regex::Regex; | ||
use std::borrow::Cow; | ||
use std::collections::HashSet; | ||
use std::env; | ||
use std::path::Path; | ||
|
@@ -481,8 +482,8 @@ impl<'a> ExecutionPlanBuilder<'a> { | |
skip_tasks_pattern, | ||
skip_init_end_tasks, | ||
} = *self; | ||
let mut task_names = HashSet::new(); | ||
let mut steps = Vec::new(); | ||
let mut task_names = HashSet::<String>::new(); | ||
let mut steps = Vec::<Step>::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; | ||
|
@@ -549,6 +550,67 @@ impl<'a> ExecutionPlanBuilder<'a> { | |
}; | ||
} | ||
|
||
Ok(ExecutionPlan { steps }) | ||
Ok(ExecutionPlan { | ||
steps: Self::handle_before_each_and_after_each_tasks(&config, &steps)?.into_owned(), | ||
}) | ||
} | ||
|
||
/// `before_each` and `after_each` interspersal for the steps vector | ||
fn handle_before_each_and_after_each_tasks<'b>( | ||
config: &Config, | ||
steps: &'b Vec<Step>, | ||
) -> Result<Cow<'b, Vec<Step>>, CargoMakeError> { | ||
let mut after_and_before_each = Vec::<Step>::with_capacity(2); | ||
let mut handle_some = |name: &String| -> Result<u8, CargoMakeError> { | ||
let task_config = get_normalized_task(config, name, false)?; | ||
if !task_config.disabled.unwrap_or(false) { | ||
after_and_before_each.push(Step { | ||
name: name.to_string(), | ||
config: task_config, | ||
}); | ||
Ok(1) | ||
} else { | ||
Ok(0) | ||
} | ||
}; | ||
let has_after_each: u8 = match &config.config.after_each_task { | ||
None => 0, | ||
Some(after_each) => handle_some(&after_each)?, | ||
}; | ||
let has_before_each: u8 = match &config.config.before_each_task { | ||
None => 0, | ||
Some(before_each) => handle_some(&before_each)?, | ||
}; | ||
let scale: u8 = has_before_each + has_after_each; | ||
if scale > 0 { | ||
let before_and_after_each_len = scale as usize + 1; | ||
let mut interspersed_steps = | ||
Vec::<Step>::with_capacity(steps.len() * before_and_after_each_len); | ||
let before_special = HashSet::from(["init", "init_task"]); | ||
let end_special = HashSet::from(["end", "end_task"]); | ||
SamuelMarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
interspersed_steps.extend(steps.into_iter().flat_map(|e| -> Vec<Step> { | ||
SamuelMarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let mut _steps = Vec::<Step>::with_capacity(before_and_after_each_len + 1); | ||
if before_special.contains(e.name.as_str()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe i'm not following this right but you are only pushing an additional task for the init/end tasks but not for other tasks instead of the opposite? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic is:
With two addenda:
|
||
if has_after_each == 1 { | ||
_steps.push( | ||
unsafe { after_and_before_each.last().unwrap_unchecked() }.to_owned(), | ||
SamuelMarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} else if end_special.contains(e.name.as_str()) { | ||
if has_before_each == 1 { | ||
_steps.push( | ||
unsafe { after_and_before_each.first().unwrap_unchecked() }.to_owned(), | ||
SamuelMarks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
); | ||
} | ||
} else { | ||
_steps.extend(after_and_before_each.clone()); | ||
} | ||
_steps.push(e.to_owned()); | ||
_steps | ||
})); | ||
Ok(Cow::Owned(interspersed_steps)) | ||
} else { | ||
Ok(Cow::Borrowed(&steps)) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what does this loop actually do? what does it mutate?
meaning are you setting config.before_each_task="before_each"? if so why loop? why not just configure it? why the naming magic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't figure out how to else get the
before_each_task
andafter_each_task
to be populated. Tried a bunch of solutions, this is the only one that seemed to make it accessible to future locations (specificallysrc/lib/execution_plan.rs
).