@@ -533,40 +533,25 @@ public final class UserToolchain: Toolchain {
533
533
if let customInstalledSwiftPMConfiguration {
534
534
self . installedSwiftPMConfiguration = customInstalledSwiftPMConfiguration
535
535
} 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 )
543
543
}
544
544
545
545
if let customProvidedLibraries {
546
546
self . providedLibraries = customProvidedLibraries
547
547
} 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: [ ] )
570
555
}
571
556
572
557
// Use the triple from Swift SDK or compute the host triple using swiftc.
@@ -895,4 +880,19 @@ public final class UserToolchain: Toolchain {
895
880
}
896
881
}
897
882
}
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
+ }
898
898
}
0 commit comments