| 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'); |
| 11 | 11 |
| 12 #import('dart:math'); | 12 #import('dart:math'); |
| 13 | 13 |
| 14 #import('utils.dart'); | 14 #import('utils.dart'); |
| 15 | 15 |
| 16 /** A parsed semantic version number. */ | 16 /** A parsed semantic version number. */ |
| 17 class Version implements Comparable, Hashable, VersionConstraint { | 17 class Version implements Comparable, Hashable, VersionConstraint { |
| 18 /** No released version: i.e. "0.0.0". */ | 18 /** No released version: i.e. "0.0.0". */ |
| 19 static Version get none() => new Version(0, 0, 0); | 19 static Version get none => new Version(0, 0, 0); |
| 20 | 20 |
| 21 static final _PARSE_REGEX = const RegExp( | 21 static final _PARSE_REGEX = const RegExp( |
| 22 @'^' // Start at beginning. | 22 @'^' // Start at beginning. |
| 23 @'(\d+).(\d+).(\d+)' // Version number. | 23 @'(\d+).(\d+).(\d+)' // Version number. |
| 24 @'(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Pre-release. | 24 @'(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Pre-release. |
| 25 @'(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Build. | 25 @'(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Build. |
| 26 @'$'); // Consume entire string. | 26 @'$'); // Consume entire string. |
| 27 | 27 |
| 28 /** The major version number: "1" in "1.2.3". */ | 28 /** The major version number: "1" in "1.2.3". */ |
| 29 final int major; | 29 final int major; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 bool operator ==(other) { | 77 bool operator ==(other) { |
| 78 if (other is! Version) return false; | 78 if (other is! Version) return false; |
| 79 return compareTo(other) == 0; | 79 return compareTo(other) == 0; |
| 80 } | 80 } |
| 81 | 81 |
| 82 bool operator <(Version other) => compareTo(other) < 0; | 82 bool operator <(Version other) => compareTo(other) < 0; |
| 83 bool operator >(Version other) => compareTo(other) > 0; | 83 bool operator >(Version other) => compareTo(other) > 0; |
| 84 bool operator <=(Version other) => compareTo(other) <= 0; | 84 bool operator <=(Version other) => compareTo(other) <= 0; |
| 85 bool operator >=(Version other) => compareTo(other) >= 0; | 85 bool operator >=(Version other) => compareTo(other) >= 0; |
| 86 | 86 |
| 87 bool get isEmpty() => false; | 87 bool get isEmpty => false; |
| 88 | 88 |
| 89 /** Tests if [other] matches this version exactly. */ | 89 /** Tests if [other] matches this version exactly. */ |
| 90 bool allows(Version other) => this == other; | 90 bool allows(Version other) => this == other; |
| 91 | 91 |
| 92 VersionConstraint intersect(VersionConstraint other) { | 92 VersionConstraint intersect(VersionConstraint other) { |
| 93 if (other.isEmpty) return other; | 93 if (other.isEmpty) return other; |
| 94 | 94 |
| 95 // Intersect a version and a range. | 95 // Intersect a version and a range. |
| 96 if (other is VersionRange) return other.intersect(this); | 96 if (other is VersionRange) return other.intersect(this); |
| 97 | 97 |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 * Creates a new version constraint that is the intersection of [constraints]. | 224 * Creates a new version constraint that is the intersection of [constraints]. |
| 225 * It will only allow versions that all of those constraints allow. If | 225 * It will only allow versions that all of those constraints allow. If |
| 226 * constraints is empty, then it returns a VersionConstraint that allows all | 226 * constraints is empty, then it returns a VersionConstraint that allows all |
| 227 * versions. | 227 * versions. |
| 228 */ | 228 */ |
| 229 VersionConstraint.intersect(Collection<VersionConstraint> constraints); | 229 VersionConstraint.intersect(Collection<VersionConstraint> constraints); |
| 230 | 230 |
| 231 /** | 231 /** |
| 232 * Returns `true` if this constraint allows no versions. | 232 * Returns `true` if this constraint allows no versions. |
| 233 */ | 233 */ |
| 234 bool get isEmpty(); | 234 bool get isEmpty; |
| 235 | 235 |
| 236 /** | 236 /** |
| 237 * Returns `true` if this constraint allows [version]. | 237 * Returns `true` if this constraint allows [version]. |
| 238 */ | 238 */ |
| 239 bool allows(Version version); | 239 bool allows(Version version); |
| 240 | 240 |
| 241 /** | 241 /** |
| 242 * Creates a new [VersionConstraint] that only allows [Version]s allowed by | 242 * Creates a new [VersionConstraint] that only allows [Version]s allowed by |
| 243 * both this and [other]. | 243 * both this and [other]. |
| 244 */ | 244 */ |
| (...skipping 22 matching lines...) Expand all Loading... |
| 267 | 267 |
| 268 bool operator ==(other) { | 268 bool operator ==(other) { |
| 269 if (other is! VersionRange) return false; | 269 if (other is! VersionRange) return false; |
| 270 | 270 |
| 271 return min == other.min && | 271 return min == other.min && |
| 272 max == other.max && | 272 max == other.max && |
| 273 includeMin == other.includeMin && | 273 includeMin == other.includeMin && |
| 274 includeMax == other.includeMax; | 274 includeMax == other.includeMax; |
| 275 } | 275 } |
| 276 | 276 |
| 277 bool get isEmpty() => false; | 277 bool get isEmpty => false; |
| 278 | 278 |
| 279 /** Tests if [other] matches falls within this version range. */ | 279 /** Tests if [other] matches falls within this version range. */ |
| 280 bool allows(Version other) { | 280 bool allows(Version other) { |
| 281 if (min != null && other < min) return false; | 281 if (min != null && other < min) return false; |
| 282 if (min != null && !includeMin && other == min) return false; | 282 if (min != null && !includeMin && other == min) return false; |
| 283 if (max != null && other > max) return false; | 283 if (max != null && other > max) return false; |
| 284 if (max != null && !includeMax && other == max) return false; | 284 if (max != null && !includeMax && other == max) return false; |
| 285 return true; | 285 return true; |
| 286 } | 286 } |
| 287 | 287 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 } | 362 } |
| 363 | 363 |
| 364 if (min == null && max == null) buffer.add('any'); | 364 if (min == null && max == null) buffer.add('any'); |
| 365 return buffer.toString(); | 365 return buffer.toString(); |
| 366 } | 366 } |
| 367 } | 367 } |
| 368 | 368 |
| 369 class _EmptyVersion implements VersionConstraint { | 369 class _EmptyVersion implements VersionConstraint { |
| 370 const _EmptyVersion(); | 370 const _EmptyVersion(); |
| 371 | 371 |
| 372 bool get isEmpty() => true; | 372 bool get isEmpty => true; |
| 373 bool allows(Version other) => false; | 373 bool allows(Version other) => false; |
| 374 VersionConstraint intersect(VersionConstraint other) => this; | 374 VersionConstraint intersect(VersionConstraint other) => this; |
| 375 String toString() => '<empty>'; | 375 String toString() => '<empty>'; |
| 376 } | 376 } |
| 377 | 377 |
| 378 class _VersionConstraintFactory { | 378 class _VersionConstraintFactory { |
| 379 factory VersionConstraint.empty() => const _EmptyVersion(); | 379 factory VersionConstraint.empty() => const _EmptyVersion(); |
| 380 | 380 |
| 381 factory VersionConstraint.parse(String text) { | 381 factory VersionConstraint.parse(String text) { |
| 382 if (text.trim() == '') { | 382 if (text.trim() == '') { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 case '<': return new VersionRange(max: version, includeMax: false); | 420 case '<': return new VersionRange(max: version, includeMax: false); |
| 421 case '>=': return new VersionRange(min: version, includeMin: true); | 421 case '>=': return new VersionRange(min: version, includeMin: true); |
| 422 case '>': return new VersionRange(min: version, includeMin: false); | 422 case '>': return new VersionRange(min: version, includeMin: false); |
| 423 } | 423 } |
| 424 } | 424 } |
| 425 | 425 |
| 426 // Otherwise, it must be an explicit version. | 426 // Otherwise, it must be an explicit version. |
| 427 return new Version.parse(text); | 427 return new Version.parse(text); |
| 428 } | 428 } |
| 429 } | 429 } |
| OLD | NEW |