| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Handles version numbers, following the [Semantic Versioning][semver] spec. | 6 * Handles version numbers, following the [Semantic Versioning][semver] spec. |
| 7 * | 7 * |
| 8 * [semver]: http://semver.org/ | 8 * [semver]: http://semver.org/ |
| 9 */ | 9 */ |
| 10 library version; | 10 library version; |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 67 |
| 68 String preRelease = match[5]; | 68 String preRelease = match[5]; |
| 69 String build = match[8]; | 69 String build = match[8]; |
| 70 | 70 |
| 71 return new Version(major, minor, patch, pre: preRelease, build: build); | 71 return new Version(major, minor, patch, pre: preRelease, build: build); |
| 72 } on FormatException catch (ex) { | 72 } on FormatException catch (ex) { |
| 73 throw new FormatException('Could not parse "$text".'); | 73 throw new FormatException('Could not parse "$text".'); |
| 74 } | 74 } |
| 75 } | 75 } |
| 76 | 76 |
| 77 /// Returns the primary version out of a list of candidates. This is the |
| 78 /// highest-numbered stable (non-prerelease) version. If there are no stable |
| 79 /// versions, it's just the highest-numbered version. |
| 80 static Version primary(List<Version> versions) { |
| 81 var primary; |
| 82 for (var version in versions) { |
| 83 if (primary == null || (!version.isPreRelease && primary.isPreRelease) || |
| 84 (version.isPreRelease == primary.isPreRelease && version > primary)) { |
| 85 primary = version; |
| 86 } |
| 87 } |
| 88 return primary; |
| 89 } |
| 90 |
| 77 bool operator ==(other) { | 91 bool operator ==(other) { |
| 78 if (other is! Version) return false; | 92 if (other is! Version) return false; |
| 79 return compareTo(other) == 0; | 93 return compareTo(other) == 0; |
| 80 } | 94 } |
| 81 | 95 |
| 82 bool operator <(Version other) => compareTo(other) < 0; | 96 bool operator <(Version other) => compareTo(other) < 0; |
| 83 bool operator >(Version other) => compareTo(other) > 0; | 97 bool operator >(Version other) => compareTo(other) > 0; |
| 84 bool operator <=(Version other) => compareTo(other) <= 0; | 98 bool operator <=(Version other) => compareTo(other) <= 0; |
| 85 bool operator >=(Version other) => compareTo(other) >= 0; | 99 bool operator >=(Version other) => compareTo(other) >= 0; |
| 86 | 100 |
| 101 bool get isAny => false; |
| 87 bool get isEmpty => false; | 102 bool get isEmpty => false; |
| 88 | 103 |
| 104 /// Whether or not this is a pre-release version. |
| 105 bool get isPreRelease => preRelease != null; |
| 106 |
| 89 /** Tests if [other] matches this version exactly. */ | 107 /** Tests if [other] matches this version exactly. */ |
| 90 bool allows(Version other) => this == other; | 108 bool allows(Version other) => this == other; |
| 91 | 109 |
| 92 VersionConstraint intersect(VersionConstraint other) { | 110 VersionConstraint intersect(VersionConstraint other) { |
| 93 if (other.isEmpty) return other; | 111 if (other.isEmpty) return other; |
| 94 | 112 |
| 95 // Intersect a version and a range. | 113 // Intersect a version and a range. |
| 96 if (other is VersionRange) return other.intersect(this); | 114 if (other is VersionRange) return other.intersect(this); |
| 97 | 115 |
| 98 // Intersecting two versions only works if they are the same. | 116 // Intersecting two versions only works if they are the same. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 constraint = constraint.intersect(other); | 263 constraint = constraint.intersect(other); |
| 246 } | 264 } |
| 247 return constraint; | 265 return constraint; |
| 248 } | 266 } |
| 249 | 267 |
| 250 /** | 268 /** |
| 251 * Returns `true` if this constraint allows no versions. | 269 * Returns `true` if this constraint allows no versions. |
| 252 */ | 270 */ |
| 253 bool get isEmpty; | 271 bool get isEmpty; |
| 254 | 272 |
| 273 /// Returns `true` if this constraint allows all versions. |
| 274 bool get isAny; |
| 275 |
| 255 /** | 276 /** |
| 256 * Returns `true` if this constraint allows [version]. | 277 * Returns `true` if this constraint allows [version]. |
| 257 */ | 278 */ |
| 258 bool allows(Version version); | 279 bool allows(Version version); |
| 259 | 280 |
| 260 /** | 281 /** |
| 261 * Creates a new [VersionConstraint] that only allows [Version]s allowed by | 282 * Creates a new [VersionConstraint] that only allows [Version]s allowed by |
| 262 * both this and [other]. | 283 * both this and [other]. |
| 263 */ | 284 */ |
| 264 VersionConstraint intersect(VersionConstraint other); | 285 VersionConstraint intersect(VersionConstraint other); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 if (other is! VersionRange) return false; | 335 if (other is! VersionRange) return false; |
| 315 | 336 |
| 316 return min == other.min && | 337 return min == other.min && |
| 317 max == other.max && | 338 max == other.max && |
| 318 includeMin == other.includeMin && | 339 includeMin == other.includeMin && |
| 319 includeMax == other.includeMax; | 340 includeMax == other.includeMax; |
| 320 } | 341 } |
| 321 | 342 |
| 322 bool get isEmpty => false; | 343 bool get isEmpty => false; |
| 323 | 344 |
| 345 bool get isAny => !includeMin && !includeMax; |
| 346 |
| 324 /** Tests if [other] matches falls within this version range. */ | 347 /** Tests if [other] matches falls within this version range. */ |
| 325 bool allows(Version other) { | 348 bool allows(Version other) { |
| 326 if (min != null && other < min) return false; | 349 if (min != null && other < min) return false; |
| 327 if (min != null && !includeMin && other == min) return false; | 350 if (min != null && !includeMin && other == min) return false; |
| 328 if (max != null && other > max) return false; | 351 if (max != null && other > max) return false; |
| 329 if (max != null && !includeMax && other == max) return false; | 352 if (max != null && !includeMax && other == max) return false; |
| 330 return true; | 353 return true; |
| 331 } | 354 } |
| 332 | 355 |
| 333 VersionConstraint intersect(VersionConstraint other) { | 356 VersionConstraint intersect(VersionConstraint other) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 431 |
| 409 if (min == null && max == null) buffer.add('any'); | 432 if (min == null && max == null) buffer.add('any'); |
| 410 return buffer.toString(); | 433 return buffer.toString(); |
| 411 } | 434 } |
| 412 } | 435 } |
| 413 | 436 |
| 414 class _EmptyVersion implements VersionConstraint { | 437 class _EmptyVersion implements VersionConstraint { |
| 415 const _EmptyVersion(); | 438 const _EmptyVersion(); |
| 416 | 439 |
| 417 bool get isEmpty => true; | 440 bool get isEmpty => true; |
| 441 bool get isAny => false; |
| 418 bool allows(Version other) => false; | 442 bool allows(Version other) => false; |
| 419 VersionConstraint intersect(VersionConstraint other) => this; | 443 VersionConstraint intersect(VersionConstraint other) => this; |
| 420 String toString() => '<empty>'; | 444 String toString() => '<empty>'; |
| 421 } | 445 } |
| OLD | NEW |