forked from tuist/tuist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GraphLinter.swift
365 lines (328 loc) · 18.4 KB
/
GraphLinter.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
import Foundation
import TuistCore
protocol GraphLinting: AnyObject {
func lint(graph: Graphing) -> [LintingIssue]
}
class GraphLinter: GraphLinting {
// MARK: - Attributes
let projectLinter: ProjectLinting
let fileHandler: FileHandling
// MARK: - Init
init(projectLinter: ProjectLinting = ProjectLinter(),
fileHandler: FileHandling = FileHandler()) {
self.projectLinter = projectLinter
self.fileHandler = fileHandler
}
struct StaticDepedencyWarning: Hashable {
let fromTargetNode: TargetNode
let toTargetNode: TargetNode
func hash(into hasher: inout Hasher) {
hasher.combine(toTargetNode)
}
static func == (lhs: StaticDepedencyWarning, rhs: StaticDepedencyWarning) -> Bool {
return lhs.toTargetNode == rhs.toTargetNode
}
}
// MARK: - GraphLinting
func lint(graph: Graphing) -> [LintingIssue] {
var issues: [LintingIssue] = []
issues.append(contentsOf: graph.projects.flatMap(projectLinter.lint))
issues.append(contentsOf: lintDependencies(graph: graph))
issues.append(contentsOf: lintMismatchingConfigurations(graph: graph))
return issues
}
// MARK: - Fileprivate
func lintDependencies(graph: Graphing) -> [LintingIssue] {
var issues: [LintingIssue] = []
var evaluatedNodes: [GraphNode] = []
var linkedStaticProducts = Set<StaticDepedencyWarning>()
graph.entryNodes.forEach {
issues.append(contentsOf: lintGraphNode(node: $0,
evaluatedNodes: &evaluatedNodes,
linkedStaticProducts: &linkedStaticProducts))
}
issues.append(contentsOf: lintCarthageDependencies(graph: graph))
return issues
}
private func lintCarthageDependencies(graph: Graphing) -> [LintingIssue] {
let frameworks = graph.frameworks
let carthageFrameworks = frameworks.filter { $0.isCarthage }
let nonCarthageFrameworks = frameworks.filter { !$0.isCarthage }
let carthageIssues = carthageFrameworks
.filter { !fileHandler.exists($0.path) }
.map { LintingIssue(reason: "Framework not found at path \($0.path.pathString). The path might be wrong or Carthage dependencies not fetched", severity: .warning) }
let nonCarthageIssues = nonCarthageFrameworks
.filter { !fileHandler.exists($0.path) }
.map { LintingIssue(reason: "Framework not found at path \($0.path.pathString)", severity: .error) }
var issues: [LintingIssue] = []
issues.append(contentsOf: carthageIssues)
issues.append(contentsOf: nonCarthageIssues)
return issues
}
private func lintGraphNode(node: GraphNode,
evaluatedNodes: inout [GraphNode],
linkedStaticProducts: inout Set<StaticDepedencyWarning>) -> [LintingIssue] {
var issues: [LintingIssue] = []
defer { evaluatedNodes.append(node) }
if evaluatedNodes.contains(node) { return issues }
guard let targetNode = node as? TargetNode else { return issues }
targetNode.dependencies.forEach { toNode in
if let toTargetNode = toNode as? TargetNode {
issues.append(contentsOf: lintDependency(from: targetNode,
to: toTargetNode,
linkedStaticProducts: &linkedStaticProducts))
}
issues.append(contentsOf: lintGraphNode(node: toNode,
evaluatedNodes: &evaluatedNodes,
linkedStaticProducts: &linkedStaticProducts))
}
return issues
}
private func lintDependency(from: TargetNode,
to: TargetNode,
linkedStaticProducts: inout Set<StaticDepedencyWarning>) -> [LintingIssue] {
var issues: [LintingIssue] = []
let fromTarget = LintableTarget(platform: from.target.platform,
product: from.target.product)
let toTarget = LintableTarget(platform: to.target.platform,
product: to.target.product)
if !GraphLinter.validLinks.keys.contains(fromTarget) {
let reason = "Target \(from.target.name) has a platform '\(from.target.platform)' and product '\(from.target.product)' invalid or not supported yet."
let issue = LintingIssue(reason: reason, severity: .error)
issues.append(issue)
}
let supportedTargets = GraphLinter.validLinks[fromTarget]!
if !supportedTargets.contains(toTarget) {
let reason = "Target \(from.target.name) has a dependency with target \(to.target.name) of type \(to.target.product) for platform '\(to.target.platform)' which is invalid or not supported yet."
let issue = LintingIssue(reason: reason, severity: .error)
issues.append(issue)
}
issues.append(contentsOf: lintStaticDependencies(from: from,
to: to,
linkedStaticProducts: &linkedStaticProducts))
return issues
}
private func lintStaticDependencies(from: TargetNode,
to: TargetNode,
linkedStaticProducts: inout Set<StaticDepedencyWarning>) -> [LintingIssue] {
guard to.target.product.isStatic, from.target.canLinkStaticProducts() else {
return []
}
let warning = StaticDepedencyWarning(fromTargetNode: from,
toTargetNode: to)
let (inserted, oldMember) = linkedStaticProducts.insert(warning)
guard inserted == false else {
return []
}
let reason = "Target \(to.target.name) has been linked against \(oldMember.fromTargetNode.target.name) and \(from.target.name), it is a static product so may introduce unwanted side effects."
let issue = LintingIssue(reason: reason, severity: .warning)
return [issue]
}
private func lintMismatchingConfigurations(graph: Graphing) -> [LintingIssue] {
let entryNodeProjects = graph.entryNodes.compactMap { $0 as? TargetNode }.map { $0.project }
let knownConfgiruations = entryNodeProjects.reduce(into: Set()) {
$0.formUnion(Set($1.settings.configurations.keys))
}
let projectBuildConfigurations = graph.projects.map {
(name: $0.name, buildConfigurations: Set($0.settings.configurations.keys))
}
let mismatchingBuildConfigurations = projectBuildConfigurations.filter {
!knownConfgiruations.isSubset(of: $0.buildConfigurations)
}
return mismatchingBuildConfigurations.map {
let expectedConfigurations = knownConfgiruations.sorted()
let configurations = $0.buildConfigurations.sorted()
let reason = "The project '\($0.name)' has missing or mismatching configurations. It has \(configurations), other projects have \(expectedConfigurations)"
return LintingIssue(reason: reason,
severity: .warning)
}
}
struct LintableTarget: Equatable, Hashable {
let platform: Platform
let product: Product
}
static let validLinks: [LintableTarget: [LintableTarget]] = [
// iOS products
LintableTarget(platform: .iOS, product: .app): [
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .dynamicLibrary),
LintableTarget(platform: .iOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .bundle),
// LintableTarget(platform: .iOS, product: .appExtension),
// LintableTarget(platform: .iOS, product: .messagesExtension),
// LintableTarget(platform: .iOS, product: .stickerPack),
// LintableTarget(platform: .watchOS, product: .watch2App),
// LintableTarget(platform: .watchOS, product: .watchApp),
],
LintableTarget(platform: .iOS, product: .staticLibrary): [
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .framework),
],
LintableTarget(platform: .iOS, product: .staticFramework): [
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .framework),
],
LintableTarget(platform: .iOS, product: .dynamicLibrary): [
LintableTarget(platform: .iOS, product: .dynamicLibrary),
],
LintableTarget(platform: .iOS, product: .framework): [
LintableTarget(platform: .iOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .bundle),
],
LintableTarget(platform: .iOS, product: .unitTests): [
LintableTarget(platform: .iOS, product: .app),
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .dynamicLibrary),
LintableTarget(platform: .iOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .bundle),
],
LintableTarget(platform: .iOS, product: .uiTests): [
LintableTarget(platform: .iOS, product: .app),
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .dynamicLibrary),
LintableTarget(platform: .iOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .bundle),
],
// LintableTarget(platform: .iOS, product: .appExtension): [
// LintableTarget(platform: .iOS, product: .staticLibrary),
// LintableTarget(platform: .iOS, product: .dynamicLibrary),
// LintableTarget(platform: .iOS, product: .framework),
// ],
// LintableTarget(platform: .iOS, product: .messagesApplication): [
// LintableTarget(platform: .iOS, product: .messagesExtension),
// LintableTarget(platform: .iOS, product: .staticLibrary),
// LintableTarget(platform: .iOS, product: .dynamicLibrary),
// LintableTarget(platform: .iOS, product: .framework),
// ],
// LintableTarget(platform: .iOS, product: .messagesExtension): [
// LintableTarget(platform: .iOS, product: .staticLibrary),
// LintableTarget(platform: .iOS, product: .dynamicLibrary),
// LintableTarget(platform: .iOS, product: .framework),
// ],
// LintableTarget(platform: .iOS, product: .stickerPack): [
// LintableTarget(platform: .iOS, product: .staticLibrary),
// LintableTarget(platform: .iOS, product: .dynamicLibrary),
// LintableTarget(platform: .iOS, product: .framework),
// ],
// macOS
LintableTarget(platform: .macOS, product: .app): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
// LintableTarget(platform: .macOS, product: .appExtension),
],
LintableTarget(platform: .macOS, product: .staticLibrary): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .framework),
],
LintableTarget(platform: .macOS, product: .staticFramework): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .framework),
],
LintableTarget(platform: .macOS, product: .dynamicLibrary): [
LintableTarget(platform: .macOS, product: .dynamicLibrary),
],
LintableTarget(platform: .macOS, product: .framework): [
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
],
LintableTarget(platform: .macOS, product: .unitTests): [
LintableTarget(platform: .macOS, product: .app),
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
],
LintableTarget(platform: .macOS, product: .uiTests): [
LintableTarget(platform: .macOS, product: .app),
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
],
// LintableTarget(platform: .macOS, product: .appExtension): [
// LintableTarget(platform: .macOS, product: .staticLibrary),
// LintableTarget(platform: .macOS, product: .dynamicLibrary),
// LintableTarget(platform: .macOS, product: .framework),
// ],
// tvOS
LintableTarget(platform: .tvOS, product: .app): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .dynamicLibrary),
LintableTarget(platform: .tvOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
// LintableTarget(platform: .tvOS, product: .tvExtension),
],
LintableTarget(platform: .tvOS, product: .staticLibrary): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
],
LintableTarget(platform: .tvOS, product: .staticFramework): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
],
LintableTarget(platform: .tvOS, product: .dynamicLibrary): [
LintableTarget(platform: .tvOS, product: .dynamicLibrary),
],
LintableTarget(platform: .tvOS, product: .framework): [
LintableTarget(platform: .tvOS, product: .framework),
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .staticFramework),
],
LintableTarget(platform: .tvOS, product: .unitTests): [
LintableTarget(platform: .tvOS, product: .app),
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .dynamicLibrary),
LintableTarget(platform: .tvOS, product: .framework),
LintableTarget(platform: .iOS, product: .staticFramework),
],
// LintableTarget(platform: .tvOS, product: .tvExtension): [
// LintableTarget(platform: .tvOS, product: .staticLibrary),
// LintableTarget(platform: .tvOS, product: .dynamicLibrary),
// LintableTarget(platform: .tvOS, product: .framework),
// ],
// watchOS
// LintableTarget(platform: .watchOS, product: .watchApp): [
// LintableTarget(platform: .watchOS, product: .staticLibrary),
// LintableTarget(platform: .watchOS, product: .dynamicLibrary),
// LintableTarget(platform: .watchOS, product: .framework),
// LintableTarget(platform: .watchOS, product: .watchExtension),
// ],
// LintableTarget(platform: .watchOS, product: .watch2App): [
// LintableTarget(platform: .watchOS, product: .staticLibrary),
// LintableTarget(platform: .watchOS, product: .dynamicLibrary),
// LintableTarget(platform: .watchOS, product: .framework),
// LintableTarget(platform: .watchOS, product: .watch2Extension),
// ],
// LintableTarget(platform: .watchOS, product: .staticLibrary): [
// LintableTarget(platform: .watchOS, product: .staticLibrary),
// ],
// LintableTarget(platform: .watchOS, product: .dynamicLibrary): [
// LintableTarget(platform: .watchOS, product: .dynamicLibrary),
// ],
// LintableTarget(platform: .watchOS, product: .framework): [
// LintableTarget(platform: .watchOS, product: .framework),
// ],
// LintableTarget(platform: .watchOS, product: .watchExtension): [
// LintableTarget(platform: .watchOS, product: .staticLibrary),
// LintableTarget(platform: .watchOS, product: .dynamicLibrary),
// LintableTarget(platform: .watchOS, product: .framework),
// ],
// LintableTarget(platform: .watchOS, product: .watch2Extension): [
// LintableTarget(platform: .watchOS, product: .staticLibrary),
// LintableTarget(platform: .watchOS, product: .dynamicLibrary),
// LintableTarget(platform: .watchOS, product: .framework),
// ],
]
}