forked from tuist/tuist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProjectGenerator.swift
271 lines (227 loc) · 11.7 KB
/
ProjectGenerator.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
import Basic
import Foundation
import TuistCore
import XcodeProj
struct ProjectConstants {
var objectVersion: UInt
var archiveVersion: UInt
}
extension ProjectConstants {
static var xcode10: ProjectConstants {
return ProjectConstants(objectVersion: 50,
archiveVersion: Xcode.LastKnown.archiveVersion)
}
static var xcode11: ProjectConstants {
return ProjectConstants(objectVersion: 52,
archiveVersion: Xcode.LastKnown.archiveVersion)
}
}
protocol ProjectGenerating: AnyObject {
func generate(project: Project,
graph: Graphing,
sourceRootPath: AbsolutePath?) throws -> GeneratedProject
}
final class ProjectGenerator: ProjectGenerating {
// MARK: - Attributes
/// Generator for the project targets.
let targetGenerator: TargetGenerating
/// Generator for the project configuration.
let configGenerator: ConfigGenerating
/// Generator for the project schemes.
let schemesGenerator: SchemesGenerating
/// Generator for the project derived files.
let derivedFileGenerator: DerivedFileGenerating
/// Printer instance to output messages to the user.
let printer: Printing
/// System instance to run commands in the system.
let system: Systeming
/// File handler instance to interact with the system files.
let fileHandler: FileHandling
// MARK: - Init
/// Initializes the project generator with its attributes.
///
/// - Parameters:
/// - targetGenerator: Generator for the project targets.
/// - configGenerator: Generator for the project configuration.
/// - schemesGenerator: Generator for the project schemes.
/// - derivedFileGenerator: Generator for the project derived files.
/// - printer: Printer instance to output messages to the user.
/// - system: System instance to run commands in the system.
/// - fileHandler: File handler instance to interact with the system files.
init(targetGenerator: TargetGenerating = TargetGenerator(),
configGenerator: ConfigGenerating = ConfigGenerator(),
schemesGenerator: SchemesGenerating = SchemesGenerator(),
derivedFileGenerator: DerivedFileGenerating = DerivedFileGenerator(),
printer: Printing = Printer(),
system: Systeming = System(),
fileHandler: FileHandling = FileHandler()) {
self.targetGenerator = targetGenerator
self.configGenerator = configGenerator
self.schemesGenerator = schemesGenerator
self.derivedFileGenerator = derivedFileGenerator
self.printer = printer
self.system = system
self.fileHandler = fileHandler
}
// MARK: - ProjectGenerating
func generate(project: Project,
graph: Graphing,
sourceRootPath: AbsolutePath? = nil) throws -> GeneratedProject {
printer.print("Generating project \(project.name)")
// Getting the path.
let sourceRootPath = sourceRootPath ?? project.path
let xcodeprojPath = sourceRootPath.appending(component: "\(project.fileName).xcodeproj")
// Derived files
let deleteOldDerivedFiles = try derivedFileGenerator.generate(project: project, sourceRootPath: sourceRootPath)
// Project and workspace.
let workspaceData = XCWorkspaceData(children: [])
let workspace = XCWorkspace(data: workspaceData)
let projectConstants = determineProjectConstants()
let pbxproj = PBXProj(objectVersion: projectConstants.objectVersion,
archiveVersion: projectConstants.archiveVersion,
classes: [:])
let groups = ProjectGroups.generate(project: project, pbxproj: pbxproj, sourceRootPath: sourceRootPath)
let fileElements = ProjectFileElements()
try fileElements.generateProjectFiles(project: project,
graph: graph,
groups: groups,
pbxproj: pbxproj,
sourceRootPath: sourceRootPath)
let configurationList = try configGenerator.generateProjectConfig(project: project,
pbxproj: pbxproj,
fileElements: fileElements)
let pbxProject = try generatePbxproject(project: project,
configurationList: configurationList,
groups: groups,
pbxproj: pbxproj)
let nativeTargets = try generateTargets(project: project,
pbxproj: pbxproj,
pbxProject: pbxProject,
groups: groups,
fileElements: fileElements,
sourceRootPath: sourceRootPath,
graph: graph)
generateTestTargetIdentity(project: project,
pbxproj: pbxproj,
pbxProject: pbxProject)
try deleteOldDerivedFiles()
return try write(xcodeprojPath: xcodeprojPath,
nativeTargets: nativeTargets,
workspace: workspace,
pbxproj: pbxproj,
project: project,
graph: graph)
}
// MARK: - Fileprivate
private func generatePbxproject(project: Project,
configurationList: XCConfigurationList,
groups: ProjectGroups,
pbxproj: PBXProj) throws -> PBXProject {
let pbxProject = PBXProject(name: project.name,
buildConfigurationList: configurationList,
compatibilityVersion: Xcode.Default.compatibilityVersion,
mainGroup: groups.main,
developmentRegion: Xcode.Default.developmentRegion,
hasScannedForEncodings: 0,
knownRegions: ["en"],
productsGroup: groups.products,
projectDirPath: "",
projects: [],
projectRoots: [],
targets: [])
pbxproj.add(object: pbxProject)
pbxproj.rootObject = pbxProject
return pbxProject
}
private func generateTargets(project: Project,
pbxproj: PBXProj,
pbxProject: PBXProject,
groups _: ProjectGroups,
fileElements: ProjectFileElements,
sourceRootPath: AbsolutePath,
graph: Graphing) throws -> [String: PBXNativeTarget] {
var nativeTargets: [String: PBXNativeTarget] = [:]
try project.targets.forEach { target in
let nativeTarget = try targetGenerator.generateTarget(target: target,
pbxproj: pbxproj,
pbxProject: pbxProject,
projectSettings: project.settings,
fileElements: fileElements,
path: project.path,
sourceRootPath: sourceRootPath,
graph: graph,
system: system)
nativeTargets[target.name] = nativeTarget
}
/// Target dependencies
try targetGenerator.generateTargetDependencies(path: project.path,
targets: project.targets,
nativeTargets: nativeTargets,
graph: graph)
return nativeTargets
}
private func generateTestTargetIdentity(project _: Project,
pbxproj: PBXProj,
pbxProject: PBXProject) {
func testTargetName(_ target: PBXTarget) -> String? {
guard let buildConfigurations = target.buildConfigurationList?.buildConfigurations else {
return nil
}
return buildConfigurations
.compactMap { $0.buildSettings["TEST_TARGET_NAME"] as? String }
.first
}
let testTargets = pbxproj.nativeTargets.filter { $0.productType == .uiTestBundle || $0.productType == .unitTestBundle }
for testTarget in testTargets {
guard let name = testTargetName(testTarget) else {
continue
}
guard let target = pbxproj.targets(named: name).first else {
continue
}
var attributes = pbxProject.targetAttributes[testTarget] ?? [:]
attributes["TestTargetID"] = target
pbxProject.setTargetAttributes(attributes, target: testTarget)
}
}
private func write(xcodeprojPath: AbsolutePath,
nativeTargets: [String: PBXNativeTarget],
workspace: XCWorkspace,
pbxproj: PBXProj,
project: Project,
graph _: Graphing) throws -> GeneratedProject {
var generatedProject: GeneratedProject!
try fileHandler.inTemporaryDirectory { temporaryPath in
try writeXcodeproj(workspace: workspace,
pbxproj: pbxproj,
xcodeprojPath: temporaryPath)
generatedProject = GeneratedProject(pbxproj: pbxproj,
path: temporaryPath,
targets: nativeTargets,
name: xcodeprojPath.components.last!)
try writeSchemes(project: project,
generatedProject: generatedProject)
try fileHandler.replace(xcodeprojPath, with: temporaryPath)
}
return try generatedProject.at(path: xcodeprojPath)
}
private func writeXcodeproj(workspace: XCWorkspace,
pbxproj: PBXProj,
xcodeprojPath: AbsolutePath) throws {
let xcodeproj = XcodeProj(workspace: workspace, pbxproj: pbxproj)
try xcodeproj.write(path: xcodeprojPath.path)
}
private func writeSchemes(project: Project,
generatedProject: GeneratedProject) throws {
try schemesGenerator.generateTargetSchemes(project: project,
generatedProject: generatedProject)
}
private func determineProjectConstants() -> ProjectConstants {
// TODO:
//
// To maintain backwards compatibility with Xcode 10
// the Xcode10 constants are used unless the use of Xcode 11
// features are detected (e.g Swift PM dependencies)
return .xcode10
}
}