Skip to content

Commit 0246381

Browse files
committed
UserToolchain: Unify handling of confg.json and provided-libraries.json
1 parent ea1730b commit 0246381

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

Sources/PackageModel/UserToolchain.swift

+29-29
Original file line numberDiff line numberDiff line change
@@ -533,40 +533,25 @@ public final class UserToolchain: Toolchain {
533533
if let customInstalledSwiftPMConfiguration {
534534
self.installedSwiftPMConfiguration = customInstalledSwiftPMConfiguration
535535
} else {
536-
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "config.json"])
537-
if localFileSystem.exists(path) {
538-
self.installedSwiftPMConfiguration = try JSONDecoder.makeWithDefaults().decode(path: path, fileSystem: localFileSystem, as: InstalledSwiftPMConfiguration.self)
539-
} else {
540-
// We *could* eventually make this an error, but not for a few releases.
541-
self.installedSwiftPMConfiguration = InstalledSwiftPMConfiguration.default
542-
}
536+
let path = swiftCompilerPath.parentDirectory.parentDirectory.appending(components: [
537+
"share", "pm", "config.json",
538+
])
539+
self.installedSwiftPMConfiguration = try Self.loadJSONResource(
540+
config: path,
541+
type: InstalledSwiftPMConfiguration.self,
542+
default: InstalledSwiftPMConfiguration.default)
543543
}
544544

545545
if let customProvidedLibraries {
546546
self.providedLibraries = customProvidedLibraries
547547
} else {
548-
// When building with CMake or `swift build --build-system xcode`, we need to skip resource support.
549-
#if SKIP_RESOURCE_SUPPORT
550-
let path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "provided-libraries.json"])
551-
#else
552-
let path: AbsolutePath
553-
if let developmentPath = Bundle.module.path(forResource: "provided-libraries", ofType: "json") {
554-
// During development, we should be able to find the metadata file using `Bundle.module`.
555-
path = try AbsolutePath(validating: developmentPath)
556-
} else {
557-
// When deployed, we can find the metadata file in the toolchain.
558-
path = self.swiftCompilerPath.parentDirectory.parentDirectory.appending(components: ["share", "pm", "provided-libraries.json"])
559-
}
560-
#endif
561-
if localFileSystem.exists(path) {
562-
self.providedLibraries = try JSONDecoder.makeWithDefaults().decode(
563-
path: path,
564-
fileSystem: localFileSystem,
565-
as: [LibraryMetadata].self
566-
)
567-
} else {
568-
self.providedLibraries = []
569-
}
548+
let path = swiftCompilerPath.parentDirectory.parentDirectory.appending(components: [
549+
"share", "pm", "provided-libraries.json",
550+
])
551+
self.providedLibraries = try Self.loadJSONResource(
552+
config: path,
553+
type: [LibraryMetadata].self,
554+
default: [])
570555
}
571556

572557
// Use the triple from Swift SDK or compute the host triple using swiftc.
@@ -895,4 +880,19 @@ public final class UserToolchain: Toolchain {
895880
}
896881
}
897882
}
883+
884+
private static func loadJSONResource<T: Decodable>(
885+
config: AbsolutePath, type: T.Type, `default`: T
886+
)
887+
throws -> T
888+
{
889+
if localFileSystem.exists(config) {
890+
return try JSONDecoder.makeWithDefaults().decode(
891+
path: config,
892+
fileSystem: localFileSystem,
893+
as: type)
894+
}
895+
896+
return `default`
897+
}
898898
}

0 commit comments

Comments
 (0)