Newer
Older
dub_jkp / source / dub / dependency.d
  1. /**
  2. Dependency specification functionality.
  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.dependency;
  9.  
  10. import dub.internal.utils;
  11. import dub.internal.vibecompat.core.file;
  12. import dub.internal.vibecompat.data.json;
  13. import dub.internal.vibecompat.inet.path;
  14. import dub.package_;
  15. import dub.semver;
  16. import dub.internal.logging;
  17.  
  18. import std.algorithm;
  19. import std.array;
  20. import std.exception;
  21. import std.string;
  22. import std.sumtype;
  23.  
  24.  
  25. /** Encapsulates the name of a package along with its dependency specification.
  26. */
  27. struct PackageDependency {
  28. /// Name of the referenced package.
  29. string name;
  30.  
  31. /// Dependency specification used to select a particular version of the package.
  32. Dependency spec;
  33. }
  34.  
  35. /**
  36. Represents a dependency specification.
  37.  
  38. A dependency specification either represents a specific version or version
  39. range, or a path to a package. In addition to that it has `optional` and
  40. `default_` flags to control how non-mandatory dependencies are handled. The
  41. package name is notably not part of the dependency specification.
  42. */
  43. struct Dependency {
  44. /// We currently support 3 'types'
  45. private alias Value = SumType!(VersionRange, NativePath, Repository);
  46.  
  47. /// Used by `toString`
  48. private static immutable string[] BooleanOptions = [ "optional", "default" ];
  49.  
  50. // Shortcut to create >=0.0.0
  51. private enum ANY_IDENT = "*";
  52.  
  53. private Value m_value;
  54. private bool m_optional;
  55. private bool m_default;
  56.  
  57. /// A Dependency, which matches every valid version.
  58. static @property Dependency any() @safe { return Dependency(VersionRange.Any); }
  59.  
  60. /// An invalid dependency (with no possible version matches).
  61. static @property Dependency invalid() @safe
  62. {
  63. return Dependency(VersionRange.Invalid);
  64. }
  65.  
  66. /** Constructs a new dependency specification from a string
  67.  
  68. See the `versionSpec` property for a description of the accepted
  69. contents of that string.
  70. */
  71. this(string spec) @safe
  72. {
  73. this(VersionRange.fromString(spec));
  74. }
  75.  
  76. /** Constructs a new dependency specification that matches a specific
  77. version.
  78. */
  79. this(const Version ver) @safe
  80. {
  81. this(VersionRange(ver, ver));
  82. }
  83.  
  84. /// Construct a version from a range of possible values
  85. private this (VersionRange rng) @safe
  86. {
  87. this.m_value = rng;
  88. }
  89.  
  90. /** Constructs a new dependency specification that matches a specific
  91. path.
  92. */
  93. this(NativePath path) @safe
  94. {
  95. this.m_value = path;
  96. }
  97.  
  98. /** Constructs a new dependency specification that matches a specific
  99. Git reference.
  100. */
  101. this(Repository repository) @safe
  102. {
  103. this.m_value = repository;
  104. }
  105.  
  106. deprecated("Instantiate the `Repository` struct with the string directy")
  107. this(Repository repository, string spec) @safe
  108. {
  109. assert(repository.m_ref is null);
  110. repository.m_ref = spec;
  111. this(repository);
  112. }
  113.  
  114. /// If set, overrides any version based dependency selection.
  115. @property void path(NativePath value) @trusted
  116. {
  117. this.m_value = value;
  118. }
  119. /// ditto
  120. @property NativePath path() const @safe
  121. {
  122. return this.m_value.match!(
  123. (const NativePath p) => p,
  124. ( any ) => NativePath.init,
  125. );
  126. }
  127.  
  128. /// If set, overrides any version based dependency selection.
  129. @property void repository(Repository value) @trusted
  130. {
  131. this.m_value = value;
  132. }
  133. /// ditto
  134. @property Repository repository() const @safe
  135. {
  136. return this.m_value.match!(
  137. (const Repository p) => p,
  138. ( any ) => Repository.init,
  139. );
  140. }
  141.  
  142. /// Determines if the dependency is required or optional.
  143. @property bool optional() const scope @safe pure nothrow @nogc
  144. {
  145. return m_optional;
  146. }
  147. /// ditto
  148. @property void optional(bool optional) scope @safe pure nothrow @nogc
  149. {
  150. m_optional = optional;
  151. }
  152.  
  153. /// Determines if an optional dependency should be chosen by default.
  154. @property bool default_() const scope @safe pure nothrow @nogc
  155. {
  156. return m_default;
  157. }
  158. /// ditto
  159. @property void default_(bool value) scope @safe pure nothrow @nogc
  160. {
  161. m_default = value;
  162. }
  163.  
  164. /// Returns true $(I iff) the version range only matches a specific version.
  165. @property bool isExactVersion() const scope @safe
  166. {
  167. return this.m_value.match!(
  168. (Repository v) => false,
  169. (NativePath v) => false,
  170. (VersionRange v) => v.isExactVersion(),
  171. );
  172. }
  173.  
  174. /// Returns the exact version matched by the version range.
  175. @property Version version_() const @safe {
  176. auto range = this.m_value.tryMatch!(
  177. (VersionRange v) => v,
  178. (NativePath p) => VersionRange.Any,
  179. (Repository r) {
  180. auto v = Version(r.m_ref);
  181. return VersionRange(v, v);
  182. },
  183. );
  184. enforce(range.isExactVersion(),
  185. "Dependency "~this.versionSpec~" is no exact version.");
  186. return range.m_versA;
  187. }
  188.  
  189. /** Sets/gets the matching version range as a specification string.
  190.  
  191. The acceptable forms for this string are as follows:
  192.  
  193. $(UL
  194. $(LI `"1.0.0"` - a single version in SemVer format)
  195. $(LI `"==1.0.0"` - alternative single version notation)
  196. $(LI `">1.0.0"` - version range with a single bound)
  197. $(LI `">1.0.0 <2.0.0"` - version range with two bounds)
  198. $(LI `"~>1.0.0"` - a fuzzy version range)
  199. $(LI `"~>1.0"` - a fuzzy version range with partial version)
  200. $(LI `"^1.0.0"` - semver compatible version range (same version if 0.x.y, ==major >=minor.patch if x.y.z))
  201. $(LI `"^1.0"` - same as ^1.0.0)
  202. $(LI `"~master"` - a branch name)
  203. $(LI `"*" - match any version (see also `any`))
  204. )
  205.  
  206. Apart from "$(LT)" and "$(GT)", "$(GT)=" and "$(LT)=" are also valid
  207. comparators.
  208.  
  209. */
  210. @property void versionSpec(string ves) @trusted
  211. {
  212. this.m_value = VersionRange.fromString(ves);
  213. }
  214.  
  215. /// ditto
  216. @property string versionSpec() const @safe {
  217. return this.m_value.match!(
  218. (const VersionRange p) => p.toString(),
  219. (const Repository r) => r.m_ref,
  220. (const NativePath p) => ANY_IDENT,
  221. );
  222. }
  223.  
  224. /** Returns a modified dependency that gets mapped to a given path.
  225.  
  226. This function will return an unmodified `Dependency` if it is not path
  227. based. Otherwise, the given `path` will be prefixed to the existing
  228. path.
  229. */
  230. Dependency mapToPath(NativePath path) const @trusted {
  231. // NOTE Path is @system in vibe.d 0.7.x and in the compatibility layer
  232. return this.m_value.match!(
  233. (Repository v) => this,
  234. (NativePath v) {
  235. if (v.empty || v.absolute) return this;
  236. return Dependency(path ~ v);
  237. },
  238. (VersionRange v) => this,
  239. );
  240. }
  241.  
  242. /** Returns a human-readable string representation of the dependency
  243. specification.
  244. */
  245. string toString() const scope @trusted {
  246. // Trusted because `SumType.match` doesn't seem to support `scope`
  247.  
  248. string Stringifier (T, string pre = null) (const T v)
  249. {
  250. const bool extra = this.optional || this.default_;
  251. return format("%s%s%s%-(%s, %)%s",
  252. pre, v,
  253. extra ? " (" : "",
  254. BooleanOptions[!this.optional .. 1 + this.default_],
  255. extra ? ")" : "");
  256. }
  257.  
  258. return this.m_value.match!(
  259. Stringifier!Repository,
  260. Stringifier!(NativePath, "@"),
  261. Stringifier!VersionRange
  262. );
  263. }
  264.  
  265. /** Returns a JSON representation of the dependency specification.
  266.  
  267. Simple specifications will be represented as a single specification
  268. string (`versionSpec`), while more complex specifications will be
  269. represented as a JSON object with optional "version", "path", "optional"
  270. and "default" fields.
  271.  
  272. Params:
  273. selections = We are serializing `dub.selections.json`, don't write out
  274. `optional` and `default`.
  275. */
  276. Json toJson(bool selections = false) const @safe
  277. {
  278. // NOTE Path and Json is @system in vibe.d 0.7.x and in the compatibility layer
  279. static void initJson(ref Json j, bool opt, bool def, bool s = selections)
  280. {
  281. j = Json.emptyObject;
  282. if (!s && opt) j["optional"] = true;
  283. if (!s && def) j["default"] = true;
  284. }
  285.  
  286. Json json;
  287. this.m_value.match!(
  288. (const NativePath v) @trusted {
  289. initJson(json, optional, default_);
  290. json["path"] = path.toString();
  291. },
  292.  
  293. (const Repository v) @trusted {
  294. initJson(json, optional, default_);
  295. json["repository"] = repository.toString();
  296. json["version"] = repository.m_ref;
  297. },
  298.  
  299. (const VersionRange v) @trusted {
  300. if (!selections && (optional || default_))
  301. {
  302. initJson(json, optional, default_);
  303. json["version"] = v.toString();
  304. }
  305. else
  306. json = Json(v.toString());
  307. },
  308. );
  309. return json;
  310. }
  311.  
  312. @trusted unittest {
  313. Dependency d = Dependency("==1.0.0");
  314. assert(d.toJson() == Json("1.0.0"), "Failed: " ~ d.toJson().toPrettyString());
  315. d = fromJson((fromJson(d.toJson())).toJson());
  316. assert(d == Dependency("1.0.0"));
  317. assert(d.toJson() == Json("1.0.0"), "Failed: " ~ d.toJson().toPrettyString());
  318. }
  319.  
  320. @trusted unittest {
  321. Dependency dependency = Dependency(Repository("git+http://localhost", "1.0.0"));
  322. Json expected = Json([
  323. "repository": Json("git+http://localhost"),
  324. "version": Json("1.0.0")
  325. ]);
  326. assert(dependency.toJson() == expected, "Failed: " ~ dependency.toJson().toPrettyString());
  327. }
  328.  
  329. @trusted unittest {
  330. Dependency d = Dependency(NativePath("dir"));
  331. Json expected = Json([ "path": Json("dir") ]);
  332. assert(d.toJson() == expected, "Failed: " ~ d.toJson().toPrettyString());
  333. }
  334.  
  335. /** Constructs a new `Dependency` from its JSON representation.
  336.  
  337. See `toJson` for a description of the JSON format.
  338. */
  339. static Dependency fromJson(Json verspec)
  340. @trusted { // NOTE Path and Json is @system in vibe.d 0.7.x and in the compatibility layer
  341. Dependency dep;
  342. if( verspec.type == Json.Type.object ){
  343. if( auto pp = "path" in verspec ) {
  344. if (auto pv = "version" in verspec)
  345. logDiagnostic("Ignoring version specification (%s) for path based dependency %s", pv.get!string, pp.get!string);
  346.  
  347. dep = Dependency.any;
  348. dep.path = NativePath(verspec["path"].get!string);
  349. } else if (auto repository = "repository" in verspec) {
  350. enforce("version" in verspec, "No version field specified!");
  351. enforce(repository.length > 0, "No repository field specified!");
  352.  
  353. dep = Dependency(Repository(
  354. repository.get!string, verspec["version"].get!string));
  355. } else {
  356. enforce("version" in verspec, "No version field specified!");
  357. auto ver = verspec["version"].get!string;
  358. // Using the string to be able to specify a range of versions.
  359. dep = Dependency(ver);
  360. }
  361.  
  362. if (auto po = "optional" in verspec) dep.optional = po.get!bool;
  363. if (auto po = "default" in verspec) dep.default_ = po.get!bool;
  364. } else {
  365. // canonical "package-id": "version"
  366. dep = Dependency(verspec.get!string);
  367. }
  368. return dep;
  369. }
  370.  
  371. @trusted unittest {
  372. assert(fromJson(parseJsonString("\">=1.0.0 <2.0.0\"")) == Dependency(">=1.0.0 <2.0.0"));
  373. Dependency parsed = fromJson(parseJsonString(`
  374. {
  375. "version": "2.0.0",
  376. "optional": true,
  377. "default": true,
  378. "path": "path/to/package"
  379. }
  380. `));
  381. Dependency d = Dependency.any; // supposed to ignore the version spec
  382. d.optional = true;
  383. d.default_ = true;
  384. d.path = NativePath("path/to/package");
  385. assert(d == parsed);
  386. // optional and path not checked by opEquals.
  387. assert(d.optional == parsed.optional);
  388. assert(d.default_ == parsed.default_);
  389. assert(d.path == parsed.path);
  390. }
  391.  
  392. /** Compares dependency specifications.
  393.  
  394. These methods are suitable for equality comparisons, as well as for
  395. using `Dependency` as a key in hash or tree maps.
  396. */
  397. bool opEquals(scope const Dependency o) const scope @safe {
  398. return this.m_value == o.m_value
  399. && o.m_optional == m_optional && o.m_default == m_default;
  400. }
  401.  
  402. /// ditto
  403. int opCmp(scope const Dependency o) const @safe {
  404. alias ResultMatch = match!(
  405. (VersionRange r1, VersionRange r2) => r1.opCmp(r2),
  406. (_1, _2) => 0,
  407. );
  408. if (auto result = ResultMatch(this.m_value, o.m_value))
  409. return result;
  410. if (m_optional != o.m_optional) return m_optional ? -1 : 1;
  411. return 0;
  412. }
  413.  
  414. /** Determines if this dependency specification is valid.
  415.  
  416. A specification is valid if it can match at least one version.
  417. */
  418. bool valid() const @safe {
  419. return this.m_value.match!(
  420. (Repository v) => true,
  421. (NativePath v) => true,
  422. (VersionRange v) => v.isValid(),
  423. );
  424. }
  425.  
  426. /** Determines if this dependency specification matches arbitrary versions.
  427.  
  428. This is true in particular for the `any` constant.
  429. */
  430. bool matchesAny() const scope @safe {
  431. return this.m_value.match!(
  432. (Repository v) => true,
  433. (NativePath v) => true,
  434. (VersionRange v) => v.matchesAny(),
  435. );
  436. }
  437.  
  438. unittest {
  439. assert(Dependency("*").matchesAny);
  440. assert(!Dependency(">0.0.0").matchesAny);
  441. assert(!Dependency(">=1.0.0").matchesAny);
  442. assert(!Dependency("<1.0.0").matchesAny);
  443. }
  444.  
  445. /** Tests if the specification matches a specific version.
  446. */
  447. bool matches(string vers, VersionMatchMode mode = VersionMatchMode.standard) const @safe
  448. {
  449. return matches(Version(vers), mode);
  450. }
  451. /// ditto
  452. bool matches(const(Version) v, VersionMatchMode mode = VersionMatchMode.standard) const @safe
  453. {
  454. return matches(v, mode);
  455. }
  456. /// ditto
  457. bool matches(ref const(Version) v, VersionMatchMode mode = VersionMatchMode.standard) const @safe {
  458. return this.m_value.match!(
  459. (Repository i) => true,
  460. (NativePath i) => true,
  461. (VersionRange i) => i.matchesAny() || i.matches(v, mode),
  462. );
  463. }
  464.  
  465. /** Merges two dependency specifications.
  466.  
  467. The result is a specification that matches the intersection of the set
  468. of versions matched by the individual specifications. Note that this
  469. result can be invalid (i.e. not match any version).
  470. */
  471. Dependency merge(ref const(Dependency) o) const @trusted {
  472. alias Merger = match!(
  473. // First check the repository. We ignore remote and simply compare ref
  474. // A repository takes precedence over any other dependency for backward
  475. // compatibility, but a later change should probably error out.
  476. (const Repository a, const Repository b) => a.m_ref == b.m_ref ? this : invalid,
  477. (const Repository a, any ) => this,
  478. ( any , const Repository b) => o,
  479.  
  480. // Likewise, path-based dependencies take precedence over versions
  481. (const NativePath a, const NativePath b) => a == b ? this : invalid,
  482. (const NativePath a, any ) => o,
  483. ( any , const NativePath b) => this,
  484.  
  485. (const VersionRange a, const VersionRange b) {
  486. if (a.matchesAny()) return o;
  487. if (b.matchesAny()) return this;
  488.  
  489. VersionRange copy = a;
  490. copy.merge(b);
  491. if (!copy.isValid()) return invalid;
  492. return Dependency(copy);
  493. }
  494. );
  495.  
  496. Dependency ret = Merger(this.m_value, o.m_value);
  497. ret.m_optional = m_optional && o.m_optional;
  498. return ret;
  499. }
  500. }
  501.  
  502. unittest {
  503. Dependency a = Dependency(">=1.1.0"), b = Dependency(">=1.3.0");
  504. assert (a.merge(b).valid() && a.merge(b).versionSpec == ">=1.3.0", a.merge(b).toString());
  505.  
  506. assertThrown(Dependency("<=2.0.0 >=1.0.0"));
  507. assertThrown(Dependency(">=2.0.0 <=1.0.0"));
  508.  
  509. a = Dependency(">=1.0.0 <=5.0.0"); b = Dependency(">=2.0.0");
  510. assert (a.merge(b).valid() && a.merge(b).versionSpec == ">=2.0.0 <=5.0.0", a.merge(b).toString());
  511.  
  512. assertThrown(a = Dependency(">1.0.0 ==5.0.0"), "Construction is invalid");
  513.  
  514. a = Dependency(">1.0.0"); b = Dependency("<2.0.0");
  515. assert (a.merge(b).valid(), a.merge(b).toString());
  516. assert (a.merge(b).versionSpec == ">1.0.0 <2.0.0", a.merge(b).toString());
  517.  
  518. a = Dependency(">2.0.0"); b = Dependency("<1.0.0");
  519. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  520.  
  521. a = Dependency(">=2.0.0"); b = Dependency("<=1.0.0");
  522. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  523.  
  524. a = Dependency("==2.0.0"); b = Dependency("==1.0.0");
  525. assert (!(a.merge(b)).valid(), a.merge(b).toString());
  526.  
  527. a = Dependency("1.0.0"); b = Dependency("==1.0.0");
  528. assert (a == b);
  529.  
  530. a = Dependency("<=2.0.0"); b = Dependency("==1.0.0");
  531. Dependency m = a.merge(b);
  532. assert (m.valid(), m.toString());
  533. assert (m.matches(Version("1.0.0")));
  534. assert (!m.matches(Version("1.1.0")));
  535. assert (!m.matches(Version("0.0.1")));
  536.  
  537.  
  538. // branches / head revisions
  539. a = Dependency(Version.masterBranch);
  540. assert(a.valid());
  541. assert(a.matches(Version.masterBranch));
  542. b = Dependency(Version.masterBranch);
  543. m = a.merge(b);
  544. assert(m.matches(Version.masterBranch));
  545.  
  546. //assertThrown(a = Dependency(Version.MASTER_STRING ~ " <=1.0.0"), "Construction invalid");
  547. assertThrown(a = Dependency(">=1.0.0 " ~ Version.masterBranch.toString()), "Construction invalid");
  548.  
  549. immutable string branch1 = Version.branchPrefix ~ "Branch1";
  550. immutable string branch2 = Version.branchPrefix ~ "Branch2";
  551.  
  552. //assertThrown(a = Dependency(branch1 ~ " " ~ branch2), "Error: '" ~ branch1 ~ " " ~ branch2 ~ "' succeeded");
  553. //assertThrown(a = Dependency(Version.MASTER_STRING ~ " " ~ branch1), "Error: '" ~ Version.MASTER_STRING ~ " " ~ branch1 ~ "' succeeded");
  554.  
  555. a = Dependency(branch1);
  556. b = Dependency(branch2);
  557. assert(!a.merge(b).valid, "Shouldn't be able to merge to different branches");
  558. b = a.merge(a);
  559. assert(b.valid, "Should be able to merge the same branches. (?)");
  560. assert(a == b);
  561.  
  562. a = Dependency(branch1);
  563. assert(a.matches(branch1), "Dependency(branch1) does not match 'branch1'");
  564. assert(a.matches(Version(branch1)), "Dependency(branch1) does not match Version('branch1')");
  565. assert(!a.matches(Version.masterBranch), "Dependency(branch1) matches Version.masterBranch");
  566. assert(!a.matches(branch2), "Dependency(branch1) matches 'branch2'");
  567. assert(!a.matches(Version("1.0.0")), "Dependency(branch1) matches '1.0.0'");
  568. a = Dependency(">=1.0.0");
  569. assert(!a.matches(Version(branch1)), "Dependency(1.0.0) matches 'branch1'");
  570.  
  571. // Testing optional dependencies.
  572. a = Dependency(">=1.0.0");
  573. assert(!a.optional, "Default is not optional.");
  574. b = a;
  575. assert(!a.merge(b).optional, "Merging two not optional dependencies wrong.");
  576. a.optional = true;
  577. assert(!a.merge(b).optional, "Merging optional with not optional wrong.");
  578. b.optional = true;
  579. assert(a.merge(b).optional, "Merging two optional dependencies wrong.");
  580.  
  581. // SemVer's sub identifiers.
  582. a = Dependency(">=1.0.0-beta");
  583. assert(!a.matches(Version("1.0.0-alpha")), "Failed: match 1.0.0-alpha with >=1.0.0-beta");
  584. assert(a.matches(Version("1.0.0-beta")), "Failed: match 1.0.0-beta with >=1.0.0-beta");
  585. assert(a.matches(Version("1.0.0")), "Failed: match 1.0.0 with >=1.0.0-beta");
  586. assert(a.matches(Version("1.0.0-rc")), "Failed: match 1.0.0-rc with >=1.0.0-beta");
  587.  
  588. // Approximate versions.
  589. a = Dependency("~>3.0");
  590. b = Dependency(">=3.0.0 <4.0.0-0");
  591. assert(a == b, "Testing failed: " ~ a.toString());
  592. assert(a.matches(Version("3.1.146")), "Failed: Match 3.1.146 with ~>0.1.2");
  593. assert(!a.matches(Version("0.2.0")), "Failed: Match 0.2.0 with ~>0.1.2");
  594. assert(!a.matches(Version("4.0.0-beta.1")));
  595. a = Dependency("~>3.0.0");
  596. assert(a == Dependency(">=3.0.0 <3.1.0-0"), "Testing failed: " ~ a.toString());
  597. a = Dependency("~>3.5");
  598. assert(a == Dependency(">=3.5.0 <4.0.0-0"), "Testing failed: " ~ a.toString());
  599. a = Dependency("~>3.5.0");
  600. assert(a == Dependency(">=3.5.0 <3.6.0-0"), "Testing failed: " ~ a.toString());
  601. assert(!Dependency("~>3.0.0").matches(Version("3.1.0-beta")));
  602.  
  603. a = Dependency("^0.1.2");
  604. assert(a == Dependency(">=0.1.2 <0.1.3-0"));
  605. a = Dependency("^1.2.3");
  606. assert(a == Dependency(">=1.2.3 <2.0.0-0"), "Testing failed: " ~ a.toString());
  607. a = Dependency("^1.2");
  608. assert(a == Dependency(">=1.2.0 <2.0.0-0"), "Testing failed: " ~ a.toString());
  609.  
  610. a = Dependency("~>0.1.1");
  611. b = Dependency("==0.1.0");
  612. assert(!a.merge(b).valid);
  613. b = Dependency("==0.1.9999");
  614. assert(a.merge(b).valid);
  615. b = Dependency("==0.2.0");
  616. assert(!a.merge(b).valid);
  617. b = Dependency("==0.2.0-beta.1");
  618. assert(!a.merge(b).valid);
  619.  
  620. a = Dependency("~>1.0.1-beta");
  621. b = Dependency(">=1.0.1-beta <1.1.0-0");
  622. assert(a == b, "Testing failed: " ~ a.toString());
  623. assert(a.matches(Version("1.0.1-beta")));
  624. assert(a.matches(Version("1.0.1-beta.6")));
  625.  
  626. a = Dependency("~d2test");
  627. assert(!a.optional);
  628. assert(a.valid);
  629. assert(a.version_ == Version("~d2test"));
  630.  
  631. a = Dependency("==~d2test");
  632. assert(!a.optional);
  633. assert(a.valid);
  634. assert(a.version_ == Version("~d2test"));
  635.  
  636. a = Dependency.any;
  637. assert(!a.optional);
  638. assert(a.valid);
  639. assertThrown(a.version_);
  640. assert(a.matches(Version.masterBranch));
  641. assert(a.matches(Version("1.0.0")));
  642. assert(a.matches(Version("0.0.1-pre")));
  643. b = Dependency(">=1.0.1");
  644. assert(b == a.merge(b));
  645. assert(b == b.merge(a));
  646. b = Dependency(Version.masterBranch);
  647. assert(a.merge(b) == b);
  648. assert(b.merge(a) == b);
  649.  
  650. a.optional = true;
  651. assert(a.matches(Version.masterBranch));
  652. assert(a.matches(Version("1.0.0")));
  653. assert(a.matches(Version("0.0.1-pre")));
  654. b = Dependency(">=1.0.1");
  655. assert(b == a.merge(b));
  656. assert(b == b.merge(a));
  657. b = Dependency(Version.masterBranch);
  658. assert(a.merge(b) == b);
  659. assert(b.merge(a) == b);
  660.  
  661. assert(Dependency("1.0.0").matches(Version("1.0.0+foo")));
  662. assert(Dependency("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.standard));
  663. assert(!Dependency("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  664. assert(Dependency("1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  665. assert(Dependency("~>1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  666. assert(Dependency("~>1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  667.  
  668. logDebug("Dependency unittest success.");
  669. }
  670.  
  671. unittest {
  672. assert(Dependency("~>1.0.4").versionSpec == "~>1.0.4");
  673. assert(Dependency("~>1.4").versionSpec == "~>1.4");
  674. assert(Dependency("~>2").versionSpec == "~>2");
  675. assert(Dependency("~>1.0.4+1.2.3").versionSpec == "~>1.0.4");
  676. assert(Dependency("^0.1.2").versionSpec == "^0.1.2");
  677. assert(Dependency("^1.2.3").versionSpec == "^1.2.3");
  678. assert(Dependency("^1.2").versionSpec == "~>1.2"); // equivalent; prefer ~>
  679. }
  680.  
  681. /**
  682. Represents an SCM repository.
  683. */
  684. struct Repository
  685. {
  686. private string m_remote;
  687. private string m_ref;
  688.  
  689. private Kind m_kind;
  690.  
  691. enum Kind
  692. {
  693. git,
  694. }
  695.  
  696. /**
  697. Params:
  698. remote = Repository remote.
  699. ref_ = Reference to use (SHA1, tag, branch name...)
  700. */
  701. this(string remote, string ref_)
  702. {
  703. enforce(remote.startsWith("git+"), "Unsupported repository type");
  704.  
  705. m_remote = remote["git+".length .. $];
  706. m_kind = Kind.git;
  707. m_ref = ref_;
  708. assert(m_remote.length);
  709. assert(m_ref.length);
  710. }
  711.  
  712. /// Ditto
  713. deprecated("Use the constructor accepting a second parameter named `ref_`")
  714. this(string remote)
  715. {
  716. enforce(remote.startsWith("git+"), "Unsupported repository type");
  717.  
  718. m_remote = remote["git+".length .. $];
  719. m_kind = Kind.git;
  720. assert(m_remote.length);
  721. }
  722.  
  723. string toString() nothrow pure @safe
  724. {
  725. if (empty) return null;
  726. string kindRepresentation;
  727.  
  728. final switch (kind)
  729. {
  730. case Kind.git:
  731. kindRepresentation = "git";
  732. }
  733. return kindRepresentation~"+"~remote;
  734. }
  735.  
  736. /**
  737. Returns:
  738. Repository URL or path.
  739. */
  740. @property string remote() @nogc nothrow pure @safe
  741. in { assert(m_remote !is null); }
  742. do
  743. {
  744. return m_remote;
  745. }
  746.  
  747. /**
  748. Returns:
  749. The reference (commit hash, branch name, tag) we are targeting
  750. */
  751. @property string ref_() @nogc nothrow pure @safe
  752. in { assert(m_remote !is null); }
  753. in { assert(m_ref !is null); }
  754. do
  755. {
  756. return m_ref;
  757. }
  758.  
  759. /**
  760. Returns:
  761. Repository type.
  762. */
  763. @property Kind kind() @nogc nothrow pure @safe
  764. {
  765. return m_kind;
  766. }
  767.  
  768. /**
  769. Returns:
  770. Whether the repository was initialized with an URL or path.
  771. */
  772. @property bool empty() const @nogc nothrow pure @safe
  773. {
  774. return m_remote.empty;
  775. }
  776. }
  777.  
  778.  
  779. /**
  780. Represents a version in semantic version format, or a branch identifier.
  781.  
  782. This can either have the form "~master", where "master" is a branch name,
  783. or the form "major.update.bugfix-prerelease+buildmetadata" (see the
  784. Semantic Versioning Specification v2.0.0 at http://semver.org/).
  785. */
  786. struct Version {
  787. private {
  788. static immutable MAX_VERS = "99999.0.0";
  789. static immutable UNKNOWN_VERS = "unknown";
  790. static immutable masterString = "~master";
  791. enum branchPrefix = '~';
  792. string m_version;
  793. }
  794.  
  795. static immutable Version minRelease = Version("0.0.0");
  796. static immutable Version maxRelease = Version(MAX_VERS);
  797. static immutable Version masterBranch = Version(masterString);
  798. static immutable Version unknown = Version(UNKNOWN_VERS);
  799.  
  800. /** Constructs a new `Version` from its string representation.
  801. */
  802. this(string vers) @safe pure
  803. {
  804. enforce(vers.length > 1, "Version strings must not be empty.");
  805. if (vers[0] != branchPrefix && vers.ptr !is UNKNOWN_VERS.ptr)
  806. enforce(vers.isValidVersion(), "Invalid SemVer format: " ~ vers);
  807. m_version = vers;
  808. }
  809.  
  810. /** Constructs a new `Version` from its string representation.
  811.  
  812. This method is equivalent to calling the constructor and is used as an
  813. endpoint for the serialization framework.
  814. */
  815. static Version fromString(string vers) @safe pure { return Version(vers); }
  816.  
  817. bool opEquals(scope const Version oth) const scope @safe pure
  818. {
  819. return opCmp(oth) == 0;
  820. }
  821.  
  822. /// Tests if this represents a branch instead of a version.
  823. @property bool isBranch() const scope @safe pure nothrow @nogc
  824. {
  825. return m_version.length > 0 && m_version[0] == branchPrefix;
  826. }
  827.  
  828. /// Tests if this represents the master branch "~master".
  829. @property bool isMaster() const scope @safe pure nothrow @nogc
  830. {
  831. return m_version == masterString;
  832. }
  833.  
  834. /** Tests if this represents a pre-release version.
  835.  
  836. Note that branches are always considered pre-release versions.
  837. */
  838. @property bool isPreRelease() const scope @safe pure nothrow @nogc
  839. {
  840. if (isBranch) return true;
  841. return isPreReleaseVersion(m_version);
  842. }
  843.  
  844. /// Tests if this represents the special unknown version constant.
  845. @property bool isUnknown() const scope @safe pure nothrow @nogc
  846. {
  847. return m_version == UNKNOWN_VERS;
  848. }
  849.  
  850. /** Tests two versions for equality, according to the selected match mode.
  851. */
  852. bool matches(Version other, VersionMatchMode mode = VersionMatchMode.standard)
  853. const scope @safe pure
  854. {
  855. if (mode == VersionMatchMode.strict)
  856. return this.toString() == other.toString();
  857. return this == other;
  858. }
  859.  
  860. /** Compares two versions/branches for precedence.
  861.  
  862. Versions generally have precedence over branches and the master branch
  863. has precedence over other branches. Apart from that, versions are
  864. compared using SemVer semantics, while branches are compared
  865. lexicographically.
  866. */
  867. int opCmp(scope ref const Version other) const scope @safe pure
  868. {
  869. if (isUnknown || other.isUnknown) {
  870. throw new Exception("Can't compare unknown versions! (this: %s, other: %s)".format(this, other));
  871. }
  872.  
  873. if (isBranch || other.isBranch) {
  874. if(m_version == other.m_version) return 0;
  875. if (!isBranch) return 1;
  876. else if (!other.isBranch) return -1;
  877. if (isMaster) return 1;
  878. else if (other.isMaster) return -1;
  879. return this.m_version < other.m_version ? -1 : 1;
  880. }
  881.  
  882. return compareVersions(m_version, other.m_version);
  883. }
  884. /// ditto
  885. int opCmp(scope const Version other) const scope @safe pure
  886. {
  887. return this.opCmp(other);
  888. }
  889.  
  890. /// Returns the string representation of the version/branch.
  891. string toString() const return scope @safe pure nothrow @nogc
  892. {
  893. return m_version;
  894. }
  895. }
  896.  
  897. /// A range of versions that are acceptable
  898. private struct VersionRange
  899. {
  900. Version m_versA;
  901. Version m_versB;
  902. bool m_inclusiveA = true; // A comparison > (true) or >= (false)
  903. bool m_inclusiveB = true; // B comparison < (true) or <= (false)
  904.  
  905. /// Matches any version
  906. public static immutable Any = VersionRange(Version.minRelease, Version.maxRelease);
  907. /// Doesn't match any version
  908. public static immutable Invalid = VersionRange(Version.maxRelease, Version.minRelease);
  909.  
  910. ///
  911. public int opCmp (scope const VersionRange o) const scope @safe
  912. {
  913. if (m_inclusiveA != o.m_inclusiveA) return m_inclusiveA < o.m_inclusiveA ? -1 : 1;
  914. if (m_inclusiveB != o.m_inclusiveB) return m_inclusiveB < o.m_inclusiveB ? -1 : 1;
  915. if (m_versA != o.m_versA) return m_versA < o.m_versA ? -1 : 1;
  916. if (m_versB != o.m_versB) return m_versB < o.m_versB ? -1 : 1;
  917. return 0;
  918. }
  919.  
  920. public bool matches (ref const Version v, VersionMatchMode mode = VersionMatchMode.standard)
  921. const @safe
  922. {
  923. if (m_versA.isBranch) {
  924. enforce(this.isExactVersion());
  925. return m_versA == v;
  926. }
  927.  
  928. if (v.isBranch)
  929. return m_versA == v;
  930.  
  931. if (m_versA == m_versB)
  932. return this.m_versA.matches(v, mode);
  933.  
  934. return doCmp(m_inclusiveA, m_versA, v) &&
  935. doCmp(m_inclusiveB, v, m_versB);
  936. }
  937.  
  938. /// Modify in place
  939. public void merge (const VersionRange o) @safe
  940. {
  941. int acmp = m_versA.opCmp(o.m_versA);
  942. int bcmp = m_versB.opCmp(o.m_versB);
  943.  
  944. this.m_inclusiveA = !m_inclusiveA && acmp >= 0 ? false : o.m_inclusiveA;
  945. this.m_versA = acmp > 0 ? m_versA : o.m_versA;
  946. this.m_inclusiveB = !m_inclusiveB && bcmp <= 0 ? false : o.m_inclusiveB;
  947. this.m_versB = bcmp < 0 ? m_versB : o.m_versB;
  948. }
  949.  
  950. /// Returns true $(I iff) the version range only matches a specific version.
  951. @property bool isExactVersion() const scope @safe
  952. {
  953. return this.m_versA == this.m_versB;
  954. }
  955.  
  956. /// Determines if this dependency specification matches arbitrary versions.
  957. /// This is true in particular for the `any` constant.
  958. public bool matchesAny() const scope @safe
  959. {
  960. return this.m_inclusiveA && this.m_inclusiveB
  961. && this.m_versA == Version.minRelease
  962. && this.m_versB == Version.maxRelease;
  963. }
  964.  
  965. public static VersionRange fromString (string ves) @safe
  966. {
  967. static import std.string;
  968.  
  969. enforce(ves.length > 0);
  970.  
  971. if (ves == Dependency.ANY_IDENT) {
  972. // Any version is good.
  973. ves = ">=0.0.0";
  974. }
  975.  
  976. if (ves.startsWith("~>")) {
  977. // Shortcut: "~>x.y.z" variant. Last non-zero number will indicate
  978. // the base for this so something like this: ">=x.y.z <x.(y+1).z"
  979. ves = ves[2..$];
  980. return VersionRange(
  981. Version(expandVersion(ves)), Version(bumpVersion(ves) ~ "-0"),
  982. true, false);
  983. }
  984.  
  985. if (ves.startsWith("^")) {
  986. // Shortcut: "^x.y.z" variant. "Semver compatible" - no breaking changes.
  987. // if 0.x.y, ==0.x.y
  988. // if x.y.z, >=x.y.z <(x+1).0.0-0
  989. // ^x.y is equivalent to ^x.y.0.
  990. ves = ves[1..$].expandVersion;
  991. return VersionRange(
  992. Version(ves), Version(bumpIncompatibleVersion(ves) ~ "-0"),
  993. true, false);
  994. }
  995.  
  996. if (ves[0] == Version.branchPrefix) {
  997. auto ver = Version(ves);
  998. return VersionRange(ver, ver, true, true);
  999. }
  1000.  
  1001. if (std.string.indexOf("><=", ves[0]) == -1) {
  1002. auto ver = Version(ves);
  1003. return VersionRange(ver, ver, true, true);
  1004. }
  1005.  
  1006. auto cmpa = skipComp(ves);
  1007. size_t idx2 = std.string.indexOf(ves, " ");
  1008. if (idx2 == -1) {
  1009. if (cmpa == "<=" || cmpa == "<")
  1010. return VersionRange(Version.minRelease, Version(ves), true, (cmpa == "<="));
  1011.  
  1012. if (cmpa == ">=" || cmpa == ">")
  1013. return VersionRange(Version(ves), Version.maxRelease, (cmpa == ">="), true);
  1014.  
  1015. // Converts "==" to ">=a&&<=a", which makes merging easier
  1016. return VersionRange(Version(ves), Version(ves), true, true);
  1017. }
  1018.  
  1019. enforce(cmpa == ">" || cmpa == ">=",
  1020. "First comparison operator expected to be either > or >=, not " ~ cmpa);
  1021. assert(ves[idx2] == ' ');
  1022. VersionRange ret;
  1023. ret.m_versA = Version(ves[0..idx2]);
  1024. ret.m_inclusiveA = cmpa == ">=";
  1025. string v2 = ves[idx2+1..$];
  1026. auto cmpb = skipComp(v2);
  1027. enforce(cmpb == "<" || cmpb == "<=",
  1028. "Second comparison operator expected to be either < or <=, not " ~ cmpb);
  1029. ret.m_versB = Version(v2);
  1030. ret.m_inclusiveB = cmpb == "<=";
  1031.  
  1032. enforce(!ret.m_versA.isBranch && !ret.m_versB.isBranch,
  1033. format("Cannot compare branches: %s", ves));
  1034. enforce(ret.m_versA <= ret.m_versB,
  1035. "First version must not be greater than the second one.");
  1036.  
  1037. return ret;
  1038. }
  1039.  
  1040. /// Returns a string representation of this range
  1041. string toString() const @safe {
  1042. static import std.string;
  1043.  
  1044. string r;
  1045.  
  1046. if (this == Invalid) return "invalid";
  1047. if (this.isExactVersion() && m_inclusiveA && m_inclusiveB) {
  1048. // Special "==" case
  1049. if (m_versA == Version.masterBranch) return "~master";
  1050. else return m_versA.toString();
  1051. }
  1052.  
  1053. // "~>", "^" case
  1054. if (m_inclusiveA && !m_inclusiveB && !m_versA.isBranch) {
  1055. auto vs = m_versA.toString();
  1056. auto i1 = std.string.indexOf(vs, '-'), i2 = std.string.indexOf(vs, '+');
  1057. auto i12 = i1 >= 0 ? i2 >= 0 ? i1 < i2 ? i1 : i2 : i1 : i2;
  1058. auto va = i12 >= 0 ? vs[0 .. i12] : vs;
  1059. auto parts = va.splitter('.').array;
  1060. assert(parts.length == 3, "Version string with a digit group count != 3: "~va);
  1061.  
  1062. foreach (i; 0 .. 3) {
  1063. auto vp = parts[0 .. i+1].join(".");
  1064. auto ve = Version(expandVersion(vp));
  1065. auto veb = Version(bumpVersion(vp) ~ "-0");
  1066. if (ve == m_versA && veb == m_versB) return "~>" ~ vp;
  1067.  
  1068. auto veb2 = Version(bumpIncompatibleVersion(expandVersion(vp)) ~ "-0");
  1069. if (ve == m_versA && veb2 == m_versB) return "^" ~ vp;
  1070. }
  1071. }
  1072.  
  1073. if (m_versA != Version.minRelease) r = (m_inclusiveA ? ">=" : ">") ~ m_versA.toString();
  1074. if (m_versB != Version.maxRelease) r ~= (r.length==0 ? "" : " ") ~ (m_inclusiveB ? "<=" : "<") ~ m_versB.toString();
  1075. if (this.matchesAny()) r = ">=0.0.0";
  1076. return r;
  1077. }
  1078.  
  1079. public bool isValid() const @safe {
  1080. return m_versA <= m_versB && doCmp(m_inclusiveA && m_inclusiveB, m_versA, m_versB);
  1081. }
  1082.  
  1083. private static bool doCmp(bool inclusive, ref const Version a, ref const Version b)
  1084. @safe
  1085. {
  1086. return inclusive ? a <= b : a < b;
  1087. }
  1088.  
  1089. private static bool isDigit(char ch) @safe { return ch >= '0' && ch <= '9'; }
  1090. private static string skipComp(ref string c) @safe {
  1091. size_t idx = 0;
  1092. while (idx < c.length && !isDigit(c[idx]) && c[idx] != Version.branchPrefix) idx++;
  1093. enforce(idx < c.length, "Expected version number in version spec: "~c);
  1094. string cmp = idx==c.length-1||idx==0? ">=" : c[0..idx];
  1095. c = c[idx..$];
  1096. switch(cmp) {
  1097. default: enforce(false, "No/Unknown comparison specified: '"~cmp~"'"); return ">=";
  1098. case ">=": goto case; case ">": goto case;
  1099. case "<=": goto case; case "<": goto case;
  1100. case "==": return cmp;
  1101. }
  1102. }
  1103. }
  1104.  
  1105. enum VersionMatchMode {
  1106. standard, /// Match according to SemVer rules
  1107. strict /// Also include build metadata suffix in the comparison
  1108. }
  1109.  
  1110. unittest {
  1111. Version a, b;
  1112.  
  1113. assertNotThrown(a = Version("1.0.0"), "Constructing Version('1.0.0') failed");
  1114. assert(!a.isBranch, "Error: '1.0.0' treated as branch");
  1115. assert(a == a, "a == a failed");
  1116.  
  1117. assertNotThrown(a = Version(Version.masterString), "Constructing Version("~Version.masterString~"') failed");
  1118. assert(a.isBranch, "Error: '"~Version.masterString~"' treated as branch");
  1119. assert(a.isMaster);
  1120. assert(a == Version.masterBranch, "Constructed master version != default master version.");
  1121.  
  1122. assertNotThrown(a = Version("~BRANCH"), "Construction of branch Version failed.");
  1123. assert(a.isBranch, "Error: '~BRANCH' not treated as branch'");
  1124. assert(!a.isMaster);
  1125. assert(a == a, "a == a with branch failed");
  1126.  
  1127. // opCmp
  1128. a = Version("1.0.0");
  1129. b = Version("1.0.0");
  1130. assert(a == b, "a == b with a:'1.0.0', b:'1.0.0' failed");
  1131. b = Version("2.0.0");
  1132. assert(a != b, "a != b with a:'1.0.0', b:'2.0.0' failed");
  1133.  
  1134. a = Version.masterBranch;
  1135. b = Version("~BRANCH");
  1136. assert(a != b, "a != b with a:MASTER, b:'~branch' failed");
  1137. assert(a > b);
  1138. assert(a < Version("0.0.0"));
  1139. assert(b < Version("0.0.0"));
  1140. assert(a > Version("~Z"));
  1141. assert(b < Version("~Z"));
  1142.  
  1143. // SemVer 2.0.0-rc.2
  1144. a = Version("2.0.0-rc.2");
  1145. b = Version("2.0.0-rc.3");
  1146. assert(a < b, "Failed: 2.0.0-rc.2 < 2.0.0-rc.3");
  1147.  
  1148. a = Version("2.0.0-rc.2+build-metadata");
  1149. b = Version("2.0.0+build-metadata");
  1150. assert(a < b, "Failed: "~a.toString()~"<"~b.toString());
  1151.  
  1152. // 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11 < 1.0.0-rc.1 < 1.0.0
  1153. Version[] versions;
  1154. versions ~= Version("1.0.0-alpha");
  1155. versions ~= Version("1.0.0-alpha.1");
  1156. versions ~= Version("1.0.0-beta.2");
  1157. versions ~= Version("1.0.0-beta.11");
  1158. versions ~= Version("1.0.0-rc.1");
  1159. versions ~= Version("1.0.0");
  1160. for(int i=1; i<versions.length; ++i)
  1161. for(int j=i-1; j>=0; --j)
  1162. assert(versions[j] < versions[i], "Failed: " ~ versions[j].toString() ~ "<" ~ versions[i].toString());
  1163.  
  1164. a = Version.unknown;
  1165. b = Version.minRelease;
  1166. assertThrown(a == b, "Failed: compared " ~ a.toString() ~ " with " ~ b.toString() ~ "");
  1167.  
  1168. a = Version.unknown;
  1169. b = Version.unknown;
  1170. assertThrown(a == b, "Failed: UNKNOWN == UNKNOWN");
  1171.  
  1172. assert(Version("1.0.0+a") == Version("1.0.0+b"));
  1173.  
  1174. assert(Version("1.0.0").matches(Version("1.0.0+foo")));
  1175. assert(Version("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.standard));
  1176. assert(!Version("1.0.0").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  1177. assert(Version("1.0.0+foo").matches(Version("1.0.0+foo"), VersionMatchMode.strict));
  1178. }
  1179.  
  1180. /// Determines whether the given string is a Git hash.
  1181. bool isGitHash(string hash) @nogc nothrow pure @safe
  1182. {
  1183. import std.ascii : isHexDigit;
  1184. import std.utf : byCodeUnit;
  1185.  
  1186. return hash.length >= 7 && hash.length <= 40 && hash.byCodeUnit.all!isHexDigit;
  1187. }
  1188.  
  1189. @nogc nothrow pure @safe unittest {
  1190. assert(isGitHash("73535568b79a0b124bc1653002637a830ce0fcb8"));
  1191. assert(!isGitHash("735"));
  1192. assert(!isGitHash("73535568b79a0b124bc1-53002637a830ce0fcb8"));
  1193. assert(!isGitHash("73535568b79a0b124bg1"));
  1194. }