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 23 matching lines...) Expand all Loading... |
34 /** The patch version number: "3" in "1.2.3". */ | 34 /** The patch version number: "3" in "1.2.3". */ |
35 final int patch; | 35 final int patch; |
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 ArgumentError( | 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 ArgumentError( | 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 ArgumentError( | 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".'); |
61 } | 61 } |
62 | 62 |
63 try { | 63 try { |
64 int major = parseInt(match[1]); | 64 int major = parseInt(match[1]); |
65 int minor = parseInt(match[2]); | 65 int minor = parseInt(match[2]); |
66 int patch = parseInt(match[3]); | 66 int patch = parseInt(match[3]); |
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, preRelease, 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 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 |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 * then this only allows versions that are at that minimum or greater. If there | 250 * then this only allows versions that are at that minimum or greater. If there |
251 * is a maximum, then only versions less than that are allowed. In other words, | 251 * is a maximum, then only versions less than that are allowed. In other words, |
252 * this allows `>= min, < max`. | 252 * this allows `>= min, < max`. |
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 ArgumentError( | 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 && |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 return const _EmptyVersion(); | 332 return const _EmptyVersion(); |
333 } | 333 } |
334 | 334 |
335 if (intersectMin != null && intersectMax != null && | 335 if (intersectMin != null && intersectMax != null && |
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(min: intersectMin, max: intersectMax, |
343 intersectIncludeMin, intersectIncludeMax); | 343 includeMin: intersectIncludeMin, includeMax: intersectIncludeMax); |
344 } | 344 } |
345 | 345 |
346 throw new ArgumentError( | 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) { |
(...skipping 66 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 |