forked from tuist/tuist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GraphLinter.swift
512 lines (458 loc) 路 24.6 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import Foundation
import struct TSCUtility.Version
import TuistCore
import TuistGraph
import TuistSupport
public protocol GraphLinting: AnyObject {
func lint(graphTraverser: GraphTraversing) -> [LintingIssue]
}
// swiftlint:disable type_body_length
public class GraphLinter: GraphLinting {
// MARK: - Attributes
private let projectLinter: ProjectLinting
private let staticProductsLinter: StaticProductsGraphLinting
// MARK: - Init
public convenience init() {
let projectLinter = ProjectLinter()
let staticProductsLinter = StaticProductsGraphLinter()
self.init(
projectLinter: projectLinter,
staticProductsLinter: staticProductsLinter
)
}
init(projectLinter: ProjectLinting,
staticProductsLinter: StaticProductsGraphLinting)
{
self.projectLinter = projectLinter
self.staticProductsLinter = staticProductsLinter
}
// MARK: - GraphLinting
public func lint(graphTraverser: GraphTraversing) -> [LintingIssue] {
var issues: [LintingIssue] = []
issues.append(contentsOf: graphTraverser.projects.flatMap { project -> [LintingIssue] in
projectLinter.lint(project.value)
})
issues.append(contentsOf: lintDependencies(graphTraverser: graphTraverser))
issues.append(contentsOf: lintMismatchingConfigurations(graphTraverser: graphTraverser))
issues.append(contentsOf: lintWatchBundleIndentifiers(graphTraverser: graphTraverser))
issues.append(contentsOf: lintBundleIdentifiers(graphTraverser: graphTraverser))
return issues
}
// MARK: - Fileprivate
func lintDependencies(graphTraverser: GraphTraversing) -> [LintingIssue] {
var issues: [LintingIssue] = []
let dependencyIssues = graphTraverser.dependencies.flatMap { (fromDependency, toDependencies) -> [LintingIssue] in
toDependencies.flatMap { (toDependency) -> [LintingIssue] in
guard case let ValueGraphDependency.target(fromTargetName, fromTargetPath) = fromDependency else { return [] }
guard case let ValueGraphDependency.target(toTargetName, toTargetPath) = toDependency else { return [] }
guard let fromTarget = graphTraverser.target(path: fromTargetPath, name: fromTargetName) else { return [] }
guard let toTarget = graphTraverser.target(path: toTargetPath, name: toTargetName) else { return [] }
return lintDependency(from: fromTarget, to: toTarget)
}
}
issues.append(contentsOf: dependencyIssues)
issues.append(contentsOf: staticProductsLinter.lint(graphTraverser: graphTraverser))
issues.append(contentsOf: lintPrecompiledFrameworkDependencies(graphTraverser: graphTraverser))
issues.append(contentsOf: lintPackageDependencies(graphTraverser: graphTraverser))
issues.append(contentsOf: lintAppClip(graphTraverser: graphTraverser))
return issues
}
private func lintDependency(from: ValueGraphTarget, to: ValueGraphTarget) -> [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 == nil || supportedTargets?.contains(toTarget) == false {
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)
}
return issues
}
private func lintMismatchingConfigurations(graphTraverser: GraphTraversing) -> [LintingIssue] {
let rootProjects = graphTraverser.rootProjects()
let knownConfigurations = rootProjects.reduce(into: Set()) {
$0.formUnion(Set($1.settings.configurations.keys))
}
let projectBuildConfigurations = graphTraverser.projects.compactMap { project in
(name: project.value.name, buildConfigurations: Set(project.value.settings.configurations.keys))
}
let mismatchingBuildConfigurations = projectBuildConfigurations.filter {
!knownConfigurations.isSubset(of: $0.buildConfigurations)
}
return mismatchingBuildConfigurations.map {
let expectedConfigurations = knownConfigurations.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
)
}
}
/// It verifies setup for packages
///
/// - Parameter graph: Project graph.
/// - Returns: Linting issues.
private func lintPackageDependencies(graphTraverser: GraphTraversing) -> [LintingIssue] {
guard graphTraverser.hasPackages else { return [] }
let version: Version
do {
version = try XcodeController.shared.selectedVersion()
} catch {
return [LintingIssue(reason: "Could not determine Xcode version", severity: .error)]
}
if version.major < 11 {
let reason = "The project contains package dependencies but the selected version of Xcode is not compatible. Need at least 11 but got \(version)"
return [LintingIssue(reason: reason, severity: .error)]
}
return []
}
private func lintAppClip(graphTraverser: GraphTraversing) -> [LintingIssue] {
let apps = graphTraverser.apps()
let issues = apps.flatMap { app -> [LintingIssue] in
let appClips = graphTraverser.directLocalTargetDependencies(path: app.path, name: app.target.name)
.filter { $0.target.product == .appClip }
if appClips.count > 1 {
return [
LintingIssue(
reason: "\(app) cannot depend on more than one app clip: \(appClips.map(\.target.name).sorted().listed())",
severity: .error
),
]
}
return appClips.flatMap { appClip -> [LintingIssue] in
lint(appClip: appClip, parentApp: app)
}
}
return issues
}
private func lintPrecompiledFrameworkDependencies(graphTraverser: GraphTraversing) -> [LintingIssue] {
let frameworks = graphTraverser.precompiledFrameworksPaths()
return frameworks
.filter { !FileHandler.shared.exists($0) }
.map { LintingIssue(reason: "Framework not found at path \($0.pathString)", severity: .error) }
}
private func lintWatchBundleIndentifiers(graphTraverser: GraphTraversing) -> [LintingIssue] {
let apps = graphTraverser.apps()
let issues = apps.flatMap { app -> [LintingIssue] in
let watchApps = graphTraverser.directLocalTargetDependencies(path: app.path, name: app.target.name)
.filter { $0.target.product == .watch2App }
return watchApps.flatMap { watchApp -> [LintingIssue] in
let watchAppIssues = lint(watchApp: watchApp, parentApp: app)
let watchExtensions = graphTraverser.directLocalTargetDependencies(path: watchApp.path, name: watchApp.target.name)
.filter { $0.target.product == .watch2Extension }
let watchExtensionIssues = watchExtensions.flatMap { watchExtension in
lint(watchExtension: watchExtension, parentWatchApp: watchApp)
}
return watchAppIssues + watchExtensionIssues
}
}
return issues
}
private func lint(watchApp: ValueGraphTarget, parentApp: ValueGraphTarget) -> [LintingIssue] {
guard watchApp.target.bundleId.hasPrefix(parentApp.target.bundleId) else {
return [
LintingIssue(reason: """
Watch app '\(watchApp.target.name)' bundleId: \(watchApp.target.bundleId) isn't prefixed with its parent's app '\(parentApp.target.bundleId)' bundleId '\(parentApp.target.bundleId)'
""", severity: .error),
]
}
return []
}
private func lint(watchExtension: ValueGraphTarget, parentWatchApp: ValueGraphTarget) -> [LintingIssue] {
guard watchExtension.target.bundleId.hasPrefix(parentWatchApp.target.bundleId) else {
return [
LintingIssue(reason: """
Watch extension '\(watchExtension.target.name)' bundleId: \(watchExtension.target.bundleId) isn't prefixed with its parent's watch app '\(parentWatchApp.target.bundleId)' bundleId '\(parentWatchApp.target.bundleId)'
""", severity: .error),
]
}
return []
}
private func lint(appClip: ValueGraphTarget, parentApp: ValueGraphTarget) -> [LintingIssue] {
var foundIssues = [LintingIssue]()
if !appClip.target.bundleId.hasPrefix(parentApp.target.bundleId) {
foundIssues.append(
LintingIssue(reason: """
AppClip '\(appClip.target.name)' bundleId: \(appClip.target.bundleId) isn't prefixed with its parent's app '\(parentApp.target.name)' bundleId '\(parentApp.target.bundleId)'
""", severity: .error))
}
if let entitlements = appClip.target.entitlements {
if !FileHandler.shared.exists(entitlements) {
foundIssues.append(LintingIssue(reason: "The entitlements at path '\(entitlements)' referenced by target does not exist", severity: .error))
}
} else {
foundIssues.append(LintingIssue(reason: "An AppClip '\(appClip.target.name)' requires its Parent Application Identifiers Entitlement to be set", severity: .error))
}
return foundIssues
}
private struct BundleIdKey: Hashable {
let bundleId: String
let platform: Platform
}
private func lintBundleIdentifiers(graphTraverser: GraphTraversing) -> [LintingIssue] {
var bundleIds = [BundleIdKey: [String]]()
graphTraverser.targets
.flatMap { $0.value.map(\.value) }
.forEach { target in
// skip duplicate check for bundle Ids that contain variables
// e.g.
// - `${MY_BUNDLE_ID}`
// - `prefix.${PRODUCT_NAME:rfc1034identifier}`
// - `${PRODUCT_NAME:rfc1034identifier}.suffix`
if target.bundleId.contains("$") {
return
}
let key = BundleIdKey(bundleId: target.bundleId, platform: target.platform)
var targetsWithThisBundleId = bundleIds[key] ?? []
targetsWithThisBundleId.append(target.name)
bundleIds[key] = targetsWithThisBundleId
}
return duplicateBundleIdLintingIssue(for: bundleIds)
}
private func duplicateBundleIdLintingIssue(for targets: [BundleIdKey: [String]]) -> [LintingIssue] {
targets.compactMap { bundleIdKey, targetNames in
guard targetNames.count > 1 else {
return nil
}
let reason = "The bundle identifier '\(bundleIdKey.bundleId)' is being used by multiple targets: \(targetNames.sorted().listed())."
return LintingIssue(reason: reason, severity: .warning)
}.sorted(by: { $0.reason < $1.reason })
}
struct LintableTarget: Equatable, Hashable {
let platform: TuistGraph.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: .stickerPackExtension),
LintableTarget(platform: .watchOS, product: .watch2App),
LintableTarget(platform: .iOS, product: .appClip),
// 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: .bundle),
],
LintableTarget(platform: .iOS, product: .staticFramework): [
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .framework),
LintableTarget(platform: .iOS, product: .bundle),
],
LintableTarget(platform: .iOS, product: .dynamicLibrary): [
LintableTarget(platform: .iOS, product: .dynamicLibrary),
LintableTarget(platform: .iOS, product: .bundle),
],
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: .appClip),
],
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: .appClip),
],
LintableTarget(platform: .iOS, product: .appExtension): [
LintableTarget(platform: .iOS, product: .staticLibrary),
LintableTarget(platform: .iOS, product: .dynamicLibrary),
LintableTarget(platform: .iOS, product: .staticFramework),
LintableTarget(platform: .iOS, product: .framework),
],
LintableTarget(platform: .iOS, product: .appClip): [
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: .stickerPackExtension): [
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: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .appExtension),
],
LintableTarget(platform: .macOS, product: .staticLibrary): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .macOS, product: .bundle),
],
LintableTarget(platform: .macOS, product: .staticFramework): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .macOS, product: .bundle),
],
LintableTarget(platform: .macOS, product: .dynamicLibrary): [
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .bundle),
],
LintableTarget(platform: .macOS, product: .framework): [
LintableTarget(platform: .macOS, product: .framework),
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
LintableTarget(platform: .macOS, product: .bundle),
],
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),
],
LintableTarget(platform: .macOS, product: .commandLineTool): [
LintableTarget(platform: .macOS, product: .staticLibrary),
LintableTarget(platform: .macOS, product: .dynamicLibrary),
LintableTarget(platform: .macOS, product: .staticFramework),
],
LintableTarget(platform: .tvOS, product: .app): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .dynamicLibrary),
LintableTarget(platform: .tvOS, product: .framework),
LintableTarget(platform: .tvOS, product: .staticFramework),
LintableTarget(platform: .tvOS, product: .bundle),
LintableTarget(platform: .tvOS, product: .tvExtension),
],
LintableTarget(platform: .tvOS, product: .staticLibrary): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .staticFramework),
LintableTarget(platform: .tvOS, product: .bundle),
],
LintableTarget(platform: .tvOS, product: .staticFramework): [
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .staticFramework),
LintableTarget(platform: .tvOS, product: .bundle),
],
LintableTarget(platform: .tvOS, product: .dynamicLibrary): [
LintableTarget(platform: .tvOS, product: .dynamicLibrary),
LintableTarget(platform: .tvOS, product: .bundle),
],
LintableTarget(platform: .tvOS, product: .framework): [
LintableTarget(platform: .tvOS, product: .framework),
LintableTarget(platform: .tvOS, product: .staticLibrary),
LintableTarget(platform: .tvOS, product: .staticFramework),
LintableTarget(platform: .tvOS, product: .bundle),
],
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: .tvOS, 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: .watch2Extension),
],
LintableTarget(platform: .watchOS, product: .staticLibrary): [
LintableTarget(platform: .watchOS, product: .staticLibrary),
LintableTarget(platform: .watchOS, product: .staticFramework),
LintableTarget(platform: .watchOS, product: .bundle),
],
LintableTarget(platform: .watchOS, product: .staticFramework): [
LintableTarget(platform: .watchOS, product: .staticLibrary),
LintableTarget(platform: .watchOS, product: .staticFramework),
LintableTarget(platform: .watchOS, product: .framework),
LintableTarget(platform: .watchOS, product: .bundle),
],
LintableTarget(platform: .watchOS, product: .dynamicLibrary): [
LintableTarget(platform: .watchOS, product: .dynamicLibrary),
LintableTarget(platform: .watchOS, product: .bundle),
],
LintableTarget(platform: .watchOS, product: .framework): [
LintableTarget(platform: .watchOS, product: .staticLibrary),
LintableTarget(platform: .watchOS, product: .framework),
LintableTarget(platform: .watchOS, product: .staticFramework),
LintableTarget(platform: .watchOS, product: .bundle),
],
// 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),
LintableTarget(platform: .watchOS, product: .staticFramework),
],
]
}