Skip to content

Commit

Permalink
Merge pull request #18 from helsing-ai/mara/semver
Browse files Browse the repository at this point in the history
  • Loading branch information
mara-schulke committed Aug 26, 2023
2 parents d7265b0 + b9b67ad commit 4179bd5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 21 deletions.
10 changes: 10 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 @@ -30,6 +30,7 @@ protoc-bin-vendored = "3.0.0"
reqwest = "0.11"
serde = { version = "1", features = ["derive"] }
serde_typename = "0.1"
semver = { version = "1", features = ["serde"] }
tar = "0.4"
tokio = { version = "1", features = ["full", "tracing"] }
toml = "0.7"
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn build(language: Language) -> eyre::Result<()> {
)
})?;

if pkg.version == dependency.manifest.version {
if dependency.manifest.version.matches(&pkg.version) {
continue;
}
}
Expand Down
20 changes: 8 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ mod cmd {
};
use eyre::{ensure, Context, ContextCompat};
use futures::future::try_join_all;
use semver::{Version, VersionReq};

/// Initializes the project
pub async fn init(r#type: Option<(PackageType, Option<PackageId>)>) -> eyre::Result<()> {
Expand All @@ -163,7 +164,7 @@ mod cmd {
manifest.package = Some(PackageManifest {
r#type,
name,
version: "0.0.1".to_owned(),
version: Version::new(0, 0, 1),
description: None,
});
}
Expand Down Expand Up @@ -200,20 +201,15 @@ mod cmd {
.parse::<PackageId>()
.wrap_err_with(|| format!("Invalid package id supplied: {package}"))?;

ensure!(
version
.chars()
.all(|c| c.is_alphanumeric() || c == '.' || c == '-'),
"Version specifications must be in the format <major>.<minor>.<patch>-<tag>"
);
let version = version
.parse::<VersionReq>()
.wrap_err_with(|| format!("Invalid version requirement supplied: {package}"))?;

let mut manifest = Manifest::read().await?;

manifest.dependencies.push(Dependency::new(
repository.to_owned(),
package,
version.to_owned(),
));
manifest
.dependencies
.push(Dependency::new(repository.to_owned(), package, version));

manifest.write().await
}
Expand Down
11 changes: 6 additions & 5 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// (c) Copyright 2023 Helsing GmbH. All rights reserved.

use eyre::Context;
use semver::{Version, VersionReq};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
use tokio::fs;
Expand Down Expand Up @@ -100,13 +101,13 @@ pub struct PackageManifest {
/// Name of the package
pub name: PackageId,
/// Version of the package
pub version: String,
pub version: Version,
/// Description of the api package
pub description: Option<String>,
}

/// Represents a single project dependency
#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq)]
pub struct Dependency {
/// Package name of this dependency
pub package: PackageId,
Expand All @@ -116,7 +117,7 @@ pub struct Dependency {

impl Dependency {
/// Creates a new dependency
pub fn new(repository: String, package: PackageId, version: String) -> Self {
pub fn new(repository: String, package: PackageId, version: VersionReq) -> Self {
Self {
package,
manifest: DependencyManifest {
Expand All @@ -138,10 +139,10 @@ impl fmt::Display for Dependency {
}

/// Manifest format for dependencies
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, Hash, Serialize, Deserialize, PartialEq, Eq)]
pub struct DependencyManifest {
/// Version requirement in the helsing format, currently only supports pinning
pub version: String,
pub version: VersionReq,
/// Artifactory repository to pull dependency from
pub repository: String,
}
2 changes: 1 addition & 1 deletion src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl PackageStore {
))?;

eyre::ensure!(
existing.version == dependency.manifest.version,
dependency.manifest.version.matches(&existing.version),
"A dependency of your project requires {}@{} which collides with {}@{} required by {}",
existing.name,
existing.version,
Expand Down
34 changes: 32 additions & 2 deletions src/registry/artifactory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::sync::Arc;

use eyre::{ensure, Context};
use eyre::{ensure, Context, ContextCompat};
use serde::{Deserialize, Serialize};
use url::Url;

Expand Down Expand Up @@ -40,6 +40,36 @@ impl Artifactory {
impl Registry for Artifactory {
/// Downloads a package from artifactory
async fn download(&self, dependency: Dependency) -> eyre::Result<Package> {
ensure!(
dependency.manifest.version.comparators.len() == 1,
"{} uses unsupported semver comparators",
dependency.package
);

let version = dependency
.manifest
.version
.comparators
.first()
.wrap_err("internal error")?;

ensure!(
version.op == semver::Op::Exact && version.minor.is_some() && version.patch.is_some(),
"artifactory only support pinned dependencies"
);

let version = format!(
"{}.{}.{}{}",
version.major,
version.minor.wrap_err("internal error")?,
version.patch.wrap_err("internal error")?,
if version.pre.is_empty() {
"".to_owned()
} else {
format!("-{}", version.pre)
}
);

let artifact_uri: Url = {
let mut url = self.0.url.clone();

Expand All @@ -49,7 +79,7 @@ impl Registry for Artifactory {
dependency.manifest.repository,
dependency.package,
dependency.package,
dependency.manifest.version
version
));

url
Expand Down

0 comments on commit 4179bd5

Please sign in to comment.