Skip to content

Commit

Permalink
Add logic to discover workspaces in parent dirs
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Bottriell <[email protected]>
  • Loading branch information
rydrman committed Jan 14, 2025
1 parent 76c6159 commit 50010a2
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ dunce = "1.0.4"
dyn-clone = "1.0"
enum_dispatch = "0.3.13"
flatbuffers = "23.5.26"
format_serde_error = { version = "0.3", default-features = false }
fuser = "0.14.0"
futures = "0.3.28"
futures-core = "0.3.28"
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ config = { workspace = true }
data-encoding = "2.3"
dunce = { workspace = true }
enum_dispatch = { workspace = true }
format_serde_error = { version = "0.3", default-features = false, features = [
format_serde_error = { workspace = true, default-features = false, features = [
"serde_yaml",
"colored",
] }
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/crates/foundation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async-trait = { workspace = true }
colored = { workspace = true }
data-encoding = "2.3"
enum_dispatch = { workspace = true }
format_serde_error = { version = "0.3", default-features = false, features = [
format_serde_error = { workspace = true, default-features = false, features = [
"serde_yaml",
"colored",
] }
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-schema/crates/ident/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ migration-to-components = ["spk-schema-foundation/migration-to-components"]

[dependencies]
colored = { workspace = true }
format_serde_error = { version = "0.3", default-features = false, features = [
format_serde_error = { workspace = true, default-features = false, features = [
"serde_yaml",
"colored",
] }
Expand Down
2 changes: 1 addition & 1 deletion crates/spk-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ colored = { workspace = true }
dashmap = "5.4.0"
data-encoding = "2.3.0"
enum_dispatch = { workspace = true }
format_serde_error = { version = "0.3", default-features = false, features = [
format_serde_error = { workspace = true, default-features = false, features = [
"serde_yaml",
"colored",
] }
Expand Down
6 changes: 6 additions & 0 deletions crates/spk-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ sentry = ["spk-solve/sentry"]
serde = { workspace = true, features = ["derive"] }
glob = { workspace = true }
spk-solve = { workspace = true }
thiserror = { workspace = true }
miette = { workspace = true }
serde_yaml = { workspace = true }
spk-schema-foundation = { workspace = true }
format_serde_error = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true }
16 changes: 16 additions & 0 deletions crates/spk-workspace/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::path::PathBuf;

#[derive(thiserror::Error, miette::Diagnostic, Debug)]
pub enum LoadWorkspaceError {
#[error(
"workspace not found, no {} in {0:?} or any parent",
crate::Workspace::FILE_NAME
)]
WorkspaceNotFound(PathBuf),
#[error("'{}' not found in {0:?}", crate::Workspace::FILE_NAME)]
NoWorkspaceFile(PathBuf),
#[error(transparent)]
ReadFailed(std::io::Error),
#[error(transparent)]
InvalidYaml(format_serde_error::SerdeError),
}
3 changes: 3 additions & 0 deletions crates/spk-workspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
pub mod error;
mod spec;

pub use spec::Workspace;
52 changes: 52 additions & 0 deletions crates/spk-workspace/src/spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::path::Path;

use serde::{Deserialize, Serialize};
use spk_schema_foundation::FromYaml;

use crate::error::LoadWorkspaceError;

#[cfg(test)]
#[path = "spec_test.rs"]
Expand All @@ -10,6 +15,53 @@ pub struct Workspace {
pub recipes: Vec<glob::Pattern>,
}

impl Workspace {
pub const FILE_NAME: &str = "workspace.spk.yaml";

/// Load a workspace from its root directory on disk
pub fn load<P: AsRef<Path>>(root: P) -> Result<Self, LoadWorkspaceError> {
let root = root
.as_ref()
.canonicalize()
.map_err(|_| LoadWorkspaceError::NoWorkspaceFile(root.as_ref().into()))?;

let workspace_file = std::fs::read_to_string(root.join(Workspace::FILE_NAME))
.map_err(LoadWorkspaceError::ReadFailed)?;
Workspace::from_yaml(workspace_file).map_err(LoadWorkspaceError::InvalidYaml)
}

/// Load the workspace for a given dir, looking at parent directories
/// as necessary to find the workspace root
pub fn discover<P: AsRef<Path>>(cwd: P) -> Result<Self, LoadWorkspaceError> {
let cwd = if cwd.as_ref().is_absolute() {
cwd.as_ref().to_owned()
} else {
// prefer PWD if available, since it may be more representative of
// how the user arrived at the current dir and avoids dereferencing
// symlinks that could otherwise make error messages harder to understand
match std::env::var("PWD").ok() {
Some(pwd) => Path::new(&pwd).join(cwd),
None => std::env::current_dir().unwrap_or_default().join(cwd),
}
};
let mut candidate = cwd.clone();
let mut last_found = None;

loop {
if candidate.join(Workspace::FILE_NAME).is_file() {
last_found = Some(candidate.clone());
}
if !candidate.pop() {
break;
}
}
match last_found {
Some(path) => Self::load(path),
None => Err(LoadWorkspaceError::WorkspaceNotFound(cwd)),
}
}
}

mod glob_from_str {
use serde::{Deserializer, Serialize, Serializer};

Expand Down
53 changes: 52 additions & 1 deletion crates/spk-workspace/src/spec_test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
use rstest::rstest;
use rstest::{fixture, rstest};

use super::Workspace;

#[fixture]
pub fn tmpdir() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("spk-test-")
.tempdir()
.expect("create a temp directory for test files")
}

const EMPTY_WORKSPACE: &str = r#"
api: v0/workspace
recipes: []
"#;

#[rstest]
fn test_workspace_roundtrip() {
let workspace = Workspace {
Expand All @@ -16,3 +29,41 @@ fn test_workspace_roundtrip() {

assert_eq!(workspace, deserialized);
}

#[rstest]
fn test_empty_workspace_loading(tmpdir: tempfile::TempDir) {
let root = tmpdir.path();
std::fs::write(root.join(Workspace::FILE_NAME), EMPTY_WORKSPACE).unwrap();
let _workspace = Workspace::load(root).expect("failed to load empty workspace");
}

#[rstest]
fn test_must_have_file(tmpdir: tempfile::TempDir) {
let root = tmpdir.path();
Workspace::load(root).expect_err("workspace should fail to load for empty dir");
}

#[rstest]
#[case("", "")]
#[case("my-workspace", "my-workspace/src/packages")]
#[should_panic]
#[case("my-workspace", "other-dir")]
#[case("", "")]
#[case("my-workspace", "my-workspace/src/packages")]
#[should_panic]
#[case("my-workspace", "other-dir/src/packages")]
fn test_workspace_discovery(
tmpdir: tempfile::TempDir,
#[case] workspace_root: &str,
#[case] discovery_start: &str,
) {
let dir = tmpdir.path();
let root = dir.join(workspace_root);
let cwd = dir.join(discovery_start);

std::fs::create_dir_all(&cwd).unwrap();
std::fs::create_dir_all(&root).unwrap();
std::fs::write(root.join(Workspace::FILE_NAME), EMPTY_WORKSPACE).unwrap();

Workspace::discover(&cwd).expect("failed to load workspace");
}
4 changes: 4 additions & 0 deletions workspace.spk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
api: v0/workspace

recipes:
- packages/**.spk.yml

0 comments on commit 50010a2

Please sign in to comment.