Browse Source

cli: Add support for simple wildcard patterns in Anchor.toml's `workspace.members` and `workspace.exclude` (#2785)

Co-authored-by: acheron <98934430+acheroncrypto@users.noreply.github.com>
Gabriele Picco 1 year ago
parent
commit
169264d730
2 changed files with 36 additions and 29 deletions
  1. 1 0
      CHANGELOG.md
  2. 35 29
      cli/src/config.rs

+ 1 - 0
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
 

+ 35 - 29
cli/src/config.rs

@@ -300,37 +300,43 @@ impl WithPath<Config> {
     }
 
     pub fn canonicalize_workspace(&self) -> Result<(Vec<PathBuf>, Vec<PathBuf>)> {
-        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<Vec<PathBuf>, 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<PathBuf, Error> {
+        path.canonicalize().map_err(|e| {
+            Error::new(io::Error::new(
+                io::ErrorKind::Other,
+                format!("Error canonicalizing path {:?}: {}", path, e),
+            ))
+        })
     }
 }