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'); |
| 13 |
12 #import('utils.dart'); | 14 #import('utils.dart'); |
13 | 15 |
14 /** A parsed semantic version number. */ | 16 /** A parsed semantic version number. */ |
15 class Version implements Comparable, Hashable, VersionConstraint { | 17 class Version implements Comparable, Hashable, VersionConstraint { |
16 /** No released version: i.e. "0.0.0". */ | 18 /** No released version: i.e. "0.0.0". */ |
17 static Version get none() => new Version(0, 0, 0); | 19 static Version get none() => new Version(0, 0, 0); |
18 | 20 |
19 static final _PARSE_REGEX = const RegExp( | 21 static final _PARSE_REGEX = const RegExp( |
20 @'^' // Start at beginning. | 22 @'^' // Start at beginning. |
21 @'(\d+).(\d+).(\d+)' // Version number. | 23 @'(\d+).(\d+).(\d+)' // Version number. |
(...skipping 30 matching lines...) Expand all Loading... |
52 /** | 54 /** |
53 * Creates a new [Version] by parsing [text]. | 55 * Creates a new [Version] by parsing [text]. |
54 */ | 56 */ |
55 factory Version.parse(String text) { | 57 factory Version.parse(String text) { |
56 final match = _PARSE_REGEX.firstMatch(text); | 58 final match = _PARSE_REGEX.firstMatch(text); |
57 if (match == null) { | 59 if (match == null) { |
58 throw new FormatException('Could not parse "$text".'); | 60 throw new FormatException('Could not parse "$text".'); |
59 } | 61 } |
60 | 62 |
61 try { | 63 try { |
62 int major = Math.parseInt(match[1]); | 64 int major = parseInt(match[1]); |
63 int minor = Math.parseInt(match[2]); | 65 int minor = parseInt(match[2]); |
64 int patch = Math.parseInt(match[3]); | 66 int patch = parseInt(match[3]); |
65 | 67 |
66 String preRelease = match[5]; | 68 String preRelease = match[5]; |
67 String build = match[8]; | 69 String build = match[8]; |
68 | 70 |
69 return new Version(major, minor, patch, preRelease, build); | 71 return new Version(major, minor, patch, preRelease, build); |
70 } catch (FormatException ex) { | 72 } catch (FormatException ex) { |
71 throw new FormatException('Could not parse "$text".'); | 73 throw new FormatException('Could not parse "$text".'); |
72 } | 74 } |
73 } | 75 } |
74 | 76 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 | 138 |
137 /** | 139 /** |
138 * Compares the string part of two versions. This is used for the pre-release | 140 * Compares the string part of two versions. This is used for the pre-release |
139 * and build version parts. This follows Rule 12. of the Semantic Versioning | 141 * and build version parts. This follows Rule 12. of the Semantic Versioning |
140 * spec. | 142 * spec. |
141 */ | 143 */ |
142 int _compareStrings(String a, String b) { | 144 int _compareStrings(String a, String b) { |
143 var aParts = _splitParts(a); | 145 var aParts = _splitParts(a); |
144 var bParts = _splitParts(b); | 146 var bParts = _splitParts(b); |
145 | 147 |
146 for (int i = 0; i < Math.max(aParts.length, bParts.length); i++) { | 148 for (int i = 0; i < max(aParts.length, bParts.length); i++) { |
147 var aPart = (i < aParts.length) ? aParts[i] : null; | 149 var aPart = (i < aParts.length) ? aParts[i] : null; |
148 var bPart = (i < bParts.length) ? bParts[i] : null; | 150 var bPart = (i < bParts.length) ? bParts[i] : null; |
149 | 151 |
150 if (aPart != bPart) { | 152 if (aPart != bPart) { |
151 // Missing parts come before present ones. | 153 // Missing parts come before present ones. |
152 if (aPart == null) return -1; | 154 if (aPart == null) return -1; |
153 if (bPart == null) return 1; | 155 if (bPart == null) return 1; |
154 | 156 |
155 if (aPart is int) { | 157 if (aPart is int) { |
156 if (bPart is int) { | 158 if (bPart is int) { |
(...skipping 16 matching lines...) Expand all Loading... |
173 } | 175 } |
174 } | 176 } |
175 | 177 |
176 /** | 178 /** |
177 * Splits a string of dot-delimited identifiers into their component parts. | 179 * Splits a string of dot-delimited identifiers into their component parts. |
178 * Identifiers that are numeric are converted to numbers. | 180 * Identifiers that are numeric are converted to numbers. |
179 */ | 181 */ |
180 List _splitParts(String text) { | 182 List _splitParts(String text) { |
181 return text.split('.').map((part) { | 183 return text.split('.').map((part) { |
182 try { | 184 try { |
183 return Math.parseInt(part); | 185 return parseInt(part); |
184 } catch (FormatException ex) { | 186 } catch (FormatException ex) { |
185 // Not a number. | 187 // Not a number. |
186 return part; | 188 return part; |
187 } | 189 } |
188 }); | 190 }); |
189 } | 191 } |
190 } | 192 } |
191 | 193 |
192 /** | 194 /** |
193 * A [VersionConstraint] is a predicate that can determine whether a given | 195 * A [VersionConstraint] is a predicate that can determine whether a given |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 case '<': return new VersionRange(max: version, includeMax: false); | 420 case '<': return new VersionRange(max: version, includeMax: false); |
419 case '>=': return new VersionRange(min: version, includeMin: true); | 421 case '>=': return new VersionRange(min: version, includeMin: true); |
420 case '>': return new VersionRange(min: version, includeMin: false); | 422 case '>': return new VersionRange(min: version, includeMin: false); |
421 } | 423 } |
422 } | 424 } |
423 | 425 |
424 // Otherwise, it must be an explicit version. | 426 // Otherwise, it must be an explicit version. |
425 return new Version.parse(text); | 427 return new Version.parse(text); |
426 } | 428 } |
427 } | 429 } |
OLD | NEW |