-
-
Notifications
You must be signed in to change notification settings - Fork 508
/
ProjectGenerator.swift
248 lines (207 loc) · 11.2 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
import Basic
import Foundation
import TuistCore
import xcodeproj
protocol ProjectGenerating: AnyObject {
func generate(project: Project,
options: GenerationOptions,
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
/// 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.
/// - 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(),
printer: Printing = Printer(),
system: Systeming = System(),
fileHandler: FileHandling = FileHandler()) {
self.targetGenerator = targetGenerator
self.configGenerator = configGenerator
self.schemesGenerator = schemesGenerator
self.printer = printer
self.system = system
self.fileHandler = fileHandler
}
// MARK: - ProjectGenerating
func generate(project: Project,
options: GenerationOptions,
graph: Graphing,
sourceRootPath: AbsolutePath? = nil) throws -> GeneratedProject {
printer.print("Generating project \(project.name)")
// Getting the path.
var sourceRootPath: AbsolutePath! = sourceRootPath
if sourceRootPath == nil {
sourceRootPath = project.path
}
let xcodeprojPath = sourceRootPath.appending(component: "\(project.name).xcodeproj")
// Project and workspace.
let workspaceData = XCWorkspaceData(children: [])
let workspace = XCWorkspace(data: workspaceData)
let pbxproj = PBXProj(objectVersion: Xcode.Default.objectVersion,
archiveVersion: Xcode.LastKnown.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,
options: options)
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,
options: options,
graph: graph)
generateTestTargetIdentity(project: project,
pbxproj: pbxproj,
pbxProject: pbxProject)
return try write(xcodeprojPath: xcodeprojPath,
nativeTargets: nativeTargets,
workspace: workspace,
pbxproj: pbxproj,
project: project,
graph: graph)
}
// MARK: - Fileprivate
fileprivate 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
}
fileprivate func generateTargets(project: Project,
pbxproj: PBXProj,
pbxProject: PBXProject,
groups: ProjectGroups,
fileElements: ProjectFileElements,
sourceRootPath: AbsolutePath,
options: GenerationOptions,
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,
groups: groups,
fileElements: fileElements,
path: project.path,
sourceRootPath: sourceRootPath,
options: options,
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
}
fileprivate 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
}
pbxProject.setTargetAttributes([ "TestTargetID": target ], target: testTarget)
}
}
fileprivate 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(path: temporaryPath,
targets: nativeTargets,
name: xcodeprojPath.components.last!)
try writeSchemes(project: project,
generatedProject: generatedProject,
graph: graph)
try fileHandler.replace(xcodeprojPath, with: temporaryPath)
}
return generatedProject.at(path: xcodeprojPath)
}
fileprivate func writeXcodeproj(workspace: XCWorkspace,
pbxproj: PBXProj,
xcodeprojPath: AbsolutePath) throws {
let xcodeproj = XcodeProj(workspace: workspace, pbxproj: pbxproj)
try xcodeproj.write(path: xcodeprojPath.path)
}
fileprivate func writeSchemes(project: Project,
generatedProject: GeneratedProject,
graph: Graphing) throws {
try schemesGenerator.generateTargetSchemes(project: project,
generatedProject: generatedProject)
try schemesGenerator.generateProjectScheme(project: project,
generatedProject: generatedProject,
graph: graph)
}
}