From 8a84b5b7f57a214cc7ba1188ce9297a20596ace1 Mon Sep 17 00:00:00 2001 From: Andrew Pantuso Date: Fri, 26 Jan 2024 09:44:57 -0500 Subject: [PATCH] feat(tasks): unify glob strategy for tasks and dependencies --- src/cli/run.rs | 24 +----------------------- src/task.rs | 47 ++++++++++++++++++++++++++++++++++++----------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/cli/run.rs b/src/cli/run.rs index 31424907c..e768d363b 100644 --- a/src/cli/run.rs +++ b/src/cli/run.rs @@ -13,7 +13,6 @@ 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; @@ -23,7 +22,7 @@ use crate::config::{Config, Settings}; use crate::errors::Error; use crate::errors::Error::ScriptFailed; use crate::file::display_path; -use crate::task::{Deps, Task}; +use crate::task::{Deps, GetMatchingExt, Task}; use crate::toolset::{InstallOptions, ToolsetBuilder}; use crate::ui::ctrlc; use crate::ui::style; @@ -532,27 +531,6 @@ fn get_color() -> Color { static COLOR_IDX: AtomicUsize = AtomicUsize::new(0); COLORS[COLOR_IDX.fetch_add(1, Ordering::Relaxed) % COLORS.len()] } -trait GetMatchingExt { - fn get_matching(&self, pat: &str) -> Result>; -} - -impl GetMatchingExt for std::collections::HashMap { - fn get_matching(&self, pat: &str) -> Result> { - let normalized = pat.split(':').collect::(); - 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 { use crate::file; diff --git a/src/task.rs b/src/task.rs index 5c748837b..0084495c6 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,3 +1,4 @@ +use globset::Glob; use petgraph::graph::DiGraph; use petgraph::prelude::*; use petgraph::{Direction, Graph}; @@ -134,17 +135,7 @@ impl Task { let depends = self .depends .iter() - .map(|name| match name.strip_suffix('*') { - Some(prefix) => Ok(tasks - .values() - .unique() - .filter(|t| *t != self && t.name.starts_with(prefix)) - .collect::>()), - None => tasks - .get(name) - .map(|task| vec![task]) - .ok_or_else(|| eyre!("task not found: {name}")), - }) + .map(|pat| match_tasks(tasks, pat)) .collect::>>()? .into_iter() .flatten() @@ -169,6 +160,15 @@ fn name_from_path(root: impl AsRef, path: impl AsRef) -> Result(tasks: &'a HashMap, pat: &str) -> Result> { + let matches = tasks.get_matching(pat)?; + if matches.is_empty() { + return Err(eyre!("task not found: {pat}")); + }; + + Ok(matches) +} + impl Display for Task { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { if let Some(cmd) = self.command_string() { @@ -368,3 +368,28 @@ mod tests { } } } + +pub trait GetMatchingExt { + fn get_matching(&self, pat: &str) -> Result>; +} + +impl GetMatchingExt for std::collections::HashMap +where + T: std::cmp::Eq + std::hash::Hash, +{ + fn get_matching(&self, pat: &str) -> Result> { + let normalized = pat.split(':').collect::(); + let matcher = Glob::new(&normalized.to_string_lossy())?.compile_matcher(); + + Ok(self + .iter() + .filter(|(k, _)| { + let p: PathBuf = k.split(':').collect(); + + matcher.is_match(p) + }) + .map(|(_, t)| t) + .unique() + .collect()) + } +}