Skip to content

Commit

Permalink
Adapt SelectedVersions to PackageName API
Browse files Browse the repository at this point in the history
Adapting to the PackageName API meant relaxing the input
a SelectedVersion method can accept, as we now handle
the subpackage part gracefully (ignoring it), instead
of potentially running into issue.
  • Loading branch information
Geod24 authored and dlang-bot committed Jan 17, 2024
1 parent 2ace859 commit 74877a3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 40 deletions.
21 changes: 12 additions & 9 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -631,14 +631,15 @@ class Dub {
if (!(options & UpgradeOptions.upgrade)) {
next_pack:
foreach (p; m_project.selections.selectedPackages) {
auto dep = m_project.selections.getSelectedVersion(p);
const name = PackageName(p); // Always a main package name
auto dep = m_project.selections.getSelectedVersion(name);
if (!dep.path.empty) {
auto path = dep.path;
if (!path.absolute) path = this.rootPath ~ path;
try if (m_packageManager.getOrLoadPackage(path)) continue;
catch (Exception e) { logDebug("Failed to load path based selection: %s", e.toString().sanitize); }
} else if (!dep.repository.empty) {
if (m_packageManager.loadSCMPackage(PackageName(p).main, dep.repository))
if (m_packageManager.loadSCMPackage(name, dep.repository))
continue;
} else {
if (m_packageManager.getPackage(p, dep.version_)) continue;
Expand All @@ -655,7 +656,7 @@ class Dub {
}

logWarn("Selected package %s %s doesn't exist. Using latest matching version instead.", p, dep);
m_project.selections.deselectVersion(p);
m_project.selections.deselectVersion(name);
}
}

Expand Down Expand Up @@ -692,7 +693,8 @@ class Dub {
}

foreach (p, ver; versions) {
assert(!p.canFind(":"), "Resolved packages contain a sub package!?: "~p);
const name = PackageName(p);
assert(!name.sub, "Resolved packages contain a sub package!?: " ~ p);
Package pack;
if (!ver.path.empty) {
try pack = m_packageManager.getOrLoadPackage(ver.path);
Expand Down Expand Up @@ -720,13 +722,13 @@ class Dub {
if (!pack) fetch(p, ver.version_, defaultPlacementLocation, fetchOpts, "getting selected version");
if ((options & UpgradeOptions.select) && p != m_project.rootPackage.name) {
if (!ver.repository.empty) {
m_project.selections.selectVersion(p, ver.repository);
m_project.selections.selectVersion(name, ver.repository);
} else if (ver.path.empty) {
m_project.selections.selectVersion(p, ver.version_);
m_project.selections.selectVersion(name, ver.version_);
} else {
NativePath relpath = ver.path;
if (relpath.absolute) relpath = relpath.relativeTo(m_project.rootPackage.path);
m_project.selections.selectVersion(p, relpath);
m_project.selections.selectVersion(name, relpath);
}
}
}
Expand Down Expand Up @@ -1682,8 +1684,9 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
if (auto pvers = pack in m_packageVersions)
return *pvers;

if ((!(m_options & UpgradeOptions.upgrade) || isFixedPackage(pack)) && m_selectedVersions.hasSelectedVersion(pack)) {
auto ret = [m_selectedVersions.getSelectedVersion(pack)];
const name = PackageName(pack);
if ((!(m_options & UpgradeOptions.upgrade) || isFixedPackage(pack)) && m_selectedVersions.hasSelectedVersion(name)) {
auto ret = [m_selectedVersions.getSelectedVersion(name)];
logDiagnostic("Using fixed selection %s %s", pack, ret[0]);
m_packageVersions[pack] = ret;
return ret;
Expand Down
113 changes: 82 additions & 31 deletions source/dub/project.d
Original file line number Diff line number Diff line change
Expand Up @@ -1332,8 +1332,9 @@ class Project {
void saveSelections()
{
assert(m_selections !is null, "Cannot save selections for non-disk based project (has no selections).");
if (m_selections.hasSelectedVersion(m_rootPackage.basePackage.name))
m_selections.deselectVersion(m_rootPackage.basePackage.name);
const name = PackageName(m_rootPackage.basePackage.name);
if (m_selections.hasSelectedVersion(name))
m_selections.deselectVersion(name);

auto path = m_rootPackage.path ~ SelectedVersions.defaultFile;
if (m_selections.dirty || !existsFile(path))
Expand Down Expand Up @@ -1762,11 +1763,16 @@ unittest
environment.remove("MY_ENV_VAR");
}

/** Holds and stores a set of version selections for package dependencies.
This is the runtime representation of the information contained in
"dub.selections.json" within a package's directory.
*/
/**
* Holds and stores a set of version selections for package dependencies.
*
* This is the runtime representation of the information contained in
* "dub.selections.json" within a package's directory.
*
* Note that as subpackages share the same version as their main package,
* this class will treat any subpackage reference as a reference to its
* main package.
*/
public class SelectedVersions {
protected {
enum FileVersion = 1;
Expand Down Expand Up @@ -1840,36 +1846,58 @@ public class SelectedVersions {
}

/// Selects a certain version for a specific package.
deprecated("Use the overload that accepts a `PackageName`")
void selectVersion(string package_id, Version version_)
{
if (auto pdep = package_id in m_selections.versions) {
if (*pdep == Dependency(version_))
return;
}
m_selections.versions[package_id] = Dependency(version_);
m_dirty = true;
const name = PackageName(package_id);
return this.selectVersion(name, version_);
}

/// Ditto
void selectVersion(in PackageName name, Version version_)
{
const dep = Dependency(version_);
this.selectVersionInternal(name, dep);
}

/// Selects a certain path for a specific package.
deprecated("Use the overload that accepts a `PackageName`")
void selectVersion(string package_id, NativePath path)
{
if (auto pdep = package_id in m_selections.versions) {
if (*pdep == Dependency(path))
return;
}
m_selections.versions[package_id] = Dependency(path);
m_dirty = true;
const name = PackageName(package_id);
return this.selectVersion(name, path);
}

/// Ditto
void selectVersion(in PackageName name, NativePath path)
{
const dep = Dependency(path);
this.selectVersionInternal(name, dep);
}

/// Selects a certain Git reference for a specific package.
deprecated("Use the overload that accepts a `PackageName`")
void selectVersion(string package_id, Repository repository)
{
const dependency = Dependency(repository);
if (auto pdep = package_id in m_selections.versions) {
if (*pdep == dependency)
const name = PackageName(package_id);
return this.selectVersion(name, repository);
}

/// Ditto
void selectVersion(in PackageName name, Repository repository)
{
const dep = Dependency(repository);
this.selectVersionInternal(name, dep);
}

/// Internal implementation of selectVersion
private void selectVersionInternal(in PackageName name, in Dependency dep)
{
if (auto pdep = name.main in m_selections.versions) {
if (*pdep == dep)
return;
}
m_selections.versions[package_id] = dependency;
m_selections.versions[name.main] = dep;
m_dirty = true;
}

Expand All @@ -1880,16 +1908,31 @@ public class SelectedVersions {
}

/// Removes the selection for a particular package.
deprecated("Use the overload that accepts a `PackageName`")
void deselectVersion(string package_id)
{
m_selections.versions.remove(package_id);
const n = PackageName(package_id);
this.deselectVersion(n);
}

/// Ditto
void deselectVersion(in PackageName name)
{
m_selections.versions.remove(name);
m_dirty = true;
}

/// Determines if a particular package has a selection set.
bool hasSelectedVersion(string packageId)
const {
return (packageId in m_selections.versions) !is null;
deprecated("Use the overload that accepts a `PackageName`")
bool hasSelectedVersion(string packageId) const {
const name = PackageName(packageId);
return this.hasSelectedVersion(name);
}

/// Ditto
bool hasSelectedVersion(in PackageName name) const
{
return (name.main in m_selections.versions) !is null;
}

/** Returns the selection for a particular package.
Expand All @@ -1899,10 +1942,18 @@ public class SelectedVersions {
is a path based selection, or its `Dependency.version_` property is
valid and it is a version selection.
*/
Dependency getSelectedVersion(string packageId)
const {
enforce(hasSelectedVersion(packageId));
return m_selections.versions[packageId];
deprecated("Use the overload that accepts a `PackageName`")
Dependency getSelectedVersion(string packageId) const
{
const name = PackageName(packageId);
return this.getSelectedVersion(name);
}

/// Ditto
Dependency getSelectedVersion(in PackageName name) const
{
enforce(hasSelectedVersion(name));
return m_selections.versions[name.main];
}

/** Stores the selections to disk.
Expand Down

0 comments on commit 74877a3

Please sign in to comment.