Newer
Older
dub_jkp / source / dub / packagemanager.d
  1. /**
  2. Management of packages on the local computer.
  3.  
  4. Copyright: © 2012-2016 rejectedsoftware e.K.
  5. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  6. Authors: Sönke Ludwig, Matthias Dondorff
  7. */
  8. module dub.packagemanager;
  9.  
  10. import dub.dependency;
  11. import dub.internal.utils;
  12. import dub.internal.vibecompat.core.file;
  13. import dub.internal.vibecompat.data.json;
  14. import dub.internal.vibecompat.inet.path;
  15. import dub.internal.logging;
  16. import dub.package_;
  17.  
  18. import std.algorithm : countUntil, filter, sort, canFind, remove;
  19. import std.array;
  20. import std.conv;
  21. import std.digest.sha;
  22. import std.encoding : sanitize;
  23. import std.exception;
  24. import std.file;
  25. import std.string;
  26. import std.sumtype;
  27. import std.zip;
  28.  
  29.  
  30. /// Indicates where a package has been or should be placed to.
  31. public enum PlacementLocation {
  32. /// Packages retrieved with 'local' will be placed in the current folder
  33. /// using the package name as destination.
  34. local,
  35. /// Packages with 'userWide' will be placed in a folder accessible by
  36. /// all of the applications from the current user.
  37. user,
  38. /// Packages retrieved with 'systemWide' will be placed in a shared folder,
  39. /// which can be accessed by all users of the system.
  40. system,
  41. }
  42.  
  43. /// The PackageManager can retrieve present packages and get / remove
  44. /// packages.
  45. class PackageManager {
  46. private {
  47. Location[] m_repositories;
  48. NativePath[] m_searchPath;
  49. Package[] m_packages;
  50. Package[] m_temporaryPackages;
  51. bool m_disableDefaultSearchPaths = false;
  52. }
  53.  
  54. /**
  55. Instantiate an instance with a single search path
  56.  
  57. This constructor is used when dub is invoked with the '--bar' CLI switch.
  58. The instance will not look up the default repositories
  59. (e.g. ~/.dub/packages), using only `path` instead.
  60.  
  61. Params:
  62. path = Path of the single repository
  63. */
  64. this(NativePath path)
  65. {
  66. this.m_searchPath = [ path ];
  67. this.m_disableDefaultSearchPaths = true;
  68. this.refresh(true);
  69. }
  70.  
  71. deprecated("Use the overload which accepts 3 `NativePath` arguments")
  72. this(NativePath user_path, NativePath system_path, bool refresh_packages = true)
  73. {
  74. m_repositories = [
  75. Location(user_path ~ "packages/"),
  76. Location(system_path ~ "packages/")];
  77.  
  78. if (refresh_packages) refresh(true);
  79. }
  80.  
  81. this(NativePath package_path, NativePath user_path, NativePath system_path, bool refresh_packages = true)
  82. {
  83. m_repositories = [
  84. Location(package_path ~ ".dub/packages/"),
  85. Location(user_path ~ "packages/"),
  86. Location(system_path ~ "packages/")];
  87.  
  88. if (refresh_packages) refresh(true);
  89. }
  90.  
  91. /** Gets/sets the list of paths to search for local packages.
  92. */
  93. @property void searchPath(NativePath[] paths)
  94. {
  95. if (paths == m_searchPath) return;
  96. m_searchPath = paths.dup;
  97. refresh(false);
  98. }
  99. /// ditto
  100. @property const(NativePath)[] searchPath() const { return m_searchPath; }
  101.  
  102. /** Disables searching DUB's predefined search paths.
  103. */
  104. deprecated("Instantiate a PackageManager instance with the single-argument constructor: `new PackageManager(path)`")
  105. @property void disableDefaultSearchPaths(bool val)
  106. {
  107. if (val == m_disableDefaultSearchPaths) return;
  108. m_disableDefaultSearchPaths = val;
  109. refresh(true);
  110. }
  111.  
  112. /** Returns the effective list of search paths, including default ones.
  113. */
  114. @property const(NativePath)[] completeSearchPath()
  115. const {
  116. auto ret = appender!(NativePath[])();
  117. ret.put(cast(NativePath[])m_searchPath); // work around Phobos 17251
  118. if (!m_disableDefaultSearchPaths) {
  119. foreach (ref repo; m_repositories) {
  120. ret.put(cast(NativePath[])repo.searchPath);
  121. ret.put(cast(NativePath)repo.packagePath);
  122. }
  123. }
  124. return ret.data;
  125. }
  126.  
  127. /** Sets additional (read-only) package cache paths to search for packages.
  128.  
  129. Cache paths have the same structure as the default cache paths, such as
  130. ".dub/packages/".
  131.  
  132. Note that previously set custom paths will be removed when setting this
  133. property.
  134. */
  135. @property void customCachePaths(NativePath[] custom_cache_paths)
  136. {
  137. import std.algorithm.iteration : map;
  138. import std.array : array;
  139.  
  140. m_repositories.length = PlacementLocation.max+1;
  141. m_repositories ~= custom_cache_paths.map!(p => Location(p)).array;
  142.  
  143. refresh(false);
  144. }
  145.  
  146.  
  147. /** Looks up a specific package.
  148.  
  149. Looks up a package matching the given version/path in the set of
  150. registered packages. The lookup order is done according the the
  151. usual rules (see getPackageIterator).
  152.  
  153. Params:
  154. name = The name of the package
  155. ver = The exact version of the package to query
  156. path = An exact path that the package must reside in. Note that
  157. the package must still be registered in the package manager.
  158. enable_overrides = Apply the local package override list before
  159. returning a package (enabled by default)
  160.  
  161. Returns:
  162. The matching package or null if no match was found.
  163. */
  164. Package getPackage(string name, Version ver, bool enable_overrides = true)
  165. {
  166. if (enable_overrides) {
  167. foreach (ref repo; m_repositories)
  168. foreach (ovr; repo.overrides)
  169. if (ovr.package_ == name && ovr.source.matches(ver)) {
  170. Package pack = ovr.target.match!(
  171. (NativePath path) => getOrLoadPackage(path),
  172. (Version vers) => getPackage(name, vers, false),
  173. );
  174. if (pack) return pack;
  175.  
  176. ovr.target.match!(
  177. (any) {
  178. logWarn("Package override %s %s -> '%s' doesn't reference an existing package.",
  179. ovr.package_, ovr.version_, any);
  180. },
  181. );
  182. }
  183. }
  184.  
  185. foreach (p; getPackageIterator(name))
  186. if (p.version_.matches(ver, isManagedPackage(p) ? VersionMatchMode.strict : VersionMatchMode.standard))
  187. return p;
  188.  
  189. return null;
  190. }
  191.  
  192. /// ditto
  193. Package getPackage(string name, string ver, bool enable_overrides = true)
  194. {
  195. return getPackage(name, Version(ver), enable_overrides);
  196. }
  197.  
  198. /// ditto
  199. Package getPackage(string name, Version ver, NativePath path)
  200. {
  201. foreach (p; getPackageIterator(name)) {
  202. auto pvm = isManagedPackage(p) ? VersionMatchMode.strict : VersionMatchMode.standard;
  203. if (p.version_.matches(ver, pvm) && p.path.startsWith(path))
  204. return p;
  205. }
  206. return null;
  207. }
  208.  
  209. /// ditto
  210. Package getPackage(string name, string ver, NativePath path)
  211. {
  212. return getPackage(name, Version(ver), path);
  213. }
  214.  
  215. /// ditto
  216. Package getPackage(string name, NativePath path)
  217. {
  218. foreach( p; getPackageIterator(name) )
  219. if (p.path.startsWith(path))
  220. return p;
  221. return null;
  222. }
  223.  
  224.  
  225. /** Looks up the first package matching the given name.
  226. */
  227. Package getFirstPackage(string name)
  228. {
  229. foreach (ep; getPackageIterator(name))
  230. return ep;
  231. return null;
  232. }
  233.  
  234. /** Looks up the latest package matching the given name.
  235. */
  236. deprecated("Use `getBestPackage` with `name, Dependency.any` instead")
  237. Package getLatestPackage(string name)
  238. {
  239. Package pkg;
  240. foreach (ep; getPackageIterator(name))
  241. if (pkg is null || pkg.version_ < ep.version_)
  242. pkg = ep;
  243. return pkg;
  244. }
  245.  
  246. /** For a given package path, returns the corresponding package.
  247.  
  248. If the package is already loaded, a reference is returned. Otherwise
  249. the package gets loaded and cached for the next call to this function.
  250.  
  251. Params:
  252. path = NativePath to the root directory of the package
  253. recipe_path = Optional path to the recipe file of the package
  254. allow_sub_packages = Also return a sub package if it resides in the given folder
  255.  
  256. Returns: The packages loaded from the given path
  257. Throws: Throws an exception if no package can be loaded
  258. */
  259. Package getOrLoadPackage(NativePath path, NativePath recipe_path = NativePath.init, bool allow_sub_packages = false)
  260. {
  261. path.endsWithSlash = true;
  262. foreach (p; getPackageIterator())
  263. if (p.path == path && (!p.parentPackage || (allow_sub_packages && p.parentPackage.path != p.path)))
  264. return p;
  265. auto pack = Package.load(path, recipe_path);
  266. addPackages(m_temporaryPackages, pack);
  267. return pack;
  268. }
  269.  
  270. /** For a given SCM repository, returns the corresponding package.
  271.  
  272. An SCM repository is provided as its remote URL, the repository is cloned
  273. and in the dependency speicfied commit is checked out.
  274.  
  275. If the target directory already exists, just returns the package
  276. without cloning.
  277.  
  278. Params:
  279. name = Package name
  280. dependency = Dependency that contains the repository URL and a specific commit
  281.  
  282. Returns:
  283. The package loaded from the given SCM repository or null if the
  284. package couldn't be loaded.
  285. */
  286. deprecated("Use the overload that accepts a `dub.dependency : Repository`")
  287. Package loadSCMPackage(string name, Dependency dependency)
  288. in { assert(!dependency.repository.empty); }
  289. do { return this.loadSCMPackage(name, dependency.repository); }
  290.  
  291. /// Ditto
  292. Package loadSCMPackage(string name, Repository repo)
  293. in { assert(!repo.empty); }
  294. do {
  295. Package pack;
  296.  
  297. final switch (repo.kind)
  298. {
  299. case repo.Kind.git:
  300. pack = loadGitPackage(name, repo);
  301. }
  302. if (pack !is null) {
  303. addPackages(m_temporaryPackages, pack);
  304. }
  305. return pack;
  306. }
  307.  
  308. private Package loadGitPackage(string name, in Repository repo)
  309. {
  310. import dub.internal.git : cloneRepository;
  311.  
  312. if (!repo.ref_.startsWith("~") && !repo.ref_.isGitHash) {
  313. return null;
  314. }
  315.  
  316. string gitReference = repo.ref_.chompPrefix("~");
  317. NativePath destination = getPackagePath(
  318. m_repositories[PlacementLocation.user].packagePath,
  319. name, repo.ref_);
  320. // For libraries leaking their import path
  321. destination ~= name;
  322. destination.endsWithSlash = true;
  323.  
  324. foreach (p; getPackageIterator(name)) {
  325. if (p.path == destination) {
  326. return p;
  327. }
  328. }
  329.  
  330. if (!cloneRepository(repo.remote, gitReference, destination.toNativeString())) {
  331. return null;
  332. }
  333.  
  334. return Package.load(destination);
  335. }
  336.  
  337. /**
  338. * Get the final destination a specific package needs to be stored in.
  339. *
  340. * Note that there needs to be an extra level for libraries like `ae`
  341. * which expects their containing folder to have an exact name and use
  342. * `importPath "../"`.
  343. *
  344. * Hence the final format should be `$BASE/$NAME-$VERSION/$NAME`,
  345. * but this function returns `$BASE/$NAME-$VERSION/`
  346. */
  347. package(dub) static NativePath getPackagePath (NativePath base, string name, string vers)
  348. {
  349. // + has special meaning for Optlink
  350. string clean_vers = vers.chompPrefix("~").replace("+", "_");
  351. NativePath result = base ~ (name ~ "-" ~ clean_vers);
  352. result.endsWithSlash = true;
  353. return result;
  354. }
  355.  
  356. /** Searches for the latest version of a package matching the given dependency.
  357. */
  358. Package getBestPackage(string name, Dependency version_spec, bool enable_overrides = true)
  359. {
  360. Package ret;
  361. foreach (p; getPackageIterator(name)) {
  362. auto vmm = isManagedPackage(p) ? VersionMatchMode.strict : VersionMatchMode.standard;
  363. if (version_spec.matches(p.version_, vmm) && (!ret || p.version_ > ret.version_))
  364. ret = p;
  365. }
  366.  
  367. if (enable_overrides && ret) {
  368. if (auto ovr = getPackage(name, ret.version_))
  369. return ovr;
  370. }
  371. return ret;
  372. }
  373.  
  374. /// ditto
  375. Package getBestPackage(string name, string version_spec)
  376. {
  377. return getBestPackage(name, Dependency(version_spec));
  378. }
  379.  
  380. /** Gets the a specific sub package.
  381.  
  382. In contrast to `Package.getSubPackage`, this function supports path
  383. based sub packages.
  384.  
  385. Params:
  386. base_package = The package from which to get a sub package
  387. sub_name = Name of the sub package (not prefixed with the base
  388. package name)
  389. silent_fail = If set to true, the function will return `null` if no
  390. package is found. Otherwise will throw an exception.
  391.  
  392. */
  393. Package getSubPackage(Package base_package, string sub_name, bool silent_fail)
  394. {
  395. foreach (p; getPackageIterator(base_package.name~":"~sub_name))
  396. if (p.parentPackage is base_package)
  397. return p;
  398. enforce(silent_fail, "Sub package \""~base_package.name~":"~sub_name~"\" doesn't exist.");
  399. return null;
  400. }
  401.  
  402.  
  403. /** Determines if a package is managed by DUB.
  404.  
  405. Managed packages can be upgraded and removed.
  406. */
  407. bool isManagedPackage(Package pack)
  408. const {
  409. auto ppath = pack.basePackage.path;
  410. return isManagedPath(ppath);
  411. }
  412.  
  413. /** Determines if a specific path is within a DUB managed package folder.
  414.  
  415. By default, managed folders are "~/.dub/packages" and
  416. "/var/lib/dub/packages".
  417. */
  418. bool isManagedPath(NativePath path)
  419. const {
  420. foreach (rep; m_repositories) {
  421. NativePath rpath = rep.packagePath;
  422. if (path.startsWith(rpath))
  423. return true;
  424. }
  425. return false;
  426. }
  427.  
  428. /** Enables iteration over all known local packages.
  429.  
  430. Returns: A delegate suitable for use with `foreach` is returned.
  431. */
  432. int delegate(int delegate(ref Package)) getPackageIterator()
  433. {
  434. int iterator(int delegate(ref Package) del)
  435. {
  436. foreach (tp; m_temporaryPackages)
  437. if (auto ret = del(tp)) return ret;
  438.  
  439. // first search local packages
  440. foreach (ref repo; m_repositories)
  441. foreach (p; repo.localPackages)
  442. if (auto ret = del(p)) return ret;
  443.  
  444. // and then all packages gathered from the search path
  445. foreach( p; m_packages )
  446. if( auto ret = del(p) )
  447. return ret;
  448. return 0;
  449. }
  450.  
  451. return &iterator;
  452. }
  453.  
  454. /** Enables iteration over all known local packages with a certain name.
  455.  
  456. Returns: A delegate suitable for use with `foreach` is returned.
  457. */
  458. int delegate(int delegate(ref Package)) getPackageIterator(string name)
  459. {
  460. int iterator(int delegate(ref Package) del)
  461. {
  462. foreach (p; getPackageIterator())
  463. if (p.name == name)
  464. if (auto ret = del(p)) return ret;
  465. return 0;
  466. }
  467.  
  468. return &iterator;
  469. }
  470.  
  471.  
  472. /** Returns a list of all package overrides for the given scope.
  473. */
  474. const(PackageOverride)[] getOverrides(PlacementLocation scope_)
  475. const {
  476. return m_repositories[scope_].overrides;
  477. }
  478.  
  479. /** Adds a new override for the given package.
  480. */
  481. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  482. void addOverride(PlacementLocation scope_, string package_, Dependency version_spec, Version target)
  483. {
  484. m_repositories[scope_].overrides ~= PackageOverride(package_, version_spec, target);
  485. writeLocalPackageOverridesFile(scope_);
  486. }
  487. /// ditto
  488. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  489. void addOverride(PlacementLocation scope_, string package_, Dependency version_spec, NativePath target)
  490. {
  491. m_repositories[scope_].overrides ~= PackageOverride(package_, version_spec, target);
  492. writeLocalPackageOverridesFile(scope_);
  493. }
  494.  
  495. /// Ditto
  496. void addOverride(PlacementLocation scope_, string package_, VersionRange source, Version target)
  497. {
  498. m_repositories[scope_].overrides ~= PackageOverride(package_, source, target);
  499. writeLocalPackageOverridesFile(scope_);
  500. }
  501. /// ditto
  502. void addOverride(PlacementLocation scope_, string package_, VersionRange source, NativePath target)
  503. {
  504. m_repositories[scope_].overrides ~= PackageOverride(package_, source, target);
  505. writeLocalPackageOverridesFile(scope_);
  506. }
  507.  
  508. /** Removes an existing package override.
  509. */
  510. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  511. void removeOverride(PlacementLocation scope_, string package_, Dependency version_spec)
  512. {
  513. version_spec.visit!(
  514. (VersionRange src) => this.removeOverride(scope_, package_, src),
  515. (any) { throw new Exception(format("No override exists for %s %s", package_, version_spec)); },
  516. );
  517. }
  518.  
  519. void removeOverride(PlacementLocation scope_, string package_, VersionRange src)
  520. {
  521. Location* rep = &m_repositories[scope_];
  522. foreach (i, ovr; rep.overrides) {
  523. if (ovr.package_ != package_ || ovr.source != src)
  524. continue;
  525. rep.overrides = rep.overrides[0 .. i] ~ rep.overrides[i+1 .. $];
  526. writeLocalPackageOverridesFile(scope_);
  527. return;
  528. }
  529. throw new Exception(format("No override exists for %s %s", package_, src));
  530. }
  531.  
  532. /// Extracts the package supplied as a path to it's zip file to the
  533. /// destination and sets a version field in the package description.
  534. Package storeFetchedPackage(NativePath zip_file_path, Json package_info, NativePath destination)
  535. {
  536. import std.range : walkLength;
  537.  
  538. auto package_name = package_info["name"].get!string;
  539. auto package_version = package_info["version"].get!string;
  540.  
  541. logDebug("Placing package '%s' version '%s' to location '%s' from file '%s'",
  542. package_name, package_version, destination.toNativeString(), zip_file_path.toNativeString());
  543.  
  544. if( existsFile(destination) ){
  545. throw new Exception(format("%s (%s) needs to be removed from '%s' prior placement.", package_name, package_version, destination));
  546. }
  547.  
  548. // open zip file
  549. ZipArchive archive;
  550. {
  551. logDebug("Opening file %s", zip_file_path);
  552. auto f = openFile(zip_file_path, FileMode.read);
  553. scope(exit) f.close();
  554. archive = new ZipArchive(f.readAll());
  555. }
  556.  
  557. logDebug("Extracting from zip.");
  558.  
  559. // In a github zip, the actual contents are in a subfolder
  560. alias PSegment = typeof(NativePath.init.head);
  561. PSegment[] zip_prefix;
  562. outer: foreach(ArchiveMember am; archive.directory) {
  563. auto path = NativePath(am.name).bySegment.array;
  564. foreach (fil; packageInfoFiles)
  565. if (path.length == 2 && path[$-1].name == fil.filename) {
  566. zip_prefix = path[0 .. $-1];
  567. break outer;
  568. }
  569. }
  570.  
  571. logDebug("zip root folder: %s", zip_prefix);
  572.  
  573. NativePath getCleanedPath(string fileName) {
  574. auto path = NativePath(fileName);
  575. if (zip_prefix.length && !path.bySegment.startsWith(zip_prefix)) return NativePath.init;
  576. static if (is(typeof(path[0 .. 1]))) return path[zip_prefix.length .. $];
  577. else return NativePath(path.bySegment.array[zip_prefix.length .. $]);
  578. }
  579.  
  580. static void setAttributes(string path, ArchiveMember am)
  581. {
  582. import std.datetime : DosFileTimeToSysTime;
  583.  
  584. auto mtime = DosFileTimeToSysTime(am.time);
  585. setTimes(path, mtime, mtime);
  586. if (auto attrs = am.fileAttributes)
  587. std.file.setAttributes(path, attrs);
  588. }
  589.  
  590. // extract & place
  591. mkdirRecurse(destination.toNativeString());
  592. logDebug("Copying all files...");
  593. int countFiles = 0;
  594. foreach(ArchiveMember a; archive.directory) {
  595. auto cleanedPath = getCleanedPath(a.name);
  596. if(cleanedPath.empty) continue;
  597. auto dst_path = destination ~ cleanedPath;
  598.  
  599. logDebug("Creating %s", cleanedPath);
  600. if( dst_path.endsWithSlash ){
  601. if( !existsDirectory(dst_path) )
  602. mkdirRecurse(dst_path.toNativeString());
  603. } else {
  604. if( !existsDirectory(dst_path.parentPath) )
  605. mkdirRecurse(dst_path.parentPath.toNativeString());
  606. {
  607. auto dstFile = openFile(dst_path, FileMode.createTrunc);
  608. scope(exit) dstFile.close();
  609. dstFile.put(archive.expand(a));
  610. }
  611. setAttributes(dst_path.toNativeString(), a);
  612. ++countFiles;
  613. }
  614. }
  615. logDebug("%s file(s) copied.", to!string(countFiles));
  616.  
  617. // overwrite dub.json (this one includes a version field)
  618. auto pack = Package.load(destination, NativePath.init, null, package_info["version"].get!string);
  619.  
  620. if (pack.recipePath.head != defaultPackageFilename)
  621. // Storeinfo saved a default file, this could be different to the file from the zip.
  622. removeFile(pack.recipePath);
  623. pack.storeInfo();
  624. addPackages(m_packages, pack);
  625. return pack;
  626. }
  627.  
  628. /// Removes the given the package.
  629. void remove(in Package pack)
  630. {
  631. logDebug("Remove %s, version %s, path '%s'", pack.name, pack.version_, pack.path);
  632. enforce(!pack.path.empty, "Cannot remove package "~pack.name~" without a path.");
  633.  
  634. // remove package from repositories' list
  635. bool found = false;
  636. bool removeFrom(Package[] packs, in Package pack) {
  637. auto packPos = countUntil!("a.path == b.path")(packs, pack);
  638. if(packPos != -1) {
  639. packs = .remove(packs, packPos);
  640. return true;
  641. }
  642. return false;
  643. }
  644. foreach(repo; m_repositories) {
  645. if(removeFrom(repo.localPackages, pack)) {
  646. found = true;
  647. break;
  648. }
  649. }
  650. if(!found)
  651. found = removeFrom(m_packages, pack);
  652. enforce(found, "Cannot remove, package not found: '"~ pack.name ~"', path: " ~ to!string(pack.path));
  653.  
  654. logDebug("About to delete root folder for package '%s'.", pack.path);
  655. rmdirRecurse(pack.path.toNativeString());
  656. logInfo("Removed", Color.yellow, "%s %s", pack.name.color(Mode.bold), pack.version_);
  657. }
  658.  
  659. /// Compatibility overload. Use the version without a `force_remove` argument instead.
  660. deprecated("Use `remove(pack)` directly instead, the boolean has no effect")
  661. void remove(in Package pack, bool force_remove)
  662. {
  663. remove(pack);
  664. }
  665.  
  666. Package addLocalPackage(NativePath path, string verName, PlacementLocation type)
  667. {
  668. path.endsWithSlash = true;
  669. auto pack = Package.load(path);
  670. enforce(pack.name.length, "The package has no name, defined in: " ~ path.toString());
  671. if (verName.length)
  672. pack.version_ = Version(verName);
  673.  
  674. // don't double-add packages
  675. Package[]* packs = &m_repositories[type].localPackages;
  676. foreach (p; *packs) {
  677. if (p.path == path) {
  678. enforce(p.version_ == pack.version_, "Adding the same local package twice with differing versions is not allowed.");
  679. logInfo("Package is already registered: %s (version: %s)", p.name, p.version_);
  680. return p;
  681. }
  682. }
  683.  
  684. addPackages(*packs, pack);
  685.  
  686. writeLocalPackageList(type);
  687.  
  688. logInfo("Registered package: %s (version: %s)", pack.name, pack.version_);
  689. return pack;
  690. }
  691.  
  692. void removeLocalPackage(NativePath path, PlacementLocation type)
  693. {
  694. path.endsWithSlash = true;
  695.  
  696. Package[]* packs = &m_repositories[type].localPackages;
  697. size_t[] to_remove;
  698. foreach( i, entry; *packs )
  699. if( entry.path == path )
  700. to_remove ~= i;
  701. enforce(to_remove.length > 0, "No "~type.to!string()~" package found at "~path.toNativeString());
  702.  
  703. string[Version] removed;
  704. foreach_reverse( i; to_remove ) {
  705. removed[(*packs)[i].version_] = (*packs)[i].name;
  706. *packs = (*packs)[0 .. i] ~ (*packs)[i+1 .. $];
  707. }
  708.  
  709. writeLocalPackageList(type);
  710.  
  711. foreach(ver, name; removed)
  712. logInfo("Deregistered package: %s (version: %s)", name, ver);
  713. }
  714.  
  715. /// For the given type add another path where packages will be looked up.
  716. void addSearchPath(NativePath path, PlacementLocation type)
  717. {
  718. m_repositories[type].searchPath ~= path;
  719. writeLocalPackageList(type);
  720. }
  721.  
  722. /// Removes a search path from the given type.
  723. void removeSearchPath(NativePath path, PlacementLocation type)
  724. {
  725. m_repositories[type].searchPath = m_repositories[type].searchPath.filter!(p => p != path)().array();
  726. writeLocalPackageList(type);
  727. }
  728.  
  729. void refresh(bool refresh_existing_packages)
  730. {
  731. logDiagnostic("Refreshing local packages (refresh existing: %s)...", refresh_existing_packages);
  732.  
  733. // load locally defined packages
  734. void scanLocalPackages(PlacementLocation type)
  735. {
  736. NativePath list_path = m_repositories[type].packagePath;
  737. Package[] packs;
  738. NativePath[] paths;
  739. try {
  740. auto local_package_file = list_path ~ LocalPackagesFilename;
  741. logDiagnostic("Looking for local package map at %s", local_package_file.toNativeString());
  742. if( !existsFile(local_package_file) ) return;
  743. logDiagnostic("Try to load local package map at %s", local_package_file.toNativeString());
  744. auto packlist = jsonFromFile(list_path ~ LocalPackagesFilename);
  745. enforce(packlist.type == Json.Type.array, LocalPackagesFilename~" must contain an array.");
  746. foreach( pentry; packlist ){
  747. try {
  748. auto name = pentry["name"].get!string;
  749. auto path = NativePath(pentry["path"].get!string);
  750. if (name == "*") {
  751. paths ~= path;
  752. } else {
  753. auto ver = Version(pentry["version"].get!string);
  754.  
  755. Package pp;
  756. if (!refresh_existing_packages) {
  757. foreach (p; m_repositories[type].localPackages)
  758. if (p.path == path) {
  759. pp = p;
  760. break;
  761. }
  762. }
  763.  
  764. if (!pp) {
  765. auto infoFile = Package.findPackageFile(path);
  766. if (!infoFile.empty) pp = Package.load(path, infoFile);
  767. else {
  768. logWarn("Locally registered package %s %s was not found. Please run 'dub remove-local \"%s\"'.",
  769. name, ver, path.toNativeString());
  770. // Store a dummy package
  771. pp = new Package(PackageRecipe(name), path);
  772. }
  773. }
  774.  
  775. if (pp.name != name)
  776. logWarn("Local package at %s has different name than %s (%s)", path.toNativeString(), name, pp.name);
  777. pp.version_ = ver;
  778.  
  779. addPackages(packs, pp);
  780. }
  781. } catch( Exception e ){
  782. logWarn("Error adding local package: %s", e.msg);
  783. }
  784. }
  785. } catch( Exception e ){
  786. logDiagnostic("Loading of local package list at %s failed: %s", list_path.toNativeString(), e.msg);
  787. }
  788. m_repositories[type].localPackages = packs;
  789. m_repositories[type].searchPath = paths;
  790. }
  791. if (!m_disableDefaultSearchPaths)
  792. {
  793. scanLocalPackages(PlacementLocation.system);
  794. scanLocalPackages(PlacementLocation.user);
  795. scanLocalPackages(PlacementLocation.local);
  796. }
  797.  
  798. auto old_packages = m_packages;
  799.  
  800. // rescan the system and user package folder
  801. void scanPackageFolder(NativePath path)
  802. {
  803. if( path.existsDirectory() ){
  804. logDebug("iterating dir %s", path.toNativeString());
  805. try foreach( pdir; iterateDirectory(path) ){
  806. logDebug("iterating dir %s entry %s", path.toNativeString(), pdir.name);
  807. if (!pdir.isDirectory) continue;
  808.  
  809. // Old / flat directory structure, used in non-standard path
  810. // Packages are stored in $ROOT/$SOMETHING/`
  811. auto pack_path = path ~ (pdir.name ~ "/");
  812. auto packageFile = Package.findPackageFile(pack_path);
  813.  
  814. // New (since 2015) managed structure:
  815. // $ROOT/$NAME-$VERSION/$NAME
  816. // This is the most common code path
  817. if (isManagedPath(path) && packageFile.empty) {
  818. foreach (subdir; iterateDirectory(path ~ (pdir.name ~ "/")))
  819. if (subdir.isDirectory && pdir.name.startsWith(subdir.name)) {
  820. pack_path ~= subdir.name ~ "/";
  821. packageFile = Package.findPackageFile(pack_path);
  822. break;
  823. }
  824. }
  825.  
  826. if (packageFile.empty) continue;
  827. Package p;
  828. try {
  829. if (!refresh_existing_packages)
  830. foreach (pp; old_packages)
  831. if (pp.path == pack_path) {
  832. p = pp;
  833. break;
  834. }
  835. if (!p) p = Package.load(pack_path, packageFile);
  836. addPackages(m_packages, p);
  837. } catch( Exception e ){
  838. logError("Failed to load package in %s: %s", pack_path, e.msg);
  839. logDiagnostic("Full error: %s", e.toString().sanitize());
  840. }
  841. }
  842. catch(Exception e) logDiagnostic("Failed to enumerate %s packages: %s", path.toNativeString(), e.toString());
  843. }
  844. }
  845.  
  846. m_packages = null;
  847. foreach (p; this.completeSearchPath)
  848. scanPackageFolder(p);
  849.  
  850. void loadOverrides(PlacementLocation type)
  851. {
  852. m_repositories[type].overrides = null;
  853. auto ovrfilepath = m_repositories[type].packagePath ~ LocalOverridesFilename;
  854. if (existsFile(ovrfilepath)) {
  855. foreach (entry; jsonFromFile(ovrfilepath)) {
  856. PackageOverride ovr;
  857. ovr.package_ = entry["name"].get!string;
  858. ovr.source = VersionRange.fromString(entry["version"].get!string);
  859. if (auto pv = "targetVersion" in entry) ovr.target = Version(pv.get!string);
  860. if (auto pv = "targetPath" in entry) ovr.target = NativePath(pv.get!string);
  861. m_repositories[type].overrides ~= ovr;
  862. }
  863. }
  864. }
  865. if (!m_disableDefaultSearchPaths)
  866. {
  867. loadOverrides(PlacementLocation.local);
  868. loadOverrides(PlacementLocation.user);
  869. loadOverrides(PlacementLocation.system);
  870. }
  871. }
  872.  
  873. alias Hash = ubyte[];
  874. /// Generates a hash value for a given package.
  875. /// Some files or folders are ignored during the generation (like .dub and
  876. /// .svn folders)
  877. Hash hashPackage(Package pack)
  878. {
  879. string[] ignored_directories = [".git", ".dub", ".svn"];
  880. // something from .dub_ignore or what?
  881. string[] ignored_files = [];
  882. SHA1 sha1;
  883. foreach(file; dirEntries(pack.path.toNativeString(), SpanMode.depth)) {
  884. if(file.isDir && ignored_directories.canFind(NativePath(file.name).head.name))
  885. continue;
  886. else if(ignored_files.canFind(NativePath(file.name).head.name))
  887. continue;
  888.  
  889. sha1.put(cast(ubyte[])NativePath(file.name).head.name);
  890. if(file.isDir) {
  891. logDebug("Hashed directory name %s", NativePath(file.name).head);
  892. }
  893. else {
  894. sha1.put(openFile(NativePath(file.name)).readAll());
  895. logDebug("Hashed file contents from %s", NativePath(file.name).head);
  896. }
  897. }
  898. auto hash = sha1.finish();
  899. logDebug("Project hash: %s", hash);
  900. return hash[].dup;
  901. }
  902.  
  903. private void writeLocalPackageList(PlacementLocation type)
  904. {
  905. Json[] newlist;
  906. foreach (p; m_repositories[type].searchPath) {
  907. auto entry = Json.emptyObject;
  908. entry["name"] = "*";
  909. entry["path"] = p.toNativeString();
  910. newlist ~= entry;
  911. }
  912.  
  913. foreach (p; m_repositories[type].localPackages) {
  914. if (p.parentPackage) continue; // do not store sub packages
  915. auto entry = Json.emptyObject;
  916. entry["name"] = p.name;
  917. entry["version"] = p.version_.toString();
  918. entry["path"] = p.path.toNativeString();
  919. newlist ~= entry;
  920. }
  921.  
  922. NativePath path = m_repositories[type].packagePath;
  923. if( !existsDirectory(path) ) mkdirRecurse(path.toNativeString());
  924. writeJsonFile(path ~ LocalPackagesFilename, Json(newlist));
  925. }
  926.  
  927. private void writeLocalPackageOverridesFile(PlacementLocation type)
  928. {
  929. Json[] newlist;
  930. foreach (ovr; m_repositories[type].overrides) {
  931. auto jovr = Json.emptyObject;
  932. jovr["name"] = ovr.package_;
  933. jovr["version"] = ovr.source.toString();
  934. ovr.target.match!(
  935. (NativePath path) { jovr["targetPath"] = path.toNativeString(); },
  936. (Version vers) { jovr["targetVersion"] = vers.toString(); },
  937. );
  938. newlist ~= jovr;
  939. }
  940. auto path = m_repositories[type].packagePath;
  941. if (!existsDirectory(path)) mkdirRecurse(path.toNativeString());
  942. writeJsonFile(path ~ LocalOverridesFilename, Json(newlist));
  943. }
  944.  
  945. /// Adds the package and scans for subpackages.
  946. private void addPackages(ref Package[] dst_repos, Package pack)
  947. const {
  948. // Add the main package.
  949. dst_repos ~= pack;
  950.  
  951. // Additionally to the internally defined subpackages, whose metadata
  952. // is loaded with the main dub.json, load all externally defined
  953. // packages after the package is available with all the data.
  954. foreach (spr; pack.subPackages) {
  955. Package sp;
  956.  
  957. if (spr.path.length) {
  958. auto p = NativePath(spr.path);
  959. p.normalize();
  960. enforce(!p.absolute, "Sub package paths must be sub paths of the parent package.");
  961. auto path = pack.path ~ p;
  962. if (!existsFile(path)) {
  963. logError("Package %s declared a sub-package, definition file is missing: %s", pack.name, path.toNativeString());
  964. continue;
  965. }
  966. sp = Package.load(path, NativePath.init, pack);
  967. } else sp = new Package(spr.recipe, pack.path, pack);
  968.  
  969. // Add the subpackage.
  970. try {
  971. dst_repos ~= sp;
  972. } catch (Exception e) {
  973. logError("Package '%s': Failed to load sub-package %s: %s", pack.name,
  974. spr.path.length ? spr.path : spr.recipe.name, e.msg);
  975. logDiagnostic("Full error: %s", e.toString().sanitize());
  976. }
  977. }
  978. }
  979. }
  980.  
  981. struct PackageOverride {
  982. private alias ResolvedDep = SumType!(NativePath, Version);
  983.  
  984. string package_;
  985. VersionRange source;
  986. ResolvedDep target;
  987.  
  988. deprecated("Use `source` instead")
  989. @property inout(Dependency) version_ () inout return @safe {
  990. return Dependency(this.source);
  991. }
  992.  
  993. deprecated("Assign `source` instead")
  994. @property ref PackageOverride version_ (Dependency v) scope return @safe pure {
  995. this.source = v.visit!(
  996. (VersionRange range) => range,
  997. (any) {
  998. int a; if (a) return VersionRange.init; // Trick the compiler
  999. throw new Exception("Cannot use anything else than a `VersionRange` for overrides");
  1000. },
  1001. );
  1002. return this;
  1003. }
  1004.  
  1005. deprecated("Use `target.match` directly instead")
  1006. @property inout(Version) targetVersion () inout return @safe pure nothrow @nogc {
  1007. return this.target.match!(
  1008. (Version v) => v,
  1009. (any) => Version.init,
  1010. );
  1011. }
  1012.  
  1013. deprecated("Assign `target` directly instead")
  1014. @property ref PackageOverride targetVersion (Version v) scope return pure nothrow @nogc {
  1015. this.target = v;
  1016. return this;
  1017. }
  1018.  
  1019. deprecated("Use `target.match` directly instead")
  1020. @property inout(NativePath) targetPath () inout return @safe pure nothrow @nogc {
  1021. return this.target.match!(
  1022. (NativePath v) => v,
  1023. (any) => NativePath.init,
  1024. );
  1025. }
  1026.  
  1027. deprecated("Assign `target` directly instead")
  1028. @property ref PackageOverride targetPath (NativePath v) scope return pure nothrow @nogc {
  1029. this.target = v;
  1030. return this;
  1031. }
  1032.  
  1033. deprecated("Use the overload that accepts a `VersionRange` as 2nd argument")
  1034. this(string package_, Dependency version_, Version target_version)
  1035. {
  1036. this.package_ = package_;
  1037. this.version_ = version_;
  1038. this.target = target_version;
  1039. }
  1040.  
  1041. deprecated("Use the overload that accepts a `VersionRange` as 2nd argument")
  1042. this(string package_, Dependency version_, NativePath target_path)
  1043. {
  1044. this.package_ = package_;
  1045. this.version_ = version_;
  1046. this.target = target_path;
  1047. }
  1048.  
  1049. this(string package_, VersionRange src, Version target)
  1050. {
  1051. this.package_ = package_;
  1052. this.source = src;
  1053. this.target = target;
  1054. }
  1055.  
  1056. this(string package_, VersionRange src, NativePath target)
  1057. {
  1058. this.package_ = package_;
  1059. this.source = src;
  1060. this.target = target;
  1061. }
  1062. }
  1063.  
  1064. deprecated("Use `PlacementLocation` instead")
  1065. enum LocalPackageType : PlacementLocation {
  1066. package_ = PlacementLocation.local,
  1067. user = PlacementLocation.user,
  1068. system = PlacementLocation.system,
  1069. }
  1070.  
  1071. private enum LocalPackagesFilename = "local-packages.json";
  1072. private enum LocalOverridesFilename = "local-overrides.json";
  1073.  
  1074. /// A managed location (see `PlacementLocation`)
  1075. private struct Location {
  1076. NativePath packagePath;
  1077. NativePath[] searchPath;
  1078. Package[] localPackages;
  1079. PackageOverride[] overrides;
  1080.  
  1081. this(NativePath path)
  1082. {
  1083. this.packagePath = path;
  1084. }
  1085. }