OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 import 'dart:math' as math; | 5 import 'dart:math' as math; |
6 | 6 |
7 import 'package:collection/collection.dart'; | 7 import 'package:collection/collection.dart'; |
8 | 8 |
9 import 'patterns.dart'; | 9 import 'patterns.dart'; |
10 import 'version_constraint.dart'; | 10 import 'version_constraint.dart'; |
11 import 'version_range.dart'; | 11 import 'version_range.dart'; |
12 | 12 |
13 /// The equality operator to use for comparing version components. | 13 /// The equality operator to use for comparing version components. |
14 final _equality = const IterableEquality(); | 14 final _equality = const IterableEquality(); |
15 | 15 |
16 /// A parsed semantic version number. | 16 /// A parsed semantic version number. |
17 class Version implements Comparable<Version>, VersionConstraint, VersionRange { | 17 class Version implements VersionConstraint, VersionRange { |
Bob Nystrom
2016/06/03 16:18:03
Can this implement Comparable<VersionRange>?
nweiz
2016/06/06 22:27:46
It already does, transitively through VersionRange
| |
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 /// Compares [a] and [b] to see which takes priority over the other. | 21 /// Compares [a] and [b] to see which takes priority over the other. |
22 /// | 22 /// |
23 /// Returns `1` if [a] takes priority over [b] and `-1` if vice versa. If | 23 /// Returns `1` if [a] takes priority over [b] and `-1` if vice versa. If |
24 /// [a] and [b] are equivalent, returns `0`. | 24 /// [a] and [b] are equivalent, returns `0`. |
25 /// | 25 /// |
26 /// Unlike [compareTo], which *orders* versions, this determines which | 26 /// Unlike [compareTo], which *orders* versions, this determines which |
27 /// version a user is likely to prefer. In particular, it prioritizes | 27 /// version a user is likely to prefer. In particular, it prioritizes |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
260 if (other.max == this) { | 260 if (other.max == this) { |
261 return new VersionRange( | 261 return new VersionRange( |
262 min: other.min, max: other.max, | 262 min: other.min, max: other.max, |
263 includeMin: other.includeMin, includeMax: true); | 263 includeMin: other.includeMin, includeMax: true); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 return new VersionConstraint.unionOf([this, other]); | 267 return new VersionConstraint.unionOf([this, other]); |
268 } | 268 } |
269 | 269 |
270 int compareTo(Version other) { | 270 int compareTo(VersionRange other) { |
271 if (major != other.major) return major.compareTo(other.major); | 271 if (other is Version) { |
Bob Nystrom
2016/06/03 16:18:03
Might be a bit simpler to do the negative test:
i
nweiz
2016/06/06 22:27:46
https://github.com/dart-lang/sdk/issues/25565 :(
| |
272 if (minor != other.minor) return minor.compareTo(other.minor); | 272 if (major != other.major) return major.compareTo(other.major); |
273 if (patch != other.patch) return patch.compareTo(other.patch); | 273 if (minor != other.minor) return minor.compareTo(other.minor); |
274 if (patch != other.patch) return patch.compareTo(other.patch); | |
274 | 275 |
275 // Pre-releases always come before no pre-release string. | 276 // Pre-releases always come before no pre-release string. |
276 if (!isPreRelease && other.isPreRelease) return 1; | 277 if (!isPreRelease && other.isPreRelease) return 1; |
277 if (!other.isPreRelease && isPreRelease) return -1; | 278 if (!other.isPreRelease && isPreRelease) return -1; |
278 | 279 |
279 var comparison = _compareLists(preRelease, other.preRelease); | 280 var comparison = _compareLists(preRelease, other.preRelease); |
280 if (comparison != 0) return comparison; | 281 if (comparison != 0) return comparison; |
281 | 282 |
282 // Builds always come after no build string. | 283 // Builds always come after no build string. |
283 if (build.isEmpty && other.build.isNotEmpty) return -1; | 284 if (build.isEmpty && other.build.isNotEmpty) return -1; |
284 if (other.build.isEmpty && build.isNotEmpty) return 1; | 285 if (other.build.isEmpty && build.isNotEmpty) return 1; |
285 return _compareLists(build, other.build); | 286 return _compareLists(build, other.build); |
287 } else { | |
288 return -other.compareTo(this); | |
289 } | |
286 } | 290 } |
287 | 291 |
288 String toString() => _text; | 292 String toString() => _text; |
289 | 293 |
290 /// Compares a dot-separated component of two versions. | 294 /// Compares a dot-separated component of two versions. |
291 /// | 295 /// |
292 /// This is used for the pre-release and build version parts. This follows | 296 /// This is used for the pre-release and build version parts. This follows |
293 /// Rule 12 of the Semantic Versioning spec (v2.0.0-rc.1). | 297 /// Rule 12 of the Semantic Versioning spec (v2.0.0-rc.1). |
294 int _compareLists(List a, List b) { | 298 int _compareLists(List a, List b) { |
295 for (var i = 0; i < math.max(a.length, b.length); i++) { | 299 for (var i = 0; i < math.max(a.length, b.length); i++) { |
(...skipping 22 matching lines...) Expand all Loading... | |
318 // Compare two strings. | 322 // Compare two strings. |
319 return aPart.compareTo(bPart); | 323 return aPart.compareTo(bPart); |
320 } | 324 } |
321 } | 325 } |
322 } | 326 } |
323 | 327 |
324 // The lists are entirely equal. | 328 // The lists are entirely equal. |
325 return 0; | 329 return 0; |
326 } | 330 } |
327 } | 331 } |
OLD | NEW |