forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Version.swift
395 lines (320 loc) · 12.1 KB
/
Version.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// Version.swift
// Carthage
//
// Created by Justin Spahr-Summers on 2014-11-08.
// Copyright (c) 2014 Carthage. All rights reserved.
//
import Foundation
import Result
import ReactiveCocoa
/// An abstract type representing a way to specify versions.
public protocol VersionType: Equatable {}
/// A semantic version.
public struct SemanticVersion: VersionType, Comparable {
/// The major version.
///
/// Increments to this component represent incompatible API changes.
public let major: Int
/// The minor version.
///
/// Increments to this component represent backwards-compatible
/// enhancements.
public let minor: Int
/// The patch version.
///
/// Increments to this component represent backwards-compatible bug fixes.
public let patch: Int
/// The pin from which this semantic version was derived.
public var pinnedVersion: PinnedVersion?
/// A list of the version components, in order from most significant to
/// least significant.
public var components: [Int] {
return [ major, minor, patch ]
}
public init(major: Int, minor: Int, patch: Int) {
self.major = major
self.minor = minor
self.patch = patch
}
/// The set of all characters present in valid semantic versions.
private static let versionCharacterSet = NSCharacterSet(charactersInString: "0123456789.")
/// Attempts to parse a semantic version from a PinnedVersion.
public static func fromPinnedVersion(pinnedVersion: PinnedVersion) -> Result<SemanticVersion, CarthageError> {
let scanner = NSScanner(string: pinnedVersion.commitish)
// Skip leading characters, like "v" or "version-" or anything like
// that.
scanner.scanUpToCharactersFromSet(versionCharacterSet, intoString: nil)
return self.fromScanner(scanner).flatMap { version in
if scanner.atEnd {
var version = version
version.pinnedVersion = pinnedVersion
return .Success(version)
} else {
// Disallow versions like "1.0a5", because we only support
// SemVer right now.
return .Failure(CarthageError.ParseError(description: "syntax of version \"\(version)\" is unsupported"))
}
}
}
}
extension SemanticVersion: Scannable {
/// Attempts to parse a semantic version from a human-readable string of the
/// form "a.b.c".
static public func fromScanner(scanner: NSScanner) -> Result<SemanticVersion, CarthageError> {
var version: NSString? = nil
if !scanner.scanCharactersFromSet(versionCharacterSet, intoString: &version) || version == nil {
return .Failure(CarthageError.ParseError(description: "expected version in line: \(scanner.currentLine)"))
}
let components = (version! as String).characters.split(allowEmptySlices: false) { $0 == "." }.map(String.init)
if components.count == 0 {
return .Failure(CarthageError.ParseError(description: "expected version in line: \(scanner.currentLine)"))
}
let major = Int(components[0])
if major == nil {
return .Failure(CarthageError.ParseError(description: "expected major version number in \"\(version!)\""))
}
let minor = (components.count > 1 ? Int(components[1]) : nil)
if minor == nil {
return .Failure(CarthageError.ParseError(description: "expected minor version number in \"\(version!)\""))
}
let patch = (components.count > 2 ? Int(components[2]) : 0)
return .Success(self.init(major: major!, minor: minor ?? 0, patch: patch ?? 0))
}
}
public func <(lhs: SemanticVersion, rhs: SemanticVersion) -> Bool {
return lhs.components.lexicographicalCompare(rhs.components)
}
public func ==(lhs: SemanticVersion, rhs: SemanticVersion) -> Bool {
return lhs.components == rhs.components
}
extension SemanticVersion: Hashable {
public var hashValue: Int {
return components.reduce(0) { $0 ^ $1.hashValue }
}
}
extension SemanticVersion: CustomStringConvertible {
public var description: String {
return components.map { $0.description }.joinWithSeparator(".")
}
}
/// An immutable version that a project can be pinned to.
public struct PinnedVersion: VersionType {
/// The commit SHA, or name of the tag, to pin to.
public let commitish: String
public init(_ commitish: String) {
self.commitish = commitish
}
}
public func ==(lhs: PinnedVersion, rhs: PinnedVersion) -> Bool {
return lhs.commitish == rhs.commitish
}
extension PinnedVersion: Scannable {
public static func fromScanner(scanner: NSScanner) -> Result<PinnedVersion, CarthageError> {
if !scanner.scanString("\"", intoString: nil) {
return .Failure(CarthageError.ParseError(description: "expected pinned version in line: \(scanner.currentLine)"))
}
var commitish: NSString? = nil
if !scanner.scanUpToString("\"", intoString: &commitish) || commitish == nil {
return .Failure(CarthageError.ParseError(description: "empty pinned version in line: \(scanner.currentLine)"))
}
if !scanner.scanString("\"", intoString: nil) {
return .Failure(CarthageError.ParseError(description: "unterminated pinned version in line: \(scanner.currentLine)"))
}
return .Success(self.init(commitish! as String))
}
}
extension PinnedVersion: CustomStringConvertible {
public var description: String {
return "\"\(commitish)\""
}
}
/// Describes which versions are acceptable for satisfying a dependency
/// requirement.
public enum VersionSpecifier: VersionType {
case Any
case AtLeast(SemanticVersion)
case CompatibleWith(SemanticVersion)
case Exactly(SemanticVersion)
case GitReference(String)
/// Determines whether the given version satisfies this version specifier.
public func satisfiedBy(version: PinnedVersion) -> Bool {
func withSemanticVersion(predicate: SemanticVersion -> Bool) -> Bool {
if let semanticVersion = SemanticVersion.fromPinnedVersion(version).value {
return predicate(semanticVersion)
} else {
// Consider non-semantic versions (e.g., branches) to meet every
// version range requirement.
return true
}
}
switch self {
case .Any, .GitReference:
return true
case let .Exactly(requirement):
return withSemanticVersion { $0 == requirement }
case let .AtLeast(requirement):
return withSemanticVersion { $0 >= requirement }
case let .CompatibleWith(requirement):
return withSemanticVersion { version in
// According to SemVer, any 0.x.y release may completely break the
// exported API, so it's not safe to consider them compatible with one
// another. Only patch versions are compatible under 0.x, meaning 0.1.1 is
// compatible with 0.1.2, but not 0.2. This isn't according to the SemVer
// spec but keeps ~> useful for 0.x.y versions.
if version.major == 0 {
return version.minor == requirement.minor && version >= requirement
}
return version.major == requirement.major && version >= requirement
}
}
}
}
public func ==(lhs: VersionSpecifier, rhs: VersionSpecifier) -> Bool {
switch (lhs, rhs) {
case (.Any, .Any):
return true
case let (.Exactly(left), .Exactly(right)):
return left == right
case let (.AtLeast(left), .AtLeast(right)):
return left == right
case let (.CompatibleWith(left), .CompatibleWith(right)):
return left == right
case let (.GitReference(left), .GitReference(right)):
return left == right
default:
return false
}
}
extension VersionSpecifier: Scannable {
/// Attempts to parse a VersionSpecifier.
public static func fromScanner(scanner: NSScanner) -> Result<VersionSpecifier, CarthageError> {
if scanner.scanString("==", intoString: nil) {
return SemanticVersion.fromScanner(scanner).map { Exactly($0) }
} else if scanner.scanString(">=", intoString: nil) {
return SemanticVersion.fromScanner(scanner).map { AtLeast($0) }
} else if scanner.scanString("~>", intoString: nil) {
return SemanticVersion.fromScanner(scanner).map { CompatibleWith($0) }
} else if scanner.scanString("\"", intoString: nil) {
var refName: NSString? = nil
if !scanner.scanUpToString("\"", intoString: &refName) || refName == nil {
return .Failure(CarthageError.ParseError(description: "expected Git reference name in line: \(scanner.currentLine)"))
}
if !scanner.scanString("\"", intoString: nil) {
return .Failure(CarthageError.ParseError(description: "unterminated Git reference name in line: \(scanner.currentLine)"))
}
return .Success(.GitReference(refName! as String))
} else {
return .Success(Any)
}
}
}
extension VersionSpecifier: CustomStringConvertible {
public var description: String {
switch self {
case .Any:
return ""
case let .Exactly(version):
return "== \(version)"
case let .AtLeast(version):
return ">= \(version)"
case let .CompatibleWith(version):
return "~> \(version)"
case let .GitReference(refName):
return "\"\(refName)\""
}
}
}
private func intersection(atLeast atLeast: SemanticVersion, compatibleWith: SemanticVersion) -> VersionSpecifier? {
if atLeast.major > compatibleWith.major {
return nil
} else if atLeast.major < compatibleWith.major {
return .CompatibleWith(compatibleWith)
} else {
return .CompatibleWith(max(atLeast, compatibleWith))
}
}
private func intersection(atLeast atLeast: SemanticVersion, exactly: SemanticVersion) -> VersionSpecifier? {
if atLeast > exactly {
return nil
}
return .Exactly(exactly)
}
private func intersection(compatibleWith compatibleWith: SemanticVersion, exactly: SemanticVersion) -> VersionSpecifier? {
if exactly.major != compatibleWith.major || compatibleWith > exactly {
return nil
}
return .Exactly(exactly)
}
/// Attempts to determine a version specifier that accurately describes the
/// intersection between the two given specifiers.
///
/// In other words, any version that satisfies the returned specifier will
/// satisfy _both_ of the given specifiers.
public func intersection(lhs: VersionSpecifier, _ rhs: VersionSpecifier) -> VersionSpecifier? {
switch (lhs, rhs) {
// Unfortunately, patterns with a wildcard _ are not considered exhaustive,
// so do the same thing manually.
case (.Any, .Any), (.Any, .AtLeast), (.Any, .CompatibleWith), (.Any, .Exactly):
return rhs
case (.AtLeast, .Any), (.CompatibleWith, .Any), (.Exactly, .Any):
return lhs
case (.GitReference, .Any), (.GitReference, .AtLeast), (.GitReference, .CompatibleWith), (.GitReference, .Exactly):
return lhs
case (.Any, .GitReference), (.AtLeast, .GitReference), (.CompatibleWith, .GitReference), (.Exactly, .GitReference):
return rhs
case let (.GitReference(lv), .GitReference(rv)):
if lv != rv {
return nil
}
return lhs
case let (.AtLeast(lv), .AtLeast(rv)):
return .AtLeast(max(lv, rv))
case let (.AtLeast(lv), .CompatibleWith(rv)):
return intersection(atLeast: lv, compatibleWith: rv)
case let (.AtLeast(lv), .Exactly(rv)):
return intersection(atLeast: lv, exactly: rv)
case let (.CompatibleWith(lv), .AtLeast(rv)):
return intersection(atLeast: rv, compatibleWith: lv)
case let (.CompatibleWith(lv), .CompatibleWith(rv)):
if lv.major != rv.major {
return nil
}
// According to SemVer, any 0.x.y release may completely break the
// exported API, so it's not safe to consider them compatible with one
// another. Only patch versions are compatible under 0.x, meaning 0.1.1 is
// compatible with 0.1.2, but not 0.2. This isn't according to the SemVer
// spec but keeps ~> useful for 0.x.y versions.
if lv.major == 0 && rv.major == 0 {
if lv.minor != rv.minor {
return nil
}
}
return .CompatibleWith(max(lv, rv))
case let (.CompatibleWith(lv), .Exactly(rv)):
return intersection(compatibleWith: lv, exactly: rv)
case let (.Exactly(lv), .AtLeast(rv)):
return intersection(atLeast: rv, exactly: lv)
case let (.Exactly(lv), .CompatibleWith(rv)):
return intersection(compatibleWith: rv, exactly: lv)
case let (.Exactly(lv), .Exactly(rv)):
if lv != rv {
return nil
}
return lhs
}
}
/// Attempts to determine a version specifier that accurately describes the
/// intersection between the given specifiers.
///
/// In other words, any version that satisfies the returned specifier will
/// satisfy _all_ of the given specifiers.
public func intersection<S: SequenceType where S.Generator.Element == VersionSpecifier>(specs: S) -> VersionSpecifier? {
return specs.reduce(nil) { (left: VersionSpecifier?, right: VersionSpecifier) -> VersionSpecifier? in
if let left = left {
return intersection(left, right)
} else {
return right
}
}
}