Skip to content

Commit

Permalink
cli: Add support for simple wildcard patterns in Anchor.toml's `works…
Browse files Browse the repository at this point in the history
…pace.members` and `workspace.exclude` (#2785)

Co-authored-by: acheron <[email protected]>
  • Loading branch information
GabrielePicco and acheroncrypto authored Jan 25, 2024
1 parent ef3b149 commit 169264d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 35 additions & 29 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
))
})
}
}

Expand Down

1 comment on commit 169264d

@vercel
Copy link

@vercel vercel bot commented on 169264d Jan 25, 2024

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-lang.com
www.anchor-lang.com
anchor-docs-200ms.vercel.app

Please sign in to comment.