Skip to content

Commit f7bfa70

Browse files
committed
add workspace
1 parent 6790db0 commit f7bfa70

File tree

9 files changed

+143
-35
lines changed

9 files changed

+143
-35
lines changed

examples/workplace/Cargo.lock

Lines changed: 8 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/workplace/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[workspace]
22
resolver = "2"
3-
members = ["simple-example"]
3+
members = ["simple-example", "not-default-example"]
44
default-members = ["simple-example"]
55

66
[workspace.package]
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+
}

examples/workplace/simple-example/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<base data-trunk-public-url />
99
</head>
1010
<body>
11-
<h1>Trunk without WASM</h1>
11+
<h1>Trunk without WASM in workspace</h1>
1212
</body>
1313
</html>

src/config/models/mod.rs

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use anyhow::{bail, Context, Result};
3030
use schemars::JsonSchema;
3131
use serde::Deserialize;
3232
use source::{workspace, Source};
33-
use std::path::PathBuf;
33+
use std::path::{Path, PathBuf};
3434
use tracing::log;
3535

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

117-
pub async fn load_workspace_config(path: &PathBuf) -> Result<Option<PathBuf>> {
118-
let cargo_toml = path.clone().join("Cargo.toml");
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");
119122
if cargo_toml.exists() {
120-
if let Ok(workspace) = workspace::workspace_from_manifest(cargo_toml).await {
121-
if let Some(workspace) = workspace.get_default_workspace() {
122-
// get the parent directory of the workspace
123-
let workspace = workspace
124-
.parent()
125-
.context("unable to get parent directory of workspace")?;
126-
return Ok(Some(workspace.to_path_buf()));
123+
if let Ok(workspace) = workspace::workspace_from_manifest(&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+
}
127164
}
128165
}
129166
}
@@ -152,25 +189,12 @@ pub async fn load(path: Option<PathBuf>) -> Result<(Configuration, PathBuf)> {
152189
Ok((Source::File(path).load().await?, cwd))
153190
}
154191
// if we have a directory, try finding a file and load it
155-
Some(path) if path.is_dir() => {
156-
let cwd = if let Some(new_cwd) = load_workspace_config(&path).await? {
157-
new_cwd
158-
} else {
159-
path.clone()
160-
};
161-
162-
Ok((Source::find(&path)?.load().await?, cwd))
163-
}
192+
Some(path) if path.is_dir() => Ok((Source::find(&path)?.load().await?, path)),
164193
// if we have something else, we can't deal with it
165194
Some(path) => bail!("{} is neither a file nor a directory", path.display()),
166195
// if we have nothing, try to find a file in the current directory and load it
167196
None => {
168197
let cwd = std::env::current_dir().context("unable to get current directory")?;
169-
let cwd = if let Some(new_cwd) = load_workspace_config(&cwd).await? {
170-
new_cwd
171-
} else {
172-
cwd
173-
};
174198
Ok((Source::find(&cwd)?.load().await?, cwd))
175199
}
176200
}

src/config/models/source/workspace.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ impl WorkspaceConfig {
1717
.await
1818
.context("error awaiting spawned cargo metadata task")?
1919
.context("error getting cargo metadata")?;
20-
return Ok(Self { metadata });
20+
Ok(Self { metadata })
2121
}
2222

2323
pub fn get_default_workspace(self) -> Option<PathBuf> {
24-
if let Some(default_members) = self.metadata.workspace_default_members.get(0) {
24+
if let Some(default_members) = self.metadata.workspace_default_members.first() {
2525
if let Some(found) = self
2626
.metadata
2727
.packages
@@ -33,6 +33,23 @@ impl WorkspaceConfig {
3333
}
3434
None
3535
}
36+
37+
pub fn get_workspace_by_name(self, name: &str) -> Option<PathBuf> {
38+
// we search for the package in the workspace packages list
39+
if let Some(one_package) = self.metadata.packages.iter().find(|m| m.name == name) {
40+
// we check if the package is present in the workspace members list
41+
if self
42+
.metadata
43+
.workspace_members
44+
.into_iter()
45+
.any(|p| p == one_package.id)
46+
{
47+
// we return the manifest path of the package
48+
return Some(one_package.manifest_path.clone().into());
49+
}
50+
}
51+
None
52+
}
3653
}
3754

3855
/// Load the trunk configuration from the cargo manifest

src/main.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod ws;
1919
use anyhow::{Context, Result};
2020
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
2121
use common::STARTING;
22+
use config::models::load_workspace_config;
2223
use std::io::IsTerminal;
2324
use std::path::PathBuf;
2425
use std::process::ExitCode;
@@ -144,6 +145,9 @@ struct Trunk {
144145
/// Support for `NO_COLOR` environment variable
145146
#[arg(long, env = "NO_COLOR", global(true))]
146147
pub no_color: bool,
148+
149+
#[arg(long = "workspace", env = "TRUNK_CRATE_WORKSPACE")]
150+
pub crate_workspace: Option<String>,
147151
}
148152

149153
impl Trunk {
@@ -174,13 +178,26 @@ impl Trunk {
174178
pub async fn run(self) -> Result<()> {
175179
version::update_check(self.skip_version_check | self.offline.unwrap_or_default());
176180

181+
let cwd = std::env::current_dir().context("unable to get current directory")?;
182+
let workspace_config = load_workspace_config(&cwd, self.crate_workspace).await?;
183+
184+
let config_path = if let Some(config_path_from_workspace) = workspace_config {
185+
tracing::info!(
186+
"Loading config from {}",
187+
config_path_from_workspace.display()
188+
);
189+
Some(config_path_from_workspace)
190+
} else {
191+
self.config
192+
};
193+
177194
match self.action {
178-
TrunkSubcommands::Build(inner) => inner.run(self.config).await,
179-
TrunkSubcommands::Clean(inner) => inner.run(self.config).await,
180-
TrunkSubcommands::Serve(inner) => inner.run(self.config).await,
181-
TrunkSubcommands::Watch(inner) => inner.run(self.config).await,
182-
TrunkSubcommands::Config(inner) => inner.run(self.config).await,
183-
TrunkSubcommands::Tools(inner) => inner.run(self.config).await,
195+
TrunkSubcommands::Build(inner) => inner.run(config_path).await,
196+
TrunkSubcommands::Clean(inner) => inner.run(config_path).await,
197+
TrunkSubcommands::Serve(inner) => inner.run(config_path).await,
198+
TrunkSubcommands::Watch(inner) => inner.run(config_path).await,
199+
TrunkSubcommands::Config(inner) => inner.run(config_path).await,
200+
TrunkSubcommands::Tools(inner) => inner.run(config_path).await,
184201
}
185202
}
186203
}

0 commit comments

Comments
 (0)