Skip to content

Commit

Permalink
Added a getPackageNames function to the PackageSupplier interface, an…
Browse files Browse the repository at this point in the history
…d an implementation for the RegistryPackageSupplier. Currently, FileSystemPackageSupplier.getPackageNames is not implemented.

The catch block of Dub.createEmptyPackage() now uses this and tries to find close matches to what the user entered.
Currently, "close matches" are defined as anything with a levenshteinDistance of <=4.
  • Loading branch information
grogancolin committed Dec 8, 2014
1 parent 19b62e0 commit 5a93ab4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion source/dub/dub.d
Expand Up @@ -616,7 +616,18 @@ class Dub {
auto versionStrings = ps.getVersions(dep);
depVers[dep] = versionStrings[$-1].toString;
} catch(Exception e){
logError("Error, no package %s found. Ignoring...", dep);
auto packages = ps.getPackageNames();
string[][size_t] lds; //holds the levenshteinDistance from dep for each package
foreach(pack; packages){
lds[dep.levenshteinDistance(pack)] ~= pack;
}
auto closestKey = lds.keys.sort.front;
if(closestKey <= 4){
logError("Error, no package \"%s\" found. Did you mean %s?", dep, lds[closestKey]);
} else{
logError("Error, no package \"%s\" found. Exiting...", dep);
}
return;
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions source/dub/packagesupplier.d
Expand Up @@ -41,6 +41,9 @@ interface PackageSupplier {

/// perform cache operation
void cacheOp(Path cacheDir, CacheOp op);

/// get all package names for this supplier
string[] getPackageNames();
}

/// operations on package supplier cache
Expand Down Expand Up @@ -92,6 +95,10 @@ class FileSystemPackageSupplier : PackageSupplier {
void cacheOp(Path cacheDir, CacheOp op) {
}

string[] getPackageNames(){
assert(false, "FileSystemPackageSupplier.getPackageNames, not implemented yet");
}

private Path bestPackageFile(string packageId, Dependency dep, bool pre_release)
{
Path toPath(Version ver) {
Expand Down Expand Up @@ -148,11 +155,23 @@ class RegistryPackageSupplier : PackageSupplier {
download(url, path);
}


Json getPackageDescription(string packageId, Dependency dep, bool pre_release)
{
return getBestPackage(packageId, dep, pre_release);
}

string[] getPackageNames(){
import std.array : replace;
auto url = m_registryUrl ~ Path(PackagesPath~"/index.json");
Json data = (cast(string)download(url)).parseJsonString();
string[] packages;
foreach(ele; data){
packages ~= ele.toString().replace("\"", "");
}
return packages;
}

void cacheOp(Path cacheDir, CacheOp op)
{
auto path = cacheDir ~ cacheFileName;
Expand Down

0 comments on commit 5a93ab4

Please sign in to comment.