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 25 matching lines...) Expand all Loading... |
36 | 36 |
37 /** The pre-release identifier: "foo" in "1.2.3-foo". May be `null`. */ | 37 /** The pre-release identifier: "foo" in "1.2.3-foo". May be `null`. */ |
38 final String preRelease; | 38 final String preRelease; |
39 | 39 |
40 /** The build identifier: "foo" in "1.2.3+foo". May be `null`. */ | 40 /** The build identifier: "foo" in "1.2.3+foo". May be `null`. */ |
41 final String build; | 41 final String build; |
42 | 42 |
43 /** Creates a new [Version] object. */ | 43 /** Creates a new [Version] object. */ |
44 Version(this.major, this.minor, this.patch, [String pre, this.build]) | 44 Version(this.major, this.minor, this.patch, [String pre, this.build]) |
45 : preRelease = pre { | 45 : preRelease = pre { |
46 if (major < 0) throw new IllegalArgumentException( | 46 if (major < 0) throw new ArgumentError( |
47 'Major version must be non-negative.'); | 47 'Major version must be non-negative.'); |
48 if (minor < 0) throw new IllegalArgumentException( | 48 if (minor < 0) throw new ArgumentError( |
49 'Minor version must be non-negative.'); | 49 'Minor version must be non-negative.'); |
50 if (patch < 0) throw new IllegalArgumentException( | 50 if (patch < 0) throw new ArgumentError( |
51 'Patch version must be non-negative.'); | 51 'Patch version must be non-negative.'); |
52 } | 52 } |
53 | 53 |
54 /** | 54 /** |
55 * Creates a new [Version] by parsing [text]. | 55 * Creates a new [Version] by parsing [text]. |
56 */ | 56 */ |
57 factory Version.parse(String text) { | 57 factory Version.parse(String text) { |
58 final match = _PARSE_REGEX.firstMatch(text); | 58 final match = _PARSE_REGEX.firstMatch(text); |
59 if (match == null) { | 59 if (match == null) { |
60 throw new FormatException('Could not parse "$text".'); | 60 throw new FormatException('Could not parse "$text".'); |
(...skipping 30 matching lines...) Expand all Loading... |
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 |
98 // Intersecting two versions only works if they are the same. | 98 // Intersecting two versions only works if they are the same. |
99 if (other is Version) return this == other ? this : const _EmptyVersion(); | 99 if (other is Version) return this == other ? this : const _EmptyVersion(); |
100 | 100 |
101 throw new IllegalArgumentException( | 101 throw new ArgumentError( |
102 'Unknown VersionConstraint type $other.'); | 102 'Unknown VersionConstraint type $other.'); |
103 } | 103 } |
104 | 104 |
105 int compareTo(Version other) { | 105 int compareTo(Version other) { |
106 if (major != other.major) return major.compareTo(other.major); | 106 if (major != other.major) return major.compareTo(other.major); |
107 if (minor != other.minor) return minor.compareTo(other.minor); | 107 if (minor != other.minor) return minor.compareTo(other.minor); |
108 if (patch != other.patch) return patch.compareTo(other.patch); | 108 if (patch != other.patch) return patch.compareTo(other.patch); |
109 | 109 |
110 if (preRelease != other.preRelease) { | 110 if (preRelease != other.preRelease) { |
111 // Pre-releases always come before no pre-release string. | 111 // Pre-releases always come before no pre-release string. |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 */ | 253 */ |
254 class VersionRange implements VersionConstraint { | 254 class VersionRange implements VersionConstraint { |
255 final Version min; | 255 final Version min; |
256 final Version max; | 256 final Version max; |
257 final bool includeMin; | 257 final bool includeMin; |
258 final bool includeMax; | 258 final bool includeMax; |
259 | 259 |
260 VersionRange([this.min, this.max, | 260 VersionRange([this.min, this.max, |
261 this.includeMin = false, this.includeMax = false]) { | 261 this.includeMin = false, this.includeMax = false]) { |
262 if (min != null && max != null && min > max) { | 262 if (min != null && max != null && min > max) { |
263 throw new IllegalArgumentException( | 263 throw new ArgumentError( |
264 'Minimum version ("$min") must be less than maximum ("$max").'); | 264 'Minimum version ("$min") must be less than maximum ("$max").'); |
265 } | 265 } |
266 } | 266 } |
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 && |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 intersectMin > intersectMax) { | 336 intersectMin > intersectMax) { |
337 // Non-overlapping ranges, so empty. | 337 // Non-overlapping ranges, so empty. |
338 return const _EmptyVersion(); | 338 return const _EmptyVersion(); |
339 } | 339 } |
340 | 340 |
341 // If we got here, there is an actual range. | 341 // If we got here, there is an actual range. |
342 return new VersionRange(intersectMin, intersectMax, | 342 return new VersionRange(intersectMin, intersectMax, |
343 intersectIncludeMin, intersectIncludeMax); | 343 intersectIncludeMin, intersectIncludeMax); |
344 } | 344 } |
345 | 345 |
346 throw new IllegalArgumentException( | 346 throw new ArgumentError( |
347 'Unknown VersionConstraint type $other.'); | 347 'Unknown VersionConstraint type $other.'); |
348 } | 348 } |
349 | 349 |
350 String toString() { | 350 String toString() { |
351 var buffer = new StringBuffer(); | 351 var buffer = new StringBuffer(); |
352 | 352 |
353 if (min != null) { | 353 if (min != null) { |
354 buffer.add(includeMin ? '>=' : '>'); | 354 buffer.add(includeMin ? '>=' : '>'); |
355 buffer.add(min); | 355 buffer.add(min); |
356 } | 356 } |
(...skipping 63 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 |