Skip to content

Commit

Permalink
Add support for brace-expanded version number lists
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 c46d0f3 commit 0980e04
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
7 changes: 7 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 @@ -44,6 +44,7 @@ description = "SPK is a Package Manager for high-velocity software environments,
[workspace.dependencies]
arc-swap = "1.6.0"
async-trait = "0.1"
bracoxide = "0.1.4"
bytes = "1.5"
cached = "0.48.1"
chrono = { version = "0.4.34", features = ["serde"] }
Expand Down
9 changes: 5 additions & 4 deletions crates/spk-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ workspace = true
sentry = ["spk-solve/sentry"]

[dependencies]
serde = { workspace = true, features = ["derive"] }
bracoxide = { workspace = true }
format_serde_error = { workspace = true }
glob = { workspace = true }
itertools = { workspace = true }
spk-solve = { workspace = true }
thiserror = { workspace = true }
miette = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_yaml = { workspace = true }
spk-schema = { workspace = true }
format_serde_error = { workspace = true }
spk-solve = { workspace = true }
thiserror = { workspace = true }

[dev-dependencies]
rstest = { workspace = true }
Expand Down
43 changes: 41 additions & 2 deletions crates/spk-workspace/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
// https://github.com/spkenv/spk

use std::path::Path;
use std::str::FromStr;

use bracoxide::tokenizer::TokenizationError;
use bracoxide::OxidizationError;
use serde::Deserialize;
use spk_schema::foundation::FromYaml;
use spk_schema::version::Version;

use crate::error::LoadWorkspaceFileError;

Expand Down Expand Up @@ -75,6 +79,7 @@ impl WorkspaceFile {
#[derive(Debug, Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
pub struct RecipesItem {
pub path: glob::Pattern,
pub versions: Vec<Version>,
}

impl<'de> serde::de::Deserialize<'de> for RecipesItem {
Expand All @@ -96,7 +101,10 @@ impl<'de> serde::de::Deserialize<'de> for RecipesItem {
E: serde::de::Error,
{
let path = glob::Pattern::new(v).map_err(serde::de::Error::custom)?;
Ok(RecipesItem { path })
Ok(RecipesItem {
path,
versions: Vec::new(),
})
}

fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
Expand All @@ -106,11 +114,42 @@ impl<'de> serde::de::Deserialize<'de> for RecipesItem {
#[derive(Deserialize)]
struct RawRecipeItem {
path: String,
#[serde(default)]
versions: Vec<String>,
}

let raw_recipe =
RawRecipeItem::deserialize(serde::de::value::MapAccessDeserializer::new(map))?;
self.visit_str(&raw_recipe.path)
let mut base = self.visit_str(&raw_recipe.path)?;
for (i, version_expr) in raw_recipe.versions.into_iter().enumerate() {
let expand_result = bracoxide::bracoxidize(&version_expr);
let expanded = match expand_result {
Ok(expanded) => expanded,
Err(OxidizationError::TokenizationError(TokenizationError::NoBraces))
| Err(OxidizationError::TokenizationError(
TokenizationError::EmptyContent,
))
| Err(OxidizationError::TokenizationError(
TokenizationError::FormatNotSupported,
)) => {
vec![version_expr]
}
Err(err) => {
return Err(serde::de::Error::custom(format!(
"invalid brace expansion in position {i}: {err:?}"
)))
}
};
for version in expanded {
let parsed = Version::from_str(&version).map_err(|err| {
serde::de::Error::custom(format!(
"brace expansion in position {i} produced invalid version '{version}': {err}"
))
})?;
base.versions.push(parsed);
}
}
Ok(base)
}
}

Expand Down
26 changes: 25 additions & 1 deletion workspace.spk.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
api: v0/workspace

recipes:
- packages/**.spk.yml
# collect all of the recipes in the workspace
- packages/**/*.spk.yaml

# some recipes require additional information
# which can be augmented even if they were already
# collected above

- path: packages/python/python2.spk.yaml
# here, we define the specific versions that can
# be build from a recipe
versions: [2.7.18]

- path: packages/python/python3.spk.yaml
# we can use bash-style brace expansion to define
# ranges of versions that are supported
versions:
- '3.7.{0..17}'
- '3.8.{0..20}'
- '3.9.{0..21}'
- '3.10.{0..16}'
- '3.11.{0..11}'
- '3.12.{0..8}'
- '3.13.{0..1}'


0 comments on commit 0980e04

Please sign in to comment.