Skip to content

Commit

Permalink
add workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Feb 15, 2025
1 parent 6790db0 commit f7bfa70
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 35 deletions.
9 changes: 8 additions & 1 deletion examples/workplace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/workplace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["simple-example"]
members = ["simple-example", "not-default-example"]
default-members = ["simple-example"]

[workspace.package]
Expand Down
15 changes: 15 additions & 0 deletions examples/workplace/not-default-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "not-default-example"
version = "0.1.0"
authors = ["Jens Reimann <[email protected]>"]
edition = "2021"

[dependencies]
web-sys = { version = "0.3", features = [
"console",
"Document",
"HtmlElement",
"Node",
"Text",
"Window",
] }
13 changes: 13 additions & 0 deletions examples/workplace/not-default-example/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Trunk | No-Rust</title>

<base data-trunk-public-url />
</head>
<body>
<h1>Trunk without WASM in workspace (not default target)</h1>
</body>
</html>
15 changes: 15 additions & 0 deletions examples/workplace/not-default-example/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use web_sys::window;

fn start_app() {
let document = window()
.and_then(|win| win.document())
.expect("Could not access document");
let body = document.body().expect("Could not access document.body");
let text_node = document.create_text_node("Hello, world from Vanilla Rust!");
body.append_child(text_node.as_ref())
.expect("Failed to append text");
}

fn main() {
start_app();
}
2 changes: 1 addition & 1 deletion examples/workplace/simple-example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
<base data-trunk-public-url />
</head>
<body>
<h1>Trunk without WASM</h1>
<h1>Trunk without WASM in workspace</h1>
</body>
</html>
72 changes: 48 additions & 24 deletions src/config/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use anyhow::{bail, Context, Result};
use schemars::JsonSchema;
use serde::Deserialize;
use source::{workspace, Source};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use tracing::log;

/// Common configuration model functionality
Expand Down Expand Up @@ -114,16 +114,53 @@ impl ConfigModel for Configuration {
}
}

pub async fn load_workspace_config(path: &PathBuf) -> Result<Option<PathBuf>> {
let cargo_toml = path.clone().join("Cargo.toml");
pub async fn load_workspace_config(
current_path: &Path,
workspace_name: Option<String>,
) -> Result<Option<PathBuf>> {
let cargo_toml = current_path.join("Cargo.toml");
if cargo_toml.exists() {
if let Ok(workspace) = workspace::workspace_from_manifest(cargo_toml).await {
if let Some(workspace) = workspace.get_default_workspace() {
// get the parent directory of the workspace
let workspace = workspace
.parent()
.context("unable to get parent directory of workspace")?;
return Ok(Some(workspace.to_path_buf()));
if let Ok(workspace) = workspace::workspace_from_manifest(&cargo_toml).await {
match workspace_name {
Some(name) => {
if let Some(workspace) = workspace.get_workspace_by_name(&name) {
// get the parent directory of the workspace
let workspace = match workspace.parent() {
Some(parent) => parent,
None => {
return Err(anyhow::format_err!(
"unable to get parent directory of workspace '{}'",
workspace.display()
));
}
};
return Ok(Some(workspace.to_path_buf()));
}
return Err(anyhow::format_err!(
"workspace '{}' not found in {}",
name,
cargo_toml.display()
));
}
None => {
if let Some(workspace) = workspace.get_default_workspace() {
// get the parent directory of the workspace
let workspace = match workspace.parent() {
Some(parent) => parent,
None => {
return Err(anyhow::format_err!(
"unable to get parent directory of workspace '{}'",
workspace.display()
));
}
};
return Ok(Some(workspace.to_path_buf()));
}
return Err(anyhow::format_err!(
"default workspace not found in {}",
cargo_toml.display()
));
}
}
}
}
Expand Down Expand Up @@ -152,25 +189,12 @@ pub async fn load(path: Option<PathBuf>) -> Result<(Configuration, PathBuf)> {
Ok((Source::File(path).load().await?, cwd))
}
// if we have a directory, try finding a file and load it
Some(path) if path.is_dir() => {
let cwd = if let Some(new_cwd) = load_workspace_config(&path).await? {
new_cwd
} else {
path.clone()
};

Ok((Source::find(&path)?.load().await?, cwd))
}
Some(path) if path.is_dir() => Ok((Source::find(&path)?.load().await?, path)),
// if we have something else, we can't deal with it
Some(path) => bail!("{} is neither a file nor a directory", path.display()),
// if we have nothing, try to find a file in the current directory and load it
None => {
let cwd = std::env::current_dir().context("unable to get current directory")?;
let cwd = if let Some(new_cwd) = load_workspace_config(&cwd).await? {
new_cwd
} else {
cwd
};
Ok((Source::find(&cwd)?.load().await?, cwd))
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/config/models/source/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ impl WorkspaceConfig {
.await
.context("error awaiting spawned cargo metadata task")?
.context("error getting cargo metadata")?;
return Ok(Self { metadata });
Ok(Self { metadata })
}

pub fn get_default_workspace(self) -> Option<PathBuf> {
if let Some(default_members) = self.metadata.workspace_default_members.get(0) {
if let Some(default_members) = self.metadata.workspace_default_members.first() {
if let Some(found) = self
.metadata
.packages
Expand All @@ -33,6 +33,23 @@ impl WorkspaceConfig {
}
None
}

pub fn get_workspace_by_name(self, name: &str) -> Option<PathBuf> {
// we search for the package in the workspace packages list
if let Some(one_package) = self.metadata.packages.iter().find(|m| m.name == name) {
// we check if the package is present in the workspace members list
if self
.metadata
.workspace_members
.into_iter()
.any(|p| p == one_package.id)
{
// we return the manifest path of the package
return Some(one_package.manifest_path.clone().into());
}
}
None
}
}

/// Load the trunk configuration from the cargo manifest
Expand Down
29 changes: 23 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod ws;
use anyhow::{Context, Result};
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
use common::STARTING;
use config::models::load_workspace_config;
use std::io::IsTerminal;
use std::path::PathBuf;
use std::process::ExitCode;
Expand Down Expand Up @@ -144,6 +145,9 @@ struct Trunk {
/// Support for `NO_COLOR` environment variable
#[arg(long, env = "NO_COLOR", global(true))]
pub no_color: bool,

#[arg(long = "workspace", env = "TRUNK_CRATE_WORKSPACE")]
pub crate_workspace: Option<String>,
}

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

let cwd = std::env::current_dir().context("unable to get current directory")?;
let workspace_config = load_workspace_config(&cwd, self.crate_workspace).await?;

let config_path = if let Some(config_path_from_workspace) = workspace_config {
tracing::info!(
"Loading config from {}",
config_path_from_workspace.display()
);
Some(config_path_from_workspace)
} else {
self.config
};

match self.action {
TrunkSubcommands::Build(inner) => inner.run(self.config).await,
TrunkSubcommands::Clean(inner) => inner.run(self.config).await,
TrunkSubcommands::Serve(inner) => inner.run(self.config).await,
TrunkSubcommands::Watch(inner) => inner.run(self.config).await,
TrunkSubcommands::Config(inner) => inner.run(self.config).await,
TrunkSubcommands::Tools(inner) => inner.run(self.config).await,
TrunkSubcommands::Build(inner) => inner.run(config_path).await,
TrunkSubcommands::Clean(inner) => inner.run(config_path).await,
TrunkSubcommands::Serve(inner) => inner.run(config_path).await,
TrunkSubcommands::Watch(inner) => inner.run(config_path).await,
TrunkSubcommands::Config(inner) => inner.run(config_path).await,
TrunkSubcommands::Tools(inner) => inner.run(config_path).await,
}
}
}
Expand Down

0 comments on commit f7bfa70

Please sign in to comment.