-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #259 from helsing-ai/gbouv/add-latest-by-default
buffrs add now accept dependency with no version and defaults to `latest`
- Loading branch information
Showing
3 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,7 +117,12 @@ pub async fn new(kind: Option<PackageType>, name: PackageName) -> miette::Result | |
struct DependencyLocator { | ||
repository: String, | ||
package: PackageName, | ||
version: VersionReq, | ||
version: DependencyLocatorVersion, | ||
} | ||
|
||
enum DependencyLocatorVersion { | ||
Version(VersionReq), | ||
Latest, | ||
} | ||
|
||
impl FromStr for DependencyLocator { | ||
|
@@ -140,18 +145,24 @@ impl FromStr for DependencyLocator { | |
|
||
let repository = repository.into(); | ||
|
||
let (package, version) = dependency.split_once('@').ok_or_else(|| { | ||
miette!("dependency specification is missing version part: {dependency}") | ||
})?; | ||
let (package, version) = dependency | ||
.split_once('@') | ||
.map(|(package, version)| (package, Some(version))) | ||
.unwrap_or_else(|| (dependency, None)); | ||
|
||
let package = package | ||
.parse::<PackageName>() | ||
.wrap_err(miette!("invalid package name: {package}"))?; | ||
|
||
let version = version | ||
.parse::<VersionReq>() | ||
.into_diagnostic() | ||
.wrap_err(miette!("not a valid version requirement: {version}"))?; | ||
let version = match version { | ||
Some("latest") | None => DependencyLocatorVersion::Latest, | ||
Some(version_str) => { | ||
let parsed_version = VersionReq::parse(version_str) | ||
.into_diagnostic() | ||
.wrap_err(miette!("not a valid version requirement: {version_str}"))?; | ||
DependencyLocatorVersion::Version(parsed_version) | ||
} | ||
}; | ||
|
||
Ok(Self { | ||
repository, | ||
|
@@ -171,6 +182,23 @@ pub async fn add(registry: RegistryUri, dependency: &str) -> miette::Result<()> | |
version, | ||
} = dependency.parse()?; | ||
|
||
let version = match version { | ||
DependencyLocatorVersion::Version(version_req) => version_req, | ||
DependencyLocatorVersion::Latest => { | ||
// query artifactory to retrieve the actual latest version | ||
let credentials = Credentials::load().await?; | ||
let artifactory = Artifactory::new(registry.clone(), &credentials)?; | ||
|
||
let latest_version = artifactory | ||
.get_latest_version(repository.clone(), package.clone()) | ||
.await?; | ||
// Convert semver::Version to semver::VersionReq. It will default to operator `>`, which is what we want for Proto.toml | ||
VersionReq::parse(&latest_version.to_string()) | ||
.into_diagnostic() | ||
.map_err(miette::Report::from)? | ||
} | ||
}; | ||
|
||
manifest | ||
.dependencies | ||
.push(Dependency::new(registry, repository, package, version)); | ||
|
@@ -548,14 +576,18 @@ mod tests { | |
assert!("repo/pkg@=1.0.0-with-prerelease" | ||
.parse::<DependencyLocator>() | ||
.is_ok()); | ||
assert!("repo/pkg@latest".parse::<DependencyLocator>().is_ok()); | ||
assert!("repo/pkg".parse::<DependencyLocator>().is_ok()); | ||
} | ||
|
||
#[test] | ||
fn invalid_dependency_locators() { | ||
assert!("/[email protected]".parse::<DependencyLocator>().is_err()); | ||
assert!("repo/@1.0.0".parse::<DependencyLocator>().is_err()); | ||
assert!("[email protected]".parse::<DependencyLocator>().is_err()); | ||
assert!("repo/pkg".parse::<DependencyLocator>().is_err()); | ||
assert!("repo/pkg@latestwithtypo" | ||
.parse::<DependencyLocator>() | ||
.is_err()); | ||
assert!("repo/pkg@=1#meta".parse::<DependencyLocator>().is_err()); | ||
assert!("repo/PKG@=1.0".parse::<DependencyLocator>().is_err()); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters