Skip to content
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

feat(run): match tasks to run with glob patterns #1528

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ eyre = "0.6.11"
filetime = "0.2.23"
flate2 = "1.0.28"
fslock = "0.2.1"
globset = "0.4.14"
globwalk = "0.9.1"
home = "0.5.9"
humantime = "2.1.0"
Expand Down
39 changes: 35 additions & 4 deletions src/cli/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use console::Color;
use demand::{DemandOption, Select};
use duct::IntoExecutablePath;
use eyre::Result;
use globset::Glob;
use globwalk::GlobWalkerBuilder;
use itertools::Itertools;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -134,11 +135,21 @@ impl Run {
}
})
.flat_map(|args| args.split_first().map(|(t, a)| (t.clone(), a.to_vec())))
.map(|(t, args)| match config.tasks_with_aliases().get(&t) {
Some(task) => Ok(task.clone().with_args(args.to_vec())),
None if t == "default" => self.prompt_for_task(config),
None => bail!("no task {} found", style::ered(t)),
.map(|(t, args)| {
let tasks = config.tasks_with_aliases().get_matching(&t)?;
if tasks.is_empty() {
ensure!(t == "default", "no task {} found", style::ered(t));

Ok(vec![self.prompt_for_task(config)?])
} else {
Ok(tasks
.iter()
.cloned()
.map(|t| t.clone().with_args(args.to_vec()))
.collect())
}
})
.flatten_ok()
Ajpantuso marked this conversation as resolved.
Show resolved Hide resolved
.collect()
}

Expand Down Expand Up @@ -521,6 +532,26 @@ fn get_color() -> Color {
static COLOR_IDX: AtomicUsize = AtomicUsize::new(0);
COLORS[COLOR_IDX.fetch_add(1, Ordering::Relaxed) % COLORS.len()]
}
trait GetMatchingExt<T> {
fn get_matching(&self, pat: &str) -> Result<Vec<&T>>;
}

impl<T> GetMatchingExt<T> for std::collections::HashMap<String, T> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we go with BTreeMap? I think the output will look a little cleaner being not randomized by HashMap

IndexMap might also be a good idea but I'm not sure what order the tasks are in

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that decision would really have to be made here, but I can definitely add sorting to guarantee ordering.

Copy link
Owner

@jdx jdx Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

up to you, could be in a different PR if you want. I'm not sure how many places that would need to change off the top of my head

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lol, played around with this a bit only to realize parallelization ends up randomizing the output ordering anyway. So I think we can defer the decision for now since ordering is only relevant for single threaded runs (which I imagine is the less likely use case).

fn get_matching(&self, pat: &str) -> Result<Vec<&T>> {
let normalized = pat.split(':').collect::<PathBuf>();
let matcher = Glob::new(&normalized.to_string_lossy())?.compile_matcher();

Ok(self
.keys()
.filter(|k| {
let p: PathBuf = k.split(':').collect();

matcher.is_match(p)
})
.flat_map(|k| self.get(k))
.collect())
}
}

#[cfg(test)]
mod tests {
Expand Down
Loading