diff --git a/CHANGELOG.md b/CHANGELOG.md index abf9463bb1..af254fb251 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Check `anchor-lang` and CLI version compatibility ([#2753](https://github.com/coral-xyz/anchor/pull/2753)). - ts: Add IdlSeed Type for IDL PDA seeds ([#2752](https://github.com/coral-xyz/anchor/pull/2752)). - cli: `idl close` accepts optional `--idl-address` parameter ([#2760](https://github.com/coral-xyz/anchor/pull/2760)). +- cli: Add support for simple wildcard patterns in Anchor.toml's `workspace.members` and `workspace.exclude`. ([#2785](https://github.com/coral-xyz/anchor/pull/2785)). ### Fixes diff --git a/cli/src/config.rs b/cli/src/config.rs index 4bed3a9ee6..efce7fd0ad 100644 --- a/cli/src/config.rs +++ b/cli/src/config.rs @@ -300,37 +300,43 @@ impl WithPath { } pub fn canonicalize_workspace(&self) -> Result<(Vec, Vec)> { - let members = self - .workspace - .members - .iter() - .map(|m| { - self.path() - .parent() - .unwrap() - .join(m) - .canonicalize() - .unwrap_or_else(|_| { - panic!("Error reading workspace.members. File {:?} does not exist at path {:?}.", m, self.path) - }) - }) - .collect(); - let exclude = self - .workspace - .exclude + let members = self.process_paths(&self.workspace.members)?; + let exclude = self.process_paths(&self.workspace.exclude)?; + Ok((members, exclude)) + } + + fn process_paths(&self, paths: &[String]) -> Result, Error> { + let base_path = self.path().parent().unwrap(); + paths .iter() - .map(|m| { - self.path() - .parent() - .unwrap() - .join(m) - .canonicalize() - .unwrap_or_else(|_| { - panic!("Error reading workspace.exclude. File {:?} does not exist at path {:?}.", m, self.path) - }) + .flat_map(|m| { + let path = base_path.join(m); + if m.ends_with("/*") { + let dir = path.parent().unwrap(); + match fs::read_dir(dir) { + Ok(entries) => entries + .filter_map(|entry| entry.ok()) + .map(|entry| self.process_single_path(&entry.path())) + .collect(), + Err(e) => vec![Err(Error::new(io::Error::new( + io::ErrorKind::Other, + format!("Error reading directory {:?}: {}", dir, e), + )))], + } + } else { + vec![self.process_single_path(&path)] + } }) - .collect(); - Ok((members, exclude)) + .collect() + } + + fn process_single_path(&self, path: &PathBuf) -> Result { + path.canonicalize().map_err(|e| { + Error::new(io::Error::new( + io::ErrorKind::Other, + format!("Error canonicalizing path {:?}: {}", path, e), + )) + }) } }