Skip to content

Commit 69dc4de

Browse files
committed
Extend TargetSourcesBuilder to return other files than sources and resources, and extend the ExtensionEvaluator to pass them on to extensions.
1 parent 6aea3c9 commit 69dc4de

File tree

5 files changed

+39
-23
lines changed

5 files changed

+39
-23
lines changed

Sources/PackageLoading/PackageBuilder.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ public final class PackageBuilder {
744744
fs: fileSystem,
745745
diags: diagnostics
746746
)
747-
let (sources, resources, headers) = try sourcesBuilder.run()
747+
let (sources, resources, headers, others) = try sourcesBuilder.run()
748748

749749
// Make sure defaultLocalization is set if the target has localized resources.
750750
let hasLocalizedResources = resources.contains(where: { $0.localization != nil })
@@ -814,6 +814,7 @@ public final class PackageBuilder {
814814
type: targetType,
815815
sources: sources,
816816
resources: resources,
817+
others: others,
817818
dependencies: dependencies,
818819
swiftVersion: try swiftVersion(),
819820
buildSettings: buildSettings

Sources/PackageLoading/TargetSourcesBuilder.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public struct TargetSourcesBuilder {
135135
}
136136

137137
/// Run the builder to produce the sources of the target.
138-
public func run() throws -> (sources: Sources, resources: [Resource], headers: [AbsolutePath]) {
138+
public func run() throws -> (sources: Sources, resources: [Resource], headers: [AbsolutePath], others: [AbsolutePath]) {
139139
let contents = computeContents()
140140
var pathToRule: [AbsolutePath: Rule] = [:]
141141

@@ -146,6 +146,7 @@ public struct TargetSourcesBuilder {
146146
// Emit an error if we found files without a matching rule in
147147
// tools version >= v5_3. This will be activated once resources
148148
// support is complete.
149+
var others: [AbsolutePath] = []
149150
if toolsVersion >= .v5_3 {
150151
let filesWithNoRules = pathToRule.filter { $0.value.rule == .none }
151152
if !filesWithNoRules.isEmpty {
@@ -155,6 +156,7 @@ public struct TargetSourcesBuilder {
155156
}
156157
diags.emit(.warning(warning))
157158
}
159+
others.append(contentsOf: filesWithNoRules.keys)
158160
}
159161

160162
let headers = pathToRule.lazy.filter { $0.value.rule == .header }.map { $0.key }.sorted()
@@ -173,7 +175,7 @@ public struct TargetSourcesBuilder {
173175
throw Target.Error.mixedSources(targetPath)
174176
}
175177

176-
return (sources, resources, headers)
178+
return (sources, resources, headers, others)
177179
}
178180

179181
private struct Rule {

Sources/PackageModel/Target.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public class Target: ObjectIdentifierProtocol, PolymorphicCodableProtocol {
123123

124124
/// The resource files in the target.
125125
public let resources: [Resource]
126+
127+
/// Other kinds of files in the target.
128+
public let others: [AbsolutePath]
126129

127130
/// The list of platforms that are supported by this target.
128131
public let platforms: [SupportedPlatform]
@@ -143,6 +146,7 @@ public class Target: ObjectIdentifierProtocol, PolymorphicCodableProtocol {
143146
type: Kind,
144147
sources: Sources,
145148
resources: [Resource] = [],
149+
others: [AbsolutePath] = [],
146150
dependencies: [Target.Dependency],
147151
buildSettings: BuildSettings.AssignmentTable
148152
) {
@@ -153,13 +157,14 @@ public class Target: ObjectIdentifierProtocol, PolymorphicCodableProtocol {
153157
self.type = type
154158
self.sources = sources
155159
self.resources = resources
160+
self.others = others
156161
self.dependencies = dependencies
157162
self.c99name = self.name.spm_mangledToC99ExtendedIdentifier()
158163
self.buildSettings = buildSettings
159164
}
160165

161166
private enum CodingKeys: String, CodingKey {
162-
case name, bundleName, defaultLocalization, platforms, type, sources, resources, buildSettings
167+
case name, bundleName, defaultLocalization, platforms, type, sources, resources, others, buildSettings
163168
}
164169

165170
public func encode(to encoder: Encoder) throws {
@@ -174,6 +179,7 @@ public class Target: ObjectIdentifierProtocol, PolymorphicCodableProtocol {
174179
try container.encode(type, forKey: .type)
175180
try container.encode(sources, forKey: .sources)
176181
try container.encode(resources, forKey: .resources)
182+
try container.encode(others, forKey: .others)
177183
try container.encode(buildSettings, forKey: .buildSettings)
178184
}
179185

@@ -186,6 +192,7 @@ public class Target: ObjectIdentifierProtocol, PolymorphicCodableProtocol {
186192
self.type = try container.decode(Kind.self, forKey: .type)
187193
self.sources = try container.decode(Sources.self, forKey: .sources)
188194
self.resources = try container.decode([Resource].self, forKey: .resources)
195+
self.others = try container.decode([AbsolutePath].self, forKey: .others)
189196
// FIXME: dependencies property is skipped on purpose as it points to
190197
// the actual target dependency object.
191198
self.dependencies = []
@@ -230,6 +237,7 @@ public final class SwiftTarget: Target {
230237
type: Kind,
231238
sources: Sources,
232239
resources: [Resource] = [],
240+
others: [AbsolutePath] = [],
233241
dependencies: [Target.Dependency] = [],
234242
swiftVersion: SwiftLanguageVersion,
235243
buildSettings: BuildSettings.AssignmentTable = .init()
@@ -243,6 +251,7 @@ public final class SwiftTarget: Target {
243251
type: type,
244252
sources: sources,
245253
resources: resources,
254+
others: others,
246255
dependencies: dependencies,
247256
buildSettings: buildSettings
248257
)

Sources/SPMBuildCore/ExtensionEvaluator.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ extension PackageGraph {
6767
targetDir: target.sources.root.pathString,
6868
packageDir: package.path.pathString,
6969
sourceFiles: target.sources.paths.map{ $0.pathString },
70+
resourceFiles: target.underlyingTarget.resources.map{ $0.path.pathString },
71+
otherFiles: target.underlyingTarget.others.map { $0.pathString },
7072
dependencies: dependencyTargets.map {
7173
.init(targetName: $0.name, moduleName: $0.c99name, targetDir: $0.sources.root.pathString)
7274
},
@@ -289,6 +291,8 @@ struct ExtensionEvaluationInput: Codable {
289291
var targetDir: String
290292
var packageDir: String
291293
var sourceFiles: [String]
294+
var resourceFiles: [String]
295+
var otherFiles: [String]
292296
var dependencies: [DependencyTarget]
293297
public struct DependencyTarget: Codable {
294298
var targetName: String

Tests/PackageLoadingTests/TargetSourcesBuilderTests.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class TargetSourcesBuilderTests: XCTestCase {
143143
toolsVersion: .minimumRequired,
144144
fileTypes: ["something"])
145145

146-
build(target: target, additionalFileRules: [somethingRule], toolsVersion: .v5, fs: fs) { _, _, _, _ in
146+
build(target: target, additionalFileRules: [somethingRule], toolsVersion: .v5, fs: fs) { _, _, _, _,_ in
147147
// No diagnostics
148148
}
149149
}
@@ -161,7 +161,7 @@ class TargetSourcesBuilderTests: XCTestCase {
161161
"/Resources/Sub/foo.txt"
162162
)
163163

164-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
164+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
165165
diagnostics.check(diagnostic: "multiple resources named 'foo.txt' in target 'Foo'", behavior: .error)
166166
diagnostics.checkUnordered(diagnostic: "found 'Resources/foo.txt'", behavior: .note)
167167
diagnostics.checkUnordered(diagnostic: "found 'Resources/Sub/foo.txt'", behavior: .note)
@@ -181,7 +181,7 @@ class TargetSourcesBuilderTests: XCTestCase {
181181
"/Copied/foo.txt"
182182
)
183183

184-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
184+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
185185
diagnostics.check(diagnostic: "multiple resources named 'foo.txt' in target 'Foo'", behavior: .error)
186186
diagnostics.checkUnordered(diagnostic: "found 'Processed/foo.txt'", behavior: .note)
187187
diagnostics.checkUnordered(diagnostic: "found 'Copied/foo.txt'", behavior: .note)
@@ -201,7 +201,7 @@ class TargetSourcesBuilderTests: XCTestCase {
201201
"/Copied/foo.txt"
202202
)
203203

204-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
204+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
205205
// No diagnostics
206206
}
207207
}
@@ -219,7 +219,7 @@ class TargetSourcesBuilderTests: XCTestCase {
219219
"/B/Copy/foo.txt"
220220
)
221221

222-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
222+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
223223
diagnostics.check(diagnostic: "multiple resources named 'Copy' in target 'Foo'", behavior: .error)
224224
diagnostics.checkUnordered(diagnostic: "found 'A/Copy'", behavior: .note)
225225
diagnostics.checkUnordered(diagnostic: "found 'B/Copy'", behavior: .note)
@@ -239,7 +239,7 @@ class TargetSourcesBuilderTests: XCTestCase {
239239
"/B/EN.lproj/foo.txt"
240240
)
241241

242-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
242+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
243243
diagnostics.check(diagnostic: "multiple resources named 'en.lproj/foo.txt' in target 'Foo'", behavior: .error)
244244
diagnostics.checkUnordered(diagnostic: "found 'A/en.lproj/foo.txt'", behavior: .note)
245245
diagnostics.checkUnordered(diagnostic: "found 'B/EN.lproj/foo.txt'", behavior: .note)
@@ -259,7 +259,7 @@ class TargetSourcesBuilderTests: XCTestCase {
259259
"/B/en.lproj/foo.txt"
260260
)
261261

262-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
262+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
263263
diagnostics.check(diagnostic: "resource 'B/en.lproj' in target 'Foo' conflicts with other localization directories", behavior: .error)
264264
}
265265
}
@@ -272,7 +272,7 @@ class TargetSourcesBuilderTests: XCTestCase {
272272
"/en.lproj/Localizable.strings"
273273
)
274274

275-
build(target: target, toolsVersion: .v5_2, fs: fs) { _, resources, _, _ in
275+
build(target: target, toolsVersion: .v5_2, fs: fs) { _, resources, _, _, _ in
276276
XCTAssert(resources.isEmpty)
277277
// No diagnostics
278278
}
@@ -289,7 +289,7 @@ class TargetSourcesBuilderTests: XCTestCase {
289289
"/Copied/en.lproj/sub/Localizable.strings"
290290
)
291291

292-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
292+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
293293
diagnostics.check(diagnostic: "localization directory 'Processed/en.lproj' in target 'Foo' contains sub-directories, which is forbidden", behavior: .error)
294294
}
295295
}
@@ -303,7 +303,7 @@ class TargetSourcesBuilderTests: XCTestCase {
303303
"/Resources/en.lproj/Localizable.strings"
304304
)
305305

306-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
306+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
307307
diagnostics.check(
308308
diagnostic: .contains("""
309309
resource 'Resources/en.lproj/Localizable.strings' in target 'Foo' is in a localization directory \
@@ -330,7 +330,7 @@ class TargetSourcesBuilderTests: XCTestCase {
330330
"/Icon.png"
331331
)
332332

333-
build(target: target, defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
333+
build(target: target, defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
334334
diagnostics.check(
335335
diagnostic: .contains("resource 'Icon.png' in target 'Foo' is missing the default localization 'fr'"),
336336
behavior: .warning)
@@ -355,7 +355,7 @@ class TargetSourcesBuilderTests: XCTestCase {
355355
"/Icon.png"
356356
)
357357

358-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
358+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
359359
diagnostics.checkUnordered(
360360
diagnostic: .contains("resource 'Localizable.strings' in target 'Foo' has both localized and un-localized variants"),
361361
behavior: .warning)
@@ -391,7 +391,7 @@ class TargetSourcesBuilderTests: XCTestCase {
391391
"/Other/Image.png"
392392
)
393393

394-
build(target: target, defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, resources, _, diagnostics in
394+
build(target: target, defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, resources, _, _, diagnostics in
395395
XCTAssertEqual(Set(resources), [
396396
Resource(rule: .process, path: AbsolutePath("/Processed/foo.txt"), localization: nil),
397397
Resource(rule: .process, path: AbsolutePath("/Processed/En-uS.lproj/Localizable.stringsdict"), localization: "en-us"),
@@ -412,7 +412,7 @@ class TargetSourcesBuilderTests: XCTestCase {
412412
"/Foo/es.lproj/Image.png"
413413
)
414414

415-
build(target: try TargetDescription(name: "Foo"), defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, resources, _, diagnostics in
415+
build(target: try TargetDescription(name: "Foo"), defaultLocalization: "fr", toolsVersion: .v5_3, fs: fs) { _, resources, _, _, diagnostics in
416416
XCTAssertEqual(Set(resources), [
417417
Resource(rule: .process, path: AbsolutePath("/Foo/fr.lproj/Image.png"), localization: "fr"),
418418
Resource(rule: .process, path: AbsolutePath("/Foo/es.lproj/Image.png"), localization: "es"),
@@ -430,7 +430,7 @@ class TargetSourcesBuilderTests: XCTestCase {
430430
"/Resources/Processed/Info.plist"
431431
)
432432

433-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
433+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
434434
diagnostics.check(
435435
diagnostic: .contains("resource 'Resources/Processed/Info.plist' in target 'Foo' is forbidden"),
436436
behavior: .error)
@@ -446,7 +446,7 @@ class TargetSourcesBuilderTests: XCTestCase {
446446
"/Resources/Copied/Info.plist"
447447
)
448448

449-
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, diagnostics in
449+
build(target: target, toolsVersion: .v5_3, fs: fs) { _, _, _, _, diagnostics in
450450
diagnostics.check(
451451
diagnostic: .contains("resource 'Resources/Copied/Info.plist' in target 'Foo' is forbidden"),
452452
behavior: .error)
@@ -462,7 +462,7 @@ class TargetSourcesBuilderTests: XCTestCase {
462462
fs: FileSystem,
463463
file: StaticString = #file,
464464
line: UInt = #line,
465-
checker: (Sources, [Resource], [AbsolutePath], DiagnosticsEngineResult) -> ()
465+
checker: (Sources, [Resource], [AbsolutePath], [AbsolutePath], DiagnosticsEngineResult) -> ()
466466
) {
467467
let diagnostics = DiagnosticsEngine()
468468
let builder = TargetSourcesBuilder(
@@ -478,10 +478,10 @@ class TargetSourcesBuilderTests: XCTestCase {
478478
)
479479

480480
do {
481-
let (sources, resources, headers) = try builder.run()
481+
let (sources, resources, headers, others) = try builder.run()
482482

483483
DiagnosticsEngineTester(diagnostics, file: file, line: line) { diagnostics in
484-
checker(sources, resources, headers, diagnostics)
484+
checker(sources, resources, headers, others, diagnostics)
485485
}
486486
} catch {
487487
XCTFail(error.localizedDescription, file: file, line: line)

0 commit comments

Comments
 (0)