Newer
Older
dub_jkp / source / dub / packagesupplier.d
  1. /**
  2. A package supplier, able to get some packages to the local FS.
  3.  
  4. Copyright: © 2012-2013 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.packagesupplier;
  9.  
  10. import dub.dependency;
  11. import dub.internal.utils;
  12. import dub.internal.vibecompat.core.log;
  13. import dub.internal.vibecompat.core.file;
  14. import dub.internal.vibecompat.data.json;
  15. import dub.internal.vibecompat.inet.url;
  16.  
  17. import std.file;
  18. import std.exception;
  19. import std.zip;
  20. import std.conv;
  21.  
  22. /// Supplies packages, this is done by supplying the latest possible version
  23. /// which is available.
  24. interface PackageSupplier {
  25. /// path: absolute path to store the package (usually in a zip format)
  26. void retrievePackage(Path path, string packageId, Dependency dep);
  27. /// returns the metadata for the package
  28. Json getPackageDescription(string packageId, Dependency dep);
  29.  
  30. /// Returns a hunman readable representation of the supplier
  31. string toString();
  32. }
  33.  
  34. class FileSystemPackageSupplier : PackageSupplier {
  35. private {
  36. Path m_path;
  37. }
  38.  
  39. this(Path root) { m_path = root; }
  40.  
  41. override string toString() { return "file repository at "~m_path.toNativeString(); }
  42. void retrievePackage(Path path, string packageId, Dependency dep)
  43. {
  44. enforce(path.absolute);
  45. logInfo("Storing package '"~packageId~"', version requirements: %s", dep);
  46. auto filename = bestPackageFile(packageId, dep);
  47. enforce(existsFile(filename));
  48. copyFile(filename, path);
  49. }
  50. Json getPackageDescription(string packageId, Dependency dep)
  51. {
  52. auto filename = bestPackageFile(packageId, dep);
  53. return jsonFromZip(filename, "package.json");
  54. }
  55. private Path bestPackageFile(string packageId, Dependency dep)
  56. const {
  57. Version bestVersion = Version.RELEASE;
  58. foreach (DirEntry d; dirEntries(m_path.toNativeString(), packageId~"*", SpanMode.shallow)) {
  59. Path p = Path(d.name);
  60. logDebug("Entry: %s", p);
  61. enforce(to!string(p.head)[$-4..$] == ".zip");
  62. string vers = to!string(p.head)[packageId.length+1..$-4];
  63. logDebug("Version string: "~vers);
  64. Version v = Version(vers);
  65. if (v > bestVersion && dep.matches(v)) {
  66. bestVersion = v;
  67. }
  68. }
  69. auto fileName = m_path ~ (packageId ~ "_" ~ to!string(bestVersion) ~ ".zip");
  70. if (bestVersion == Version.RELEASE || !existsFile(fileName))
  71. throw new Exception("No matching package found");
  72. logDiagnostic("Found best matching package: '%s'", fileName);
  73. return fileName;
  74. }
  75. }
  76.  
  77.  
  78. /// Client PackageSupplier using the registry available via registerVpmRegistry
  79. class RegistryPackageSupplier : PackageSupplier {
  80. private {
  81. Url m_registryUrl;
  82. Json[string] m_allMetadata;
  83. }
  84. this(Url registry)
  85. {
  86. m_registryUrl = registry;
  87. }
  88.  
  89. override string toString() { return "registry at "~m_registryUrl.toString(); }
  90. void retrievePackage(Path path, string packageId, Dependency dep)
  91. {
  92. Json best = getBestPackage(packageId, dep);
  93. auto url = m_registryUrl ~ Path(PackagesPath~"/"~packageId~"/"~best["version"].get!string~".zip");
  94. logDiagnostic("Found download URL: '%s'", url);
  95. download(url, path);
  96. }
  97. Json getPackageDescription(string packageId, Dependency dep)
  98. {
  99. return getBestPackage(packageId, dep);
  100. }
  101. private Json getMetadata(string packageId)
  102. {
  103. if (auto json = packageId in m_allMetadata)
  104. return *json;
  105.  
  106. auto url = m_registryUrl ~ Path(PackagesPath ~ "/" ~ packageId ~ ".json");
  107. logDebug("Downloading metadata for %s", packageId);
  108. logDebug("Getting from %s", url);
  109.  
  110. auto jsonData = cast(string)download(url);
  111. Json json = parseJson(jsonData);
  112. m_allMetadata[packageId] = json;
  113. return json;
  114. }
  115. private Json getBestPackage(string packageId, Dependency dep)
  116. {
  117. Json md = getMetadata(packageId);
  118. Json best = null;
  119. foreach (json; md["versions"]) {
  120. auto cur = Version(cast(string)json["version"]);
  121. if (dep.matches(cur) && (best == null || Version(cast(string)best["version"]) < cur))
  122. best = json;
  123. }
  124. enforce(best != null, "No package candidate found for "~packageId~" "~dep.toString());
  125. return best;
  126. }
  127. }
  128.  
  129. private enum PackagesPath = "packages";