-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
112 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use serde_json::Value; | ||
use std::process::Command; | ||
|
||
pub(crate) enum WorkspaceMemberType { | ||
Crate, | ||
Example, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct WorkspaceMember { | ||
pub name: String, | ||
pub path: String, | ||
} | ||
|
||
impl WorkspaceMember { | ||
fn new(name: String, path: String) -> Self { | ||
Self { name, path } | ||
} | ||
} | ||
|
||
// Get project workspaces | ||
pub(crate) fn get_workspaces(w_type: WorkspaceMemberType) -> Vec<WorkspaceMember> { | ||
// Run `cargo metadata` command to get project metadata | ||
let output = Command::new("cargo") | ||
.arg("metadata") | ||
.output() | ||
.expect("Failed to execute command"); | ||
|
||
// Parse the JSON output | ||
let metadata: Value = serde_json::from_slice(&output.stdout).expect("Failed to parse JSON"); | ||
|
||
// Extract workspaces from the metadata, excluding examples/ and xtask | ||
let workspaces = metadata["workspace_members"] | ||
.as_array() | ||
.expect("Expected an array of workspace members") | ||
.iter() | ||
.filter_map(|member| { | ||
let parts: Vec<_> = member.as_str()?.split_whitespace().collect(); | ||
let (workspace_name, workspace_path) = | ||
(parts.first()?.to_owned(), parts.last()?.to_owned()); | ||
|
||
let workspace_path = workspace_path.replace("(path+file://", "").replace(')', ""); | ||
|
||
match w_type { | ||
WorkspaceMemberType::Crate | ||
if workspace_name != "xtask" && !workspace_path.contains("examples/") => | ||
{ | ||
Some(WorkspaceMember::new( | ||
workspace_name.to_string(), | ||
workspace_path.to_string(), | ||
)) | ||
} | ||
WorkspaceMemberType::Example | ||
if workspace_name != "xtask" && workspace_path.contains("examples/") => | ||
{ | ||
Some(WorkspaceMember::new( | ||
workspace_name.to_string(), | ||
workspace_path.to_string(), | ||
)) | ||
} | ||
_ => None, | ||
} | ||
}) | ||
.collect(); | ||
|
||
workspaces | ||
} |