Skip to content

Commit ffbaa1c

Browse files
committed
configuration utilties
motivation: with SE-0292 adding more configuration, refactor the configuration uttilities to their own module so they can be used consistently across the code changes: * create a new Configuration module * refactor Collections sources storage to use the configuration module * refactor Mirrors to use the configuraiton module * refactor netrc to use the configuraiton module * adjust and add tests
1 parent 7cb50e4 commit ffbaa1c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1247
-303
lines changed

.swiftpm.lock

Whitespace-only changes.

Package.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ let package = Package(
128128
name: "Basics",
129129
dependencies: ["SwiftToolsSupport-auto"]),
130130

131+
.target(
132+
name: "Configurations",
133+
dependencies: ["SwiftToolsSupport-auto", "Basics"]),
134+
131135
.target(
132136
/** The llbuild manifest model */
133137
name: "LLBuildManifest",
@@ -151,7 +155,7 @@ let package = Package(
151155
.target(
152156
/** Package model conventions and loading support */
153157
name: "PackageLoading",
154-
dependencies: ["SwiftToolsSupport-auto", "Basics", "PackageModel", "SourceControl"]),
158+
dependencies: ["SwiftToolsSupport-auto", "Basics", "Configurations", "PackageModel", "SourceControl"]),
155159

156160
// MARK: Package Dependency Resolution
157161

@@ -182,7 +186,7 @@ let package = Package(
182186
.target(
183187
/** Data structures and support for package collections */
184188
name: "PackageCollections",
185-
dependencies: ["SwiftToolsSupport-auto", "Basics", "PackageModel", "SourceControl", "PackageCollectionsModel", "PackageCollectionsSigning"]),
189+
dependencies: ["SwiftToolsSupport-auto", "Basics", "Configurations", "PackageModel", "SourceControl", "PackageCollectionsModel", "PackageCollectionsSigning"]),
186190

187191
// MARK: Package Manager Functionality
188192

@@ -206,7 +210,7 @@ let package = Package(
206210
.target(
207211
/** High level functionality */
208212
name: "Workspace",
209-
dependencies: ["SwiftToolsSupport-auto", "Basics", "SPMBuildCore", "PackageGraph", "PackageModel", "SourceControl", "Xcodeproj"]),
213+
dependencies: ["SwiftToolsSupport-auto", "Basics", "Configurations", "SPMBuildCore", "PackageGraph", "PackageModel", "SourceControl", "Xcodeproj"]),
210214

211215
// MARK: Commands
212216

Sources/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
add_subdirectory(Basics)
1010
add_subdirectory(Build)
1111
add_subdirectory(Commands)
12+
add_subdirectory(Configurations)
1213
add_subdirectory(LLBuildManifest)
1314
add_subdirectory(PackageCollections)
1415
add_subdirectory(PackageCollectionsModel)

Sources/Commands/SwiftPackageCollectionsTool.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import ArgumentParser
1212
import Basics
13+
import Configurations
1314
import Foundation
1415
import PackageCollections
1516
import PackageModel
@@ -359,7 +360,9 @@ private extension JSONEncoder {
359360

360361
private extension ParsableCommand {
361362
func with<T>(handler: (_ collections: PackageCollectionsProtocol) throws -> T) throws -> T {
362-
let collections = PackageCollections()
363+
let configuration = Configuration.Collections(fileSystem: localFileSystem) // FIXME: share across calls
364+
let diagnosticsEngine = DiagnosticsEngine() // // FIXME: share across calls
365+
let collections = PackageCollections(configuration: configuration, diagnosticsEngine: diagnosticsEngine)
363366
defer {
364367
do {
365368
try collections.shutdown()

Sources/Commands/SwiftPackageTool.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,6 @@ extension SwiftPackageTool.Config {
886886
var mirrorURL: String
887887

888888
func run(_ swiftTool: SwiftTool) throws {
889-
let config = try swiftTool.getSwiftPMConfig()
890-
891889
if packageURL != nil {
892890
swiftTool.diagnostics.emit(
893891
warning: "'--package-url' option is deprecated; use '--original-url' instead")
@@ -898,8 +896,10 @@ extension SwiftPackageTool.Config {
898896
throw ExitCode.failure
899897
}
900898

901-
config.mirrors.set(mirrorURL: mirrorURL, forURL: originalURL)
902-
try config.saveState()
899+
let config = try swiftTool.getSwiftPMConfiguration()
900+
try config.mirrors.withMapping { mapping in
901+
mapping.set(mirrorURL: mirrorURL, forURL: originalURL)
902+
}
903903
}
904904
}
905905

@@ -920,8 +920,6 @@ extension SwiftPackageTool.Config {
920920
var mirrorURL: String?
921921

922922
func run(_ swiftTool: SwiftTool) throws {
923-
let config = try swiftTool.getSwiftPMConfig()
924-
925923
if packageURL != nil {
926924
swiftTool.diagnostics.emit(
927925
warning: "'--package-url' option is deprecated; use '--original-url' instead")
@@ -932,8 +930,10 @@ extension SwiftPackageTool.Config {
932930
throw ExitCode.failure
933931
}
934932

935-
try config.mirrors.unset(originalOrMirrorURL: originalOrMirrorURL)
936-
try config.saveState()
933+
let config = try swiftTool.getSwiftPMConfiguration()
934+
try config.mirrors.withMapping { mapping in
935+
try mapping.unset(originalOrMirrorURL: originalOrMirrorURL)
936+
}
937937
}
938938
}
939939

@@ -951,8 +951,6 @@ extension SwiftPackageTool.Config {
951951
var originalURL: String?
952952

953953
func run(_ swiftTool: SwiftTool) throws {
954-
let config = try swiftTool.getSwiftPMConfig()
955-
956954
if packageURL != nil {
957955
swiftTool.diagnostics.emit(
958956
warning: "'--package-url' option is deprecated; use '--original-url' instead")
@@ -963,6 +961,7 @@ extension SwiftPackageTool.Config {
963961
throw ExitCode.failure
964962
}
965963

964+
let config = try swiftTool.getSwiftPMConfiguration()
966965
if let mirror = config.mirrors.mirrorURL(for: originalURL) {
967966
print(mirror)
968967
} else {

Sources/Commands/SwiftTool.swift

Lines changed: 107 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11+
import ArgumentParser
12+
import Basics
13+
import Build
14+
import Configurations
15+
import Dispatch
1116
import func Foundation.NSUserName
1217
import class Foundation.ProcessInfo
1318
import func Foundation.NSHomeDirectory
14-
import Dispatch
15-
16-
import ArgumentParser
17-
import TSCLibc
18-
import TSCBasic
19-
import TSCUtility
20-
21-
import PackageModel
2219
import PackageGraph
20+
import PackageModel
2321
import SourceControl
2422
import SPMBuildCore
25-
import Build
26-
import XCBuildSupport
23+
import TSCBasic
24+
import TSCLibc
25+
import TSCUtility
2726
import Workspace
28-
import Basics
27+
import XCBuildSupport
2928

3029
typealias Diagnostic = TSCBasic.Diagnostic
3130

@@ -459,14 +458,81 @@ public class SwiftTool {
459458
return try getPackageRoot().appending(component: "Packages")
460459
}
461460

461+
// FIXME: move out of here
462462
func resolvedFilePath() throws -> AbsolutePath {
463463
if let multiRootPackageDataFile = options.multirootPackageDataFile {
464464
return multiRootPackageDataFile.appending(components: "xcshareddata", "swiftpm", "Package.resolved")
465465
}
466466
return try getPackageRoot().appending(component: "Package.resolved")
467467
}
468468

469-
func configFilePath() throws -> AbsolutePath {
469+
// FIXME: global vs local, default locations
470+
func getSwiftPMConfiguration() throws -> Configuration {
471+
let fileSystem = localFileSystem
472+
return .init(resolution: try self.getResolutionConfiguration(fileSystem: fileSystem),
473+
manifestsLoading: try self.getManifestLoadingConfiguration(fileSystem: fileSystem),
474+
mirrors: try self.getMirrorsConfiguration(fileSystem: fileSystem),
475+
netrc: try self.getNetrcConfiguration(fileSystem: fileSystem),
476+
collections: try self.getCollectionsConfiguration(fileSystem: fileSystem)
477+
)
478+
}
479+
480+
func getManifestLoadingConfiguration(fileSystem: FileSystem) throws -> Configuration.ManifestsLoading {
481+
return .init(
482+
cachePath: try self.resolveManifestCachePath(fileSystem: fileSystem),
483+
isManifestSandboxEnabled: !self.options.shouldDisableSandbox
484+
)
485+
}
486+
487+
// FIXME: move out of here
488+
func resolveManifestCachePath(fileSystem: FileSystem) throws -> AbsolutePath? {
489+
switch (self.options.shouldDisableManifestCaching, self.options.manifestCachingMode) {
490+
case (true, _):
491+
// backwards compatibility
492+
return .none
493+
case (false, .none):
494+
return .none
495+
case (false, .local):
496+
return self.buildPath
497+
case (false, .shared):
498+
return try self.resolveCachePath(fileSystem: fileSystem).map { Configuration.ManifestsLoading.cachePath(rootCachePath: $0) }
499+
}
500+
}
501+
502+
func getResolutionConfiguration(fileSystem: FileSystem) throws -> Configuration.Resolution {
503+
return .init(
504+
repositories: try getRepositoriesResolutionConfiguration(fileSystem: fileSystem),
505+
prefetchingEnabled: options.shouldEnableResolverPrefetching,
506+
tracingEnabled: options.enableResolverTrace,
507+
skipUpdate: options.skipDependencyUpdate
508+
)
509+
}
510+
511+
func getRepositoriesResolutionConfiguration(fileSystem: FileSystem) throws -> Configuration.Resolution.Repositories {
512+
return .init(
513+
cachePath: try self.resolveRepositoriesResolutionCachePath(fileSystem: fileSystem)
514+
)
515+
}
516+
517+
// FIXME: move out of here
518+
func resolveRepositoriesResolutionCachePath(fileSystem: FileSystem) throws -> AbsolutePath? {
519+
guard self.options.useRepositoriesCache else {
520+
return .none
521+
}
522+
// FIXME
523+
return try self.resolveCachePath(fileSystem: fileSystem).map { Configuration.Resolution.Repositories.cachePath(rootCachePath: $0) }
524+
}
525+
526+
func getMirrorsConfiguration(fileSystem: FileSystem) throws -> Configuration.Mirrors {
527+
return .init(
528+
fileSystem: fileSystem,
529+
path: try self.resolveMirrorsConfigFilePath(fileSystem: fileSystem)
530+
)
531+
}
532+
533+
// FIXME: move out of here
534+
// FIXME: global vs local, default locations
535+
func resolveMirrorsConfigFilePath(fileSystem: FileSystem) throws -> AbsolutePath {
470536
// Look for the override in the environment.
471537
if let envPath = ProcessEnv.vars["SWIFTPM_MIRROR_CONFIG"] {
472538
return try AbsolutePath(validating: envPath)
@@ -479,21 +545,21 @@ public class SwiftTool {
479545
return try getPackageRoot().appending(components: ".swiftpm", "config")
480546
}
481547

482-
func getSwiftPMConfig() throws -> Workspace.Configuration {
483-
return try _swiftpmConfig.get()
548+
func getNetrcConfiguration(fileSystem: FileSystem) throws -> Configuration.Netrc {
549+
return .init(
550+
fileSystem: fileSystem,
551+
path: try self.resolveNetrcConfigFilePath(fileSystem: fileSystem)
552+
)
484553
}
485554

486-
private lazy var _swiftpmConfig: Result<Workspace.Configuration, Swift.Error> = {
487-
return Result(catching: { try Workspace.Configuration(path: try configFilePath()) })
488-
}()
489-
490-
func resolvedNetrcFilePath() throws -> AbsolutePath? {
555+
// FIXME: move out of here
556+
func resolveNetrcConfigFilePath(fileSystem: FileSystem) throws -> AbsolutePath? {
491557
guard options.netrc ||
492558
options.netrcFilePath != nil ||
493559
options.netrcOptional else { return nil }
494560

495561
let resolvedPath: AbsolutePath = options.netrcFilePath ?? AbsolutePath("\(NSHomeDirectory())/.netrc")
496-
guard localFileSystem.exists(resolvedPath) else {
562+
guard fileSystem.exists(resolvedPath) else {
497563
if !options.netrcOptional {
498564
diagnostics.emit(error: "Cannot find mandatory .netrc file at \(resolvedPath.pathString). To make .netrc file optional, use --netrc-optional flag.")
499565
throw ExitCode.failure
@@ -505,7 +571,20 @@ public class SwiftTool {
505571
return resolvedPath
506572
}
507573

508-
private func getCachePath(fileSystem: FileSystem = localFileSystem) throws -> AbsolutePath? {
574+
func getCollectionsConfiguration(fileSystem: FileSystem) throws -> Configuration.Collections {
575+
return .init(
576+
fileSystem: fileSystem,
577+
path: try self.resolveCollectionsFilePath(fileSystem: fileSystem)
578+
)
579+
}
580+
581+
// FIXME: move out of here
582+
func resolveCollectionsFilePath(fileSystem: FileSystem) throws -> AbsolutePath? {
583+
return try self.resolveConfigPath(fileSystem: fileSystem).flatMap{ $0.appending(component: "collections.json") }
584+
}
585+
586+
// FIXME: move out of here
587+
private func resolveCachePath(fileSystem: FileSystem) throws -> AbsolutePath? {
509588
if let explicitCachePath = options.cachePath {
510589
// Create the explicit cache path if necessary
511590
if !fileSystem.exists(explicitCachePath) {
@@ -522,7 +601,8 @@ public class SwiftTool {
522601
}
523602
}
524603

525-
private func getConfigPath(fileSystem: FileSystem = localFileSystem) throws -> AbsolutePath? {
604+
// FIXME: move out of here
605+
private func resolveConfigPath(fileSystem: FileSystem) throws -> AbsolutePath? {
526606
if let explicitConfigPath = options.configPath {
527607
// Create the explicit config path if necessary
528608
if !fileSystem.exists(explicitConfigPath) {
@@ -548,24 +628,17 @@ public class SwiftTool {
548628
let isVerbose = options.verbosity != 0
549629
let delegate = ToolWorkspaceDelegate(self.stdoutStream, isVerbose: isVerbose, diagnostics: diagnostics)
550630
let provider = GitRepositoryProvider(processSet: processSet)
551-
let cachePath = self.options.useRepositoriesCache ? try self.getCachePath() : .none
552-
_ = try self.getConfigPath() // TODO: actually use this in the workspace
553631
let isXcodeBuildSystemEnabled = self.options.buildSystem == .xcode
554632
let workspace = Workspace(
633+
configuration: try getSwiftPMConfiguration(),
555634
dataPath: buildPath,
556635
editablesPath: try editablesPath(),
557636
pinsFile: try resolvedFilePath(),
558637
manifestLoader: try getManifestLoader(),
559638
toolsVersionLoader: ToolsVersionLoader(),
560639
delegate: delegate,
561-
config: try getSwiftPMConfig(),
562640
repositoryProvider: provider,
563-
netrcFilePath: try resolvedNetrcFilePath(),
564-
additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription.xcbuildFileTypes : FileRuleDescription.swiftpmFileTypes,
565-
isResolverPrefetchingEnabled: options.shouldEnableResolverPrefetching,
566-
skipUpdate: options.skipDependencyUpdate,
567-
enableResolverTrace: options.enableResolverTrace,
568-
cachePath: cachePath
641+
additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription.xcbuildFileTypes : FileRuleDescription.swiftpmFileTypes
569642
)
570643
_workspace = workspace
571644
_workspaceDelegate = delegate
@@ -852,30 +925,17 @@ public class SwiftTool {
852925

853926
private lazy var _manifestLoader: Result<ManifestLoader, Swift.Error> = {
854927
return Result(catching: {
855-
let cachePath: AbsolutePath?
856-
switch (self.options.shouldDisableManifestCaching, self.options.manifestCachingMode) {
857-
case (true, _):
858-
// backwards compatibility
859-
cachePath = nil
860-
case (false, .none):
861-
cachePath = nil
862-
case (false, .local):
863-
cachePath = self.buildPath
864-
case (false, .shared):
865-
cachePath = try self.getCachePath().map{ $0.appending(component: "manifests") }
866-
}
867-
868928
var extraManifestFlags = self.options.manifestFlags
869929
// Disable the implicit concurrency import if the compiler in use supports it to avoid warnings if we are building against an older SDK that does not contain a Concurrency module.
870930
if SwiftTargetBuildDescription.checkSupportedFrontendFlags(flags: ["disable-implicit-concurrency-module-import"], fs: localFileSystem) {
871931
extraManifestFlags += ["-Xfrontend", "-disable-implicit-concurrency-module-import"]
872932
}
873933

934+
let configuration = try self.getSwiftPMConfiguration()
874935
return try ManifestLoader(
875-
// Always use the host toolchain's resources for parsing manifest.
936+
configuration: configuration.manifestsLoading,
937+
// Always use the host toolchain's for parsing manifest.
876938
toolchain: self._hostToolchain.get().configuration,
877-
isManifestSandboxEnabled: !self.options.shouldDisableSandbox,
878-
cacheDir: cachePath,
879939
extraManifestFlags: extraManifestFlags
880940
)
881941
})

0 commit comments

Comments
 (0)