refactor(rust): share workspace path resolution
This commit is contained in:
5
Cargo.lock
generated
5
Cargo.lock
generated
@@ -57,6 +57,7 @@ name = "ariadne"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"monorepo-util",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
@@ -135,6 +136,10 @@ version = "2.8.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "monorepo-util"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "once_cell_polyfill"
|
name = "once_cell_polyfill"
|
||||||
version = "1.70.2"
|
version = "1.70.2"
|
||||||
|
|||||||
@@ -2,10 +2,12 @@
|
|||||||
members = [
|
members = [
|
||||||
"src/ariadne",
|
"src/ariadne",
|
||||||
"src/machoke",
|
"src/machoke",
|
||||||
|
"src/monorepo-util",
|
||||||
]
|
]
|
||||||
resolver = "3"
|
resolver = "3"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
clap = "4"
|
clap = "4"
|
||||||
|
monorepo-util = { path = "src/monorepo-util" }
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ rust_binary(
|
|||||||
srcs = ["ariadne_main.rs"],
|
srcs = ["ariadne_main.rs"],
|
||||||
deps = [
|
deps = [
|
||||||
":ariadne_lib",
|
":ariadne_lib",
|
||||||
|
"//src/monorepo-util:monorepo_util",
|
||||||
"//third_party/crates:clap",
|
"//third_party/crates:clap",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -12,5 +12,6 @@ path = "ariadne_main.rs"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { workspace = true, features = ["derive"] }
|
clap = { workspace = true, features = ["derive"] }
|
||||||
|
monorepo-util = { workspace = true }
|
||||||
serde = { workspace = true, features = ["derive"] }
|
serde = { workspace = true, features = ["derive"] }
|
||||||
serde_json = { workspace = true }
|
serde_json = { workspace = true }
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ struct Args {
|
|||||||
impl From<Args> for ariadne::Config {
|
impl From<Args> for ariadne::Config {
|
||||||
fn from(args: Args) -> Self {
|
fn from(args: Args) -> Self {
|
||||||
ariadne::Config {
|
ariadne::Config {
|
||||||
output_dir: resolve_workspace_path(args.output_dir),
|
output_dir: monorepo_util::resolve_workspace_path(args.output_dir),
|
||||||
force: args.force,
|
force: args.force,
|
||||||
target_stage: args.target_stage,
|
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}"))
|
.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<()> {
|
fn main() -> ariadne::Result<()> {
|
||||||
let config: ariadne::Config = Args::parse().into();
|
let config: ariadne::Config = Args::parse().into();
|
||||||
ariadne::run(&config)
|
ariadne::run(&config)
|
||||||
|
|||||||
14
src/monorepo-util/BUILD
Normal file
14
src/monorepo-util/BUILD
Normal 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"],
|
||||||
|
)
|
||||||
9
src/monorepo-util/Cargo.toml
Normal file
9
src/monorepo-util/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "monorepo-util"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
path = "monorepo_util.rs"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
20
src/monorepo-util/monorepo_util.rs
Normal file
20
src/monorepo-util/monorepo_util.rs
Normal 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
|
||||||
|
}
|
||||||
31
src/monorepo-util/monorepo_util_test.rs
Normal file
31
src/monorepo-util/monorepo_util_test.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user