Skip to content

Commit

Permalink
Remove PackageName alias this to toString
Browse files Browse the repository at this point in the history
The alias this was originally added to make the transition
less painful, however due to an overload resolution bug,
it makes it harder at the moment, as thje compiler prefers
calling the deprecated `string` overloads over the
`in PackageName` ones. So remove it and a few hacks along
the way.
  • Loading branch information
Geod24 committed Jan 18, 2024
1 parent b7569b0 commit a813a39
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 85 deletions.
8 changes: 4 additions & 4 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -1559,7 +1559,7 @@ class BuildCommand : GenerateCommand {

const baseName = PackageName(packageParts.name).main;
// Found locally
if (dub.packageManager.getBestPackage(baseName, packageParts.range))
if (dub.packageManager.getBestPackage(baseName.toString(), packageParts.range))
return 0;

// Non-interactive, either via flag, or because a version was provided
Expand All @@ -1569,8 +1569,8 @@ class BuildCommand : GenerateCommand {
}
// Otherwise we go the long way of asking the user.
// search for the package and filter versions for exact matches
auto search = dub.searchPackages(baseName)
.map!(tup => tup[1].find!(p => p.name == baseName))
auto search = dub.searchPackages(baseName.toString())
.map!(tup => tup[1].find!(p => p.name == baseName.toString()))
.filter!(ps => !ps.empty);
if (search.empty) {
logWarn("Package '%s' was neither found locally nor online.", packageParts);
Expand Down Expand Up @@ -3133,7 +3133,7 @@ private bool addDependency(Dub dub, ref PackageRecipe recipe, string depspec)
}
else
dep = Dependency(parts.range);
recipe.buildSettings.dependencies[depname] = dep;
recipe.buildSettings.dependencies[depname.toString()] = dep;
logInfo("Adding dependency %s %s", depname, dep.toString());
return true;
}
Expand Down
3 changes: 0 additions & 3 deletions source/dub/dependency.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public struct PackageName
/// Where the separator lies, if any
private size_t separator;

/// For compatibility in `PackageDependency`
alias toString this;

/// Creates a new instance of this struct
public this(string fn) @safe pure
{
Expand Down
54 changes: 27 additions & 27 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ class Dub {
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(name, dep.repository))
if (m_packageManager.loadSCMPackage(name.toString(), dep.repository))
continue;
} else {
if (m_packageManager.getPackage(p, dep.version_)) continue;
Expand All @@ -666,13 +666,13 @@ class Dub {

if (options & UpgradeOptions.dryRun) {
bool any = false;
string rootbasename = PackageName(m_project.rootPackage.name).main;
string rootbasename = PackageName(m_project.rootPackage.name).main.toString();

foreach (p, ver; versions) {
if (!ver.path.empty || !ver.repository.empty) continue;

auto basename = PackageName(p).main;
if (basename == rootbasename) continue;
if (basename.toString() == rootbasename) continue;

if (!m_project.selections.hasSelectedVersion(basename)) {
logInfo("Upgrade", Color.cyan,
Expand All @@ -685,7 +685,7 @@ class Dub {
if (ver.version_ <= sver.version_) continue;
logInfo("Upgrade", Color.cyan,
"%s would be upgraded from %s to %s.",
basename.color(Mode.bold), sver, ver);
basename.toString().color(Mode.bold), sver, ver);
any = true;
}
if (any) logInfo("Use \"%s\" to perform those changes", "dub upgrade".color(Mode.bold));
Expand All @@ -703,7 +703,7 @@ class Dub {
continue;
}
} else if (!ver.repository.empty) {
pack = m_packageManager.loadSCMPackage(name, ver.repository);
pack = m_packageManager.loadSCMPackage(name.toString(), ver.repository);
} else {
assert(ver.isExactVersion, "Resolved dependency is neither path, nor repository, nor exact version based!?");
pack = m_packageManager.getPackage(p, ver.version_);
Expand Down Expand Up @@ -797,7 +797,7 @@ class Dub {

auto tool = PackageName("dscanner");

auto tool_pack = m_packageManager.getBestPackage(tool);
auto tool_pack = m_packageManager.getBestPackage(tool.toString());
if (!tool_pack) {
logInfo("Hint", Color.light_blue, "%s is not present, getting and storing it locally", tool);
tool_pack = this.fetch(tool);
Expand Down Expand Up @@ -904,7 +904,7 @@ class Dub {
Package fetch(string name, in Version vers, PlacementLocation location, FetchOptions options, string reason = "")
{
const n = PackageName(name);
return this.fetch(n, VersionRange(vers, vers), location, options, reason);
return this.fetch(n, VersionRange(vers, vers), options, location, reason);
}

deprecated("Use `fetch(PackageName, VersionRange, [FetchOptions, PlacementLocation, string])`")
Expand Down Expand Up @@ -973,7 +973,7 @@ class Dub {
PackageSupplier supplier;
foreach(ps; m_packageSuppliers){
try {
pinfo = ps.fetchPackageRecipe(basePackageName, range, (options & FetchOptions.usePrerelease) != 0);
pinfo = ps.fetchPackageRecipe(name.main, range, (options & FetchOptions.usePrerelease) != 0);
if (pinfo.type == Json.Type.null_)
continue;
supplier = ps;
Expand All @@ -989,7 +989,7 @@ class Dub {
Version ver = Version(pinfo["version"].get!string);

// always upgrade branch based versions - TODO: actually check if there is a new commit available
Package existing = m_packageManager.getPackage(name, ver, location);
Package existing = m_packageManager.getPackage(name.toString(), ver, location);
if (options & FetchOptions.printOnly) {
if (existing && existing.version_ != ver)
logInfo("A new version for %s is available (%s -> %s). Run \"%s\" to switch.",
Expand Down Expand Up @@ -1023,13 +1023,13 @@ class Dub {
{
import std.zip : ZipException;

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

try {
return m_packageManager.store(path, location, basePackageName, ver);
return m_packageManager.store(path, location, name.main.toString(), ver);
} catch (ZipException e) {
logInfo("Failed to extract zip archive for %s@%s...", name, ver);
// re-throw the exception at the end of the loop
Expand Down Expand Up @@ -1471,7 +1471,7 @@ class Dub {
? PackageName("ddox")
: PackageName(m_project.rootPackage.recipe.ddoxTool);

auto tool_pack = m_packageManager.getBestPackage(tool);
auto tool_pack = m_packageManager.getBestPackage(tool.toString());
if (!tool_pack) {
logInfo("%s is not present, getting and storing it user wide", tool);
tool_pack = this.fetch(tool);
Expand Down Expand Up @@ -1847,18 +1847,18 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
auto basepack = pack.basePackage;

foreach (d; pack.getAllDependenciesRange()) {
auto dbasename = PackageName(d.name).main;
auto dbasename = d.name.main.toString();

// detect dependencies to the root package (or sub packages thereof)
if (dbasename == basepack.name) {
auto absdeppath = d.spec.mapToPath(pack.path).path;
absdeppath.endsWithSlash = true;
auto subpack = m_dub.m_packageManager.getSubPackage(basepack, PackageName(d.name).sub, true);
auto subpack = m_dub.m_packageManager.getSubPackage(basepack, d.name.sub, true);
if (subpack) {
auto desireddeppath = basepack.path;
desireddeppath.endsWithSlash = true;

auto altdeppath = d.name == dbasename ? basepack.path : subpack.path;
auto altdeppath = d.name == d.name.main ? basepack.path : subpack.path;
altdeppath.endsWithSlash = true;

if (!d.spec.path.empty && absdeppath != desireddeppath)
Expand All @@ -1869,7 +1869,7 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
format("Dependency from %s to %s uses wrong path: %s vs. %s",
node.pack, subpack.name, absdeppath.toNativeString(), desireddeppath.toNativeString()));
}
ret ~= TreeNodes(d.name, node.config);
ret ~= TreeNodes(d.name.toString(), node.config);
continue;
}

Expand All @@ -1882,18 +1882,18 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
Dependency dspec = d.spec.mapToPath(pack.path);

// if not upgrading, use the selected version
if (!(m_options & UpgradeOptions.upgrade) && m_selectedVersions.hasSelectedVersion(dbasename))
dspec = m_selectedVersions.getSelectedVersion(dbasename);
if (!(m_options & UpgradeOptions.upgrade) && m_selectedVersions.hasSelectedVersion(d.name.main))
dspec = m_selectedVersions.getSelectedVersion(d.name.main);

// keep selected optional dependencies and avoid non-selected optional-default dependencies by default
if (!m_selectedVersions.bare) {
if (dt == DependencyType.optionalDefault && !m_selectedVersions.hasSelectedVersion(dbasename))
if (dt == DependencyType.optionalDefault && !m_selectedVersions.hasSelectedVersion(d.name.main))
dt = DependencyType.optional;
else if (dt == DependencyType.optional && m_selectedVersions.hasSelectedVersion(dbasename))
else if (dt == DependencyType.optional && m_selectedVersions.hasSelectedVersion(d.name.main))
dt = DependencyType.optionalDefault;
}

ret ~= TreeNodes(d.name, dspec, dt);
ret ~= TreeNodes(d.name.toString(), dspec, dt);
}
return ret.data;
}
Expand Down Expand Up @@ -1930,11 +1930,11 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
}

// shortcut if the referenced package is the root package
if (name.main == m_rootPackage.basePackage.name)
if (name.main.toString() == m_rootPackage.basePackage.name)
return m_rootPackage.basePackage;

if (!dep.repository.empty) {
auto ret = m_dub.packageManager.loadSCMPackage(name, dep.repository);
auto ret = m_dub.packageManager.loadSCMPackage(name.toString(), dep.repository);
return ret !is null && dep.matches(ret.version_) ? ret : null;
}
if (!dep.path.empty) {
Expand All @@ -1948,10 +1948,10 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
}
const vers = dep.version_;

if (auto ret = m_dub.m_packageManager.getBestPackage(name, vers))
if (auto ret = m_dub.m_packageManager.getBestPackage(name.toString(), vers))
return ret;

auto key = name ~ ":" ~ vers.toString();
auto key = name.toString() ~ ":" ~ vers.toString();
if (auto ret = key in m_remotePackages)
return *ret;

Expand All @@ -1978,7 +1978,7 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
FetchOptions fetchOpts;
fetchOpts |= prerelease ? FetchOptions.usePrerelease : FetchOptions.none;
m_dub.fetch(name.main, vers, fetchOpts, m_dub.defaultPlacementLocation, "need sub package description");
auto ret = m_dub.m_packageManager.getBestPackage(name, vers);
auto ret = m_dub.m_packageManager.getBestPackage(name.toString(), vers);
if (!ret) {
logWarn("Package %s %s doesn't have a sub package %s", name.main, dep, name);
return null;
Expand Down
5 changes: 3 additions & 2 deletions source/dub/packagemanager.d
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ class PackageManager {
string gitReference = repo.ref_.chompPrefix("~");
NativePath destination = this.getPackagePath(PlacementLocation.user, name, repo.ref_);

foreach (p; getPackageIterator(name)) {
foreach (p; getPackageIterator(name.toString())) {
if (p.path == destination) {
return p;
}
Expand Down Expand Up @@ -1474,7 +1474,8 @@ package struct Location {
*/
NativePath getPackagePath (in PackageName name, string vers)
{
NativePath result = this.packagePath ~ name.main ~ vers ~ name.main;
NativePath result = this.packagePath ~ name.main.toString() ~ vers ~
name.main.toString();
result.endsWithSlash = true;
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion source/dub/packagesuppliers/filesystem.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class FileSystemPackageSupplier : PackageSupplier {
const zipFileGlob = name.main.toString() ~ "*.zip";
foreach (DirEntry d; dirEntries(m_path.toNativeString(), zipFileGlob, SpanMode.shallow)) {
NativePath p = NativePath(d.name);
auto vers = p.head.name[name.main.length+1..$-4];
auto vers = p.head.name[name.main.toString().length+1..$-4];
if (!isValidVersion(vers)) {
logDebug("Ignoring entry '%s' because it isn't a version of package '%s'", p, name.main);
continue;
Expand Down
10 changes: 8 additions & 2 deletions source/dub/packagesuppliers/maven.d
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,18 @@ class MavenRegistryPackageSupplier : PackageSupplier {
else throw e;
}

auto json = Json(["name": Json(name.main), "versions": Json.emptyArray]);
auto json = Json([
"name": Json(name.main.toString()),
"versions": Json.emptyArray
]);
auto xml = new DocumentParser(xmlData);

xml.onStartTag["versions"] = (ElementParser xml) {
xml.onEndTag["version"] = (in Element e) {
json["versions"] ~= serializeToJson(["name": name.main, "version": e.text]);
json["versions"] ~= serializeToJson([
"name": name.main.toString(),
"version": e.text,
]);
};
xml.parse();
};
Expand Down
8 changes: 5 additions & 3 deletions source/dub/packagesuppliers/registry.d
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ class RegistryPackageSupplier : PackageSupplier {
if (best.type != Json.Type.null_)
{
auto vers = best["version"].get!string;
ret = m_registryUrl ~ NativePath(PackagesPath~"/"~name.main~"/"~vers~".zip");
ret = m_registryUrl ~ NativePath(
"%s/%s/%s.zip".format(PackagesPath, name.main, vers));
}
return ret;
}
Expand Down Expand Up @@ -106,7 +107,8 @@ class RegistryPackageSupplier : PackageSupplier {
auto url = m_registryUrl ~ NativePath("api/packages/infos");

url.queryString = "packages=" ~
encodeComponent(`["` ~ name.main ~ `"]`) ~ "&include_dependencies=true&minimize=true";
encodeComponent(`["` ~ name.main.toString() ~ `"]`) ~
"&include_dependencies=true&minimize=true";

logDebug("Downloading metadata for %s", name.main);
string jsonData;
Expand All @@ -119,7 +121,7 @@ class RegistryPackageSupplier : PackageSupplier {
logDebug("adding %s to metadata cache", pkg);
m_metadataCache[PackageName(pkg)] = CacheEntry(info, now);
}
return json[name.main];
return json[name.main.toString()];
}

SearchResult[] searchPackages(string query) {
Expand Down
Loading

0 comments on commit a813a39

Please sign in to comment.