Skip to content

Commit

Permalink
Change PackageSupplier interface to accept VersionRange
Browse files Browse the repository at this point in the history
This is a leftover of the time when everything in dub accepted Dependency.
While the mess had been untangled, doing so on this interface proved harder,
as there is no good way to deprecate it in a fully backward-compatible manner
without introducing a lot of overhead (a new type). Instead, this uses
a middle ground approach, where the external implementers (which most likely
do not exists) would break (although the fix is trivial), but callers would not.
  • Loading branch information
Geod24 authored and dlang-bot committed Dec 18, 2023
1 parent 13ee9ce commit b77bc6a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 18 deletions.
6 changes: 3 additions & 3 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ class Dub {
PackageSupplier supplier;
foreach(ps; m_packageSuppliers){
try {
pinfo = ps.fetchPackageRecipe(basePackageName, Dependency(range), (options & FetchOptions.usePrerelease) != 0);
pinfo = ps.fetchPackageRecipe(basePackageName, range, (options & FetchOptions.usePrerelease) != 0);
if (pinfo.type == Json.Type.null_)
continue;
supplier = ps;
Expand Down Expand Up @@ -895,7 +895,7 @@ class Dub {
import std.zip : ZipException;

auto path = getTempFile(basePackageName, ".zip");
supplier.fetchPackage(path, basePackageName, Dependency(range), (options & FetchOptions.usePrerelease) != 0); // Q: continue on fail?
supplier.fetchPackage(path, basePackageName, range, (options & FetchOptions.usePrerelease) != 0); // Q: continue on fail?
scope(exit) removeFile(path);
logDiagnostic("Placing to %s...", location.toString());

Expand Down Expand Up @@ -1790,7 +1790,7 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
foreach (ps; m_dub.m_packageSuppliers) {
if (rootpack == name) {
try {
auto desc = ps.fetchPackageRecipe(name, dep, prerelease);
auto desc = ps.fetchPackageRecipe(name, VersionRange(vers, vers), prerelease);
if (desc.type == Json.Type.null_)
continue;
auto ret = new Package(desc);
Expand Down
4 changes: 2 additions & 2 deletions source/dub/packagesuppliers/fallback.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ package abstract class AbstractFallbackPackageSupplier : PackageSupplier

// Workaround https://issues.dlang.org/show_bug.cgi?id=2525
abstract override Version[] getVersions(string package_id);
abstract override void fetchPackage(NativePath path, string package_id, Dependency dep, bool pre_release);
abstract override Json fetchPackageRecipe(string package_id, Dependency dep, bool pre_release);
abstract override void fetchPackage(NativePath path, string package_id, in VersionRange dep, bool pre_release);
abstract override Json fetchPackageRecipe(string package_id, in VersionRange dep, bool pre_release);
abstract override SearchResult[] searchPackages(string query);
}

Expand Down
6 changes: 3 additions & 3 deletions source/dub/packagesuppliers/filesystem.d
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FileSystemPackageSupplier : PackageSupplier {
return ret;
}

void fetchPackage(NativePath path, string packageId, Dependency dep, bool pre_release)
void fetchPackage(NativePath path, string packageId, in VersionRange dep, bool pre_release)
{
import dub.internal.vibecompat.core.file : copyFile, existsFile;
enforce(path.absolute);
Expand All @@ -49,7 +49,7 @@ class FileSystemPackageSupplier : PackageSupplier {
copyFile(filename, path);
}

Json fetchPackageRecipe(string packageId, Dependency dep, bool pre_release)
Json fetchPackageRecipe(string packageId, in VersionRange dep, bool pre_release)
{
import std.array : split;
import std.path : stripExtension;
Expand All @@ -76,7 +76,7 @@ class FileSystemPackageSupplier : PackageSupplier {
return null;
}

private NativePath bestPackageFile(string packageId, Dependency dep, bool pre_release)
private NativePath bestPackageFile(string packageId, in VersionRange dep, bool pre_release)
{
import std.algorithm.iteration : filter;
import std.array : array;
Expand Down
6 changes: 3 additions & 3 deletions source/dub/packagesuppliers/maven.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MavenRegistryPackageSupplier : PackageSupplier {
return ret;
}

void fetchPackage(NativePath path, string packageId, Dependency dep, bool pre_release)
void fetchPackage(NativePath path, string packageId, in VersionRange dep, bool pre_release)
{
import std.format : format;
auto md = getMetadata(packageId);
Expand All @@ -71,7 +71,7 @@ class MavenRegistryPackageSupplier : PackageSupplier {
throw new Exception("Failed to download package %s from %s".format(packageId, url));
}

Json fetchPackageRecipe(string packageId, Dependency dep, bool pre_release)
Json fetchPackageRecipe(string packageId, in VersionRange dep, bool pre_release)
{
auto md = getMetadata(packageId);
return getBestPackage(md, packageId, dep, pre_release);
Expand Down Expand Up @@ -125,7 +125,7 @@ class MavenRegistryPackageSupplier : PackageSupplier {
auto md = getMetadata(query);
if (md.type == Json.Type.null_)
return null;
auto json = getBestPackage(md, query, Dependency.any, true);
auto json = getBestPackage(md, query, VersionRange.Any, true);
return [SearchResult(json["name"].opt!string, "", json["version"].opt!string)];
}
}
33 changes: 29 additions & 4 deletions source/dub/packagesuppliers/packagesupplier.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module dub.packagesuppliers.packagesupplier;

public import dub.dependency : Dependency, Version;
public import dub.dependency : Dependency, Version, VersionRange;
import dub.dependency : visit;
public import dub.internal.vibecompat.core.file : NativePath;
public import dub.internal.vibecompat.data.json : Json;

Expand Down Expand Up @@ -33,7 +34,19 @@ interface PackageSupplier {
pre_release = If true, matches the latest pre-release version.
Otherwise prefers stable versions.
*/
void fetchPackage(NativePath path, string package_id, Dependency dep, bool pre_release);
void fetchPackage(NativePath path, string package_id, in VersionRange dep, bool pre_release);

deprecated("Use the overload that accepts a `VersionRange` instead")
final void fetchPackage(NativePath path, string package_id, Dependency dep, bool pre_release)
{
return dep.visit!(
(const VersionRange rng) {
return this.fetchPackage(path, package_id, rng, pre_release);
}, (any) {
assert(0, "Trying to fetch a package with a non-version dependency: " ~ any.toString());
},
);
}

/** Retrieves only the recipe of a particular package.
Expand All @@ -43,7 +56,19 @@ interface PackageSupplier {
pre_release = If true, matches the latest pre-release version.
Otherwise prefers stable versions.
*/
Json fetchPackageRecipe(string package_id, Dependency dep, bool pre_release);
Json fetchPackageRecipe(string package_id, in VersionRange dep, bool pre_release);

deprecated("Use the overload that accepts a `VersionRange` instead")
final Json fetchPackageRecipe(string package_id, Dependency dep, bool pre_release)
{
return dep.visit!(
(const VersionRange rng) {
return this.fetchPackageRecipe(package_id, rng, pre_release);
}, (any) {
return Json.init;
},
);
}

/** Searches for packages matching the given search query term.
Expand All @@ -59,7 +84,7 @@ interface PackageSupplier {
// a package recipe instead of one (first get version list, then the
// package recipe)

package Json getBestPackage(Json metadata, string packageId, Dependency dep, bool pre_release)
package Json getBestPackage(Json metadata, string packageId, in VersionRange dep, bool pre_release)
{
import std.exception : enforce;
if (metadata.type == Json.Type.null_)
Expand Down
7 changes: 4 additions & 3 deletions source/dub/packagesuppliers/registry.d
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module dub.packagesuppliers.registry;

import dub.dependency;
import dub.packagesuppliers.packagesupplier;

package enum PackagesPath = "packages";
Expand Down Expand Up @@ -48,7 +49,7 @@ class RegistryPackageSupplier : PackageSupplier {
return ret;
}

auto genPackageDownloadUrl(string packageId, Dependency dep, bool pre_release)
auto genPackageDownloadUrl(string packageId, in VersionRange dep, bool pre_release)
{
import std.array : replace;
import std.format : format;
Expand All @@ -64,7 +65,7 @@ class RegistryPackageSupplier : PackageSupplier {
return ret;
}

void fetchPackage(NativePath path, string packageId, Dependency dep, bool pre_release)
void fetchPackage(NativePath path, string packageId, in VersionRange dep, bool pre_release)
{
import std.format : format;
auto url = genPackageDownloadUrl(packageId, dep, pre_release);
Expand All @@ -84,7 +85,7 @@ class RegistryPackageSupplier : PackageSupplier {
throw new Exception("Failed to download package %s from %s".format(packageId, url));
}

Json fetchPackageRecipe(string packageId, Dependency dep, bool pre_release)
Json fetchPackageRecipe(string packageId, in VersionRange dep, bool pre_release)
{
auto md = getMetadata(packageId);
return getBestPackage(md, packageId, dep, pre_release);
Expand Down

0 comments on commit b77bc6a

Please sign in to comment.