Skip to content

Commit f1a4c86

Browse files
committed
feat: add support for cargo workspaces
1 parent e221ba3 commit f1a4c86

File tree

12 files changed

+383
-8
lines changed

12 files changed

+383
-8
lines changed

examples/workplace/Cargo.lock

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/workplace/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[workspace]
2+
resolver = "2"
3+
members = ["simple-example", "not-default-example"]
4+
default-members = ["simple-example"]
5+
6+
[workspace.package]
7+
edition = "2021"
8+
license = "MIT OR Apache-2.0"
9+
rust-version = "1.76"
10+
version = "0.27.2"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "not-default-example"
3+
version = "0.1.0"
4+
authors = ["Jens Reimann <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
web-sys = { version = "0.3", features = [
9+
"console",
10+
"Document",
11+
"HtmlElement",
12+
"Node",
13+
"Text",
14+
"Window",
15+
] }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>Trunk | No-Rust</title>
7+
8+
<base data-trunk-public-url />
9+
</head>
10+
<body>
11+
<h1>Trunk without WASM in workspace (not default target)</h1>
12+
</body>
13+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use web_sys::window;
2+
3+
fn start_app() {
4+
let document = window()
5+
.and_then(|win| win.document())
6+
.expect("Could not access document");
7+
let body = document.body().expect("Could not access document.body");
8+
let text_node = document.create_text_node("Hello, world from Vanilla Rust!");
9+
body.append_child(text_node.as_ref())
10+
.expect("Failed to append text");
11+
}
12+
13+
fn main() {
14+
start_app();
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "simple-example"
3+
version = "0.1.0"
4+
authors = ["Jens Reimann <[email protected]>"]
5+
edition = "2021"
6+
7+
[dependencies]
8+
web-sys = { version = "0.3", features = [
9+
"console",
10+
"Document",
11+
"HtmlElement",
12+
"Node",
13+
"Text",
14+
"Window",
15+
] }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1" />
6+
<title>Trunk | No-Rust</title>
7+
8+
<base data-trunk-public-url />
9+
</head>
10+
<body>
11+
<h1>Trunk without WASM in workspace</h1>
12+
</body>
13+
</html>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use web_sys::window;
2+
3+
fn start_app() {
4+
let document = window()
5+
.and_then(|win| win.document())
6+
.expect("Could not access document");
7+
let body = document.body().expect("Could not access document.body");
8+
let text_node = document.create_text_node("Hello, world from Vanilla Rust!");
9+
body.append_child(text_node.as_ref())
10+
.expect("Failed to append text");
11+
}
12+
13+
fn main() {
14+
start_app();
15+
}

src/config/models/mod.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ mod test;
2929
use anyhow::{bail, Context, Result};
3030
use schemars::JsonSchema;
3131
use serde::Deserialize;
32-
use source::Source;
33-
use std::path::PathBuf;
32+
use source::{workspace::WorkspaceConfig, Source};
33+
use std::path::{Path, PathBuf};
3434
use tracing::log;
3535

3636
/// Common configuration model functionality
@@ -114,6 +114,59 @@ impl ConfigModel for Configuration {
114114
}
115115
}
116116

117+
pub async fn load_workspace_config(
118+
current_path: &Path,
119+
workspace_name: Option<String>,
120+
) -> Result<Option<PathBuf>> {
121+
let cargo_toml = current_path.join("Cargo.toml");
122+
if cargo_toml.exists() {
123+
if let Ok(workspace) = WorkspaceConfig::new(&cargo_toml).await {
124+
match workspace_name {
125+
Some(name) => {
126+
if let Some(workspace) = workspace.get_workspace_by_name(&name) {
127+
// get the parent directory of the workspace
128+
let workspace = match workspace.parent() {
129+
Some(parent) => parent,
130+
None => {
131+
return Err(anyhow::format_err!(
132+
"unable to get parent directory of workspace '{}'",
133+
workspace.display()
134+
));
135+
}
136+
};
137+
return Ok(Some(workspace.to_path_buf()));
138+
}
139+
return Err(anyhow::format_err!(
140+
"workspace '{}' not found in {}",
141+
name,
142+
cargo_toml.display()
143+
));
144+
}
145+
None => {
146+
if let Some(workspace) = workspace.get_default_workspace() {
147+
// get the parent directory of the workspace
148+
let workspace = match workspace.parent() {
149+
Some(parent) => parent,
150+
None => {
151+
return Err(anyhow::format_err!(
152+
"unable to get parent directory of workspace '{}'",
153+
workspace.display()
154+
));
155+
}
156+
};
157+
return Ok(Some(workspace.to_path_buf()));
158+
}
159+
return Err(anyhow::format_err!(
160+
"default workspace not found in {}",
161+
cargo_toml.display()
162+
));
163+
}
164+
}
165+
}
166+
}
167+
Ok(None)
168+
}
169+
117170
/// Locate and load the configuration, given an optional file or directory. Falling back to the
118171
/// current directory.
119172
pub async fn load(path: Option<PathBuf>) -> Result<(Configuration, PathBuf)> {

src/config/models/source/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod cargo;
2+
pub mod workspace;
23

34
use crate::config::{models::ConfigModel, Configuration};
45
use anyhow::bail;

0 commit comments

Comments
 (0)