| 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('pub_version'); | 10 #library('pub_version'); |
| 11 | 11 |
| 12 #import('utils.dart'); |
| 13 |
| 12 /** A parsed semantic version number. */ | 14 /** A parsed semantic version number. */ |
| 13 class Version implements Comparable, VersionConstraint { | 15 class Version implements Comparable, Hashable, VersionConstraint { |
| 16 static get none() => new Version(0, 0, 0); |
| 17 |
| 14 static final _PARSE_REGEX = const RegExp( | 18 static final _PARSE_REGEX = const RegExp( |
| 15 @'^' // Start at beginning. | 19 @'^' // Start at beginning. |
| 16 @'(\d+).(\d+).(\d+)' // Version number. | 20 @'(\d+).(\d+).(\d+)' // Version number. |
| 17 @'(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Pre-release. | 21 @'(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Pre-release. |
| 18 @'(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Build. | 22 @'(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?' // Build. |
| 19 @'$'); // Consume entire string. | 23 @'$'); // Consume entire string. |
| 20 | 24 |
| 21 /** The major version number: "1" in "1.2.3". */ | 25 /** The major version number: "1" in "1.2.3". */ |
| 22 final int major; | 26 final int major; |
| 23 | 27 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // Builds always come after no build string. | 101 // Builds always come after no build string. |
| 98 if (build == null) return -1; | 102 if (build == null) return -1; |
| 99 if (other.build == null) return 1; | 103 if (other.build == null) return 1; |
| 100 | 104 |
| 101 return _compareStrings(build, other.build); | 105 return _compareStrings(build, other.build); |
| 102 } | 106 } |
| 103 | 107 |
| 104 return 0; | 108 return 0; |
| 105 } | 109 } |
| 106 | 110 |
| 111 int hashCode() => toString().hashCode(); |
| 112 |
| 107 String toString() { | 113 String toString() { |
| 108 var buffer = new StringBuffer(); | 114 var buffer = new StringBuffer(); |
| 109 buffer.add('$major.$minor.$patch'); | 115 buffer.add('$major.$minor.$patch'); |
| 110 if (preRelease != null) buffer.add('-$preRelease'); | 116 if (preRelease != null) buffer.add('-$preRelease'); |
| 111 if (build != null) buffer.add('+$build'); | 117 if (build != null) buffer.add('+$build'); |
| 112 return buffer.toString(); | 118 return buffer.toString(); |
| 113 } | 119 } |
| 114 | 120 |
| 115 /** | 121 /** |
| 116 * Compares the string part of two versions. This is used for the pre-release | 122 * Compares the string part of two versions. This is used for the pre-release |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 200 } |
| 195 } | 201 } |
| 196 | 202 |
| 197 /** Tests if [other] matches falls within this version range. */ | 203 /** Tests if [other] matches falls within this version range. */ |
| 198 bool allows(Version other) { | 204 bool allows(Version other) { |
| 199 if (min != null && other < min) return false; | 205 if (min != null && other < min) return false; |
| 200 if (max != null && other >= max) return false; | 206 if (max != null && other >= max) return false; |
| 201 return true; | 207 return true; |
| 202 } | 208 } |
| 203 } | 209 } |
| 204 | |
| 205 /** Thrown by [Version.parse()] if the argument isn't a valid version string. */ | |
| 206 class FormatException implements Exception { | |
| 207 final String message; | |
| 208 | |
| 209 FormatException(this.message); | |
| 210 | |
| 211 String toString() => message; | |
| 212 } | |
| OLD | NEW |