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

DRAFT: Start adding support for workspace #804

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description = "Build, bundle & ship your Rust WASM application to the web."
license = "MIT/Apache-2.0"
authors = [
"Anthony Dodd <[email protected]>",
"Jens Reimann <[email protected]>"
"Jens Reimann <[email protected]>",
]
repository = "https://github.com/trunk-rs/trunk"
readme = "README.md"
Expand Down Expand Up @@ -34,7 +34,9 @@ console = "0.15"
directories = "5"
dunce = "1"
flate2 = "1"
futures-util = { version = "0.3", default-features = false, features = ["sink"] }
futures-util = { version = "0.3", default-features = false, features = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep the one line format, as this allows to easily sort the dependencies alphabetically.

"sink",
] }
htmlescape = "0.3.1"
humantime = "2"
humantime-serde = "1"
Expand All @@ -51,7 +53,10 @@ open = "5"
oxipng = "9"
parking_lot = "0.12"
remove_dir_all = "0.8"
reqwest = { version = "0.12", default-features = false, features = ["stream", "trust-dns"] }
reqwest = { version = "0.12", default-features = false, features = [
"stream",
"trust-dns",
] }
sha2 = "0.10"
schemars = { version = "0.8", features = ["derive"] }
seahash = { version = "4", features = ["use_std"] }
Expand All @@ -64,7 +69,10 @@ tar = "0.4"
time = { version = "0.3", features = ["serde-well-known"] }
thiserror = "1"
tokio = { version = "1", default-features = false, features = ["full"] }
tokio-stream = { version = "0.1", default-features = false, features = ["fs", "sync"] }
tokio-stream = { version = "0.1", default-features = false, features = [
"fs",
"sync",
] }
tokio-tungstenite = "0.21"
toml = "0.8"
tower-http = { version = "0.5.1", features = ["fs", "trace", "set-header"] }
Expand Down Expand Up @@ -104,6 +112,4 @@ native-tls = [
]

# enable the update check on startup
update_check = [
"crates_io_api"
]
update_check = ["crates_io_api"]
142 changes: 142 additions & 0 deletions examples/workplace/Cargo.lock

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

10 changes: 10 additions & 0 deletions examples/workplace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[workspace]
resolver = "2"
members = ["simple-example"]
default-members = ["simple-example"]

[workspace.package]
edition = "2021"
license = "MIT OR Apache-2.0"
rust-version = "1.76"
version = "0.27.2"
15 changes: 15 additions & 0 deletions examples/workplace/simple-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "simple-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/simple-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</h1>
</body>
</html>
15 changes: 15 additions & 0 deletions examples/workplace/simple-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();
}
33 changes: 31 additions & 2 deletions src/config/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod test;
use anyhow::{bail, Context, Result};
use schemars::JsonSchema;
use serde::Deserialize;
use source::Source;
use source::{workspace, Source};
use std::path::PathBuf;
use tracing::log;

Expand Down Expand Up @@ -111,6 +111,22 @@ impl ConfigModel for Configuration {
}
}

pub async fn load_workspace_config(path: &PathBuf) -> Result<Option<PathBuf>> {
let cargo_toml = path.clone().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()));
}
}
}
Ok(None)
}

/// Locate and load the configuration, given an optional file or directory. Falling back to the
/// current directory.
pub async fn load(path: Option<PathBuf>) -> Result<(Configuration, PathBuf)> {
Expand All @@ -125,12 +141,25 @@ 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() => Ok((Source::find(&path)?.load().await?, path)),
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))
}
// 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
1 change: 1 addition & 0 deletions src/config/models/source/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod cargo;
pub mod workspace;

use crate::config::{models::ConfigModel, Configuration};
use anyhow::bail;
Expand Down
42 changes: 42 additions & 0 deletions src/config/models/source/workspace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use anyhow::{Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
use std::path::Path;
use std::path::PathBuf;
use tokio::task::spawn_blocking;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct WorkspaceConfig {
pub metadata: Metadata,
}

impl WorkspaceConfig {
pub async fn new(manifest: &Path) -> Result<Self> {
let mut cmd = MetadataCommand::new();
cmd.manifest_path(dunce::simplified(manifest));
let metadata = spawn_blocking(move || cmd.exec())
.await
.context("error awaiting spawned cargo metadata task")?
.context("error getting cargo metadata")?;
return 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(found) = self
.metadata
.packages
.into_iter()
.find(|p| p.id == *default_members)
{
return Some(found.manifest_path.clone().into());
}
}
None
}
}

/// Load the trunk configuration from the cargo manifest
pub async fn workspace_from_manifest(file: impl AsRef<Path>) -> anyhow::Result<WorkspaceConfig> {
let workspace_config = WorkspaceConfig::new(file.as_ref()).await?;
Ok(workspace_config)
}