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 : FileInfo;
  13. import dub.internal.vibecompat.data.json;
  14. import dub.internal.vibecompat.inet.path;
  15. import dub.internal.logging;
  16. import dub.package_;
  17. import dub.recipe.io;
  18. import dub.recipe.selection;
  19. import dub.internal.configy.Exceptions;
  20. public import dub.internal.configy.Read : StrictMode;
  21.  
  22. import dub.internal.dyaml.stdsumtype;
  23.  
  24. import std.algorithm : countUntil, filter, map, sort, canFind, remove;
  25. import std.array;
  26. import std.conv;
  27. import std.digest.sha;
  28. import std.encoding : sanitize;
  29. import std.exception;
  30. import std.file;
  31. import std.range;
  32. import std.string;
  33. import std.zip;
  34.  
  35.  
  36. /// Indicates where a package has been or should be placed to.
  37. public enum PlacementLocation {
  38. /// Packages retrieved with 'local' will be placed in the current folder
  39. /// using the package name as destination.
  40. local,
  41. /// Packages with 'userWide' will be placed in a folder accessible by
  42. /// all of the applications from the current user.
  43. user,
  44. /// Packages retrieved with 'systemWide' will be placed in a shared folder,
  45. /// which can be accessed by all users of the system.
  46. system,
  47. }
  48.  
  49. /// Converts a `PlacementLocation` to a string
  50. public string toString (PlacementLocation loc) @safe pure nothrow @nogc
  51. {
  52. final switch (loc) {
  53. case PlacementLocation.local:
  54. return "Local";
  55. case PlacementLocation.user:
  56. return "User";
  57. case PlacementLocation.system:
  58. return "System";
  59. }
  60. }
  61.  
  62. /// The PackageManager can retrieve present packages and get / remove
  63. /// packages.
  64. class PackageManager {
  65. protected {
  66. /**
  67. * The 'internal' location, for packages not attributable to a location.
  68. *
  69. * There are two uses for this:
  70. * - In `bare` mode, the search paths are set at this scope,
  71. * and packages gathered are stored in `localPackage`;
  72. * - In the general case, any path-based or SCM-based dependency
  73. * is loaded in `fromPath`;
  74. */
  75. Location m_internal;
  76. /**
  77. * List of locations that are managed by this `PackageManager`
  78. *
  79. * The `PackageManager` can be instantiated either in 'bare' mode,
  80. * in which case this array will be empty, or in the normal mode,
  81. * this array will have 3 entries, matching values
  82. * in the `PlacementLocation` enum.
  83. *
  84. * See_Also: `Location`, `PlacementLocation`
  85. */
  86. Location[] m_repositories;
  87. /**
  88. * Whether `refresh` has been called or not
  89. *
  90. * Dub versions because v1.31 eagerly scan all available repositories,
  91. * leading to slowdown for the most common operation - `dub build` with
  92. * already resolved dependencies.
  93. * From v1.31 onward, those locations are not scanned eagerly,
  94. * unless one of the function requiring eager scanning does,
  95. * such as `getBestPackage` - as it needs to iterate the list
  96. * of available packages.
  97. */
  98. bool m_initialized;
  99. }
  100.  
  101. /**
  102. Instantiate an instance with a single search path
  103.  
  104. This constructor is used when dub is invoked with the '--bare' CLI switch.
  105. The instance will not look up the default repositories
  106. (e.g. ~/.dub/packages), using only `path` instead.
  107.  
  108. Params:
  109. path = Path of the single repository
  110. */
  111. this(NativePath path)
  112. {
  113. this.m_internal.searchPath = [ path ];
  114. this.refresh();
  115. }
  116.  
  117. this(NativePath package_path, NativePath user_path, NativePath system_path, bool refresh_packages = true)
  118. {
  119. m_repositories = [
  120. Location(package_path ~ ".dub/packages/"),
  121. Location(user_path ~ "packages/"),
  122. Location(system_path ~ "packages/")];
  123.  
  124. if (refresh_packages) refresh();
  125. }
  126.  
  127. /** Gets/sets the list of paths to search for local packages.
  128. */
  129. @property void searchPath(NativePath[] paths)
  130. {
  131. if (paths == this.m_internal.searchPath) return;
  132. this.m_internal.searchPath = paths.dup;
  133. this.refresh();
  134. }
  135. /// ditto
  136. @property const(NativePath)[] searchPath() const { return this.m_internal.searchPath; }
  137.  
  138. /** Returns the effective list of search paths, including default ones.
  139. */
  140. deprecated("Use the `PackageManager` facilities instead")
  141. @property const(NativePath)[] completeSearchPath()
  142. const {
  143. auto ret = appender!(const(NativePath)[])();
  144. ret.put(this.m_internal.searchPath);
  145. foreach (ref repo; m_repositories) {
  146. ret.put(repo.searchPath);
  147. ret.put(repo.packagePath);
  148. }
  149. return ret.data;
  150. }
  151.  
  152. /** Sets additional (read-only) package cache paths to search for packages.
  153.  
  154. Cache paths have the same structure as the default cache paths, such as
  155. ".dub/packages/".
  156.  
  157. Note that previously set custom paths will be removed when setting this
  158. property.
  159. */
  160. @property void customCachePaths(NativePath[] custom_cache_paths)
  161. {
  162. import std.algorithm.iteration : map;
  163. import std.array : array;
  164.  
  165. m_repositories.length = PlacementLocation.max+1;
  166. m_repositories ~= custom_cache_paths.map!(p => Location(p)).array;
  167.  
  168. this.refresh();
  169. }
  170.  
  171. /**
  172. * Looks up a package, first in the list of loaded packages,
  173. * then directly on the file system.
  174. *
  175. * This function allows for lazy loading of packages, without needing to
  176. * first scan all the available locations (as `refresh` does).
  177. *
  178. * Note:
  179. * This function does not take overrides into account. Overrides need
  180. * to be resolved by the caller before `lookup` is called.
  181. * Additionally, if a package of the same version is loaded in multiple
  182. * locations, the first one matching (local > user > system)
  183. * will be returned.
  184. *
  185. * Params:
  186. * name = The full name of the package to look up
  187. * vers = The version the package must match
  188. *
  189. * Returns:
  190. * A `Package` if one was found, `null` if none exists.
  191. */
  192. protected Package lookup (in PackageName name, in Version vers) {
  193. if (!this.m_initialized)
  194. this.refresh();
  195.  
  196. if (auto pkg = this.m_internal.lookup(name, vers))
  197. return pkg;
  198.  
  199. foreach (ref location; this.m_repositories)
  200. if (auto p = location.load(name, vers, this))
  201. return p;
  202.  
  203. return null;
  204. }
  205.  
  206. /** Looks up a specific package.
  207.  
  208. Looks up a package matching the given version/path in the set of
  209. registered packages. The lookup order is done according the the
  210. usual rules (see getPackageIterator).
  211.  
  212. Params:
  213. name = The name of the package
  214. ver = The exact version of the package to query
  215. path = An exact path that the package must reside in. Note that
  216. the package must still be registered in the package manager.
  217. enable_overrides = Apply the local package override list before
  218. returning a package (enabled by default)
  219.  
  220. Returns:
  221. The matching package or null if no match was found.
  222. */
  223. Package getPackage(in PackageName name, in Version ver, bool enable_overrides = true)
  224. {
  225. if (enable_overrides) {
  226. foreach (ref repo; m_repositories)
  227. foreach (ovr; repo.overrides)
  228. if (ovr.package_ == name.toString() && ovr.source.matches(ver)) {
  229. Package pack = ovr.target.match!(
  230. (NativePath path) => getOrLoadPackage(path),
  231. (Version vers) => getPackage(name, vers, false),
  232. );
  233. if (pack) return pack;
  234.  
  235. ovr.target.match!(
  236. (any) {
  237. logWarn("Package override %s %s -> '%s' doesn't reference an existing package.",
  238. ovr.package_, ovr.source, any);
  239. },
  240. );
  241. }
  242. }
  243.  
  244. return this.lookup(name, ver);
  245. }
  246.  
  247. deprecated("Use the overload that accepts a `PackageName` instead")
  248. Package getPackage(string name, Version ver, bool enable_overrides = true)
  249. {
  250. return this.getPackage(PackageName(name), ver, enable_overrides);
  251. }
  252.  
  253. /// ditto
  254. deprecated("Use the overload that accepts a `Version` as second argument")
  255. Package getPackage(string name, string ver, bool enable_overrides = true)
  256. {
  257. return getPackage(name, Version(ver), enable_overrides);
  258. }
  259.  
  260. /// ditto
  261. deprecated("Use the overload that takes a `PlacementLocation`")
  262. Package getPackage(string name, Version ver, NativePath path)
  263. {
  264. foreach (p; getPackageIterator(name)) {
  265. auto pvm = isManagedPackage(p) ? VersionMatchMode.strict : VersionMatchMode.standard;
  266. if (p.version_.matches(ver, pvm) && p.path.startsWith(path))
  267. return p;
  268. }
  269. return null;
  270. }
  271.  
  272. /// Ditto
  273. deprecated("Use the overload that accepts a `PackageName` instead")
  274. Package getPackage(string name, Version ver, PlacementLocation loc)
  275. {
  276. return this.getPackage(PackageName(name), ver, loc);
  277. }
  278.  
  279. /// Ditto
  280. Package getPackage(in PackageName name, in Version ver, PlacementLocation loc)
  281. {
  282. // Bare mode
  283. if (loc >= this.m_repositories.length)
  284. return null;
  285. return this.m_repositories[loc].load(name, ver, this);
  286. }
  287.  
  288. /// ditto
  289. deprecated("Use the overload that accepts a `Version` as second argument")
  290. Package getPackage(string name, string ver, NativePath path)
  291. {
  292. return getPackage(name, Version(ver), path);
  293. }
  294.  
  295. /// ditto
  296. deprecated("Use another `PackageManager` API, open an issue if none suits you")
  297. Package getPackage(string name, NativePath path)
  298. {
  299. foreach( p; getPackageIterator(name) )
  300. if (p.path.startsWith(path))
  301. return p;
  302. return null;
  303. }
  304.  
  305.  
  306. /** Looks up the first package matching the given name.
  307. */
  308. deprecated("Use `getBestPackage` instead")
  309. Package getFirstPackage(string name)
  310. {
  311. foreach (ep; getPackageIterator(name))
  312. return ep;
  313. return null;
  314. }
  315.  
  316. /** Looks up the latest package matching the given name.
  317. */
  318. deprecated("Use `getBestPackage` with `name, Dependency.any` instead")
  319. Package getLatestPackage(string name)
  320. {
  321. Package pkg;
  322. foreach (ep; getPackageIterator(name))
  323. if (pkg is null || pkg.version_ < ep.version_)
  324. pkg = ep;
  325. return pkg;
  326. }
  327.  
  328. /** For a given package path, returns the corresponding package.
  329.  
  330. If the package is already loaded, a reference is returned. Otherwise
  331. the package gets loaded and cached for the next call to this function.
  332.  
  333. Params:
  334. path = NativePath to the root directory of the package
  335. recipe_path = Optional path to the recipe file of the package
  336. allow_sub_packages = Also return a sub package if it resides in the given folder
  337. mode = Whether to issue errors, warning, or ignore unknown keys in dub.json
  338.  
  339. Returns: The packages loaded from the given path
  340. Throws: Throws an exception if no package can be loaded
  341. */
  342. Package getOrLoadPackage(NativePath path, NativePath recipe_path = NativePath.init,
  343. bool allow_sub_packages = false, StrictMode mode = StrictMode.Ignore)
  344. {
  345. path.endsWithSlash = true;
  346. foreach (p; this.m_internal.fromPath)
  347. if (p.path == path && (!p.parentPackage || (allow_sub_packages && p.parentPackage.path != p.path)))
  348. return p;
  349. auto pack = this.load(path, recipe_path, null, null, mode);
  350. addPackages(this.m_internal.fromPath, pack);
  351. return pack;
  352. }
  353.  
  354. /**
  355. * Loads a `Package` from the filesystem
  356. *
  357. * This is called when a `Package` needs to be loaded from the path.
  358. * This does not change the internal state of the `PackageManager`,
  359. * it simply loads the `Package` and returns it - it is up to the caller
  360. * to call `addPackages`.
  361. *
  362. * Throws:
  363. * If no package can be found at the `path` / with the `recipe`.
  364. *
  365. * Params:
  366. * path = The directory in which the package resides.
  367. * recipe = Optional path to the package recipe file. If left empty,
  368. * the `path` directory will be searched for a recipe file.
  369. * parent = Reference to the parent package, if the new package is a
  370. * sub package.
  371. * version_ = Optional version to associate to the package instead of
  372. * the one declared in the package recipe, or the one
  373. * determined by invoking the VCS (GIT currently).
  374. * mode = Whether to issue errors, warning, or ignore unknown keys in
  375. * dub.json
  376. *
  377. * Returns: A populated `Package`.
  378. */
  379. protected Package load(NativePath path, NativePath recipe = NativePath.init,
  380. Package parent = null, string version_ = null,
  381. StrictMode mode = StrictMode.Ignore)
  382. {
  383. if (recipe.empty)
  384. recipe = Package.findPackageFile(path);
  385.  
  386. enforce(!recipe.empty,
  387. "No package file found in %s, expected one of %s"
  388. .format(path.toNativeString(),
  389. packageInfoFiles.map!(f => cast(string)f.filename).join("/")));
  390.  
  391. const PackageName pname = parent
  392. ? PackageName(parent.name) : PackageName.init;
  393. auto content = readPackageRecipe(recipe, pname, mode);
  394. auto ret = new Package(content, path, parent, version_);
  395. ret.m_infoFile = recipe;
  396. return ret;
  397. }
  398.  
  399. /** For a given SCM repository, returns the corresponding package.
  400.  
  401. An SCM repository is provided as its remote URL, the repository is cloned
  402. and in the dependency specified commit is checked out.
  403.  
  404. If the target directory already exists, just returns the package
  405. without cloning.
  406.  
  407. Params:
  408. name = Package name
  409. dependency = Dependency that contains the repository URL and a specific commit
  410.  
  411. Returns:
  412. The package loaded from the given SCM repository or null if the
  413. package couldn't be loaded.
  414. */
  415. Package loadSCMPackage(in PackageName name, in Repository repo)
  416. in { assert(!repo.empty); }
  417. do {
  418. Package pack;
  419.  
  420. final switch (repo.kind)
  421. {
  422. case repo.Kind.git:
  423. return this.loadGitPackage(name, repo);
  424. }
  425. }
  426.  
  427. deprecated("Use the overload that accepts a `dub.dependency : Repository`")
  428. Package loadSCMPackage(string name, Dependency dependency)
  429. in { assert(!dependency.repository.empty); }
  430. do { return this.loadSCMPackage(name, dependency.repository); }
  431.  
  432. deprecated("Use `loadSCMPackage(PackageName, Repository)`")
  433. Package loadSCMPackage(string name, Repository repo)
  434. {
  435. return this.loadSCMPackage(PackageName(name), repo);
  436. }
  437.  
  438. private Package loadGitPackage(in PackageName name, in Repository repo)
  439. {
  440. if (!repo.ref_.startsWith("~") && !repo.ref_.isGitHash) {
  441. return null;
  442. }
  443.  
  444. string gitReference = repo.ref_.chompPrefix("~");
  445. NativePath destination = this.getPackagePath(PlacementLocation.user, name, repo.ref_);
  446.  
  447. foreach (p; getPackageIterator(name.toString())) {
  448. if (p.path == destination) {
  449. return p;
  450. }
  451. }
  452.  
  453. if (!this.gitClone(repo.remote, gitReference, destination))
  454. return null;
  455.  
  456. Package result = this.load(destination);
  457. if (result !is null)
  458. this.addPackages(this.m_internal.fromPath, result);
  459. return result;
  460. }
  461.  
  462. /**
  463. * Perform a `git clone` operation at `dest` using `repo`
  464. *
  465. * Params:
  466. * remote = The remote to clone from
  467. * gitref = The git reference to use
  468. * dest = Where the result of git clone operation is to be stored
  469. *
  470. * Returns:
  471. * Whether or not the clone operation was successfull.
  472. */
  473. protected bool gitClone(string remote, string gitref, in NativePath dest)
  474. {
  475. static import dub.internal.git;
  476. return dub.internal.git.cloneRepository(remote, gitref, dest.toNativeString());
  477. }
  478.  
  479. /**
  480. * Get the final destination a specific package needs to be stored in.
  481. *
  482. * See `Location.getPackagePath`.
  483. */
  484. package(dub) NativePath getPackagePath(PlacementLocation base, in PackageName name, string vers)
  485. {
  486. assert(this.m_repositories.length == 3, "getPackagePath called in bare mode");
  487. return this.m_repositories[base].getPackagePath(name, vers);
  488. }
  489.  
  490. /**
  491. * Searches for the latest version of a package matching the version range.
  492. *
  493. * This will search the local file system only (it doesn't connect
  494. * to the registry) for the "best" (highest version) that matches `range`.
  495. * An overload with a single version exists to search for an exact version.
  496. *
  497. * Params:
  498. * name = Package name to search for
  499. * vers = Exact version to search for
  500. * range = Range of versions to search for, defaults to any
  501. *
  502. * Returns:
  503. * The best package matching the parameters, or `null` if none was found.
  504. */
  505. deprecated("Use the overload that accepts a `PackageName` instead")
  506. Package getBestPackage(string name, Version vers)
  507. {
  508. return this.getBestPackage(PackageName(name), vers);
  509. }
  510.  
  511. /// Ditto
  512. Package getBestPackage(in PackageName name, in Version vers)
  513. {
  514. return this.getBestPackage(name, VersionRange(vers, vers));
  515. }
  516.  
  517. /// Ditto
  518. deprecated("Use the overload that accepts a `PackageName` instead")
  519. Package getBestPackage(string name, VersionRange range = VersionRange.Any)
  520. {
  521. return this.getBestPackage(PackageName(name), range);
  522. }
  523.  
  524. /// Ditto
  525. Package getBestPackage(in PackageName name, in VersionRange range = VersionRange.Any)
  526. {
  527. return this.getBestPackage_(name, Dependency(range));
  528. }
  529.  
  530. /// Ditto
  531. deprecated("Use the overload that accepts a `Version` or a `VersionRange`")
  532. Package getBestPackage(string name, string range)
  533. {
  534. return this.getBestPackage(name, VersionRange.fromString(range));
  535. }
  536.  
  537. /// Ditto
  538. deprecated("`getBestPackage` should only be used with a `Version` or `VersionRange` argument")
  539. Package getBestPackage(string name, Dependency version_spec, bool enable_overrides = true)
  540. {
  541. return this.getBestPackage_(PackageName(name), version_spec, enable_overrides);
  542. }
  543.  
  544. // TODO: Merge this into `getBestPackage(string, VersionRange)`
  545. private Package getBestPackage_(in PackageName name, in Dependency version_spec,
  546. bool enable_overrides = true)
  547. {
  548. Package ret;
  549. foreach (p; getPackageIterator(name.toString())) {
  550. auto vmm = isManagedPackage(p) ? VersionMatchMode.strict : VersionMatchMode.standard;
  551. if (version_spec.matches(p.version_, vmm) && (!ret || p.version_ > ret.version_))
  552. ret = p;
  553. }
  554.  
  555. if (enable_overrides && ret) {
  556. if (auto ovr = getPackage(name, ret.version_))
  557. return ovr;
  558. }
  559. return ret;
  560. }
  561.  
  562. /** Gets the a specific sub package.
  563.  
  564. Params:
  565. base_package = The package from which to get a sub package
  566. sub_name = Name of the sub package (not prefixed with the base
  567. package name)
  568. silent_fail = If set to true, the function will return `null` if no
  569. package is found. Otherwise will throw an exception.
  570.  
  571. */
  572. Package getSubPackage(Package base_package, string sub_name, bool silent_fail)
  573. {
  574. foreach (p; getPackageIterator(base_package.name~":"~sub_name))
  575. if (p.parentPackage is base_package)
  576. return p;
  577. enforce(silent_fail, "Sub package \""~base_package.name~":"~sub_name~"\" doesn't exist.");
  578. return null;
  579. }
  580.  
  581.  
  582. /** Determines if a package is managed by DUB.
  583.  
  584. Managed packages can be upgraded and removed.
  585. */
  586. bool isManagedPackage(const(Package) pack)
  587. const {
  588. auto ppath = pack.basePackage.path;
  589. return isManagedPath(ppath);
  590. }
  591.  
  592. /** Determines if a specific path is within a DUB managed package folder.
  593.  
  594. By default, managed folders are "~/.dub/packages" and
  595. "/var/lib/dub/packages".
  596. */
  597. bool isManagedPath(NativePath path)
  598. const {
  599. foreach (rep; m_repositories)
  600. if (rep.isManaged(path))
  601. return true;
  602. return false;
  603. }
  604.  
  605. /** Enables iteration over all known local packages.
  606.  
  607. Returns: A delegate suitable for use with `foreach` is returned.
  608. */
  609. int delegate(int delegate(ref Package)) getPackageIterator()
  610. {
  611. // See `m_initialized` documentation
  612. if (!this.m_initialized)
  613. this.refresh();
  614.  
  615. int iterator(int delegate(ref Package) del)
  616. {
  617. // Search scope by priority, internal has the highest
  618. foreach (p; this.m_internal.fromPath)
  619. if (auto ret = del(p)) return ret;
  620. foreach (p; this.m_internal.localPackages)
  621. if (auto ret = del(p)) return ret;
  622.  
  623. foreach (ref repo; m_repositories) {
  624. foreach (p; repo.localPackages)
  625. if (auto ret = del(p)) return ret;
  626. foreach (p; repo.fromPath)
  627. if (auto ret = del(p)) return ret;
  628. }
  629. return 0;
  630. }
  631.  
  632. return &iterator;
  633. }
  634.  
  635. /** Enables iteration over all known local packages with a certain name.
  636.  
  637. Returns: A delegate suitable for use with `foreach` is returned.
  638. */
  639. int delegate(int delegate(ref Package)) getPackageIterator(string name)
  640. {
  641. int iterator(int delegate(ref Package) del)
  642. {
  643. foreach (p; getPackageIterator())
  644. if (p.name == name)
  645. if (auto ret = del(p)) return ret;
  646. return 0;
  647. }
  648.  
  649. return &iterator;
  650. }
  651.  
  652.  
  653. /** Returns a list of all package overrides for the given scope.
  654. */
  655. deprecated(OverrideDepMsg)
  656. const(PackageOverride)[] getOverrides(PlacementLocation scope_)
  657. const {
  658. return cast(typeof(return)) this.getOverrides_(scope_);
  659. }
  660.  
  661. package(dub) const(PackageOverride_)[] getOverrides_(PlacementLocation scope_)
  662. const {
  663. return m_repositories[scope_].overrides;
  664. }
  665.  
  666. /** Adds a new override for the given package.
  667. */
  668. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  669. void addOverride(PlacementLocation scope_, string package_, Dependency version_spec, Version target)
  670. {
  671. m_repositories[scope_].overrides ~= PackageOverride(package_, version_spec, target);
  672. m_repositories[scope_].writeOverrides();
  673. }
  674. /// ditto
  675. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  676. void addOverride(PlacementLocation scope_, string package_, Dependency version_spec, NativePath target)
  677. {
  678. m_repositories[scope_].overrides ~= PackageOverride(package_, version_spec, target);
  679. m_repositories[scope_].writeOverrides();
  680. }
  681.  
  682. /// Ditto
  683. deprecated(OverrideDepMsg)
  684. void addOverride(PlacementLocation scope_, string package_, VersionRange source, Version target)
  685. {
  686. this.addOverride_(scope_, package_, source, target);
  687. }
  688. /// ditto
  689. deprecated(OverrideDepMsg)
  690. void addOverride(PlacementLocation scope_, string package_, VersionRange source, NativePath target)
  691. {
  692. this.addOverride_(scope_, package_, source, target);
  693. }
  694.  
  695. // Non deprecated version that is used by `commandline`. Do not use!
  696. package(dub) void addOverride_(PlacementLocation scope_, string package_, VersionRange source, Version target)
  697. {
  698. m_repositories[scope_].overrides ~= PackageOverride_(package_, source, target);
  699. m_repositories[scope_].writeOverrides();
  700. }
  701. // Non deprecated version that is used by `commandline`. Do not use!
  702. package(dub) void addOverride_(PlacementLocation scope_, string package_, VersionRange source, NativePath target)
  703. {
  704. m_repositories[scope_].overrides ~= PackageOverride_(package_, source, target);
  705. m_repositories[scope_].writeOverrides();
  706. }
  707.  
  708. /** Removes an existing package override.
  709. */
  710. deprecated("Use the overload that accepts a `VersionRange` as 3rd argument")
  711. void removeOverride(PlacementLocation scope_, string package_, Dependency version_spec)
  712. {
  713. version_spec.visit!(
  714. (VersionRange src) => this.removeOverride(scope_, package_, src),
  715. (any) { throw new Exception(format("No override exists for %s %s", package_, version_spec)); },
  716. );
  717. }
  718.  
  719. deprecated(OverrideDepMsg)
  720. void removeOverride(PlacementLocation scope_, string package_, VersionRange src)
  721. {
  722. this.removeOverride_(scope_, package_, src);
  723. }
  724.  
  725. package(dub) void removeOverride_(PlacementLocation scope_, string package_, VersionRange src)
  726. {
  727. Location* rep = &m_repositories[scope_];
  728. foreach (i, ovr; rep.overrides) {
  729. if (ovr.package_ != package_ || ovr.source != src)
  730. continue;
  731. rep.overrides = rep.overrides[0 .. i] ~ rep.overrides[i+1 .. $];
  732. (*rep).writeOverrides();
  733. return;
  734. }
  735. throw new Exception(format("No override exists for %s %s", package_, src));
  736. }
  737.  
  738. deprecated("Use `store(NativePath source, PlacementLocation dest, string name, Version vers)`")
  739. Package storeFetchedPackage(NativePath zip_file_path, Json package_info, NativePath destination)
  740. {
  741. return this.store_(zip_file_path, destination,
  742. PackageName(package_info["name"].get!string),
  743. Version(package_info["version"].get!string));
  744. }
  745.  
  746. /**
  747. * Store a zip file stored at `src` into a managed location `destination`
  748. *
  749. * This will extracts the package supplied as (as a zip file) to the
  750. * `destination` and sets a version field in the package description.
  751. * In the future, we should aim not to alter the package description,
  752. * but this is done for backward compatibility.
  753. *
  754. * Params:
  755. * src = The path to the zip file containing the package
  756. * dest = At which `PlacementLocation` the package should be stored
  757. * name = Name of the package being stored
  758. * vers = Version of the package
  759. *
  760. * Returns:
  761. * The `Package` after it has been loaded.
  762. *
  763. * Throws:
  764. * If the package cannot be loaded / the zip is corrupted / the package
  765. * already exists, etc...
  766. */
  767. deprecated("Use the overload that accepts a `PackageName` instead")
  768. Package store(NativePath src, PlacementLocation dest, string name, Version vers)
  769. {
  770. return this.store(src, dest, PackageName(name), vers);
  771. }
  772.  
  773. /// Ditto
  774. Package store(NativePath src, PlacementLocation dest, in PackageName name,
  775. in Version vers)
  776. {
  777. import dub.internal.vibecompat.core.file;
  778.  
  779. assert(!name.sub.length, "Cannot store a subpackage, use main package instead");
  780. NativePath dstpath = this.getPackagePath(dest, name, vers.toString());
  781. ensureDirectory(dstpath.parentPath());
  782. const lockPath = dstpath.parentPath() ~ ".lock";
  783.  
  784. // possibly wait for other dub instance
  785. import core.time : seconds;
  786. auto lock = lockFile(lockPath.toNativeString(), 30.seconds);
  787. if (dstpath.existsFile()) {
  788. return this.getPackage(name, vers, dest);
  789. }
  790. return this.store_(src, dstpath, name, vers);
  791. }
  792.  
  793. /// Backward-compatibility for deprecated overload, simplify once `storeFetchedPatch`
  794. /// is removed
  795. private Package store_(NativePath src, NativePath destination,
  796. in PackageName name, in Version vers)
  797. {
  798. import dub.internal.vibecompat.core.file;
  799. import std.range : walkLength;
  800.  
  801. logDebug("Placing package '%s' version '%s' to location '%s' from file '%s'",
  802. name, vers, destination.toNativeString(), src.toNativeString());
  803.  
  804. enforce(!existsFile(destination),
  805. "%s (%s) needs to be removed from '%s' prior placement."
  806. .format(name, vers, destination));
  807.  
  808. logDebug("Opening file %s", src);
  809. ZipArchive archive = new ZipArchive(readFile(src));
  810. logDebug("Extracting from zip.");
  811.  
  812. // In a GitHub zip, the actual contents are in a sub-folder
  813. alias PSegment = typeof(NativePath.init.head);
  814. PSegment[] zip_prefix;
  815. outer: foreach(ArchiveMember am; archive.directory) {
  816. auto path = NativePath(am.name).bySegment.array;
  817. foreach (fil; packageInfoFiles)
  818. if (path.length == 2 && path[$-1].name == fil.filename) {
  819. zip_prefix = path[0 .. $-1];
  820. break outer;
  821. }
  822. }
  823.  
  824. logDebug("zip root folder: %s", zip_prefix);
  825.  
  826. NativePath getCleanedPath(string fileName) {
  827. auto path = NativePath(fileName);
  828. if (zip_prefix.length && !path.bySegment.startsWith(zip_prefix)) return NativePath.init;
  829. static if (is(typeof(path[0 .. 1]))) return path[zip_prefix.length .. $];
  830. else return NativePath(path.bySegment.array[zip_prefix.length .. $]);
  831. }
  832.  
  833. static void setAttributes(string path, ArchiveMember am)
  834. {
  835. import std.datetime : DosFileTimeToSysTime;
  836.  
  837. auto mtime = DosFileTimeToSysTime(am.time);
  838. setTimes(path, mtime, mtime);
  839. if (auto attrs = am.fileAttributes)
  840. std.file.setAttributes(path, attrs);
  841. }
  842.  
  843. // extract & place
  844. ensureDirectory(destination);
  845. logDebug("Copying all files...");
  846. int countFiles = 0;
  847. foreach(ArchiveMember a; archive.directory) {
  848. auto cleanedPath = getCleanedPath(a.name);
  849. if(cleanedPath.empty) continue;
  850. auto dst_path = destination ~ cleanedPath;
  851.  
  852. logDebug("Creating %s", cleanedPath);
  853. if (dst_path.endsWithSlash) {
  854. ensureDirectory(dst_path);
  855. } else {
  856. ensureDirectory(dst_path.parentPath);
  857. // for symlinks on posix systems, use the symlink function to
  858. // create them. Windows default unzip doesn't handle symlinks,
  859. // so we don't need to worry about it for Windows.
  860. version(Posix) {
  861. import core.sys.posix.sys.stat;
  862. if( S_ISLNK(cast(mode_t)a.fileAttributes) ){
  863. import core.sys.posix.unistd;
  864. // need to convert name and target to zero-terminated string
  865. auto target = toStringz(cast(const(char)[])archive.expand(a));
  866. auto dstFile = toStringz(dst_path.toNativeString());
  867. enforce(symlink(target, dstFile) == 0, "Error creating symlink: " ~ dst_path.toNativeString());
  868. goto symlink_exit;
  869. }
  870. }
  871.  
  872. writeFile(dst_path, archive.expand(a));
  873. setAttributes(dst_path.toNativeString(), a);
  874. symlink_exit:
  875. ++countFiles;
  876. }
  877. }
  878. logDebug("%s file(s) copied.", to!string(countFiles));
  879.  
  880. // overwrite dub.json (this one includes a version field)
  881. auto pack = this.load(destination, NativePath.init, null, vers.toString());
  882.  
  883. if (pack.recipePath.head != defaultPackageFilename)
  884. // Storeinfo saved a default file, this could be different to the file from the zip.
  885. removeFile(pack.recipePath);
  886. pack.storeInfo();
  887. addPackages(this.m_internal.localPackages, pack);
  888. return pack;
  889. }
  890.  
  891. /// Removes the given the package.
  892. void remove(in Package pack)
  893. {
  894. logDebug("Remove %s, version %s, path '%s'", pack.name, pack.version_, pack.path);
  895. enforce(!pack.path.empty, "Cannot remove package "~pack.name~" without a path.");
  896. enforce(pack.parentPackage is null, "Cannot remove subpackage %s".format(pack.name));
  897.  
  898. // remove package from repositories' list
  899. bool found = false;
  900. bool removeFrom(Package[] packs, in Package pack) {
  901. auto packPos = countUntil!("a.path == b.path")(packs, pack);
  902. if(packPos != -1) {
  903. packs = .remove(packs, packPos);
  904. return true;
  905. }
  906. return false;
  907. }
  908. foreach(repo; m_repositories) {
  909. if (removeFrom(repo.fromPath, pack)) {
  910. found = true;
  911. break;
  912. }
  913. // Maintain backward compatibility with pre v1.30.0 behavior,
  914. // this is equivalent to remove-local
  915. if (removeFrom(repo.localPackages, pack)) {
  916. found = true;
  917. break;
  918. }
  919. }
  920. if(!found)
  921. found = removeFrom(this.m_internal.localPackages, pack);
  922. enforce(found, "Cannot remove, package not found: '"~ pack.name ~"', path: " ~ to!string(pack.path));
  923.  
  924. logDebug("About to delete root folder for package '%s'.", pack.path);
  925. rmdirRecurse(pack.path.toNativeString());
  926. logInfo("Removed", Color.yellow, "%s %s", pack.name.color(Mode.bold), pack.version_);
  927. }
  928.  
  929. /// Compatibility overload. Use the version without a `force_remove` argument instead.
  930. deprecated("Use `remove(pack)` directly instead, the boolean has no effect")
  931. void remove(in Package pack, bool force_remove)
  932. {
  933. remove(pack);
  934. }
  935.  
  936. Package addLocalPackage(NativePath path, string verName, PlacementLocation type)
  937. {
  938. // As we iterate over `localPackages` we need it to be populated
  939. // In theory we could just populate that specific repository,
  940. // but multiple calls would then become inefficient.
  941. if (!this.m_initialized)
  942. this.refresh();
  943.  
  944. path.endsWithSlash = true;
  945. auto pack = this.load(path);
  946. enforce(pack.name.length, "The package has no name, defined in: " ~ path.toString());
  947. if (verName.length)
  948. pack.version_ = Version(verName);
  949.  
  950. // don't double-add packages
  951. Package[]* packs = &m_repositories[type].localPackages;
  952. foreach (p; *packs) {
  953. if (p.path == path) {
  954. enforce(p.version_ == pack.version_, "Adding the same local package twice with differing versions is not allowed.");
  955. logInfo("Package is already registered: %s (version: %s)", p.name, p.version_);
  956. return p;
  957. }
  958. }
  959.  
  960. addPackages(*packs, pack);
  961.  
  962. this.m_repositories[type].writeLocalPackageList();
  963.  
  964. logInfo("Registered package: %s (version: %s)", pack.name, pack.version_);
  965. return pack;
  966. }
  967.  
  968. void removeLocalPackage(NativePath path, PlacementLocation type)
  969. {
  970. // As we iterate over `localPackages` we need it to be populated
  971. // In theory we could just populate that specific repository,
  972. // but multiple calls would then become inefficient.
  973. if (!this.m_initialized)
  974. this.refresh();
  975.  
  976. path.endsWithSlash = true;
  977. Package[]* packs = &m_repositories[type].localPackages;
  978. size_t[] to_remove;
  979. foreach( i, entry; *packs )
  980. if( entry.path == path )
  981. to_remove ~= i;
  982. enforce(to_remove.length > 0, "No "~type.to!string()~" package found at "~path.toNativeString());
  983.  
  984. string[Version] removed;
  985. foreach (i; to_remove)
  986. removed[(*packs)[i].version_] = (*packs)[i].name;
  987.  
  988. *packs = (*packs).enumerate
  989. .filter!(en => !to_remove.canFind(en.index))
  990. .map!(en => en.value).array;
  991.  
  992. this.m_repositories[type].writeLocalPackageList();
  993.  
  994. foreach(ver, name; removed)
  995. logInfo("Deregistered package: %s (version: %s)", name, ver);
  996. }
  997.  
  998. /// For the given type add another path where packages will be looked up.
  999. void addSearchPath(NativePath path, PlacementLocation type)
  1000. {
  1001. m_repositories[type].searchPath ~= path;
  1002. this.m_repositories[type].writeLocalPackageList();
  1003. }
  1004.  
  1005. /// Removes a search path from the given type.
  1006. void removeSearchPath(NativePath path, PlacementLocation type)
  1007. {
  1008. m_repositories[type].searchPath = m_repositories[type].searchPath.filter!(p => p != path)().array();
  1009. this.m_repositories[type].writeLocalPackageList();
  1010. }
  1011.  
  1012. deprecated("Use `refresh()` without boolean argument(same as `refresh(false)`")
  1013. void refresh(bool refresh)
  1014. {
  1015. if (refresh)
  1016. logDiagnostic("Refreshing local packages (refresh existing: true)...");
  1017. this.refresh_(refresh);
  1018. }
  1019.  
  1020. void refresh()
  1021. {
  1022. this.refresh_(false);
  1023. }
  1024.  
  1025. private void refresh_(bool refresh)
  1026. {
  1027. if (!refresh)
  1028. logDiagnostic("Scanning local packages...");
  1029.  
  1030. foreach (ref repository; this.m_repositories)
  1031. repository.scanLocalPackages(refresh, this);
  1032.  
  1033. this.m_internal.scan(this, refresh);
  1034. foreach (ref repository; this.m_repositories)
  1035. repository.scan(this, refresh);
  1036.  
  1037. foreach (ref repository; this.m_repositories)
  1038. repository.loadOverrides();
  1039. this.m_initialized = true;
  1040. }
  1041.  
  1042. alias Hash = ubyte[];
  1043. /// Generates a hash digest for a given package.
  1044. /// Some files or folders are ignored during the generation (like .dub and
  1045. /// .svn folders)
  1046. Hash hashPackage(Package pack)
  1047. {
  1048. import dub.internal.vibecompat.core.file;
  1049.  
  1050. string[] ignored_directories = [".git", ".dub", ".svn"];
  1051. // something from .dub_ignore or what?
  1052. string[] ignored_files = [];
  1053. SHA256 hash;
  1054. foreach(file; dirEntries(pack.path.toNativeString(), SpanMode.depth)) {
  1055. const isDir = file.isDir;
  1056. if(isDir && ignored_directories.canFind(NativePath(file.name).head.name))
  1057. continue;
  1058. else if(ignored_files.canFind(NativePath(file.name).head.name))
  1059. continue;
  1060.  
  1061. hash.put(cast(ubyte[])NativePath(file.name).head.name);
  1062. if(isDir) {
  1063. logDebug("Hashed directory name %s", NativePath(file.name).head);
  1064. }
  1065. else {
  1066. hash.put(cast(ubyte[]) readFile(NativePath(file.name)));
  1067. logDebug("Hashed file contents from %s", NativePath(file.name).head);
  1068. }
  1069. }
  1070. auto digest = hash.finish();
  1071. logDebug("Project hash: %s", digest);
  1072. return digest[].dup;
  1073. }
  1074.  
  1075. /**
  1076. * Writes the selections file (`dub.selections.json`)
  1077. *
  1078. * The selections file is only used for the root package / project.
  1079. * However, due to it being a filesystem interaction, it is managed
  1080. * from the `PackageManager`.
  1081. *
  1082. * Params:
  1083. * project = The root package / project to read the selections file for.
  1084. * selections = The `SelectionsFile` to write.
  1085. * overwrite = Whether to overwrite an existing selections file.
  1086. * True by default.
  1087. */
  1088. public void writeSelections(in Package project, in Selections!1 selections,
  1089. bool overwrite = true)
  1090. {
  1091. import dub.internal.vibecompat.core.file;
  1092.  
  1093. const path = project.path ~ "dub.selections.json";
  1094. if (!overwrite && existsFile(path))
  1095. return;
  1096. writeFile(path, selectionsToString(selections));
  1097. }
  1098.  
  1099. /// Package function to avoid code duplication with deprecated
  1100. /// SelectedVersions.save, merge with `writeSelections` in
  1101. /// the future.
  1102. package static string selectionsToString (in Selections!1 s)
  1103. {
  1104. Json json = selectionsToJSON(s);
  1105. assert(json.type == Json.Type.object);
  1106. assert(json.length == 2);
  1107. assert(json["versions"].type != Json.Type.undefined);
  1108.  
  1109. auto result = appender!string();
  1110. result.put("{\n\t\"fileVersion\": ");
  1111. result.writeJsonString(json["fileVersion"]);
  1112. result.put(",\n\t\"versions\": {");
  1113. auto vers = json["versions"].get!(Json[string]);
  1114. bool first = true;
  1115. foreach (k; vers.byKey.array.sort()) {
  1116. if (!first) result.put(",");
  1117. else first = false;
  1118. result.put("\n\t\t");
  1119. result.writeJsonString(Json(k));
  1120. result.put(": ");
  1121. result.writeJsonString(vers[k]);
  1122. }
  1123. result.put("\n\t}\n}\n");
  1124. return result.data;
  1125. }
  1126.  
  1127. /// Ditto
  1128. package static Json selectionsToJSON (in Selections!1 s)
  1129. {
  1130. Json serialized = Json.emptyObject;
  1131. serialized["fileVersion"] = s.fileVersion;
  1132. serialized["versions"] = Json.emptyObject;
  1133. foreach (p, dep; s.versions)
  1134. serialized["versions"][p] = dep.toJson(true);
  1135. return serialized;
  1136. }
  1137.  
  1138. /// Adds the package and scans for sub-packages.
  1139. protected void addPackages(ref Package[] dst_repos, Package pack)
  1140. {
  1141. // Add the main package.
  1142. dst_repos ~= pack;
  1143.  
  1144. // Additionally to the internally defined sub-packages, whose metadata
  1145. // is loaded with the main dub.json, load all externally defined
  1146. // packages after the package is available with all the data.
  1147. foreach (spr; pack.subPackages) {
  1148. Package sp;
  1149.  
  1150. if (spr.path.length) {
  1151. auto p = NativePath(spr.path);
  1152. p.normalize();
  1153. enforce(!p.absolute, "Sub package paths must be sub paths of the parent package.");
  1154. auto path = pack.path ~ p;
  1155. sp = this.load(path, NativePath.init, pack);
  1156. } else sp = new Package(spr.recipe, pack.path, pack);
  1157.  
  1158. // Add the sub-package.
  1159. try {
  1160. dst_repos ~= sp;
  1161. } catch (Exception e) {
  1162. logError("Package '%s': Failed to load sub-package %s: %s", pack.name,
  1163. spr.path.length ? spr.path : spr.recipe.name, e.msg);
  1164. logDiagnostic("Full error: %s", e.toString().sanitize());
  1165. }
  1166. }
  1167. }
  1168.  
  1169. /// Used for dependency injection in `Location`
  1170. protected bool existsDirectory(NativePath path)
  1171. {
  1172. static import dub.internal.vibecompat.core.file;
  1173. return dub.internal.vibecompat.core.file.existsDirectory(path);
  1174. }
  1175.  
  1176. /// Ditto
  1177. protected alias IterateDirDg = int delegate(scope int delegate(ref FileInfo));
  1178.  
  1179. /// Ditto
  1180. protected IterateDirDg iterateDirectory(NativePath path)
  1181. {
  1182. static import dub.internal.vibecompat.core.file;
  1183. return dub.internal.vibecompat.core.file.iterateDirectory(path);
  1184. }
  1185. }
  1186.  
  1187. deprecated(OverrideDepMsg)
  1188. alias PackageOverride = PackageOverride_;
  1189.  
  1190. package(dub) struct PackageOverride_ {
  1191. private alias ResolvedDep = SumType!(NativePath, Version);
  1192. string package_;
  1193. VersionRange source;
  1194. ResolvedDep target;
  1195.  
  1196. deprecated("Use `source` instead")
  1197. @property inout(Dependency) version_ () inout return @safe {
  1198. return Dependency(this.source);
  1199. }
  1200.  
  1201. deprecated("Assign `source` instead")
  1202. @property ref PackageOverride version_ (Dependency v) scope return @safe pure {
  1203. this.source = v.visit!(
  1204. (VersionRange range) => range,
  1205. (any) {
  1206. int a; if (a) return VersionRange.init; // Trick the compiler
  1207. throw new Exception("Cannot use anything else than a `VersionRange` for overrides");
  1208. },
  1209. );
  1210. return this;
  1211. }
  1212.  
  1213. deprecated("Use `target.match` directly instead")
  1214. @property inout(Version) targetVersion () inout return @safe pure nothrow @nogc {
  1215. return this.target.match!(
  1216. (Version v) => v,
  1217. (any) => Version.init,
  1218. );
  1219. }
  1220.  
  1221. deprecated("Assign `target` directly instead")
  1222. @property ref PackageOverride targetVersion (Version v) scope return pure nothrow @nogc {
  1223. this.target = v;
  1224. return this;
  1225. }
  1226.  
  1227. deprecated("Use `target.match` directly instead")
  1228. @property inout(NativePath) targetPath () inout return @safe pure nothrow @nogc {
  1229. return this.target.match!(
  1230. (NativePath v) => v,
  1231. (any) => NativePath.init,
  1232. );
  1233. }
  1234.  
  1235. deprecated("Assign `target` directly instead")
  1236. @property ref PackageOverride targetPath (NativePath v) scope return pure nothrow @nogc {
  1237. this.target = v;
  1238. return this;
  1239. }
  1240.  
  1241. deprecated("Use the overload that accepts a `VersionRange` as 2nd argument")
  1242. this(string package_, Dependency version_, Version target_version)
  1243. {
  1244. this.package_ = package_;
  1245. this.version_ = version_;
  1246. this.target = target_version;
  1247. }
  1248.  
  1249. deprecated("Use the overload that accepts a `VersionRange` as 2nd argument")
  1250. this(string package_, Dependency version_, NativePath target_path)
  1251. {
  1252. this.package_ = package_;
  1253. this.version_ = version_;
  1254. this.target = target_path;
  1255. }
  1256.  
  1257. this(string package_, VersionRange src, Version target)
  1258. {
  1259. this.package_ = package_;
  1260. this.source = src;
  1261. this.target = target;
  1262. }
  1263.  
  1264. this(string package_, VersionRange src, NativePath target)
  1265. {
  1266. this.package_ = package_;
  1267. this.source = src;
  1268. this.target = target;
  1269. }
  1270. }
  1271.  
  1272. deprecated("Use `PlacementLocation` instead")
  1273. enum LocalPackageType : PlacementLocation {
  1274. package_ = PlacementLocation.local,
  1275. user = PlacementLocation.user,
  1276. system = PlacementLocation.system,
  1277. }
  1278.  
  1279. private enum LocalPackagesFilename = "local-packages.json";
  1280. private enum LocalOverridesFilename = "local-overrides.json";
  1281.  
  1282. /**
  1283. * A managed location, with packages, configuration, and overrides
  1284. *
  1285. * There exists three standards locations, listed in `PlacementLocation`.
  1286. * The user one is the default, with the system and local one meeting
  1287. * different needs.
  1288. *
  1289. * Each location has a root, under which the following may be found:
  1290. * - A `packages/` directory, where packages are stored (see `packagePath`);
  1291. * - A `local-packages.json` file, with extra search paths
  1292. * and manually added packages (see `dub add-local`);
  1293. * - A `local-overrides.json` file, with manually added overrides (`dub add-override`);
  1294. *
  1295. * Additionally, each location host a config file,
  1296. * which is not managed by this module, but by dub itself.
  1297. */
  1298. package struct Location {
  1299. /// The absolute path to the root of the location
  1300. NativePath packagePath;
  1301.  
  1302. /// Configured (extra) search paths for this `Location`
  1303. NativePath[] searchPath;
  1304.  
  1305. /**
  1306. * List of manually registered packages at this `Location`
  1307. * and stored in `local-packages.json`
  1308. */
  1309. Package[] localPackages;
  1310.  
  1311. /// List of overrides stored at this `Location`
  1312. PackageOverride_[] overrides;
  1313.  
  1314. /**
  1315. * List of packages stored under `packagePath` and automatically detected
  1316. */
  1317. Package[] fromPath;
  1318.  
  1319. this(NativePath path) @safe pure nothrow @nogc
  1320. {
  1321. this.packagePath = path;
  1322. }
  1323.  
  1324. void loadOverrides()
  1325. {
  1326. import dub.internal.vibecompat.core.file;
  1327.  
  1328. this.overrides = null;
  1329. auto ovrfilepath = this.packagePath ~ LocalOverridesFilename;
  1330. if (existsFile(ovrfilepath)) {
  1331. logWarn("Found local override file: %s", ovrfilepath);
  1332. logWarn(OverrideDepMsg);
  1333. logWarn("Replace with a path-based dependency in your project or a custom cache path");
  1334. foreach (entry; jsonFromFile(ovrfilepath)) {
  1335. PackageOverride_ ovr;
  1336. ovr.package_ = entry["name"].get!string;
  1337. ovr.source = VersionRange.fromString(entry["version"].get!string);
  1338. if (auto pv = "targetVersion" in entry) ovr.target = Version(pv.get!string);
  1339. if (auto pv = "targetPath" in entry) ovr.target = NativePath(pv.get!string);
  1340. this.overrides ~= ovr;
  1341. }
  1342. }
  1343. }
  1344.  
  1345. private void writeOverrides()
  1346. {
  1347. import dub.internal.vibecompat.core.file;
  1348.  
  1349. Json[] newlist;
  1350. foreach (ovr; this.overrides) {
  1351. auto jovr = Json.emptyObject;
  1352. jovr["name"] = ovr.package_;
  1353. jovr["version"] = ovr.source.toString();
  1354. ovr.target.match!(
  1355. (NativePath path) { jovr["targetPath"] = path.toNativeString(); },
  1356. (Version vers) { jovr["targetVersion"] = vers.toString(); },
  1357. );
  1358. newlist ~= jovr;
  1359. }
  1360. auto path = this.packagePath;
  1361. ensureDirectory(path);
  1362. writeJsonFile(path ~ LocalOverridesFilename, Json(newlist));
  1363. }
  1364.  
  1365. private void writeLocalPackageList()
  1366. {
  1367. import dub.internal.vibecompat.core.file;
  1368.  
  1369. Json[] newlist;
  1370. foreach (p; this.searchPath) {
  1371. auto entry = Json.emptyObject;
  1372. entry["name"] = "*";
  1373. entry["path"] = p.toNativeString();
  1374. newlist ~= entry;
  1375. }
  1376.  
  1377. foreach (p; this.localPackages) {
  1378. if (p.parentPackage) continue; // do not store sub packages
  1379. auto entry = Json.emptyObject;
  1380. entry["name"] = p.name;
  1381. entry["version"] = p.version_.toString();
  1382. entry["path"] = p.path.toNativeString();
  1383. newlist ~= entry;
  1384. }
  1385.  
  1386. NativePath path = this.packagePath;
  1387. ensureDirectory(path);
  1388. writeJsonFile(path ~ LocalPackagesFilename, Json(newlist));
  1389. }
  1390.  
  1391. // load locally defined packages
  1392. void scanLocalPackages(bool refresh, PackageManager manager)
  1393. {
  1394. import dub.internal.vibecompat.core.file;
  1395.  
  1396. NativePath list_path = this.packagePath;
  1397. Package[] packs;
  1398. NativePath[] paths;
  1399. try {
  1400. auto local_package_file = list_path ~ LocalPackagesFilename;
  1401. if (!existsFile(local_package_file)) return;
  1402.  
  1403. logDiagnostic("Loading local package map at %s", local_package_file.toNativeString());
  1404. auto packlist = jsonFromFile(local_package_file);
  1405. enforce(packlist.type == Json.Type.array, LocalPackagesFilename ~ " must contain an array.");
  1406. foreach (pentry; packlist) {
  1407. try {
  1408. auto name = pentry["name"].get!string;
  1409. auto path = NativePath(pentry["path"].get!string);
  1410. if (name == "*") {
  1411. paths ~= path;
  1412. } else {
  1413. auto ver = Version(pentry["version"].get!string);
  1414.  
  1415. Package pp;
  1416. if (!refresh) {
  1417. foreach (p; this.localPackages)
  1418. if (p.path == path) {
  1419. pp = p;
  1420. break;
  1421. }
  1422. }
  1423.  
  1424. if (!pp) {
  1425. auto infoFile = Package.findPackageFile(path);
  1426. if (!infoFile.empty) pp = manager.load(path, infoFile);
  1427. else {
  1428. logWarn("Locally registered package %s %s was not found. Please run 'dub remove-local \"%s\"'.",
  1429. name, ver, path.toNativeString());
  1430. // Store a dummy package
  1431. pp = new Package(PackageRecipe(name), path);
  1432. }
  1433. }
  1434.  
  1435. if (pp.name != name)
  1436. logWarn("Local package at %s has different name than %s (%s)", path.toNativeString(), name, pp.name);
  1437. pp.version_ = ver;
  1438. manager.addPackages(packs, pp);
  1439. }
  1440. } catch (Exception e) {
  1441. logWarn("Error adding local package: %s", e.msg);
  1442. }
  1443. }
  1444. } catch (Exception e) {
  1445. logDiagnostic("Loading of local package list at %s failed: %s", list_path.toNativeString(), e.msg);
  1446. }
  1447. this.localPackages = packs;
  1448. this.searchPath = paths;
  1449. }
  1450.  
  1451. /**
  1452. * Scan this location
  1453. */
  1454. void scan(PackageManager mgr, bool refresh)
  1455. {
  1456. // If we're asked to refresh, reload the packages from scratch
  1457. auto existing = refresh ? null : this.fromPath;
  1458. if (this.packagePath !is NativePath.init) {
  1459. // For the internal location, we use `fromPath` to store packages
  1460. // loaded by the user (e.g. the project and its sub-packages),
  1461. // so don't clean it.
  1462. this.fromPath = null;
  1463. }
  1464. foreach (path; this.searchPath)
  1465. this.scanPackageFolder(path, mgr, existing);
  1466. if (this.packagePath !is NativePath.init)
  1467. this.scanPackageFolder(this.packagePath, mgr, existing);
  1468. }
  1469.  
  1470. /**
  1471. * Scan the content of a folder (`packagePath` or in `searchPaths`),
  1472. * and add all packages that were found to this location.
  1473. */
  1474. void scanPackageFolder(NativePath path, PackageManager mgr,
  1475. Package[] existing_packages)
  1476. {
  1477. if (!mgr.existsDirectory(path))
  1478. return;
  1479.  
  1480. void loadInternal (NativePath pack_path, NativePath packageFile)
  1481. {
  1482. import std.algorithm.searching : find;
  1483.  
  1484. // If the package has already been loaded, no need to re-load it.
  1485. auto rng = existing_packages.find!(pp => pp.path == pack_path);
  1486. if (!rng.empty)
  1487. return mgr.addPackages(this.fromPath, rng.front);
  1488.  
  1489. try {
  1490. mgr.addPackages(this.fromPath, mgr.load(pack_path, packageFile));
  1491. } catch (ConfigException exc) {
  1492. // Configy error message already include the path
  1493. logError("Invalid recipe for local package: %S", exc);
  1494. } catch (Exception e) {
  1495. logError("Failed to load package in %s: %s", pack_path, e.msg);
  1496. logDiagnostic("Full error: %s", e.toString().sanitize());
  1497. }
  1498. }
  1499.  
  1500. logDebug("iterating dir %s", path.toNativeString());
  1501. try foreach (pdir; mgr.iterateDirectory(path)) {
  1502. logDebug("iterating dir %s entry %s", path.toNativeString(), pdir.name);
  1503. if (!pdir.isDirectory) continue;
  1504.  
  1505. const pack_path = path ~ (pdir.name ~ "/");
  1506. auto packageFile = Package.findPackageFile(pack_path);
  1507.  
  1508. if (isManaged(path)) {
  1509. // Old / flat directory structure, used in non-standard path
  1510. // Packages are stored in $ROOT/$SOMETHING/`
  1511. if (!packageFile.empty) {
  1512. // Deprecated flat managed directory structure
  1513. logWarn("Package at path '%s' should be under '%s'",
  1514. pack_path.toNativeString().color(Mode.bold),
  1515. (pack_path ~ "$VERSION" ~ pdir.name).toNativeString().color(Mode.bold));
  1516. logWarn("The package will no longer be detected starting from v1.42.0");
  1517. loadInternal(pack_path, packageFile);
  1518. } else {
  1519. // New managed structure: $ROOT/$NAME/$VERSION/$NAME
  1520. // This is the most common code path
  1521.  
  1522. // Iterate over versions of a package
  1523. foreach (versdir; mgr.iterateDirectory(pack_path)) {
  1524. if (!versdir.isDirectory) continue;
  1525. auto vers_path = pack_path ~ versdir.name ~ (pdir.name ~ "/");
  1526. if (!mgr.existsDirectory(vers_path)) continue;
  1527. packageFile = Package.findPackageFile(vers_path);
  1528. loadInternal(vers_path, packageFile);
  1529. }
  1530. }
  1531. } else {
  1532. // Unmanaged directories (dub add-path) are always stored as a
  1533. // flat list of packages, as these are the working copies managed
  1534. // by the user. The nested structure should not be supported,
  1535. // even optionally, because that would lead to bogus "no package
  1536. // file found" errors in case the internal directory structure
  1537. // accidentally matches the $NAME/$VERSION/$NAME scheme
  1538. if (!packageFile.empty)
  1539. loadInternal(pack_path, packageFile);
  1540. }
  1541. }
  1542. catch (Exception e)
  1543. logDiagnostic("Failed to enumerate %s packages: %s", path.toNativeString(), e.toString());
  1544. }
  1545.  
  1546. /**
  1547. * Looks up already-loaded packages at a specific version
  1548. *
  1549. * Looks up a package according to this `Location`'s priority,
  1550. * that is, packages from the search path and local packages
  1551. * have the highest priority.
  1552. *
  1553. * Params:
  1554. * name = The full name of the package to look up
  1555. * ver = The version to look up
  1556. *
  1557. * Returns:
  1558. * A `Package` if one was found, `null` if none exists.
  1559. */
  1560. inout(Package) lookup(in PackageName name, in Version ver) inout {
  1561. foreach (pkg; this.localPackages)
  1562. if (pkg.name == name.toString() &&
  1563. pkg.version_.matches(ver, VersionMatchMode.standard))
  1564. return pkg;
  1565. foreach (pkg; this.fromPath) {
  1566. auto pvm = this.isManaged(pkg.basePackage.path) ?
  1567. VersionMatchMode.strict : VersionMatchMode.standard;
  1568. if (pkg.name == name.toString() && pkg.version_.matches(ver, pvm))
  1569. return pkg;
  1570. }
  1571. return null;
  1572. }
  1573.  
  1574. /**
  1575. * Looks up a package, first in the list of loaded packages,
  1576. * then directly on the file system.
  1577. *
  1578. * This function allows for lazy loading of packages, without needing to
  1579. * first scan all the available locations (as `scan` does).
  1580. *
  1581. * Params:
  1582. * name = The full name of the package to look up
  1583. * vers = The version the package must match
  1584. * mgr = The `PackageManager` to use for adding packages
  1585. *
  1586. * Returns:
  1587. * A `Package` if one was found, `null` if none exists.
  1588. */
  1589. Package load (in PackageName name, Version vers, PackageManager mgr)
  1590. {
  1591. if (auto pkg = this.lookup(name, vers))
  1592. return pkg;
  1593.  
  1594. string versStr = vers.toString();
  1595. const path = this.getPackagePath(name, versStr);
  1596. if (!mgr.existsDirectory(path))
  1597. return null;
  1598.  
  1599. logDiagnostic("Lazily loading package %s:%s from %s", name.main, vers, path);
  1600. auto p = mgr.load(path);
  1601. enforce(
  1602. p.version_ == vers,
  1603. format("Package %s located in %s has a different version than its path: Got %s, expected %s",
  1604. name, path, p.version_, vers));
  1605. mgr.addPackages(this.fromPath, p);
  1606. return p;
  1607. }
  1608.  
  1609. /**
  1610. * Get the final destination a specific package needs to be stored in.
  1611. *
  1612. * Note that there needs to be an extra level for libraries like `ae`
  1613. * which expects their containing folder to have an exact name and use
  1614. * `importPath "../"`.
  1615. *
  1616. * Hence the final format returned is `$BASE/$NAME/$VERSION/$NAME`,
  1617. * `$BASE` is `this.packagePath`.
  1618. *
  1619. * Params:
  1620. * name = The package name - if the name is that of a subpackage,
  1621. * only the path to the main package is returned, as the
  1622. * subpackage path can only be known after reading the recipe.
  1623. * vers = A version string. Typed as a string because git hashes
  1624. * can be used with this function.
  1625. *
  1626. * Returns:
  1627. * An absolute `NativePath` nested in this location.
  1628. */
  1629. NativePath getPackagePath (in PackageName name, string vers)
  1630. {
  1631. NativePath result = this.packagePath ~ name.main.toString() ~ vers ~
  1632. name.main.toString();
  1633. result.endsWithSlash = true;
  1634. return result;
  1635. }
  1636.  
  1637. /// Determines if a specific path is within a DUB managed Location.
  1638. bool isManaged(NativePath path) const {
  1639. return path.startsWith(this.packagePath);
  1640. }
  1641. }
  1642.  
  1643. private immutable string OverrideDepMsg =
  1644. "Overrides are deprecated as they are redundant with more fine-grained approaches";