forked from tuist/tuist
/
ResourcesProjectMapper.swift
300 lines (250 loc) 路 11.8 KB
/
ResourcesProjectMapper.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
import Foundation
import TSCBasic
import TuistCore
import TuistGraph
import TuistSupport
/// A project mapper that adds support for defining resources in targets that don't support it
public class ResourcesProjectMapper: ProjectMapping { // swiftlint:disable:this type_body_length
private let contentHasher: ContentHashing
public init(contentHasher: ContentHashing) {
self.contentHasher = contentHasher
}
public func map(project: Project) throws -> (Project, [SideEffectDescriptor]) {
guard !project.options.disableBundleAccessors else {
return (project, [])
}
logger.debug("Transforming project \(project.name): Generating bundles for libraries'")
var sideEffects: [SideEffectDescriptor] = []
var targets: [Target] = []
for target in project.targets {
let (mappedTargets, targetSideEffects) = try mapTarget(target, project: project)
targets.append(contentsOf: mappedTargets)
sideEffects.append(contentsOf: targetSideEffects)
}
return (project.with(targets: targets), sideEffects)
}
// swiftlint:disable:next function_body_length
public func mapTarget(_ target: Target, project: Project) throws -> ([Target], [SideEffectDescriptor]) {
if target.resources.isEmpty, target.coreDataModels.isEmpty { return ([target], []) }
var additionalTargets: [Target] = []
var sideEffects: [SideEffectDescriptor] = []
let bundleName = "\(project.name)_\(target.name)"
var modifiedTarget = target
if !target.supportsResources {
let resourcesTarget = Target(
name: bundleName,
destinations: target.destinations,
product: .bundle,
productName: nil,
bundleId: "\(target.bundleId).resources",
deploymentTargets: target.deploymentTargets,
infoPlist: .extendingDefault(with: [:]),
settings: Settings(
base: [
"CODE_SIGNING_ALLOWED": "NO",
],
configurations: [:]
),
resources: target.resources,
copyFiles: target.copyFiles,
coreDataModels: target.coreDataModels,
filesGroup: target.filesGroup
)
modifiedTarget.resources = []
modifiedTarget.copyFiles = []
modifiedTarget.dependencies.append(.target(name: bundleName, condition: .when(target.dependencyPlatformFilters)))
additionalTargets.append(resourcesTarget)
}
if target.supportsSources,
target.sources.contains(where: { $0.path.extension == "swift" }),
!target.sources.contains(where: { $0.path.basename == "\(target.name)Resources.swift" })
{
let (filePath, data) = synthesizedSwiftFile(bundleName: bundleName, target: target, project: project)
let hash = try data.map(contentHasher.hash)
let sourceFile = SourceFile(path: filePath, contentHash: hash)
let sideEffect = SideEffectDescriptor.file(.init(path: filePath, contents: data, state: .present))
modifiedTarget.sources.append(sourceFile)
sideEffects.append(sideEffect)
}
if project.isExternal,
target.supportsSources,
target.sources.contains(where: { $0.path.extension == "m" || $0.path.extension == "mm" }),
!target.resources.filter({ $0.path.extension != "xcprivacy" }).isEmpty
{
let (headerFilePath, headerData) = synthesizedObjcHeaderFile(bundleName: bundleName, target: target, project: project)
let headerHash = try headerData.map(contentHasher.hash)
let headerFile = SourceFile(path: headerFilePath, contentHash: headerHash)
let headerSideEffect = SideEffectDescriptor.file(.init(path: headerFilePath, contents: headerData, state: .present))
let gccPrefixHeader = "$(SRCROOT)/\(headerFile.path.relative(to: project.path).pathString)"
var settings = modifiedTarget.settings?.base ?? SettingsDictionary()
settings["GCC_PREFIX_HEADER"] = .string(gccPrefixHeader)
modifiedTarget.settings = modifiedTarget.settings?.with(base: settings)
sideEffects.append(headerSideEffect)
let (resourceAccessorPath, resourceAccessorData) = synthesizedObjcImplementationFile(
bundleName: bundleName,
target: target,
project: project
)
modifiedTarget.sources.append(
SourceFile(
path: resourceAccessorPath,
contentHash: try resourceAccessorData.map(contentHasher.hash)
)
)
sideEffects.append(
SideEffectDescriptor.file(
FileDescriptor(
path: resourceAccessorPath,
contents: resourceAccessorData,
state: .present
)
)
)
}
return ([modifiedTarget] + additionalTargets, sideEffects)
}
func synthesizedSwiftFile(bundleName: String, target: Target, project: Project) -> (AbsolutePath, Data?) {
let filePath = project.derivedDirectoryPath(for: target)
.appending(component: Constants.DerivedDirectory.sources)
.appending(component: "TuistBundle+\(target.name.toValidSwiftIdentifier()).swift")
let content: String = ResourcesProjectMapper.fileContent(
targetName: target.name,
bundleName: bundleName.replacingOccurrences(of: "-", with: "_"),
target: target
)
return (filePath, content.data(using: .utf8))
}
private func synthesizedObjcHeaderFile(bundleName: String, target: Target, project: Project) -> (AbsolutePath, Data?) {
let filePath = synthesizedFilePath(target: target, project: project, fileExtension: "h")
let content: String = ResourcesProjectMapper.objcHeaderFileContent(
targetName: target.name,
bundleName: bundleName.replacingOccurrences(of: "-", with: "_"),
target: target
)
return (filePath, content.data(using: .utf8))
}
private func synthesizedObjcImplementationFile(
bundleName: String,
target: Target,
project: Project
) -> (AbsolutePath, Data?) {
let filePath = synthesizedFilePath(target: target, project: project, fileExtension: "m")
let content: String = ResourcesProjectMapper.objcImplementationFileContent(
targetName: target.name,
bundleName: bundleName.replacingOccurrences(of: "-", with: "_")
)
return (filePath, content.data(using: .utf8))
}
private func synthesizedFilePath(target: Target, project: Project, fileExtension: String) -> AbsolutePath {
let filename = "TuistBundle+\(target.name.camelized.uppercasingFirst).\(fileExtension)"
return project.derivedDirectoryPath(for: target).appending(components: Constants.DerivedDirectory.sources, filename)
}
// swiftlint:disable:next function_body_length
static func fileContent(targetName: String, bundleName: String, target: Target) -> String {
if !target.supportsResources {
return """
// swiftlint:disable all
// swift-format-ignore-file
// swiftformat:disable all
import Foundation
// MARK: - Swift Bundle Accessor
private class BundleFinder {}
extension Foundation.Bundle {
/// Since \(targetName) is a \(
target
.product
), the bundle containing the resources is copied into the final product.
static let module: Bundle = {
let bundleName = "\(bundleName)"
var candidates = [
Bundle.main.resourceURL,
Bundle(for: BundleFinder.self).resourceURL,
Bundle.main.bundleURL,
]
// This is a fix to make Previews work with bundled resources.
// Logic here is taken from SPM's generated `resource_bundle_accessors.swift` file,
// which is located under the derived data directory after building the project.
if let override = ProcessInfo.processInfo.environment["PACKAGE_RESOURCE_BUNDLE_PATH"] {
candidates.append(URL(fileURLWithPath: override))
// Deleting derived data and not rebuilding the frameworks containing resources may result in a state
// where the bundles are only available in the framework's directory that is actively being previewed.
// Since we don't know which framework this is, we also need to look in all the framework subpaths.
if let subpaths = try? FileManager.default.contentsOfDirectory(atPath: override) {
for subpath in subpaths {
if subpath.hasSuffix(".framework") {
candidates.append(URL(fileURLWithPath: override + "/" + subpath))
}
}
}
}
for candidate in candidates {
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle named \(bundleName)")
}()
}
// MARK: - Objective-C Bundle Accessor
@objc
public class \(target.productName.toValidSwiftIdentifier())Resources: NSObject {
@objc public class var bundle: Bundle {
return .module
}
}
// swiftlint:enable all
// swiftformat:enable all
"""
} else {
return """
// swiftlint:disable all
// swift-format-ignore-file
// swiftformat:disable all
import Foundation
// MARK: - Swift Bundle Accessor
private class BundleFinder {}
extension Foundation.Bundle {
/// Since \(targetName) is a \(
target
.product
), the bundle for classes within this module can be used directly.
static let module = Bundle(for: BundleFinder.self)
}
// MARK: - Objective-C Bundle Accessor
@objc
public class \(target.productName.toValidSwiftIdentifier())Resources: NSObject {
@objc public class var bundle: Bundle {
return .module
}
}
// swiftlint:enable all
// swiftformat:enable all
"""
}
}
static func objcHeaderFileContent(targetName: String, bundleName _: String, target _: Target) -> String {
return """
#import <Foundation/Foundation.h>
#if __cplusplus
extern "C" {
#endif
NSBundle* \(targetName)_SWIFTPM_MODULE_BUNDLE(void);
#define SWIFTPM_MODULE_BUNDLE \(targetName)_SWIFTPM_MODULE_BUNDLE()
#if __cplusplus
}
#endif
"""
}
static func objcImplementationFileContent(targetName: String, bundleName: String) -> String {
return """
#import <Foundation/Foundation.h>
#import "TuistBundle+\(targetName).h"
NSBundle* \(targetName)_SWIFTPM_MODULE_BUNDLE() {
NSURL *bundleURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"\(bundleName).bundle"];
NSBundle *bundle = [NSBundle bundleWithURL:bundleURL];
return bundle;
}
"""
}
}