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

Enables explicit root setting for [tool.rye.workspace] table #1023

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
73 changes: 58 additions & 15 deletions rye/src/pyproject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ impl fmt::Display for Script {
#[derive(Debug)]
pub struct Workspace {
root: PathBuf,
search_root: PathBuf,
doc: DocumentMut,
members: Option<Vec<String>>,
}
Expand All @@ -382,17 +383,38 @@ impl Workspace {
.and_then(|x| x.get("rye"))
.and_then(|x| x.get("workspace"))
.and_then(|x| x.as_table_like())
.map(|workspace| Workspace {
root: path.to_path_buf(),
doc: doc.clone(),
members: workspace
.get("members")
.and_then(|x| x.as_array())
.map(|x| {
x.iter()
.filter_map(|item| item.as_str().map(|x| x.to_string()))
.collect::<Vec<_>>()
}),
.map(|workspace| {
// take explicit search root from tool.rye.workspace.root if
// specified
let search_root = workspace
.get("root")
.and_then(|p| p.as_str())
.and_then(|p| {
let path = PathBuf::from(p).canonicalize();
match path {
Ok(path) => Some(path),
Err(_) => {
// TODO: should this be an error?
warn!("Bad root path {}", p);
None
}
}
})
.unwrap_or_else(|| path.to_path_buf());

Workspace {
root: path.to_path_buf(),
search_root,
doc: doc.clone(),
members: workspace
.get("members")
.and_then(|x| x.as_array())
.map(|x| {
x.iter()
.filter_map(|item| item.as_str().map(|x| x.to_string()))
.collect::<Vec<_>>()
}),
}
})
}

Expand Down Expand Up @@ -427,9 +449,10 @@ impl Workspace {
Cow::Borrowed(&self.root)
}

/// Checks if a project is a member of the declared workspace.
pub fn is_member(&self, path: &Path) -> bool {
if let Ok(relative) = path.strip_prefix(&self.root) {
/// Checks if a project is a member of the declared workspace,
/// relative to a specific root.
fn is_member_rooted(&self, path: &Path, root: &Path) -> bool {
if let Ok(relative) = path.strip_prefix(root) {
if relative == Path::new("") {
true
} else {
Expand Down Expand Up @@ -464,11 +487,31 @@ impl Workspace {
}
}

/// Checks if a project is a member of the declared workspace.
/// Checks current project root first (self.root) then, if it was specified
/// search_root.
pub fn is_member(&self, path: &Path) -> bool {
match self.is_member_rooted(path, &self.root) {
true => true,
false => {
if self.root == self.search_root {
// explicit root wasn't set or is equal to project root
false
} else {
// search explicit root
self.is_member_rooted(path, &self.search_root)
}
}
}
}

/// Iterates through all projects in the workspace.
/// Search is performed relative to explicitly defined root
/// if it was set, otherwise project root.
pub fn iter_projects<'a>(
self: &'a Arc<Self>,
) -> impl Iterator<Item = Result<PyProject, Error>> + 'a {
walkdir::WalkDir::new(&self.root)
walkdir::WalkDir::new(&self.search_root)
.into_iter()
.filter_entry(|entry| {
!(entry.file_type().is_dir() && skip_recurse_into(entry.file_name()))
Expand Down
Loading