-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathProjectGenerator.swift
210 lines (177 loc) · 6.61 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
//
// ProjectGenerator.swift
// swift-create-xcframework
//
// Created by Rob Amos on 7/5/20.
//
import Foundation
import TSCBasic
import TSCUtility
import Xcodeproj
struct ProjectGenerator {
private enum Constants {
static let `extension` = "xcodeproj"
}
// MARK: - Properties
let package: PackageInfo
func projectPath() throws -> AbsolutePath {
let dir = try AbsolutePath(validating: self.package.projectBuildDirectory.path)
#if swift(>=5.7)
return try XcodeProject.makePath(outputDir: dir, projectName: self.package.manifest.displayName)
#else
return buildXcodeprojPath(outputDir: dir, projectName: self.package.manifest.displayName)
#endif
}
// MARK: - Initialisation
init (package: PackageInfo) {
self.package = package
}
// MARK: - Generation
/// Writes out the Xcconfig file
func writeDistributionXcconfig () throws {
guard self.package.hasDistributionBuildXcconfig else {
return
}
try makeDirectories(self.projectPath())
let path = try AbsolutePath(validating: self.package.distributionBuildXcconfig.path)
try open(path) { stream in
if let absolutePath = self.package.overridesXcconfig?.path {
stream (
"""
#include "\(try AbsolutePath(validating: absolutePath).relative(to: AbsolutePath(validating: path.dirname)).pathString)"
"""
)
}
stream (
"""
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
"""
)
}
}
/// Generate an Xcode project.
///
/// This is basically a copy of Xcodeproj.generate()
///
func generate () throws -> Xcode.Project {
let path = try self.projectPath()
try makeDirectories(path)
// Generate the contents of project.xcodeproj (inside the .xcodeproj).
#if swift(>=5.6)
let project = try pbxproj (
xcodeprojPath: path,
graph: self.package.graph,
extraDirs: [],
extraFiles: [],
options: XcodeprojOptions (
xcconfigOverrides: (self.package.overridesXcconfig?.path).flatMap { try AbsolutePath(validating: $0) },
useLegacySchemeGenerator: true
),
fileSystem: reallyLocalFileSystem,
observabilityScope: self.package.observabilitySystem.topScope
)
#else
let project = try pbxproj (
xcodeprojPath: path,
graph: self.package.graph,
extraDirs: [],
extraFiles: [],
options: XcodeprojOptions (
xcconfigOverrides: (self.package.overridesXcconfig?.path).flatMap { AbsolutePath($0) },
useLegacySchemeGenerator: true
),
diagnostics: self.package.diagnostics
)
#endif
return project
}
}
// MARK: - Saving Xcode Projects
extension Xcode.Project {
/// This is the group that is normally created in Xcodeproj.xcodeProject() when you specify an xcconfigOverride
var configGroup: Xcode.Group {
let name = "Configs"
if let group = self.mainGroup.subitems.lazy.compactMap({ $0 as? Xcode.Group }).first(where: { $0.name == name }) {
return group
}
// doesn't exist - lets creat it
return self.mainGroup.addGroup(path: "", name: name)
}
func enableDistribution (targets: [String], xcconfig: RelativePath) throws {
let group = self.configGroup
let ref = group.addFileReference (
path: xcconfig.pathString,
name: xcconfig.basename
)
for target in self.targets where targets.contains(target.name) {
target.buildSettings.xcconfigFileRef = ref
}
}
func save (to path: AbsolutePath) throws {
try open(path.appending(component: "project.pbxproj")) { stream in
// Serialize the project model we created to a plist, and return
// its string description.
#if swift(>=5.6)
let str = try "// !$*UTF8*$!\n" + self.generatePlist().description
#else
let str = "// !$*UTF8*$!\n" + self.generatePlist().description
#endif
stream(str)
}
for target in self.frameworkTargets {
///// For framework targets, generate target.c99Name_Info.plist files in the
///// directory that Xcode project is generated
let name = "\(target.name.spm_mangledToC99ExtendedIdentifier())_Info.plist"
try open(path.appending(RelativePath(name))) { print in
print(
"""
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
"""
)
}
}
}
}
/// Writes the contents to the file specified.
///
/// This method doesn't rewrite the file in case the new and old contents of
/// file are same.
private func open(_ path: AbsolutePath, body: ((String) -> Void) throws -> Void) throws {
let stream = BufferedOutputByteStream()
try body { line in
stream <<< line
stream <<< "\n"
}
// If the file exists with the identical contents, we don't need to rewrite it.
//
// This avoids unnecessarily triggering Xcode reloads of the project file.
if let contents = try? localFileSystem.readFileContents(path), contents == stream.bytes {
return
}
// Write the real file.
try localFileSystem.writeFileContents(path, bytes: stream.bytes)
}