forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cartfile.swift
164 lines (138 loc) · 5.06 KB
/
Cartfile.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
import Foundation
import Result
/// The relative path to a project's checked out dependencies.
public let carthageProjectCheckoutsPath = "Carthage/Checkouts"
/// Represents a Cartfile, which is a specification of a project's dependencies
/// and any other settings Carthage needs to build it.
public struct Cartfile {
/// The dependencies listed in the Cartfile.
public var dependencies: [Dependency: VersionSpecifier]
public init(dependencies: [Dependency: VersionSpecifier] = [:]) {
self.dependencies = dependencies
}
/// Returns the location where Cartfile should exist within the given
/// directory.
public static func url(in directoryURL: URL) -> URL {
return directoryURL.appendingPathComponent("Cartfile")
}
/// Attempts to parse Cartfile information from a string.
public static func from(string: String) -> Result<Cartfile, CarthageError> {
var dependencies: [Dependency: VersionSpecifier] = [:]
var duplicates: [Dependency] = []
var result: Result<(), CarthageError> = .success(())
let commentIndicator = "#"
string.enumerateLines { line, stop in
let scanner = Scanner(string: line)
if scanner.scanString(commentIndicator, into: nil) {
// Skip the rest of the line.
return
}
if scanner.isAtEnd {
// The line was all whitespace.
return
}
switch Dependency.from(scanner).fanout(VersionSpecifier.from(scanner)) {
case let .success((dependency, version)):
if case .binary = dependency, case .gitReference = version {
result = .failure(
CarthageError.parseError(
description: "binary dependencies cannot have a git reference for the version specifier in line: \(scanner.currentLine)"
)
)
stop = true
return
}
if dependencies[dependency] == nil {
dependencies[dependency] = version
} else {
duplicates.append(dependency)
}
case let .failure(error):
result = .failure(CarthageError(scannableError: error))
stop = true
return
}
if scanner.scanString(commentIndicator, into: nil) {
// Skip the rest of the line.
return
}
if !scanner.isAtEnd {
result = .failure(CarthageError.parseError(description: "unexpected trailing characters in line: \(line)"))
stop = true
}
}
return result.flatMap { _ in
if !duplicates.isEmpty {
return .failure(.duplicateDependencies(duplicates.map { DuplicateDependency(dependency: $0, locations: []) }))
}
return .success(Cartfile(dependencies: dependencies))
}
}
/// Attempts to parse a Cartfile from a file at a given URL.
public static func from(file cartfileURL: URL) -> Result<Cartfile, CarthageError> {
return Result(attempt: { try String(contentsOf: cartfileURL, encoding: .utf8) })
.mapError { .readFailed(cartfileURL, $0) }
.flatMap(Cartfile.from(string:))
.mapError { error in
guard case let .duplicateDependencies(dupes) = error else { return error }
let dependencies = dupes
.map { dupe in
return DuplicateDependency(
dependency: dupe.dependency,
locations: [ cartfileURL.path ]
)
}
return .duplicateDependencies(dependencies)
}
}
/// Appends the contents of another Cartfile to that of the receiver.
public mutating func append(_ cartfile: Cartfile) {
for (dependency, version) in cartfile.dependencies {
dependencies[dependency] = version
}
}
}
/// Returns an array containing dependencies that are listed in both arguments.
public func duplicateDependenciesIn(_ cartfile1: Cartfile, _ cartfile2: Cartfile) -> [Dependency] {
let projects1 = cartfile1.dependencies.keys
let projects2 = cartfile2.dependencies.keys
return Array(Set(projects1).intersection(Set(projects2)))
}
/// Represents a parsed Cartfile.resolved, which specifies which exact version was
/// checked out for each dependency.
public struct ResolvedCartfile {
/// The dependencies listed in the Cartfile.resolved.
public var dependencies: [Dependency: PinnedVersion]
public init(dependencies: [Dependency: PinnedVersion]) {
self.dependencies = dependencies
}
/// Returns the location where Cartfile.resolved should exist within the given
/// directory.
public static func url(in directoryURL: URL) -> URL {
return directoryURL.appendingPathComponent("Cartfile.resolved")
}
/// Attempts to parse Cartfile.resolved information from a string.
public static func from(string: String) -> Result<ResolvedCartfile, CarthageError> {
var cartfile = self.init(dependencies: [:])
var result: Result<(), CarthageError> = .success(())
let scanner = Scanner(string: string)
scannerLoop: while !scanner.isAtEnd {
switch Dependency.from(scanner).fanout(PinnedVersion.from(scanner)) {
case let .success((dep, version)):
cartfile.dependencies[dep] = version
case let .failure(error):
result = .failure(CarthageError(scannableError: error))
break scannerLoop
}
}
return result.map { _ in cartfile }
}
}
extension ResolvedCartfile: CustomStringConvertible {
public var description: String {
return dependencies
.sorted { $0.key.description < $1.key.description }
.map { "\($0.key) \($0.value)\n" }
.joined(separator: "")
}
}