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 'patterns.dart'; | 5 import 'patterns.dart'; |
6 import 'version.dart'; | 6 import 'version.dart'; |
7 import 'version_range.dart'; | 7 import 'version_range.dart'; |
8 import 'version_union.dart'; | 8 import 'version_union.dart'; |
| 9 import 'utils.dart'; |
9 | 10 |
10 /// A [VersionConstraint] is a predicate that can determine whether a given | 11 /// A [VersionConstraint] is a predicate that can determine whether a given |
11 /// version is valid or not. | 12 /// version is valid or not. |
12 /// | 13 /// |
13 /// For example, a ">= 2.0.0" constraint allows any version that is "2.0.0" or | 14 /// For example, a ">= 2.0.0" constraint allows any version that is "2.0.0" or |
14 /// greater. Version objects themselves implement this to match a specific | 15 /// greater. Version objects themselves implement this to match a specific |
15 /// version. | 16 /// version. |
16 abstract class VersionConstraint { | 17 abstract class VersionConstraint { |
17 /// A [VersionConstraint] that allows all versions. | 18 /// A [VersionConstraint] that allows all versions. |
18 static VersionConstraint any = new VersionRange(); | 19 static VersionConstraint any = new VersionRange(); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 constraint = constraint.intersect(other); | 166 constraint = constraint.intersect(other); |
166 } | 167 } |
167 return constraint; | 168 return constraint; |
168 } | 169 } |
169 | 170 |
170 /// Creates a new version constraint that is the union of [constraints]. | 171 /// Creates a new version constraint that is the union of [constraints]. |
171 /// | 172 /// |
172 /// It allows any versions that any of those constraints allows. If | 173 /// It allows any versions that any of those constraints allows. If |
173 /// [constraints] is empty, this returns a constraint that allows no versions. | 174 /// [constraints] is empty, this returns a constraint that allows no versions. |
174 factory VersionConstraint.unionOf( | 175 factory VersionConstraint.unionOf( |
175 Iterable<VersionConstraint> constraints) => | 176 Iterable<VersionConstraint> constraints) { |
176 VersionUnion.create(constraints); | 177 var flattened = constraints.expand((constraint) { |
| 178 if (constraint.isEmpty) return []; |
| 179 if (constraint is VersionUnion) return constraint.ranges; |
| 180 return [constraint]; |
| 181 }).toList(); |
| 182 |
| 183 if (flattened.isEmpty) return VersionConstraint.empty; |
| 184 |
| 185 if (flattened.any((constraint) => constraint.isAny)) { |
| 186 return VersionConstraint.any; |
| 187 } |
| 188 |
| 189 // Only allow Versions and VersionRanges here so we can more easily reason |
| 190 // about everything in [flattened]. _EmptyVersions and VersionUnions are |
| 191 // filtered out above. |
| 192 for (var constraint in flattened) { |
| 193 if (constraint is VersionRange) continue; |
| 194 throw new ArgumentError('Unknown VersionConstraint type $constraint.'); |
| 195 } |
| 196 |
| 197 flattened.sort(compareMax); |
| 198 |
| 199 var merged = <VersionRange>[]; |
| 200 for (var constraint in flattened) { |
| 201 // Merge this constraint with the previous one, but only if they touch. |
| 202 if (merged.isEmpty || |
| 203 (!merged.last.allowsAny(constraint) && |
| 204 !areAdjacent(merged.last, constraint))) { |
| 205 merged.add(constraint); |
| 206 } else { |
| 207 merged[merged.length - 1] = merged.last.union(constraint); |
| 208 } |
| 209 } |
| 210 |
| 211 if (merged.length == 1) return merged.single; |
| 212 return new VersionUnion.fromRanges(merged); |
| 213 } |
177 | 214 |
178 /// Returns `true` if this constraint allows no versions. | 215 /// Returns `true` if this constraint allows no versions. |
179 bool get isEmpty; | 216 bool get isEmpty; |
180 | 217 |
181 /// Returns `true` if this constraint allows all versions. | 218 /// Returns `true` if this constraint allows all versions. |
182 bool get isAny; | 219 bool get isAny; |
183 | 220 |
184 /// Returns `true` if this constraint allows [version]. | 221 /// Returns `true` if this constraint allows [version]. |
185 bool allows(Version version); | 222 bool allows(Version version); |
186 | 223 |
(...skipping 27 matching lines...) Expand all Loading... |
214 String toString() => '<empty>'; | 251 String toString() => '<empty>'; |
215 } | 252 } |
216 | 253 |
217 class _CompatibleWithVersionRange extends VersionRange { | 254 class _CompatibleWithVersionRange extends VersionRange { |
218 _CompatibleWithVersionRange(Version version) : super( | 255 _CompatibleWithVersionRange(Version version) : super( |
219 min: version, includeMin: true, | 256 min: version, includeMin: true, |
220 max: version.nextBreaking, includeMax: false); | 257 max: version.nextBreaking, includeMax: false); |
221 | 258 |
222 String toString() => '^$min'; | 259 String toString() => '^$min'; |
223 } | 260 } |
OLD | NEW |