forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dependency.swift
217 lines (177 loc) · 6.32 KB
/
Dependency.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
import Foundation
import Result
import Tentacle
/// Uniquely identifies a Binary Spec's resolved URL and its description
public struct BinaryURL: CustomStringConvertible {
/// A Resolved URL
public let url: URL
/// A custom description
public let resolvedDescription: String
public var description: String {
return resolvedDescription
}
init(url: URL, resolvedDescription: String) {
self.url = url
self.resolvedDescription = resolvedDescription
}
}
/// Uniquely identifies a project that can be used as a dependency.
public enum Dependency: Hashable {
/// A repository hosted on GitHub.com or GitHub Enterprise.
case gitHub(Server, Repository)
/// An arbitrary Git repository.
case git(GitURL)
/// A binary-only framework
case binary(BinaryURL)
/// The unique, user-visible name for this project.
public var name: String {
switch self {
case let .gitHub(_, repo):
return repo.name
case let .git(url):
return url.name ?? url.urlString
case let .binary(url):
return url.name
}
}
/// The path at which this project will be checked out, relative to the
/// working directory of the main project.
public var relativePath: String {
return (Constants.checkoutsFolderPath as NSString).appendingPathComponent(name)
}
public var xcframeworkPath: String {
return (Constants.binariesFolderPath as NSString).appendingPathComponent("\(name).xcframework")
}
}
extension Dependency {
fileprivate init(gitURL: GitURL) {
let githubHostIdentifier = "github.com"
let urlString = gitURL.urlString
if urlString.contains(githubHostIdentifier) {
let gitbubHostScanner = Scanner(string: urlString)
gitbubHostScanner.scanUpTo(githubHostIdentifier, into: nil)
gitbubHostScanner.scanString(githubHostIdentifier, into: nil)
// find an SCP or URL path separator
let separatorFound = (gitbubHostScanner.scanString("/", into: nil) || gitbubHostScanner.scanString(":", into: nil)) && !gitbubHostScanner.isAtEnd
let startOfOwnerAndNameSubstring = gitbubHostScanner.scanLocation
if separatorFound && startOfOwnerAndNameSubstring <= urlString.utf16.count {
let ownerAndNameSubstring = String(urlString[urlString.index(urlString.startIndex, offsetBy: startOfOwnerAndNameSubstring)..<urlString.endIndex])
switch Repository.fromIdentifier(ownerAndNameSubstring as String) {
case .success(let server, let repository):
self = Dependency.gitHub(server, repository)
default:
self = Dependency.git(gitURL)
}
return
}
}
self = Dependency.git(gitURL)
}
}
extension Dependency: Comparable {
public static func < (_ lhs: Dependency, _ rhs: Dependency) -> Bool {
return lhs.name.caseInsensitiveCompare(rhs.name) == .orderedAscending
}
}
extension Dependency: Scannable {
/// Attempts to parse a Dependency.
public static func from(_ scanner: Scanner) -> Result<Dependency, ScannableError> {
return from(scanner, base: nil)
}
public static func from(_ scanner: Scanner, base: URL? = nil) -> Result<Dependency, ScannableError> {
let parser: (String) -> Result<Dependency, ScannableError>
if scanner.scanString("github", into: nil) {
parser = { repoIdentifier in
return Repository.fromIdentifier(repoIdentifier).map { self.gitHub($0, $1) }
}
} else if scanner.scanString("git", into: nil) {
parser = { urlString in
return .success(Dependency(gitURL: GitURL(urlString)))
}
} else if scanner.scanString("binary", into: nil) {
parser = { urlString in
if let url = URL(string: urlString) {
if url.scheme == "https" || url.scheme == "file" {
return .success(self.binary(BinaryURL(url: url, resolvedDescription: url.description)))
} else if url.scheme == nil {
// This can use URL.init(fileURLWithPath:isDirectory:relativeTo:) once we can target 10.11+
let absoluteURL = url.relativePath
.withCString { URL(fileURLWithFileSystemRepresentation: $0, isDirectory: false, relativeTo: base) }
.standardizedFileURL
return .success(self.binary(BinaryURL(url: absoluteURL, resolvedDescription: url.absoluteString)))
} else {
return .failure(ScannableError(message: "non-https, non-file URL found for dependency type `binary`", currentLine: scanner.currentLine))
}
} else {
return .failure(ScannableError(message: "invalid URL found for dependency type `binary`", currentLine: scanner.currentLine))
}
}
} else {
return .failure(ScannableError(message: "unexpected dependency type", currentLine: scanner.currentLine))
}
if !scanner.scanString("\"", into: nil) {
return .failure(ScannableError(message: "expected string after dependency type", currentLine: scanner.currentLine))
}
var address: NSString?
if !scanner.scanUpTo("\"", into: &address) || !scanner.scanString("\"", into: nil) {
return .failure(ScannableError(message: "empty or unterminated string after dependency type", currentLine: scanner.currentLine))
}
if let address = address {
return parser(address as String)
} else {
return .failure(ScannableError(message: "empty string after dependency type", currentLine: scanner.currentLine))
}
}
}
extension Dependency: CustomStringConvertible {
public var description: String {
switch self {
case let .gitHub(server, repo):
let repoDescription: String
switch server {
case .dotCom:
repoDescription = "\(repo.owner)/\(repo.name)"
case .enterprise:
repoDescription = "\(server.url(for: repo))"
}
return "github \"\(repoDescription)\""
case let .git(url):
return "git \"\(url)\""
case let .binary(binary):
return "binary \"\(binary)\""
}
}
}
extension Dependency {
/// Returns the URL that the dependency's remote repository exists at.
func gitURL(preferHTTPS: Bool) -> GitURL? {
switch self {
case let .gitHub(server, repository):
if preferHTTPS {
return server.httpsURL(for: repository)
} else {
return server.sshURL(for: repository)
}
case let .git(url):
return url
case .binary:
return nil
}
}
}
extension BinaryURL: Equatable {
public static func == (_ lhs: BinaryURL, _ rhs: BinaryURL) -> Bool {
return lhs.description == rhs.description
}
}
extension BinaryURL: Hashable {
public func hash(into hasher: inout Hasher) {
hasher.combine(description)
}
}
extension BinaryURL {
/// The unique, user-visible name for this project.
public var name: String {
return url.lastPathComponent.stripping(suffix: ".json")
}
}