Newer
Older
dub_jkp / source / dub / dub.d
  1. /**
  2. A package manager.
  3.  
  4. Copyright: © 2012-2013 Matthias Dondorff, 2012-2016 Sönke Ludwig
  5. License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file.
  6. Authors: Matthias Dondorff, Sönke Ludwig
  7. */
  8. module dub.dub;
  9.  
  10. import dub.compilers.compiler;
  11. import dub.dependency;
  12. import dub.dependencyresolver;
  13. import dub.internal.utils;
  14. import dub.internal.vibecompat.core.file;
  15. import dub.internal.vibecompat.data.json;
  16. import dub.internal.vibecompat.inet.url;
  17. import dub.internal.logging;
  18. import dub.package_;
  19. import dub.packagemanager;
  20. import dub.packagesuppliers;
  21. import dub.project;
  22. import dub.generators.generator;
  23. import dub.init;
  24.  
  25. import std.algorithm;
  26. import std.array : array, replace;
  27. import std.conv : text, to;
  28. import std.encoding : sanitize;
  29. import std.exception : enforce;
  30. import std.file;
  31. import std.process : environment;
  32. import std.range : assumeSorted, empty;
  33. import std.string;
  34.  
  35. // Set output path and options for coverage reports
  36. version (DigitalMars) version (D_Coverage)
  37. {
  38. shared static this()
  39. {
  40. import core.runtime, std.file, std.path, std.stdio;
  41. dmd_coverSetMerge(true);
  42. auto path = buildPath(dirName(thisExePath()), "../cov");
  43. if (!path.exists)
  44. mkdir(path);
  45. dmd_coverDestPath(path);
  46. }
  47. }
  48.  
  49. static this()
  50. {
  51. import dub.compilers.dmd : DMDCompiler;
  52. import dub.compilers.gdc : GDCCompiler;
  53. import dub.compilers.ldc : LDCCompiler;
  54. registerCompiler(new DMDCompiler);
  55. registerCompiler(new GDCCompiler);
  56. registerCompiler(new LDCCompiler);
  57. }
  58.  
  59. deprecated("use defaultRegistryURLs") enum defaultRegistryURL = defaultRegistryURLs[0];
  60.  
  61. /// The URL to the official package registry and it's default fallback registries.
  62. static immutable string[] defaultRegistryURLs = [
  63. "https://code.dlang.org/",
  64. "https://codemirror.dlang.org/",
  65. "https://dub.bytecraft.nl/",
  66. "https://code-mirror.dlang.io/",
  67. ];
  68.  
  69. /** Returns a default list of package suppliers.
  70.  
  71. This will contain a single package supplier that points to the official
  72. package registry.
  73.  
  74. See_Also: `defaultRegistryURLs`
  75. */
  76. PackageSupplier[] defaultPackageSuppliers()
  77. {
  78. logDiagnostic("Using dub registry url '%s'", defaultRegistryURLs[0]);
  79. return [new FallbackPackageSupplier(defaultRegistryURLs.map!getRegistryPackageSupplier.array)];
  80. }
  81.  
  82. /** Returns a registry package supplier according to protocol.
  83.  
  84. Allowed protocols are dub+http(s):// and maven+http(s)://.
  85. */
  86. PackageSupplier getRegistryPackageSupplier(string url)
  87. {
  88. switch (url.startsWith("dub+", "mvn+", "file://"))
  89. {
  90. case 1:
  91. return new RegistryPackageSupplier(URL(url[4..$]));
  92. case 2:
  93. return new MavenRegistryPackageSupplier(URL(url[4..$]));
  94. case 3:
  95. return new FileSystemPackageSupplier(NativePath(url[7..$]));
  96. default:
  97. return new RegistryPackageSupplier(URL(url));
  98. }
  99. }
  100.  
  101. unittest
  102. {
  103. auto dubRegistryPackageSupplier = getRegistryPackageSupplier("dub+https://code.dlang.org");
  104. assert(dubRegistryPackageSupplier.description.canFind(" https://code.dlang.org"));
  105.  
  106. dubRegistryPackageSupplier = getRegistryPackageSupplier("https://code.dlang.org");
  107. assert(dubRegistryPackageSupplier.description.canFind(" https://code.dlang.org"));
  108.  
  109. auto mavenRegistryPackageSupplier = getRegistryPackageSupplier("mvn+http://localhost:8040/maven/libs-release/dubpackages");
  110. assert(mavenRegistryPackageSupplier.description.canFind(" http://localhost:8040/maven/libs-release/dubpackages"));
  111.  
  112. auto fileSystemPackageSupplier = getRegistryPackageSupplier("file:///etc/dubpackages");
  113. assert(fileSystemPackageSupplier.description.canFind(" " ~ NativePath("/etc/dubpackages").toNativeString));
  114. }
  115.  
  116. /** Provides a high-level entry point for DUB's functionality.
  117.  
  118. This class provides means to load a certain project (a root package with
  119. all of its dependencies) and to perform high-level operations as found in
  120. the command line interface.
  121. */
  122. class Dub {
  123. private {
  124. bool m_dryRun = false;
  125. PackageManager m_packageManager;
  126. PackageSupplier[] m_packageSuppliers;
  127. NativePath m_rootPath;
  128. SpecialDirs m_dirs;
  129. UserConfiguration m_config;
  130. Project m_project;
  131. string m_defaultCompiler;
  132. }
  133.  
  134. /** The default placement location of fetched packages.
  135.  
  136. This property can be altered, so that packages which are downloaded as part
  137. of the normal upgrade process are stored in a certain location. This is
  138. how the "--local" and "--system" command line switches operate.
  139. */
  140. PlacementLocation defaultPlacementLocation = PlacementLocation.user;
  141.  
  142.  
  143. /** Initializes the instance for use with a specific root package.
  144.  
  145. Note that a package still has to be loaded using one of the
  146. `loadPackage` overloads.
  147.  
  148. Params:
  149. root_path = Path to the root package
  150. additional_package_suppliers = A list of package suppliers to try
  151. before the suppliers found in the configurations files and the
  152. `defaultPackageSuppliers`.
  153. skip_registry = Can be used to skip using the configured package
  154. suppliers, as well as the default suppliers.
  155. */
  156. this(string root_path = ".", PackageSupplier[] additional_package_suppliers = null,
  157. SkipPackageSuppliers skip_registry = SkipPackageSuppliers.none)
  158. {
  159. m_rootPath = NativePath(root_path);
  160. if (!m_rootPath.absolute) m_rootPath = NativePath(getcwd()) ~ m_rootPath;
  161.  
  162. init();
  163.  
  164. m_packageSuppliers = this.computePkgSuppliers(additional_package_suppliers,
  165. skip_registry, environment.get("DUB_REGISTRY", null));
  166. m_packageManager = new PackageManager(m_rootPath, m_dirs.localRepository, m_dirs.systemSettings);
  167.  
  168. auto ccps = m_config.customCachePaths;
  169. if (ccps.length)
  170. m_packageManager.customCachePaths = ccps;
  171.  
  172. // TODO: Move this environment read out of the ctor
  173. if (auto p = environment.get("DUBPATH")) {
  174. version(Windows) enum pathsep = ";";
  175. else enum pathsep = ":";
  176. NativePath[] paths = p.split(pathsep)
  177. .map!(p => NativePath(p))().array();
  178. m_packageManager.searchPath = paths;
  179. }
  180. }
  181.  
  182. /** Initializes the instance with a single package search path, without
  183. loading a package.
  184.  
  185. This constructor corresponds to the "--bare" option of the command line
  186. interface. Use
  187. */
  188. this(NativePath override_path)
  189. {
  190. init();
  191. m_packageManager = new PackageManager(override_path);
  192. }
  193.  
  194. private void init()
  195. {
  196. this.m_dirs = SpecialDirs.make();
  197. this.loadConfig();
  198. this.determineDefaultCompiler();
  199. }
  200.  
  201. /**
  202. * Load user configuration for this instance
  203. *
  204. * This can be overloaded in child classes to prevent library / unittest
  205. * dub from doing any kind of file IO.
  206. */
  207. protected void loadConfig()
  208. {
  209. import configy.Read;
  210.  
  211. void readSettingsFile (NativePath path_)
  212. {
  213. // TODO: Remove `StrictMode.Warn` after v1.40 release
  214. // The default is to error, but as the previous parser wasn't
  215. // complaining, we should first warn the user.
  216. const path = path_.toNativeString();
  217. if (path.exists) {
  218. auto newConf = parseConfigFileSimple!UserConfiguration(path, StrictMode.Warn);
  219. if (!newConf.isNull())
  220. this.m_config = this.m_config.merge(newConf.get());
  221. }
  222. }
  223.  
  224. const dubFolderPath = NativePath(thisExePath).parentPath;
  225.  
  226. // override default userSettings + localRepository if a $DPATH or
  227. // $DUB_HOME environment variable is set.
  228. bool overrideDubHomeFromEnv;
  229. {
  230. string dubHome = environment.get("DUB_HOME");
  231. if (!dubHome.length) {
  232. auto dpath = environment.get("DPATH");
  233. if (dpath.length)
  234. dubHome = (NativePath(dpath) ~ "dub/").toNativeString();
  235.  
  236. }
  237. if (dubHome.length) {
  238. overrideDubHomeFromEnv = true;
  239.  
  240. m_dirs.userSettings = NativePath(dubHome);
  241. m_dirs.localRepository = m_dirs.userSettings;
  242. }
  243. }
  244.  
  245. readSettingsFile(m_dirs.systemSettings ~ "settings.json");
  246. readSettingsFile(dubFolderPath ~ "../etc/dub/settings.json");
  247. version (Posix) {
  248. if (dubFolderPath.absolute && dubFolderPath.startsWith(NativePath("usr")))
  249. readSettingsFile(NativePath("/etc/dub/settings.json"));
  250. }
  251.  
  252. // Override user + local package path from system / binary settings
  253. // Then continues loading local settings from these folders. (keeping
  254. // global /etc/dub/settings.json settings intact)
  255. //
  256. // Don't use it if either $DPATH or $DUB_HOME are set, as environment
  257. // variables usually take precedence over configuration.
  258. if (!overrideDubHomeFromEnv && this.m_config.dubHome.set) {
  259. m_dirs.userSettings = NativePath(this.m_config.dubHome.expandEnvironmentVariables);
  260. }
  261.  
  262. // load user config:
  263. readSettingsFile(m_dirs.userSettings ~ "settings.json");
  264.  
  265. // load per-package config:
  266. if (!this.m_rootPath.empty)
  267. readSettingsFile(this.m_rootPath ~ "dub.settings.json");
  268.  
  269. // same as userSettings above, but taking into account the
  270. // config loaded from user settings and per-package config as well.
  271. if (!overrideDubHomeFromEnv && this.m_config.dubHome.set) {
  272. m_dirs.localRepository = NativePath(this.m_config.dubHome.expandEnvironmentVariables);
  273. }
  274. }
  275.  
  276. unittest
  277. {
  278. scope (exit) environment.remove("DUB_REGISTRY");
  279. auto dub = new TestDub(".", null, SkipPackageSuppliers.configured);
  280. assert(dub.m_packageSuppliers.length == 0);
  281. environment["DUB_REGISTRY"] = "http://example.com/";
  282. dub = new TestDub(".", null, SkipPackageSuppliers.configured);
  283. assert(dub.m_packageSuppliers.length == 1);
  284. environment["DUB_REGISTRY"] = "http://example.com/;http://foo.com/";
  285. dub = new TestDub(".", null, SkipPackageSuppliers.configured);
  286. assert(dub.m_packageSuppliers.length == 2);
  287. dub = new TestDub(".", [new RegistryPackageSupplier(URL("http://bar.com/"))], SkipPackageSuppliers.configured);
  288. assert(dub.m_packageSuppliers.length == 3);
  289. }
  290.  
  291. /** Get the list of package suppliers.
  292.  
  293. Params:
  294. additional_package_suppliers = A list of package suppliers to try
  295. before the suppliers found in the configurations files and the
  296. `defaultPackageSuppliers`.
  297. skip_registry = Can be used to skip using the configured package
  298. suppliers, as well as the default suppliers.
  299. */
  300. deprecated("This is an implementation detail. " ~
  301. "Use `packageSuppliers` to get the computed list of package " ~
  302. "suppliers once a `Dub` instance has been constructed.")
  303. public PackageSupplier[] getPackageSuppliers(PackageSupplier[] additional_package_suppliers, SkipPackageSuppliers skip_registry)
  304. {
  305. return this.computePkgSuppliers(additional_package_suppliers, skip_registry, environment.get("DUB_REGISTRY", null));
  306. }
  307.  
  308. /// Ditto
  309. private PackageSupplier[] computePkgSuppliers(
  310. PackageSupplier[] additional_package_suppliers, SkipPackageSuppliers skip_registry,
  311. string dub_registry_var)
  312. {
  313. PackageSupplier[] ps = additional_package_suppliers;
  314.  
  315. if (skip_registry < SkipPackageSuppliers.all)
  316. {
  317. ps ~= dub_registry_var
  318. .splitter(";")
  319. .map!(url => getRegistryPackageSupplier(url))
  320. .array;
  321. }
  322.  
  323. if (skip_registry < SkipPackageSuppliers.configured)
  324. {
  325. ps ~= m_config.registryUrls
  326. .map!(url => getRegistryPackageSupplier(url))
  327. .array;
  328. }
  329.  
  330. if (skip_registry < SkipPackageSuppliers.standard)
  331. ps ~= defaultPackageSuppliers();
  332.  
  333. return ps;
  334. }
  335.  
  336. /// ditto
  337. deprecated("This is an implementation detail. " ~
  338. "Use `packageSuppliers` to get the computed list of package " ~
  339. "suppliers once a `Dub` instance has been constructed.")
  340. public PackageSupplier[] getPackageSuppliers(PackageSupplier[] additional_package_suppliers)
  341. {
  342. return getPackageSuppliers(additional_package_suppliers, m_config.skipRegistry);
  343. }
  344.  
  345. unittest
  346. {
  347. auto dub = new TestDub();
  348.  
  349. assert(dub.computePkgSuppliers(null, SkipPackageSuppliers.none, null).length == 1);
  350. assert(dub.computePkgSuppliers(null, SkipPackageSuppliers.configured, null).length == 0);
  351. assert(dub.computePkgSuppliers(null, SkipPackageSuppliers.standard, null).length == 0);
  352.  
  353. assert(dub.computePkgSuppliers(null, SkipPackageSuppliers.standard, "http://example.com/")
  354. .length == 1);
  355. }
  356.  
  357. @property bool dryRun() const { return m_dryRun; }
  358. @property void dryRun(bool v) { m_dryRun = v; }
  359.  
  360. /** Returns the root path (usually the current working directory).
  361. */
  362. @property NativePath rootPath() const { return m_rootPath; }
  363. /// ditto
  364. @property void rootPath(NativePath root_path)
  365. {
  366. m_rootPath = root_path;
  367. if (!m_rootPath.absolute) m_rootPath = NativePath(getcwd()) ~ m_rootPath;
  368. }
  369.  
  370. /// Returns the name listed in the dub.json of the current
  371. /// application.
  372. @property string projectName() const { return m_project.name; }
  373.  
  374. @property NativePath projectPath() const { return this.m_project.rootPackage.path; }
  375.  
  376. @property string[] configurations() const { return m_project.configurations; }
  377.  
  378. @property inout(PackageManager) packageManager() inout { return m_packageManager; }
  379.  
  380. @property inout(Project) project() inout { return m_project; }
  381.  
  382. @property inout(PackageSupplier)[] packageSuppliers() inout { return m_packageSuppliers; }
  383.  
  384. /** Returns the default compiler binary to use for building D code.
  385.  
  386. If set, the "defaultCompiler" field of the DUB user or system
  387. configuration file will be used. Otherwise the PATH environment variable
  388. will be searched for files named "dmd", "gdc", "gdmd", "ldc2", "ldmd2"
  389. (in that order, taking into account operating system specific file
  390. extensions) and the first match is returned. If no match is found, "dmd"
  391. will be used.
  392. */
  393. @property string defaultCompiler() const { return m_defaultCompiler; }
  394.  
  395. /** Returns the default architecture to use for building D code.
  396.  
  397. If set, the "defaultArchitecture" field of the DUB user or system
  398. configuration file will be used. Otherwise null will be returned.
  399. */
  400. @property string defaultArchitecture() const { return this.m_config.defaultArchitecture; }
  401.  
  402. /** Returns the default low memory option to use for building D code.
  403.  
  404. If set, the "defaultLowMemory" field of the DUB user or system
  405. configuration file will be used. Otherwise false will be returned.
  406. */
  407. @property bool defaultLowMemory() const { return this.m_config.defaultLowMemory; }
  408.  
  409. @property const(string[string]) defaultEnvironments() const { return this.m_config.defaultEnvironments; }
  410. @property const(string[string]) defaultBuildEnvironments() const { return this.m_config.defaultBuildEnvironments; }
  411. @property const(string[string]) defaultRunEnvironments() const { return this.m_config.defaultRunEnvironments; }
  412. @property const(string[string]) defaultPreGenerateEnvironments() const { return this.m_config.defaultPreGenerateEnvironments; }
  413. @property const(string[string]) defaultPostGenerateEnvironments() const { return this.m_config.defaultPostGenerateEnvironments; }
  414. @property const(string[string]) defaultPreBuildEnvironments() const { return this.m_config.defaultPreBuildEnvironments; }
  415. @property const(string[string]) defaultPostBuildEnvironments() const { return this.m_config.defaultPostBuildEnvironments; }
  416. @property const(string[string]) defaultPreRunEnvironments() const { return this.m_config.defaultPreRunEnvironments; }
  417. @property const(string[string]) defaultPostRunEnvironments() const { return this.m_config.defaultPostRunEnvironments; }
  418.  
  419. /** Loads the package that resides within the configured `rootPath`.
  420. */
  421. void loadPackage()
  422. {
  423. loadPackage(m_rootPath);
  424. }
  425.  
  426. /// Loads the package from the specified path as the main project package.
  427. void loadPackage(NativePath path)
  428. {
  429. m_project = new Project(m_packageManager, path);
  430. }
  431.  
  432. /// Loads a specific package as the main project package (can be a sub package)
  433. void loadPackage(Package pack)
  434. {
  435. m_project = new Project(m_packageManager, pack);
  436. }
  437.  
  438. /** Loads a single file package.
  439.  
  440. Single-file packages are D files that contain a package receipe comment
  441. at their top. A recipe comment must be a nested `/+ ... +/` style
  442. comment, containing the virtual recipe file name and a colon, followed by the
  443. recipe contents (what would normally be in dub.sdl/dub.json).
  444.  
  445. Example:
  446. ---
  447. /+ dub.sdl:
  448. name "test"
  449. dependency "vibe-d" version="~>0.7.29"
  450. +/
  451. import vibe.http.server;
  452.  
  453. void main()
  454. {
  455. auto settings = new HTTPServerSettings;
  456. settings.port = 8080;
  457. listenHTTP(settings, &hello);
  458. }
  459.  
  460. void hello(HTTPServerRequest req, HTTPServerResponse res)
  461. {
  462. res.writeBody("Hello, World!");
  463. }
  464. ---
  465.  
  466. The script above can be invoked with "dub --single test.d".
  467. */
  468. void loadSingleFilePackage(NativePath path)
  469. {
  470. import dub.recipe.io : parsePackageRecipe;
  471. import std.file : mkdirRecurse, readText;
  472. import std.path : baseName, stripExtension;
  473.  
  474. path = makeAbsolute(path);
  475.  
  476. string file_content = readText(path.toNativeString());
  477.  
  478. if (file_content.startsWith("#!")) {
  479. auto idx = file_content.indexOf('\n');
  480. enforce(idx > 0, "The source fine doesn't contain anything but a shebang line.");
  481. file_content = file_content[idx+1 .. $];
  482. }
  483.  
  484. file_content = file_content.strip();
  485.  
  486. string recipe_content;
  487.  
  488. if (file_content.startsWith("/+")) {
  489. file_content = file_content[2 .. $];
  490. auto idx = file_content.indexOf("+/");
  491. enforce(idx >= 0, "Missing \"+/\" to close comment.");
  492. recipe_content = file_content[0 .. idx].strip();
  493. } else throw new Exception("The source file must start with a recipe comment.");
  494.  
  495. auto nidx = recipe_content.indexOf('\n');
  496.  
  497. auto idx = recipe_content.indexOf(':');
  498. enforce(idx > 0 && (nidx < 0 || nidx > idx),
  499. "The first line of the recipe comment must list the recipe file name followed by a colon (e.g. \"/+ dub.sdl:\").");
  500. auto recipe_filename = recipe_content[0 .. idx];
  501. recipe_content = recipe_content[idx+1 .. $];
  502. auto recipe_default_package_name = path.toString.baseName.stripExtension.strip;
  503.  
  504. auto recipe = parsePackageRecipe(recipe_content, recipe_filename, null, recipe_default_package_name);
  505. enforce(recipe.buildSettings.sourceFiles.length == 0, "Single-file packages are not allowed to specify source files.");
  506. enforce(recipe.buildSettings.sourcePaths.length == 0, "Single-file packages are not allowed to specify source paths.");
  507. enforce(recipe.buildSettings.importPaths.length == 0, "Single-file packages are not allowed to specify import paths.");
  508. recipe.buildSettings.sourceFiles[""] = [path.toNativeString()];
  509. recipe.buildSettings.sourcePaths[""] = [];
  510. recipe.buildSettings.importPaths[""] = [];
  511. recipe.buildSettings.mainSourceFile = path.toNativeString();
  512. if (recipe.buildSettings.targetType == TargetType.autodetect)
  513. recipe.buildSettings.targetType = TargetType.executable;
  514.  
  515. auto pack = new Package(recipe, path.parentPath, null, "~master");
  516. loadPackage(pack);
  517. }
  518. /// ditto
  519. void loadSingleFilePackage(string path)
  520. {
  521. loadSingleFilePackage(NativePath(path));
  522. }
  523.  
  524. /** Gets the default configuration for a particular build platform.
  525.  
  526. This forwards to `Project.getDefaultConfiguration` and requires a
  527. project to be loaded.
  528. */
  529. string getDefaultConfiguration(in BuildPlatform platform, bool allow_non_library_configs = true) const { return m_project.getDefaultConfiguration(platform, allow_non_library_configs); }
  530.  
  531. /** Attempts to upgrade the dependency selection of the loaded project.
  532.  
  533. Params:
  534. options = Flags that control how the upgrade is carried out
  535. packages_to_upgrade = Optional list of packages. If this list
  536. contains one or more packages, only those packages will
  537. be upgraded. Otherwise, all packages will be upgraded at
  538. once.
  539. */
  540. void upgrade(UpgradeOptions options, string[] packages_to_upgrade = null)
  541. {
  542. // clear non-existent version selections
  543. if (!(options & UpgradeOptions.upgrade)) {
  544. next_pack:
  545. foreach (p; m_project.selections.selectedPackages) {
  546. auto dep = m_project.selections.getSelectedVersion(p);
  547. if (!dep.path.empty) {
  548. auto path = dep.path;
  549. if (!path.absolute) path = this.rootPath ~ path;
  550. try if (m_packageManager.getOrLoadPackage(path)) continue;
  551. catch (Exception e) { logDebug("Failed to load path based selection: %s", e.toString().sanitize); }
  552. } else if (!dep.repository.empty) {
  553. if (m_packageManager.loadSCMPackage(getBasePackageName(p), dep.repository))
  554. continue;
  555. } else {
  556. if (m_packageManager.getPackage(p, dep.version_)) continue;
  557. foreach (ps; m_packageSuppliers) {
  558. try {
  559. auto versions = ps.getVersions(p);
  560. if (versions.canFind!(v => dep.matches(v, VersionMatchMode.strict)))
  561. continue next_pack;
  562. } catch (Exception e) {
  563. logWarn("Error querying versions for %s, %s: %s", p, ps.description, e.msg);
  564. logDebug("Full error: %s", e.toString().sanitize());
  565. }
  566. }
  567. }
  568.  
  569. logWarn("Selected package %s %s doesn't exist. Using latest matching version instead.", p, dep);
  570. m_project.selections.deselectVersion(p);
  571. }
  572. }
  573.  
  574. auto resolver = new DependencyVersionResolver(
  575. this, options, m_project.rootPackage, m_project.selections);
  576. Dependency[string] versions = resolver.resolve(packages_to_upgrade);
  577.  
  578. if (options & UpgradeOptions.dryRun) {
  579. bool any = false;
  580. string rootbasename = getBasePackageName(m_project.rootPackage.name);
  581.  
  582. foreach (p, ver; versions) {
  583. if (!ver.path.empty || !ver.repository.empty) continue;
  584.  
  585. auto basename = getBasePackageName(p);
  586. if (basename == rootbasename) continue;
  587.  
  588. if (!m_project.selections.hasSelectedVersion(basename)) {
  589. logInfo("Upgrade", Color.cyan,
  590. "Package %s would be selected with version %s", basename, ver);
  591. any = true;
  592. continue;
  593. }
  594. auto sver = m_project.selections.getSelectedVersion(basename);
  595. if (!sver.path.empty || !sver.repository.empty) continue;
  596. if (ver.version_ <= sver.version_) continue;
  597. logInfo("Upgrade", Color.cyan,
  598. "%s would be upgraded from %s to %s.",
  599. basename.color(Mode.bold), sver, ver);
  600. any = true;
  601. }
  602. if (any) logInfo("Use \"%s\" to perform those changes", "dub upgrade".color(Mode.bold));
  603. return;
  604. }
  605.  
  606. foreach (p, ver; versions) {
  607. assert(!p.canFind(":"), "Resolved packages contain a sub package!?: "~p);
  608. Package pack;
  609. if (!ver.path.empty) {
  610. try pack = m_packageManager.getOrLoadPackage(ver.path);
  611. catch (Exception e) {
  612. logDebug("Failed to load path based selection: %s", e.toString().sanitize);
  613. continue;
  614. }
  615. } else if (!ver.repository.empty) {
  616. pack = m_packageManager.loadSCMPackage(p, ver.repository);
  617. } else {
  618. assert(ver.isExactVersion, "Resolved dependency is neither path, nor repository, nor exact version based!?");
  619. pack = m_packageManager.getPackage(p, ver.version_);
  620. if (pack && m_packageManager.isManagedPackage(pack)
  621. && ver.version_.isBranch && (options & UpgradeOptions.upgrade) != 0)
  622. {
  623. // TODO: only re-install if there is actually a new commit available
  624. logInfo("Re-installing branch based dependency %s %s", p, ver.toString());
  625. m_packageManager.remove(pack);
  626. pack = null;
  627. }
  628. }
  629.  
  630. FetchOptions fetchOpts;
  631. fetchOpts |= (options & UpgradeOptions.preRelease) != 0 ? FetchOptions.usePrerelease : FetchOptions.none;
  632. if (!pack) fetch(p, ver.version_, defaultPlacementLocation, fetchOpts, "getting selected version");
  633. if ((options & UpgradeOptions.select) && p != m_project.rootPackage.name) {
  634. if (!ver.repository.empty) {
  635. m_project.selections.selectVersion(p, ver.repository);
  636. } else if (ver.path.empty) {
  637. m_project.selections.selectVersion(p, ver.version_);
  638. } else {
  639. NativePath relpath = ver.path;
  640. if (relpath.absolute) relpath = relpath.relativeTo(m_project.rootPackage.path);
  641. m_project.selections.selectVersion(p, relpath);
  642. }
  643. }
  644. }
  645.  
  646. string[] missingDependenciesBeforeReinit = m_project.missingDependencies;
  647. m_project.reinit();
  648.  
  649. if (!m_project.hasAllDependencies) {
  650. auto resolvedDependencies = setDifference(
  651. assumeSorted(missingDependenciesBeforeReinit),
  652. assumeSorted(m_project.missingDependencies)
  653. );
  654. if (!resolvedDependencies.empty)
  655. upgrade(options, m_project.missingDependencies);
  656. }
  657.  
  658. if ((options & UpgradeOptions.select) && !(options & (UpgradeOptions.noSaveSelections | UpgradeOptions.dryRun)))
  659. m_project.saveSelections();
  660. }
  661.  
  662. /** Generate project files for a specified generator.
  663.  
  664. Any existing project files will be overridden.
  665. */
  666. void generateProject(string ide, GeneratorSettings settings)
  667. {
  668. // With a requested `unittest` config, switch to the special test runner
  669. // config (which doesn't require an existing `unittest` configuration).
  670. if (settings.config == "unittest") {
  671. const test_config = m_project.addTestRunnerConfiguration(settings, !m_dryRun);
  672. if (test_config) settings.config = test_config;
  673. }
  674.  
  675. auto generator = createProjectGenerator(ide, m_project);
  676. if (m_dryRun) return; // TODO: pass m_dryRun to the generator
  677. generator.generate(settings);
  678. }
  679.  
  680. /** Generate project files using the special test runner (`dub test`) configuration.
  681.  
  682. Any existing project files will be overridden.
  683. */
  684. void testProject(GeneratorSettings settings, string config, NativePath custom_main_file)
  685. {
  686. if (!custom_main_file.empty && !custom_main_file.absolute) custom_main_file = getWorkingDirectory() ~ custom_main_file;
  687.  
  688. const test_config = m_project.addTestRunnerConfiguration(settings, !m_dryRun, config, custom_main_file);
  689. if (!test_config) return; // target type "none"
  690.  
  691. settings.config = test_config;
  692.  
  693. auto generator = createProjectGenerator("build", m_project);
  694. generator.generate(settings);
  695. }
  696.  
  697. /** Executes D-Scanner tests on the current project. **/
  698. void lintProject(string[] args)
  699. {
  700. import std.path : buildPath, buildNormalizedPath;
  701.  
  702. if (m_dryRun) return;
  703.  
  704. auto tool = "dscanner";
  705.  
  706. auto tool_pack = m_packageManager.getBestPackage(tool);
  707. if (!tool_pack) {
  708. logInfo("Hint", Color.light_blue, "%s is not present, getting and storing it user wide", tool);
  709. tool_pack = fetch(tool, VersionRange.Any, defaultPlacementLocation, FetchOptions.none);
  710. }
  711.  
  712. auto dscanner_dub = new Dub(null, m_packageSuppliers);
  713. dscanner_dub.loadPackage(tool_pack);
  714. dscanner_dub.upgrade(UpgradeOptions.select);
  715.  
  716. GeneratorSettings settings = this.makeAppSettings();
  717. foreach (dependencyPackage; m_project.dependencies)
  718. {
  719. auto cfgs = m_project.getPackageConfigs(settings.platform, null, true);
  720. auto buildSettings = dependencyPackage.getBuildSettings(settings.platform, cfgs[dependencyPackage.name]);
  721. foreach (importPath; buildSettings.importPaths) {
  722. settings.runArgs ~= ["-I", buildNormalizedPath(dependencyPackage.path.toNativeString(), importPath.idup)];
  723. }
  724. }
  725.  
  726. string configFilePath = buildPath(m_project.rootPackage.path.toNativeString(), "dscanner.ini");
  727. if (!args.canFind("--config") && exists(configFilePath)) {
  728. settings.runArgs ~= ["--config", configFilePath];
  729. }
  730.  
  731. settings.runArgs ~= args ~ [m_project.rootPackage.path.toNativeString()];
  732. dscanner_dub.generateProject("build", settings);
  733. }
  734.  
  735. /** Prints the specified build settings necessary for building the root package.
  736. */
  737. void listProjectData(GeneratorSettings settings, string[] requestedData, ListBuildSettingsFormat list_type)
  738. {
  739. import std.stdio;
  740. import std.ascii : newline;
  741.  
  742. // Split comma-separated lists
  743. string[] requestedDataSplit =
  744. requestedData
  745. .map!(a => a.splitter(",").map!strip)
  746. .joiner()
  747. .array();
  748.  
  749. auto data = m_project.listBuildSettings(settings, requestedDataSplit, list_type);
  750.  
  751. string delimiter;
  752. final switch (list_type) with (ListBuildSettingsFormat) {
  753. case list: delimiter = newline ~ newline; break;
  754. case listNul: delimiter = "\0\0"; break;
  755. case commandLine: delimiter = " "; break;
  756. case commandLineNul: delimiter = "\0\0"; break;
  757. }
  758.  
  759. write(data.joiner(delimiter));
  760. if (delimiter != "\0\0") writeln();
  761. }
  762.  
  763. /// Cleans intermediate/cache files of the given package
  764. void cleanPackage(NativePath path)
  765. {
  766. logInfo("Cleaning", Color.green, "package at %s", path.toNativeString().color(Mode.bold));
  767. enforce(!Package.findPackageFile(path).empty, "No package found.", path.toNativeString());
  768.  
  769. // TODO: clear target files and copy files
  770.  
  771. if (existsFile(path ~ ".dub/build")) rmdirRecurse((path ~ ".dub/build").toNativeString());
  772. if (existsFile(path ~ ".dub/metadata_cache.json")) std.file.remove((path ~ ".dub/metadata_cache.json").toNativeString());
  773.  
  774. auto p = Package.load(path);
  775. if (p.getBuildSettings().targetType == TargetType.none) {
  776. foreach (sp; p.subPackages.filter!(sp => !sp.path.empty)) {
  777. cleanPackage(path ~ sp.path);
  778. }
  779. }
  780. }
  781.  
  782. /// Fetches the package matching the dependency and places it in the specified location.
  783. deprecated("Use the overload that accepts either a `Version` or a `VersionRange` as second argument")
  784. Package fetch(string packageId, const Dependency dep, PlacementLocation location, FetchOptions options, string reason = "")
  785. {
  786. const vrange = dep.visit!(
  787. (VersionRange range) => range,
  788. (any) => throw new Exception("Cannot call `dub.fetch` with a " ~ typeof(any).stringof ~ " dependency"),
  789. );
  790. return this.fetch(packageId, vrange, location, options, reason);
  791. }
  792.  
  793. /// Ditto
  794. Package fetch(string packageId, in Version vers, PlacementLocation location, FetchOptions options, string reason = "")
  795. {
  796. return this.fetch(packageId, VersionRange(vers, vers), location, options, reason);
  797. }
  798.  
  799. /// Ditto
  800. Package fetch(string packageId, in VersionRange range, PlacementLocation location, FetchOptions options, string reason = "")
  801. {
  802. auto basePackageName = getBasePackageName(packageId);
  803. Json pinfo;
  804. PackageSupplier supplier;
  805. foreach(ps; m_packageSuppliers){
  806. try {
  807. pinfo = ps.fetchPackageRecipe(basePackageName, Dependency(range), (options & FetchOptions.usePrerelease) != 0);
  808. if (pinfo.type == Json.Type.null_)
  809. continue;
  810. supplier = ps;
  811. break;
  812. } catch(Exception e) {
  813. logWarn("Package %s not found for %s: %s", packageId, ps.description, e.msg);
  814. logDebug("Full error: %s", e.toString().sanitize());
  815. }
  816. }
  817. enforce(pinfo.type != Json.Type.undefined, "No package "~packageId~" was found matching the dependency " ~ range.toString());
  818. Version ver = Version(pinfo["version"].get!string);
  819.  
  820. // always upgrade branch based versions - TODO: actually check if there is a new commit available
  821. Package existing = m_packageManager.getPackage(packageId, ver, location);
  822. if (options & FetchOptions.printOnly) {
  823. if (existing && existing.version_ != ver)
  824. logInfo("A new version for %s is available (%s -> %s). Run \"%s\" to switch.",
  825. packageId.color(Mode.bold), existing.version_, ver,
  826. text("dub upgrade ", packageId).color(Mode.bold));
  827. return null;
  828. }
  829.  
  830. if (existing) {
  831. if (!ver.isBranch() || !(options & FetchOptions.forceBranchUpgrade) || location == PlacementLocation.local) {
  832. // TODO: support git working trees by performing a "git pull" instead of this
  833. logDiagnostic("Package %s %s (in %s packages) is already present with the latest version, skipping upgrade.",
  834. packageId, ver, location.toString);
  835. return existing;
  836. } else {
  837. logInfo("Removing", Color.yellow, "%s %s to prepare replacement with a new version", packageId.color(Mode.bold), ver);
  838. if (!m_dryRun) m_packageManager.remove(existing);
  839. }
  840. }
  841.  
  842. if (reason.length) logInfo("Fetching", Color.yellow, "%s %s (%s)", packageId.color(Mode.bold), ver, reason);
  843. else logInfo("Fetching", Color.yellow, "%s %s", packageId.color(Mode.bold), ver);
  844. if (m_dryRun) return null;
  845.  
  846. logDebug("Acquiring package zip file");
  847.  
  848. NativePath dstpath = this.m_packageManager.getPackagePath(location, basePackageName, ver.toString());
  849. if (!dstpath.existsFile())
  850. mkdirRecurse(dstpath.toNativeString());
  851. // For libraries leaking their import path
  852. dstpath = dstpath ~ basePackageName;
  853.  
  854. import std.datetime : seconds;
  855. auto lock = lockFile(dstpath.toNativeString() ~ ".lock", 30.seconds); // possibly wait for other dub instance
  856. if (dstpath.existsFile())
  857. {
  858. m_packageManager.refresh(false);
  859. return m_packageManager.getPackage(packageId, ver, location);
  860. }
  861.  
  862. // repeat download on corrupted zips, see #1336
  863. foreach_reverse (i; 0..3)
  864. {
  865. import std.zip : ZipException;
  866.  
  867. auto path = getTempFile(basePackageName, ".zip");
  868. supplier.fetchPackage(path, basePackageName, Dependency(range), (options & FetchOptions.usePrerelease) != 0); // Q: continue on fail?
  869. scope(exit) std.file.remove(path.toNativeString());
  870. logDiagnostic("Placing to %s...", dstpath.toNativeString());
  871.  
  872. try {
  873. return m_packageManager.storeFetchedPackage(path, pinfo, dstpath);
  874. } catch (ZipException e) {
  875. logInfo("Failed to extract zip archive for %s %s...", packageId, ver);
  876. // rethrow the exception at the end of the loop
  877. if (i == 0)
  878. throw e;
  879. }
  880. }
  881. assert(0, "Should throw a ZipException instead.");
  882. }
  883.  
  884. /** Removes a specific locally cached package.
  885.  
  886. This will delete the package files from disk and removes the
  887. corresponding entry from the list of known packages.
  888.  
  889. Params:
  890. pack = Package instance to remove
  891. */
  892. void remove(in Package pack)
  893. {
  894. logInfo("Removing", Color.yellow, "%s (in %s)", pack.name.color(Mode.bold), pack.path.toNativeString());
  895. if (!m_dryRun) m_packageManager.remove(pack);
  896. }
  897.  
  898. /// Compatibility overload. Use the version without a `force_remove` argument instead.
  899. deprecated("Use `remove(pack)` directly instead, the boolean has no effect")
  900. void remove(in Package pack, bool force_remove)
  901. {
  902. remove(pack);
  903. }
  904.  
  905. /// @see remove(string, string, RemoveLocation)
  906. enum RemoveVersionWildcard = "*";
  907.  
  908. /** Removes one or more versions of a locally cached package.
  909.  
  910. This will remove a given package with a specified version from the
  911. given location. It will remove at most one package, unless `version_`
  912. is set to `RemoveVersionWildcard`.
  913.  
  914. Params:
  915. package_id = Name of the package to be removed
  916. location_ = Specifies the location to look for the given package
  917. name/version.
  918. resolve_version = Callback to select package version.
  919. */
  920. void remove(string package_id, PlacementLocation location,
  921. scope size_t delegate(in Package[] packages) resolve_version)
  922. {
  923. enforce(!package_id.empty);
  924. if (location == PlacementLocation.local) {
  925. logInfo("To remove a locally placed package, make sure you don't have any data"
  926. ~ "\nleft in it's directory and then simply remove the whole directory.");
  927. throw new Exception("dub cannot remove locally installed packages.");
  928. }
  929.  
  930. Package[] packages;
  931.  
  932. // Retrieve packages to be removed.
  933. foreach(pack; m_packageManager.getPackageIterator(package_id))
  934. if (m_packageManager.isManagedPackage(pack))
  935. packages ~= pack;
  936.  
  937. // Check validity of packages to be removed.
  938. if(packages.empty) {
  939. throw new Exception("Cannot find package to remove. ("
  940. ~ "id: '" ~ package_id ~ "', location: '" ~ to!string(location) ~ "'"
  941. ~ ")");
  942. }
  943.  
  944. // Sort package list in ascending version order
  945. packages.sort!((a, b) => a.version_ < b.version_);
  946.  
  947. immutable idx = resolve_version(packages);
  948. if (idx == size_t.max)
  949. return;
  950. else if (idx != packages.length)
  951. packages = packages[idx .. idx + 1];
  952.  
  953. logDebug("Removing %s packages.", packages.length);
  954. foreach(pack; packages) {
  955. try {
  956. remove(pack);
  957. } catch (Exception e) {
  958. logError("Failed to remove %s %s: %s", package_id, pack.version_, e.msg);
  959. logInfo("Continuing with other packages (if any).");
  960. }
  961. }
  962. }
  963.  
  964. /// Compatibility overload. Use the version without a `force_remove` argument instead.
  965. void remove(string package_id, PlacementLocation location, bool force_remove,
  966. scope size_t delegate(in Package[] packages) resolve_version)
  967. {
  968. remove(package_id, location, resolve_version);
  969. }
  970.  
  971. /** Removes a specific version of a package.
  972.  
  973. Params:
  974. package_id = Name of the package to be removed
  975. version_ = Identifying a version or a wild card. If an empty string
  976. is passed, the package will be removed from the location, if
  977. there is only one version retrieved. This will throw an
  978. exception, if there are multiple versions retrieved.
  979. location_ = Specifies the location to look for the given package
  980. name/version.
  981. */
  982. void remove(string package_id, string version_, PlacementLocation location)
  983. {
  984. remove(package_id, location, (in packages) {
  985. if (version_ == RemoveVersionWildcard || version_.empty)
  986. return packages.length;
  987.  
  988. foreach (i, p; packages) {
  989. if (p.version_ == Version(version_))
  990. return i;
  991. }
  992. throw new Exception("Cannot find package to remove. ("
  993. ~ "id: '" ~ package_id ~ "', version: '" ~ version_ ~ "', location: '" ~ to!string(location) ~ "'"
  994. ~ ")");
  995. });
  996. }
  997.  
  998. /// Compatibility overload. Use the version without a `force_remove` argument instead.
  999. deprecated("Use the overload without force_remove instead")
  1000. void remove(string package_id, string version_, PlacementLocation location, bool force_remove)
  1001. {
  1002. remove(package_id, version_, location);
  1003. }
  1004.  
  1005. /** Adds a directory to the list of locally known packages.
  1006.  
  1007. Forwards to `PackageManager.addLocalPackage`.
  1008.  
  1009. Params:
  1010. path = Path to the package
  1011. ver = Optional version to associate with the package (can be left
  1012. empty)
  1013. system = Make the package known system wide instead of user wide
  1014. (requires administrator privileges).
  1015.  
  1016. See_Also: `removeLocalPackage`
  1017. */
  1018. void addLocalPackage(string path, string ver, bool system)
  1019. {
  1020. if (m_dryRun) return;
  1021. m_packageManager.addLocalPackage(makeAbsolute(path), ver, system ? PlacementLocation.system : PlacementLocation.user);
  1022. }
  1023.  
  1024. /** Removes a directory from the list of locally known packages.
  1025.  
  1026. Forwards to `PackageManager.removeLocalPackage`.
  1027.  
  1028. Params:
  1029. path = Path to the package
  1030. system = Make the package known system wide instead of user wide
  1031. (requires administrator privileges).
  1032.  
  1033. See_Also: `addLocalPackage`
  1034. */
  1035. void removeLocalPackage(string path, bool system)
  1036. {
  1037. if (m_dryRun) return;
  1038. m_packageManager.removeLocalPackage(makeAbsolute(path), system ? PlacementLocation.system : PlacementLocation.user);
  1039. }
  1040.  
  1041. /** Registers a local directory to search for packages to use for satisfying
  1042. dependencies.
  1043.  
  1044. Params:
  1045. path = Path to a directory containing package directories
  1046. system = Make the package known system wide instead of user wide
  1047. (requires administrator privileges).
  1048.  
  1049. See_Also: `removeSearchPath`
  1050. */
  1051. void addSearchPath(string path, bool system)
  1052. {
  1053. if (m_dryRun) return;
  1054. m_packageManager.addSearchPath(makeAbsolute(path), system ? PlacementLocation.system : PlacementLocation.user);
  1055. }
  1056.  
  1057. /** Unregisters a local directory search path.
  1058.  
  1059. Params:
  1060. path = Path to a directory containing package directories
  1061. system = Make the package known system wide instead of user wide
  1062. (requires administrator privileges).
  1063.  
  1064. See_Also: `addSearchPath`
  1065. */
  1066. void removeSearchPath(string path, bool system)
  1067. {
  1068. if (m_dryRun) return;
  1069. m_packageManager.removeSearchPath(makeAbsolute(path), system ? PlacementLocation.system : PlacementLocation.user);
  1070. }
  1071.  
  1072. /** Queries all package suppliers with the given query string.
  1073.  
  1074. Returns a list of tuples, where the first entry is the human readable
  1075. name of the package supplier and the second entry is the list of
  1076. matched packages.
  1077.  
  1078. Params:
  1079. query = the search term to match packages on
  1080.  
  1081. See_Also: `PackageSupplier.searchPackages`
  1082. */
  1083. auto searchPackages(string query)
  1084. {
  1085. import std.typecons : Tuple, tuple;
  1086. Tuple!(string, PackageSupplier.SearchResult[])[] results;
  1087. foreach (ps; this.m_packageSuppliers) {
  1088. try
  1089. results ~= tuple(ps.description, ps.searchPackages(query));
  1090. catch (Exception e) {
  1091. logWarn("Searching %s for '%s' failed: %s", ps.description, query, e.msg);
  1092. }
  1093. }
  1094. return results.filter!(tup => tup[1].length);
  1095. }
  1096.  
  1097. /** Returns a list of all available versions (including branches) for a
  1098. particular package.
  1099.  
  1100. The list returned is based on the registered package suppliers. Local
  1101. packages are not queried in the search for versions.
  1102.  
  1103. See_also: `getLatestVersion`
  1104. */
  1105. Version[] listPackageVersions(string name)
  1106. {
  1107. Version[] versions;
  1108. auto basePackageName = getBasePackageName(name);
  1109. foreach (ps; this.m_packageSuppliers) {
  1110. try versions ~= ps.getVersions(basePackageName);
  1111. catch (Exception e) {
  1112. logWarn("Failed to get versions for package %s on provider %s: %s", name, ps.description, e.msg);
  1113. }
  1114. }
  1115. return versions.sort().uniq.array;
  1116. }
  1117.  
  1118. /** Returns the latest available version for a particular package.
  1119.  
  1120. This function returns the latest numbered version of a package. If no
  1121. numbered versions are available, it will return an available branch,
  1122. preferring "~master".
  1123.  
  1124. Params:
  1125. package_name: The name of the package in question.
  1126. prefer_stable: If set to `true` (the default), returns the latest
  1127. stable version, even if there are newer pre-release versions.
  1128.  
  1129. See_also: `listPackageVersions`
  1130. */
  1131. Version getLatestVersion(string package_name, bool prefer_stable = true)
  1132. {
  1133. auto vers = listPackageVersions(package_name);
  1134. enforce(!vers.empty, "Failed to find any valid versions for a package name of '"~package_name~"'.");
  1135. auto final_versions = vers.filter!(v => !v.isBranch && !v.isPreRelease).array;
  1136. if (prefer_stable && final_versions.length) return final_versions[$-1];
  1137. else return vers[$-1];
  1138. }
  1139.  
  1140. /** Initializes a directory with a package skeleton.
  1141.  
  1142. Params:
  1143. path = Path of the directory to create the new package in. The
  1144. directory will be created if it doesn't exist.
  1145. deps = List of dependencies to add to the package recipe.
  1146. type = Specifies the type of the application skeleton to use.
  1147. format = Determines the package recipe format to use.
  1148. recipe_callback = Optional callback that can be used to
  1149. customize the recipe before it gets written.
  1150. */
  1151. void createEmptyPackage(NativePath path, string[] deps, string type,
  1152. PackageFormat format = PackageFormat.sdl,
  1153. scope void delegate(ref PackageRecipe, ref PackageFormat) recipe_callback = null,
  1154. string[] app_args = [])
  1155. {
  1156. if (!path.absolute) path = m_rootPath ~ path;
  1157. path.normalize();
  1158.  
  1159. VersionRange[string] depVers;
  1160. string[] notFound; // keep track of any failed packages in here
  1161. foreach (dep; deps) {
  1162. try {
  1163. Version ver = getLatestVersion(dep);
  1164. if (ver.isBranch())
  1165. depVers[dep] = VersionRange(ver);
  1166. else
  1167. depVers[dep] = VersionRange.fromString("~>" ~ ver.toString());
  1168. } catch (Exception e) {
  1169. notFound ~= dep;
  1170. }
  1171. }
  1172.  
  1173. if(notFound.length > 1){
  1174. throw new Exception(.format("Couldn't find packages: %-(%s, %).", notFound));
  1175. }
  1176. else if(notFound.length == 1){
  1177. throw new Exception(.format("Couldn't find package: %-(%s, %).", notFound));
  1178. }
  1179.  
  1180. if (m_dryRun) return;
  1181.  
  1182. initPackage(path, depVers, type, format, recipe_callback);
  1183.  
  1184. if (!["vibe.d", "deimos", "minimal"].canFind(type)) {
  1185. runCustomInitialization(path, type, app_args);
  1186. }
  1187.  
  1188. //Act smug to the user.
  1189. logInfo("Success", Color.green, "created empty project in %s", path.toNativeString().color(Mode.bold));
  1190. }
  1191.  
  1192. private void runCustomInitialization(NativePath path, string type, string[] runArgs)
  1193. {
  1194. string packageName = type;
  1195. auto template_pack = m_packageManager.getBestPackage(packageName);
  1196. if (!template_pack) {
  1197. logInfo("%s is not present, getting and storing it user wide", packageName);
  1198. template_pack = fetch(packageName, VersionRange.Any, defaultPlacementLocation, FetchOptions.none);
  1199. }
  1200.  
  1201. Package initSubPackage = m_packageManager.getSubPackage(template_pack, "init-exec", false);
  1202. auto template_dub = new Dub(null, m_packageSuppliers);
  1203. template_dub.loadPackage(initSubPackage);
  1204.  
  1205. GeneratorSettings settings = this.makeAppSettings();
  1206. settings.runArgs = runArgs;
  1207.  
  1208. initSubPackage.recipe.buildSettings.workingDirectory = path.toNativeString();
  1209. template_dub.generateProject("build", settings);
  1210. }
  1211.  
  1212. /** Converts the package recipe of the loaded root package to the given format.
  1213.  
  1214. Params:
  1215. destination_file_ext = The file extension matching the desired
  1216. format. Possible values are "json" or "sdl".
  1217. print_only = Print the converted recipe instead of writing to disk
  1218. */
  1219. void convertRecipe(string destination_file_ext, bool print_only = false)
  1220. {
  1221. import std.path : extension;
  1222. import std.stdio : stdout;
  1223. import dub.recipe.io : serializePackageRecipe, writePackageRecipe;
  1224.  
  1225. if (print_only) {
  1226. auto dst = stdout.lockingTextWriter;
  1227. serializePackageRecipe(dst, m_project.rootPackage.rawRecipe, "dub."~destination_file_ext);
  1228. return;
  1229. }
  1230.  
  1231. auto srcfile = m_project.rootPackage.recipePath;
  1232. auto srcext = srcfile.head.name.extension;
  1233. if (srcext == "."~destination_file_ext) {
  1234. // no logging before this point
  1235. tagWidth.push(5);
  1236. logError("Package format is already %s.", destination_file_ext);
  1237. return;
  1238. }
  1239.  
  1240. writePackageRecipe(srcfile.parentPath ~ ("dub."~destination_file_ext), m_project.rootPackage.rawRecipe);
  1241. removeFile(srcfile);
  1242. }
  1243.  
  1244. /** Runs DDOX to generate or serve documentation.
  1245.  
  1246. Params:
  1247. run = If set to true, serves documentation on a local web server.
  1248. Otherwise generates actual HTML files.
  1249. generate_args = Additional command line arguments to pass to
  1250. "ddox generate-html" or "ddox serve-html".
  1251. */
  1252. void runDdox(bool run, string[] generate_args = null)
  1253. {
  1254. import std.process : browse;
  1255.  
  1256. if (m_dryRun) return;
  1257.  
  1258. // allow to choose a custom ddox tool
  1259. auto tool = m_project.rootPackage.recipe.ddoxTool;
  1260. if (tool.empty) tool = "ddox";
  1261.  
  1262. auto tool_pack = m_packageManager.getBestPackage(tool);
  1263. if (!tool_pack) {
  1264. logInfo("%s is not present, getting and storing it user wide", tool);
  1265. tool_pack = fetch(tool, VersionRange.Any, defaultPlacementLocation, FetchOptions.none);
  1266. }
  1267.  
  1268. auto ddox_dub = new Dub(null, m_packageSuppliers);
  1269. ddox_dub.loadPackage(tool_pack);
  1270. ddox_dub.upgrade(UpgradeOptions.select);
  1271.  
  1272. GeneratorSettings settings = this.makeAppSettings();
  1273.  
  1274. auto filterargs = m_project.rootPackage.recipe.ddoxFilterArgs.dup;
  1275. if (filterargs.empty) filterargs = ["--min-protection=Protected", "--only-documented"];
  1276.  
  1277. settings.runArgs = "filter" ~ filterargs ~ "docs.json";
  1278. ddox_dub.generateProject("build", settings);
  1279.  
  1280. auto p = tool_pack.path;
  1281. p.endsWithSlash = true;
  1282. auto tool_path = p.toNativeString();
  1283.  
  1284. if (run) {
  1285. settings.runArgs = ["serve-html", "--navigation-type=ModuleTree", "docs.json", "--web-file-dir="~tool_path~"public"] ~ generate_args;
  1286. browse("http://127.0.0.1:8080/");
  1287. } else {
  1288. settings.runArgs = ["generate-html", "--navigation-type=ModuleTree", "docs.json", "docs"] ~ generate_args;
  1289. }
  1290. ddox_dub.generateProject("build", settings);
  1291.  
  1292. if (!run) {
  1293. // TODO: ddox should copy those files itself
  1294. version(Windows) runCommand(`xcopy /S /D "`~tool_path~`public\*" docs\`);
  1295. else runCommand("rsync -ru '"~tool_path~"public/' docs/");
  1296. }
  1297. }
  1298.  
  1299. /// Make a `GeneratorSettings` suitable to generate tools (DDOC, DScanner, etc...)
  1300. private GeneratorSettings makeAppSettings () const
  1301. {
  1302. GeneratorSettings settings;
  1303. auto compiler_binary = this.defaultCompiler;
  1304.  
  1305. settings.config = "application";
  1306. settings.buildType = "debug";
  1307. settings.compiler = getCompiler(compiler_binary);
  1308. settings.platform = settings.compiler.determinePlatform(
  1309. settings.buildSettings, compiler_binary, this.defaultArchitecture);
  1310. if (this.defaultLowMemory)
  1311. settings.buildSettings.options |= BuildOption.lowmem;
  1312. if (this.defaultEnvironments)
  1313. settings.buildSettings.addEnvironments(this.defaultEnvironments);
  1314. if (this.defaultBuildEnvironments)
  1315. settings.buildSettings.addBuildEnvironments(this.defaultBuildEnvironments);
  1316. if (this.defaultRunEnvironments)
  1317. settings.buildSettings.addRunEnvironments(this.defaultRunEnvironments);
  1318. if (this.defaultPreGenerateEnvironments)
  1319. settings.buildSettings.addPreGenerateEnvironments(this.defaultPreGenerateEnvironments);
  1320. if (this.defaultPostGenerateEnvironments)
  1321. settings.buildSettings.addPostGenerateEnvironments(this.defaultPostGenerateEnvironments);
  1322. if (this.defaultPreBuildEnvironments)
  1323. settings.buildSettings.addPreBuildEnvironments(this.defaultPreBuildEnvironments);
  1324. if (this.defaultPostBuildEnvironments)
  1325. settings.buildSettings.addPostBuildEnvironments(this.defaultPostBuildEnvironments);
  1326. if (this.defaultPreRunEnvironments)
  1327. settings.buildSettings.addPreRunEnvironments(this.defaultPreRunEnvironments);
  1328. if (this.defaultPostRunEnvironments)
  1329. settings.buildSettings.addPostRunEnvironments(this.defaultPostRunEnvironments);
  1330. settings.run = true;
  1331.  
  1332. return settings;
  1333. }
  1334.  
  1335. private void determineDefaultCompiler()
  1336. {
  1337. import std.file : thisExePath;
  1338. import std.path : buildPath, dirName, expandTilde, isAbsolute, isDirSeparator;
  1339. import std.range : front;
  1340.  
  1341. // Env takes precedence
  1342. if (auto envCompiler = environment.get("DC"))
  1343. m_defaultCompiler = envCompiler;
  1344. else
  1345. m_defaultCompiler = m_config.defaultCompiler.expandTilde;
  1346. if (m_defaultCompiler.length && m_defaultCompiler.isAbsolute)
  1347. return;
  1348.  
  1349. static immutable BinaryPrefix = `$DUB_BINARY_PATH`;
  1350. if(m_defaultCompiler.startsWith(BinaryPrefix))
  1351. {
  1352. m_defaultCompiler = thisExePath().dirName() ~ m_defaultCompiler[BinaryPrefix.length .. $];
  1353. return;
  1354. }
  1355.  
  1356. if (!find!isDirSeparator(m_defaultCompiler).empty)
  1357. throw new Exception("defaultCompiler specified in a DUB config file cannot use an unqualified relative path:\n\n" ~ m_defaultCompiler ~
  1358. "\n\nUse \"$DUB_BINARY_PATH/../path/you/want\" instead.");
  1359.  
  1360. version (Windows) enum sep = ";", exe = ".exe";
  1361. version (Posix) enum sep = ":", exe = "";
  1362.  
  1363. auto compilers = ["dmd", "gdc", "gdmd", "ldc2", "ldmd2"];
  1364. // If a compiler name is specified, look for it next to dub.
  1365. // Otherwise, look for any of the common compilers adjacent to dub.
  1366. if (m_defaultCompiler.length)
  1367. {
  1368. string compilerPath = buildPath(thisExePath().dirName(), m_defaultCompiler ~ exe);
  1369. if (existsFile(compilerPath))
  1370. {
  1371. m_defaultCompiler = compilerPath;
  1372. return;
  1373. }
  1374. }
  1375. else
  1376. {
  1377. auto nextFound = compilers.find!(bin => existsFile(buildPath(thisExePath().dirName(), bin ~ exe)));
  1378. if (!nextFound.empty)
  1379. {
  1380. m_defaultCompiler = buildPath(thisExePath().dirName(), nextFound.front ~ exe);
  1381. return;
  1382. }
  1383. }
  1384.  
  1385. // If nothing found next to dub, search the user's PATH, starting
  1386. // with the compiler name from their DUB config file, if specified.
  1387. auto paths = environment.get("PATH", "").splitter(sep).map!NativePath;
  1388. if (m_defaultCompiler.length && paths.canFind!(p => existsFile(p ~ (m_defaultCompiler~exe))))
  1389. return;
  1390. foreach (p; paths) {
  1391. auto res = compilers.find!(bin => existsFile(p ~ (bin~exe)));
  1392. if (!res.empty) {
  1393. m_defaultCompiler = res.front;
  1394. return;
  1395. }
  1396. }
  1397. m_defaultCompiler = compilers[0];
  1398. }
  1399.  
  1400. unittest
  1401. {
  1402. import std.path: buildPath, absolutePath;
  1403. auto dub = new TestDub(".", null, SkipPackageSuppliers.configured);
  1404. immutable olddc = environment.get("DC", null);
  1405. immutable oldpath = environment.get("PATH", null);
  1406. immutable testdir = "test-determineDefaultCompiler";
  1407. void repairenv(string name, string var)
  1408. {
  1409. if (var !is null)
  1410. environment[name] = var;
  1411. else if (name in environment)
  1412. environment.remove(name);
  1413. }
  1414. scope (exit) repairenv("DC", olddc);
  1415. scope (exit) repairenv("PATH", oldpath);
  1416. scope (exit) rmdirRecurse(testdir);
  1417.  
  1418. version (Windows) enum sep = ";", exe = ".exe";
  1419. version (Posix) enum sep = ":", exe = "";
  1420.  
  1421. immutable dmdpath = testdir.buildPath("dmd", "bin");
  1422. immutable ldcpath = testdir.buildPath("ldc", "bin");
  1423. mkdirRecurse(dmdpath);
  1424. mkdirRecurse(ldcpath);
  1425. immutable dmdbin = dmdpath.buildPath("dmd"~exe);
  1426. immutable ldcbin = ldcpath.buildPath("ldc2"~exe);
  1427. std.file.write(dmdbin, null);
  1428. std.file.write(ldcbin, null);
  1429.  
  1430. environment["DC"] = dmdbin.absolutePath();
  1431. dub.determineDefaultCompiler();
  1432. assert(dub.m_defaultCompiler == dmdbin.absolutePath());
  1433.  
  1434. environment["DC"] = "dmd";
  1435. environment["PATH"] = dmdpath ~ sep ~ ldcpath;
  1436. dub.determineDefaultCompiler();
  1437. assert(dub.m_defaultCompiler == "dmd");
  1438.  
  1439. environment["DC"] = "ldc2";
  1440. environment["PATH"] = dmdpath ~ sep ~ ldcpath;
  1441. dub.determineDefaultCompiler();
  1442. assert(dub.m_defaultCompiler == "ldc2");
  1443.  
  1444. environment.remove("DC");
  1445. environment["PATH"] = ldcpath ~ sep ~ dmdpath;
  1446. dub.determineDefaultCompiler();
  1447. assert(dub.m_defaultCompiler == "ldc2");
  1448. }
  1449.  
  1450. private NativePath makeAbsolute(NativePath p) const { return p.absolute ? p : m_rootPath ~ p; }
  1451. private NativePath makeAbsolute(string p) const { return makeAbsolute(NativePath(p)); }
  1452. }
  1453.  
  1454.  
  1455. /// Option flags for `Dub.fetch`
  1456. enum FetchOptions
  1457. {
  1458. none = 0,
  1459. forceBranchUpgrade = 1<<0,
  1460. usePrerelease = 1<<1,
  1461. forceRemove = 1<<2, /// Deprecated, does nothing.
  1462. printOnly = 1<<3,
  1463. }
  1464.  
  1465. /// Option flags for `Dub.upgrade`
  1466. enum UpgradeOptions
  1467. {
  1468. none = 0,
  1469. upgrade = 1<<1, /// Upgrade existing packages
  1470. preRelease = 1<<2, /// inclde pre-release versions in upgrade
  1471. forceRemove = 1<<3, /// Deprecated, does nothing.
  1472. select = 1<<4, /// Update the dub.selections.json file with the upgraded versions
  1473. dryRun = 1<<5, /// Instead of downloading new packages, just print a message to notify the user of their existence
  1474. /*deprecated*/ printUpgradesOnly = dryRun, /// deprecated, use dryRun instead
  1475. /*deprecated*/ useCachedResult = 1<<6, /// deprecated, has no effect
  1476. noSaveSelections = 1<<7, /// Don't store updated selections on disk
  1477. }
  1478.  
  1479. /// Determines which of the default package suppliers are queried for packages.
  1480. enum SkipPackageSuppliers {
  1481. none, /// Uses all configured package suppliers.
  1482. standard, /// Does not use the default package suppliers (`defaultPackageSuppliers`).
  1483. configured, /// Does not use default suppliers or suppliers configured in DUB's configuration file
  1484. all /// Uses only manually specified package suppliers.
  1485. }
  1486.  
  1487. private class DependencyVersionResolver : DependencyResolver!(Dependency, Dependency) {
  1488. protected {
  1489. Dub m_dub;
  1490. UpgradeOptions m_options;
  1491. Dependency[][string] m_packageVersions;
  1492. Package[string] m_remotePackages;
  1493. SelectedVersions m_selectedVersions;
  1494. Package m_rootPackage;
  1495. bool[string] m_packagesToUpgrade;
  1496. Package[PackageDependency] m_packages;
  1497. TreeNodes[][TreeNode] m_children;
  1498. }
  1499.  
  1500.  
  1501. this(Dub dub, UpgradeOptions options, Package root, SelectedVersions selected_versions)
  1502. {
  1503. assert(dub !is null);
  1504. assert(root !is null);
  1505. assert(selected_versions !is null);
  1506.  
  1507. if (environment.get("DUB_NO_RESOLVE_LIMIT") !is null)
  1508. super(ulong.max);
  1509. else
  1510. super(1_000_000);
  1511.  
  1512. m_dub = dub;
  1513. m_options = options;
  1514. m_rootPackage = root;
  1515. m_selectedVersions = selected_versions;
  1516. }
  1517.  
  1518. Dependency[string] resolve(string[] filter)
  1519. {
  1520. foreach (name; filter)
  1521. m_packagesToUpgrade[name] = true;
  1522. return super.resolve(TreeNode(m_rootPackage.name, Dependency(m_rootPackage.version_)),
  1523. (m_options & UpgradeOptions.dryRun) == 0);
  1524. }
  1525.  
  1526. protected bool isFixedPackage(string pack)
  1527. {
  1528. return m_packagesToUpgrade !is null && pack !in m_packagesToUpgrade;
  1529. }
  1530.  
  1531. protected override Dependency[] getAllConfigs(string pack)
  1532. {
  1533. if (auto pvers = pack in m_packageVersions)
  1534. return *pvers;
  1535.  
  1536. if ((!(m_options & UpgradeOptions.upgrade) || isFixedPackage(pack)) && m_selectedVersions.hasSelectedVersion(pack)) {
  1537. auto ret = [m_selectedVersions.getSelectedVersion(pack)];
  1538. logDiagnostic("Using fixed selection %s %s", pack, ret[0]);
  1539. m_packageVersions[pack] = ret;
  1540. return ret;
  1541. }
  1542.  
  1543. logDiagnostic("Search for versions of %s (%s package suppliers)", pack, m_dub.m_packageSuppliers.length);
  1544. Version[] versions;
  1545. foreach (p; m_dub.packageManager.getPackageIterator(pack))
  1546. versions ~= p.version_;
  1547.  
  1548. foreach (ps; m_dub.m_packageSuppliers) {
  1549. try {
  1550. auto vers = ps.getVersions(pack);
  1551. vers.reverse();
  1552. if (!vers.length) {
  1553. logDiagnostic("No versions for %s for %s", pack, ps.description);
  1554. continue;
  1555. }
  1556.  
  1557. versions ~= vers;
  1558. break;
  1559. } catch (Exception e) {
  1560. logWarn("Package %s not found in %s: %s", pack, ps.description, e.msg);
  1561. logDebug("Full error: %s", e.toString().sanitize);
  1562. }
  1563. }
  1564.  
  1565. // sort by version, descending, and remove duplicates
  1566. versions = versions.sort!"a>b".uniq.array;
  1567.  
  1568. // move pre-release versions to the back of the list if no preRelease flag is given
  1569. if (!(m_options & UpgradeOptions.preRelease))
  1570. versions = versions.filter!(v => !v.isPreRelease).array ~ versions.filter!(v => v.isPreRelease).array;
  1571.  
  1572. // filter out invalid/unreachable dependency specs
  1573. versions = versions.filter!((v) {
  1574. bool valid = getPackage(pack, Dependency(v)) !is null;
  1575. if (!valid) logDiagnostic("Excluding invalid dependency specification %s %s from dependency resolution process.", pack, v);
  1576. return valid;
  1577. }).array;
  1578.  
  1579. if (!versions.length) logDiagnostic("Nothing found for %s", pack);
  1580. else logDiagnostic("Return for %s: %s", pack, versions);
  1581.  
  1582. auto ret = versions.map!(v => Dependency(v)).array;
  1583. m_packageVersions[pack] = ret;
  1584. return ret;
  1585. }
  1586.  
  1587. protected override Dependency[] getSpecificConfigs(string pack, TreeNodes nodes)
  1588. {
  1589. if (!nodes.configs.path.empty || !nodes.configs.repository.empty) {
  1590. if (getPackage(pack, nodes.configs)) return [nodes.configs];
  1591. else return null;
  1592. }
  1593. else return null;
  1594. }
  1595.  
  1596.  
  1597. protected override TreeNodes[] getChildren(TreeNode node)
  1598. {
  1599. if (auto pc = node in m_children)
  1600. return *pc;
  1601. auto ret = getChildrenRaw(node);
  1602. m_children[node] = ret;
  1603. return ret;
  1604. }
  1605.  
  1606. private final TreeNodes[] getChildrenRaw(TreeNode node)
  1607. {
  1608. import std.array : appender;
  1609. auto ret = appender!(TreeNodes[]);
  1610. auto pack = getPackage(node.pack, node.config);
  1611. if (!pack) {
  1612. // this can hapen when the package description contains syntax errors
  1613. logDebug("Invalid package in dependency tree: %s %s", node.pack, node.config);
  1614. return null;
  1615. }
  1616. auto basepack = pack.basePackage;
  1617.  
  1618. foreach (d; pack.getAllDependenciesRange()) {
  1619. auto dbasename = getBasePackageName(d.name);
  1620.  
  1621. // detect dependencies to the root package (or sub packages thereof)
  1622. if (dbasename == basepack.name) {
  1623. auto absdeppath = d.spec.mapToPath(pack.path).path;
  1624. absdeppath.endsWithSlash = true;
  1625. auto subpack = m_dub.m_packageManager.getSubPackage(basepack, getSubPackageName(d.name), true);
  1626. if (subpack) {
  1627. auto desireddeppath = basepack.path;
  1628. desireddeppath.endsWithSlash = true;
  1629.  
  1630. auto altdeppath = d.name == dbasename ? basepack.path : subpack.path;
  1631. altdeppath.endsWithSlash = true;
  1632.  
  1633. if (!d.spec.path.empty && absdeppath != desireddeppath)
  1634. logWarn("Sub package %s, referenced by %s %s must be referenced using the path to its base package",
  1635. subpack.name, pack.name, pack.version_);
  1636.  
  1637. enforce(d.spec.path.empty || absdeppath == desireddeppath || absdeppath == altdeppath,
  1638. format("Dependency from %s to %s uses wrong path: %s vs. %s",
  1639. node.pack, subpack.name, absdeppath.toNativeString(), desireddeppath.toNativeString()));
  1640. }
  1641. ret ~= TreeNodes(d.name, node.config);
  1642. continue;
  1643. }
  1644.  
  1645. DependencyType dt;
  1646. if (d.spec.optional) {
  1647. if (d.spec.default_) dt = DependencyType.optionalDefault;
  1648. else dt = DependencyType.optional;
  1649. } else dt = DependencyType.required;
  1650.  
  1651. Dependency dspec = d.spec.mapToPath(pack.path);
  1652.  
  1653. // if not upgrading, use the selected version
  1654. if (!(m_options & UpgradeOptions.upgrade) && m_selectedVersions.hasSelectedVersion(dbasename))
  1655. dspec = m_selectedVersions.getSelectedVersion(dbasename);
  1656.  
  1657. // keep selected optional dependencies and avoid non-selected optional-default dependencies by default
  1658. if (!m_selectedVersions.bare) {
  1659. if (dt == DependencyType.optionalDefault && !m_selectedVersions.hasSelectedVersion(dbasename))
  1660. dt = DependencyType.optional;
  1661. else if (dt == DependencyType.optional && m_selectedVersions.hasSelectedVersion(dbasename))
  1662. dt = DependencyType.optionalDefault;
  1663. }
  1664.  
  1665. ret ~= TreeNodes(d.name, dspec, dt);
  1666. }
  1667. return ret.data;
  1668. }
  1669.  
  1670. protected override bool matches(Dependency configs, Dependency config)
  1671. {
  1672. if (!configs.path.empty) return configs.path == config.path;
  1673. return configs.merge(config).valid;
  1674. }
  1675.  
  1676. private Package getPackage(string name, Dependency dep)
  1677. {
  1678. auto key = PackageDependency(name, dep);
  1679. if (auto pp = key in m_packages)
  1680. return *pp;
  1681. auto p = getPackageRaw(name, dep);
  1682. m_packages[key] = p;
  1683. return p;
  1684. }
  1685.  
  1686. private Package getPackageRaw(string name, Dependency dep)
  1687. {
  1688. auto basename = getBasePackageName(name);
  1689.  
  1690. // for sub packages, first try to get them from the base package
  1691. if (basename != name) {
  1692. auto subname = getSubPackageName(name);
  1693. auto basepack = getPackage(basename, dep);
  1694. if (!basepack) return null;
  1695. if (auto sp = m_dub.m_packageManager.getSubPackage(basepack, subname, true)) {
  1696. return sp;
  1697. } else if (!basepack.subPackages.canFind!(p => p.path.length)) {
  1698. // note: external sub packages are handled further below
  1699. auto spr = basepack.getInternalSubPackage(subname);
  1700. if (!spr.isNull) {
  1701. auto sp = new Package(spr.get, basepack.path, basepack);
  1702. m_remotePackages[sp.name] = sp;
  1703. return sp;
  1704. } else {
  1705. logDiagnostic("Sub package %s doesn't exist in %s %s.", name, basename, dep.version_);
  1706. return null;
  1707. }
  1708. } else if (auto ret = m_dub.m_packageManager.getBestPackage(name, dep)) {
  1709. return ret;
  1710. } else {
  1711. logDiagnostic("External sub package %s %s not found.", name, dep.version_);
  1712. return null;
  1713. }
  1714. }
  1715.  
  1716. // shortcut if the referenced package is the root package
  1717. if (basename == m_rootPackage.basePackage.name)
  1718. return m_rootPackage.basePackage;
  1719.  
  1720. if (!dep.repository.empty) {
  1721. auto ret = m_dub.packageManager.loadSCMPackage(name, dep.repository);
  1722. return ret !is null && dep.matches(ret.version_) ? ret : null;
  1723. } else if (!dep.path.empty) {
  1724. try {
  1725. return m_dub.packageManager.getOrLoadPackage(dep.path);
  1726. } catch (Exception e) {
  1727. logDiagnostic("Failed to load path based dependency %s: %s", name, e.msg);
  1728. logDebug("Full error: %s", e.toString().sanitize);
  1729. return null;
  1730. }
  1731. }
  1732. const vers = dep.version_;
  1733.  
  1734. if (auto ret = m_dub.m_packageManager.getBestPackage(name, dep))
  1735. return ret;
  1736.  
  1737. auto key = name ~ ":" ~ vers.toString();
  1738. if (auto ret = key in m_remotePackages)
  1739. return *ret;
  1740.  
  1741. auto prerelease = (m_options & UpgradeOptions.preRelease) != 0;
  1742.  
  1743. auto rootpack = name.split(":")[0];
  1744.  
  1745. foreach (ps; m_dub.m_packageSuppliers) {
  1746. if (rootpack == name) {
  1747. try {
  1748. auto desc = ps.fetchPackageRecipe(name, dep, prerelease);
  1749. if (desc.type == Json.Type.null_)
  1750. continue;
  1751. auto ret = new Package(desc);
  1752. m_remotePackages[key] = ret;
  1753. return ret;
  1754. } catch (Exception e) {
  1755. logDiagnostic("Metadata for %s %s could not be downloaded from %s: %s", name, vers, ps.description, e.msg);
  1756. logDebug("Full error: %s", e.toString().sanitize);
  1757. }
  1758. } else {
  1759. logDiagnostic("Package %s not found in base package description (%s). Downloading whole package.", name, vers.toString());
  1760. try {
  1761. FetchOptions fetchOpts;
  1762. fetchOpts |= prerelease ? FetchOptions.usePrerelease : FetchOptions.none;
  1763. m_dub.fetch(rootpack, vers, m_dub.defaultPlacementLocation, fetchOpts, "need sub package description");
  1764. auto ret = m_dub.m_packageManager.getBestPackage(name, dep);
  1765. if (!ret) {
  1766. logWarn("Package %s %s doesn't have a sub package %s", rootpack, dep.version_, name);
  1767. return null;
  1768. }
  1769. m_remotePackages[key] = ret;
  1770. return ret;
  1771. } catch (Exception e) {
  1772. logDiagnostic("Package %s could not be downloaded from %s: %s", rootpack, ps.description, e.msg);
  1773. logDebug("Full error: %s", e.toString().sanitize);
  1774. }
  1775. }
  1776. }
  1777.  
  1778. m_remotePackages[key] = null;
  1779.  
  1780. logWarn("Package %s %s could not be loaded either locally, or from the configured package registries.", name, dep);
  1781. return null;
  1782. }
  1783. }
  1784.  
  1785. /**
  1786. * An instance of Dub that does not rely on the environment
  1787. *
  1788. * This instance of dub should not read any environment variables,
  1789. * nor should it do any file IO, to make it usable and reliable in unittests.
  1790. * Currently it reads environment variables but does not read the configuration.
  1791. */
  1792. package final class TestDub : Dub
  1793. {
  1794. /// Forward to base constructor
  1795. public this (string root = ".", PackageSupplier[] extras = null,
  1796. SkipPackageSuppliers skip = SkipPackageSuppliers.none)
  1797. {
  1798. super(root, extras, skip);
  1799. }
  1800.  
  1801. /// Avoid loading user configuration
  1802. protected override void loadConfig() { /* No-op */ }
  1803. }
  1804.  
  1805. private struct SpecialDirs {
  1806. /// The path where to store temporary files and directory
  1807. NativePath temp;
  1808. /// The system-wide dub-specific folder
  1809. NativePath systemSettings;
  1810. /// The dub-specific folder in the user home directory
  1811. NativePath userSettings;
  1812. /**
  1813. * Windows-only: the local, user-specific folder
  1814. *
  1815. * This folder, unlike `userSettings`, does not roam, IOW an account
  1816. * on a company network will not save the content of this data,
  1817. * unlike `userSettings`.
  1818. * On Posix, this is equivalent to `userSettings`.
  1819. *
  1820. * See_Also: https://docs.microsoft.com/en-us/windows/win32/shell/knownfolderid
  1821. */
  1822. NativePath localRepository;
  1823.  
  1824. /// Returns: An instance of `SpecialDirs` initialized from the environment
  1825. public static SpecialDirs make () {
  1826. import std.file : tempDir;
  1827.  
  1828. SpecialDirs result;
  1829. result.temp = NativePath(tempDir);
  1830.  
  1831. version(Windows) {
  1832. result.systemSettings = NativePath(environment.get("ProgramData")) ~ "dub/";
  1833. immutable appDataDir = environment.get("APPDATA");
  1834. result.userSettings = NativePath(appDataDir) ~ "dub/";
  1835. // LOCALAPPDATA is not defined before Windows Vista
  1836. result.localRepository = NativePath(environment.get("LOCALAPPDATA", appDataDir)) ~ "dub";
  1837. } else version(Posix) {
  1838. result.systemSettings = NativePath("/var/lib/dub/");
  1839. result.userSettings = NativePath(environment.get("HOME")) ~ ".dub/";
  1840. if (!result.userSettings.absolute)
  1841. result.userSettings = NativePath(getcwd()) ~ result.userSettings;
  1842. result.localRepository = result.userSettings;
  1843. }
  1844. return result;
  1845. }
  1846. }
  1847.  
  1848. /**
  1849. * User-provided configuration
  1850. *
  1851. * All fields in this struct should be optional.
  1852. * Fields that are *not* optional should be mandatory from the POV
  1853. * of the application, not the POV of file parsing.
  1854. * For example, git's `core.author` and `core.email` are required to commit,
  1855. * but the error happens on the commit, not when the gitconfig is parsed.
  1856. *
  1857. * We have multiple configuration locations, and two kinds of fields:
  1858. * additive and non-additive. Additive fields are fields which are the union
  1859. * of all configuration files (e.g. `registryURLs`). Non-additive fields
  1860. * will ignore values set in lower priorities configuration, although parsing
  1861. * must still succeed. Additive fields are marked as `@Optional`,
  1862. * non-additive are marked as `SetInfo`.
  1863. */
  1864. private struct UserConfiguration {
  1865. import configy.Attributes;
  1866.  
  1867. @Optional string[] registryUrls;
  1868. @Optional NativePath[] customCachePaths;
  1869.  
  1870. SetInfo!(SkipPackageSuppliers) skipRegistry;
  1871. SetInfo!(string) defaultCompiler;
  1872. SetInfo!(string) defaultArchitecture;
  1873. SetInfo!(bool) defaultLowMemory;
  1874.  
  1875. SetInfo!(string[string]) defaultEnvironments;
  1876. SetInfo!(string[string]) defaultBuildEnvironments;
  1877. SetInfo!(string[string]) defaultRunEnvironments;
  1878. SetInfo!(string[string]) defaultPreGenerateEnvironments;
  1879. SetInfo!(string[string]) defaultPostGenerateEnvironments;
  1880. SetInfo!(string[string]) defaultPreBuildEnvironments;
  1881. SetInfo!(string[string]) defaultPostBuildEnvironments;
  1882. SetInfo!(string[string]) defaultPreRunEnvironments;
  1883. SetInfo!(string[string]) defaultPostRunEnvironments;
  1884. SetInfo!(string) dubHome;
  1885.  
  1886. /// Merge a lower priority config (`this`) with a `higher` priority config
  1887. public UserConfiguration merge(UserConfiguration higher)
  1888. return @safe pure nothrow
  1889. {
  1890. import std.traits : hasUDA;
  1891. UserConfiguration result;
  1892.  
  1893. static foreach (idx, _; UserConfiguration.tupleof) {
  1894. static if (hasUDA!(UserConfiguration.tupleof[idx], Optional))
  1895. result.tupleof[idx] = higher.tupleof[idx] ~ this.tupleof[idx];
  1896. else static if (IsSetInfo!(typeof(this.tupleof[idx]))) {
  1897. if (higher.tupleof[idx].set)
  1898. result.tupleof[idx] = higher.tupleof[idx];
  1899. else
  1900. result.tupleof[idx] = this.tupleof[idx];
  1901. } else
  1902. static assert(false,
  1903. "Expect `@Optional` or `SetInfo` on: `" ~
  1904. __traits(identifier, this.tupleof[idx]) ~
  1905. "` of type : `" ~
  1906. typeof(this.tupleof[idx]).stringof ~ "`");
  1907. }
  1908.  
  1909. return result;
  1910. }
  1911.  
  1912. /// Workaround multiple `E` declaration in `static foreach` when inline
  1913. private template IsSetInfo(T) { enum bool IsSetInfo = is(T : SetInfo!E, E); }
  1914. }
  1915.  
  1916. unittest {
  1917. import configy.Read;
  1918.  
  1919. const str1 = `{
  1920. "registryUrls": [ "http://foo.bar\/optional\/escape" ],
  1921. "customCachePaths": [ "foo/bar", "foo/foo" ],
  1922.  
  1923. "skipRegistry": "all",
  1924. "defaultCompiler": "dmd",
  1925. "defaultArchitecture": "fooarch",
  1926. "defaultLowMemory": false,
  1927.  
  1928. "defaultEnvironments": {
  1929. "VAR2": "settings.VAR2",
  1930. "VAR3": "settings.VAR3",
  1931. "VAR4": "settings.VAR4"
  1932. }
  1933. }`;
  1934.  
  1935. const str2 = `{
  1936. "registryUrls": [ "http://bar.foo" ],
  1937. "customCachePaths": [ "bar/foo", "bar/bar" ],
  1938.  
  1939. "skipRegistry": "none",
  1940. "defaultCompiler": "ldc",
  1941. "defaultArchitecture": "bararch",
  1942. "defaultLowMemory": true,
  1943.  
  1944. "defaultEnvironments": {
  1945. "VAR": "Hi",
  1946. }
  1947. }`;
  1948.  
  1949. auto c1 = parseConfigString!UserConfiguration(str1, "/dev/null");
  1950. assert(c1.registryUrls == [ "http://foo.bar/optional/escape" ]);
  1951. assert(c1.customCachePaths == [ NativePath("foo/bar"), NativePath("foo/foo") ]);
  1952. assert(c1.skipRegistry == SkipPackageSuppliers.all);
  1953. assert(c1.defaultCompiler == "dmd");
  1954. assert(c1.defaultArchitecture == "fooarch");
  1955. assert(c1.defaultLowMemory == false);
  1956. assert(c1.defaultEnvironments.length == 3);
  1957. assert(c1.defaultEnvironments["VAR2"] == "settings.VAR2");
  1958. assert(c1.defaultEnvironments["VAR3"] == "settings.VAR3");
  1959. assert(c1.defaultEnvironments["VAR4"] == "settings.VAR4");
  1960.  
  1961. auto c2 = parseConfigString!UserConfiguration(str2, "/dev/null");
  1962. assert(c2.registryUrls == [ "http://bar.foo" ]);
  1963. assert(c2.customCachePaths == [ NativePath("bar/foo"), NativePath("bar/bar") ]);
  1964. assert(c2.skipRegistry == SkipPackageSuppliers.none);
  1965. assert(c2.defaultCompiler == "ldc");
  1966. assert(c2.defaultArchitecture == "bararch");
  1967. assert(c2.defaultLowMemory == true);
  1968. assert(c2.defaultEnvironments.length == 1);
  1969. assert(c2.defaultEnvironments["VAR"] == "Hi");
  1970.  
  1971. auto m1 = c2.merge(c1);
  1972. // c1 takes priority, so its registryUrls is first
  1973. assert(m1.registryUrls == [ "http://foo.bar/optional/escape", "http://bar.foo" ]);
  1974. // Same with CCP
  1975. assert(m1.customCachePaths == [
  1976. NativePath("foo/bar"), NativePath("foo/foo"),
  1977. NativePath("bar/foo"), NativePath("bar/bar"),
  1978. ]);
  1979.  
  1980. // c1 fields only
  1981. assert(m1.skipRegistry == c1.skipRegistry);
  1982. assert(m1.defaultCompiler == c1.defaultCompiler);
  1983. assert(m1.defaultArchitecture == c1.defaultArchitecture);
  1984. assert(m1.defaultLowMemory == c1.defaultLowMemory);
  1985. assert(m1.defaultEnvironments == c1.defaultEnvironments);
  1986.  
  1987. auto m2 = c1.merge(c2);
  1988. assert(m2.registryUrls == [ "http://bar.foo", "http://foo.bar/optional/escape" ]);
  1989. assert(m2.customCachePaths == [
  1990. NativePath("bar/foo"), NativePath("bar/bar"),
  1991. NativePath("foo/bar"), NativePath("foo/foo"),
  1992. ]);
  1993. assert(m2.skipRegistry == c2.skipRegistry);
  1994. assert(m2.defaultCompiler == c2.defaultCompiler);
  1995. assert(m2.defaultArchitecture == c2.defaultArchitecture);
  1996. assert(m2.defaultLowMemory == c2.defaultLowMemory);
  1997. assert(m2.defaultEnvironments == c2.defaultEnvironments);
  1998.  
  1999. auto m3 = UserConfiguration.init.merge(c1);
  2000. assert(m3 == c1);
  2001. }