refactor(rust): share workspace path resolution

This commit is contained in:
2026-09-17 09:40:31 -04:00
parent 3f49392e3c
commit b6b9f9b7e5
9 changed files with 84 additions and 14 deletions

5
Cargo.lock generated
View File

@@ -57,6 +57,7 @@ name = "ariadne"
version = "0.1.0"
dependencies = [
"clap",
"monorepo-util",
"serde",
"serde_json",
]
@@ -135,6 +136,10 @@ version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "monorepo-util"
version = "0.1.0"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"

View File

@@ -2,10 +2,12 @@
members = [
"src/ariadne",
"src/machoke",
"src/monorepo-util",
]
resolver = "3"
[workspace.dependencies]
clap = "4"
monorepo-util = { path = "src/monorepo-util" }
serde = "1.0"
serde_json = "1.0"

View File

@@ -20,6 +20,7 @@ rust_binary(
srcs = ["ariadne_main.rs"],
deps = [
":ariadne_lib",
"//src/monorepo-util:monorepo_util",
"//third_party/crates:clap",
],
)

View File

@@ -12,5 +12,6 @@ path = "ariadne_main.rs"
[dependencies]
clap = { workspace = true, features = ["derive"] }
monorepo-util = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

View File

@@ -17,7 +17,7 @@ struct Args {
impl From<Args> for ariadne::Config {
fn from(args: Args) -> Self {
ariadne::Config {
output_dir: resolve_workspace_path(args.output_dir),
output_dir: monorepo_util::resolve_workspace_path(args.output_dir),
force: args.force,
target_stage: args.target_stage,
}
@@ -32,19 +32,6 @@ fn parse_stage(value: &str) -> Result<ariadne::pipeline::Stage, String> {
.ok_or_else(|| format!("unknown stage: {value}"))
}
// TODO: Move this into a shared Rust utility crate.
fn resolve_workspace_path(path: PathBuf) -> PathBuf {
if path.is_absolute() {
return path;
}
if let Some(workspace) = std::env::var_os("BUILD_WORKSPACE_DIRECTORY") {
return PathBuf::from(workspace).join(path);
}
path
}
fn main() -> ariadne::Result<()> {
let config: ariadne::Config = Args::parse().into();
ariadne::run(&config)

14
src/monorepo-util/BUILD Normal file
View File

@@ -0,0 +1,14 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
rust_library(
name = "monorepo_util",
crate_name = "monorepo_util",
srcs = ["monorepo_util.rs"],
visibility = ["//visibility:public"],
)
rust_test(
name = "monorepo_util_test",
srcs = ["monorepo_util_test.rs"],
deps = [":monorepo_util"],
)

View File

@@ -0,0 +1,9 @@
[package]
name = "monorepo-util"
version = "0.1.0"
edition = "2024"
[lib]
path = "monorepo_util.rs"
[dependencies]

View File

@@ -0,0 +1,20 @@
use std::path::PathBuf;
pub fn resolve_workspace_path(path: PathBuf) -> PathBuf {
resolve_workspace_path_from(
path,
std::env::var_os("BUILD_WORKSPACE_DIRECTORY").map(PathBuf::from),
)
}
pub fn resolve_workspace_path_from(path: PathBuf, workspace: Option<PathBuf>) -> PathBuf {
if path.is_absolute() {
return path;
}
if let Some(workspace) = workspace {
return workspace.join(path);
}
path
}

View File

@@ -0,0 +1,31 @@
use std::path::PathBuf;
use monorepo_util::resolve_workspace_path_from;
#[test]
fn absolute_path_returns_unchanged_path() {
let path = PathBuf::from("/tmp/output");
let resolved = resolve_workspace_path_from(path.clone(), Some(PathBuf::from("/workspace")));
assert_eq!(resolved, path);
}
#[test]
fn relative_path_joins_workspace_path() {
let resolved = resolve_workspace_path_from(
PathBuf::from("output/graph"),
Some(PathBuf::from("/workspace")),
);
assert_eq!(resolved, PathBuf::from("/workspace/output/graph"));
}
#[test]
fn relative_path_without_workspace_returns_unchanged_path() {
let path = PathBuf::from("output/graph");
let resolved = resolve_workspace_path_from(path.clone(), None);
assert_eq!(resolved, path);
}