Newer
Older
dub_jkp / source / dub / registry.d
  1. /**
  2. A package supplier using the registry server.
  3.  
  4. Copyright: © 2012 Matthias Dondorff
  5. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  6. Authors: Matthias Dondorff
  7. */
  8. module dub.registry;
  9.  
  10. import dub.dependency;
  11. import dub.internal.vibecompat.core.file;
  12. import dub.internal.vibecompat.core.log;
  13. import dub.internal.vibecompat.data.json;
  14. import dub.internal.vibecompat.inet.url;
  15. import dub.packagesupplier;
  16. import dub.utils;
  17.  
  18. import std.conv;
  19. import std.exception;
  20. import std.file;
  21.  
  22. private const string PackagesPath = "packages";
  23.  
  24.  
  25. /// Client PackageSupplier using the registry available via registerVpmRegistry
  26. class RegistryPS : PackageSupplier {
  27. this(Url registry) { m_registryUrl = registry; }
  28.  
  29. override string toString() { return "registry at "~m_registryUrl.toString(); }
  30. void retrievePackage(const Path path, const string packageId, const Dependency dep) {
  31. Json best = getBestPackage(packageId, dep);
  32. auto url = m_registryUrl ~ Path("packages/"~packageId~"/"~best["version"].get!string~".zip");
  33. logDiagnostic("Found download URL: '%s'", url);
  34. download(url, path);
  35. }
  36. Json getPackageDescription(const string packageId, const Dependency dep) {
  37. return getBestPackage(packageId, dep);
  38. }
  39. private {
  40. Url m_registryUrl;
  41. Json[string] m_allMetadata;
  42. }
  43. private Json getMetadata(const string packageId) {
  44. if( auto json = packageId in m_allMetadata )
  45. return *json;
  46.  
  47. auto url = m_registryUrl ~ Path(PackagesPath ~ "/" ~ packageId ~ ".json");
  48. logDebug("Downloading metadata for %s", packageId);
  49. logDebug("Getting from %s", url);
  50.  
  51. auto jsonData = cast(string)download(url);
  52. Json json = parseJson(jsonData);
  53. m_allMetadata[packageId] = json;
  54. return json;
  55. }
  56. private Json getBestPackage(const string packageId, const Dependency dep) {
  57. Json md = getMetadata(packageId);
  58. Json best = null;
  59. foreach(json; md["versions"]) {
  60. auto cur = Version(cast(string)json["version"]);
  61. if(dep.matches(cur) && (best == null || Version(cast(string)best["version"]) < cur))
  62. best = json;
  63. }
  64. enforce(best != null, "No package candidate found for "~packageId~" "~dep.toString());
  65. return best;
  66. }
  67. }