Skip to content

Commit

Permalink
refactor workspace initializer
Browse files Browse the repository at this point in the history
motivation: workspace initializer is very robust, accomodating the needs of both libSwiftPM consumer and testing. we want to seperate these concerns a bit so that libSwiftPM exposes the minimum API surface area

changes:
* deprecate existing workspace inisitlazer
* create new minimalistic public initializer oriented towards libSwiftPM consumers
* create internal initializer with all custmization options oriented towards testing
* better abstract resolver configuraiton and other behavior settings into a struct
* update callsite and tests to the new API
  • Loading branch information
tomerd committed Dec 11, 2021
1 parent e0b1bd2 commit d2ba4e0
Show file tree
Hide file tree
Showing 4 changed files with 353 additions and 193 deletions.
20 changes: 11 additions & 9 deletions Sources/Commands/SwiftTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ public class SwiftTool {
}

let delegate = ToolWorkspaceDelegate(self.outputStream, logLevel: self.logLevel, observabilityScope: self.observabilityScope)
let provider = GitRepositoryProvider(processSet: processSet)
let provider = GitRepositoryProvider(processSet: self.processSet)
let sharedSecurityDirectory = try self.getSharedSecurityDirectory()
let sharedCacheDirectory = try self.getSharedCacheDirectory()
let sharedConfigurationDirectory = try self.getSharedConfigurationDirectory()
Expand All @@ -677,16 +677,18 @@ public class SwiftTool {
sharedCacheDirectory: sharedCacheDirectory,
sharedConfigurationDirectory: sharedConfigurationDirectory
),
mirrors: self.getMirrorsConfig(sharedConfigurationDirectory: sharedConfigurationDirectory).mirrors,
registries: try self.getRegistriesConfig(sharedConfigurationDirectory: sharedConfigurationDirectory).configuration,
authorizationProvider: self.getAuthorizationProvider(),
customManifestLoader: self.getManifestLoader(), // FIXME: doe we really need to customize it?
customRepositoryProvider: provider, // FIXME: doe we really need to customize it?
additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription.xcbuildFileTypes : FileRuleDescription.swiftpmFileTypes,
resolverUpdateEnabled: !options.skipDependencyUpdate,
resolverPrefetchingEnabled: options.shouldEnableResolverPrefetching,
resolverFingerprintCheckingMode: self.options.resolverFingerprintCheckingMode,
sharedRepositoriesCacheEnabled: self.options.useRepositoriesCache,
mirrors: self.getMirrorsConfig(sharedConfigurationDirectory: sharedConfigurationDirectory).mirrors,
resolutionConfiguration: ResolutionConfiguration(
updateEnabled: !options.skipDependencyUpdate,
prefetchingEnabled: options.shouldEnableResolverPrefetching,
additionalFileRules: isXcodeBuildSystemEnabled ? FileRuleDescription.xcbuildFileTypes : FileRuleDescription.swiftpmFileTypes,
sharedRepositoriesCacheEnabled: self.options.useRepositoriesCache,
fingerprintCheckingMode: self.options.resolverFingerprintCheckingMode
),
customManifestLoader: self.getManifestLoader(), // FIXME: ideally we would not customize the manifest loader
customRepositoryProvider: provider, // FIXME: ideally we would not customize the repository provider. its currently done for shutdown handling which can be better abstracted
delegate: delegate
)
_workspace = workspace
Expand Down
34 changes: 19 additions & 15 deletions Sources/SPMTestSupport/MockWorkspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public final class MockWorkspace {
public var registryClient: RegistryClient
public let archiver: MockArchiver
public let checksumAlgorithm: MockHashAlgorithm
public let fingerprintStorage: MockPackageFingerprintStorage
let roots: [MockPackage]
let packages: [MockPackage]
public let mirrors: DependencyMirrors
public let fingerprints: MockPackageFingerprintStorage
let identityResolver: IdentityResolver
public var manifestLoader: MockManifestLoader
public var repositoryProvider: InMemoryGitRepositoryProvider
Expand All @@ -42,15 +42,15 @@ public final class MockWorkspace {
public init(
sandbox: AbsolutePath,
fileSystem: InMemoryFileSystem,
mirrors: DependencyMirrors? = nil,
roots: [MockPackage],
packages: [MockPackage],
toolsVersion: ToolsVersion = ToolsVersion.currentToolsVersion,
customHttpClient: HTTPClient? = .none,
customRegistryClient: RegistryClient? = .none,
customBinaryArchiver: MockArchiver? = .none,
customChecksumAlgorithm: MockHashAlgorithm? = .none,
customFingerprintStorage: MockPackageFingerprintStorage? = .none,
mirrors customMirrors: DependencyMirrors? = nil,
fingerprints customFingerprints: MockPackageFingerprintStorage? = .none,
httpClient customHttpClient: HTTPClient? = .none,
registryClient customRegistryClient: RegistryClient? = .none,
binaryArchiver customBinaryArchiver: MockArchiver? = .none,
checksumAlgorithm customChecksumAlgorithm: MockHashAlgorithm? = .none,
resolverUpdateEnabled: Bool = true
) throws {
let archiver = customBinaryArchiver ?? MockArchiver()
Expand All @@ -61,8 +61,8 @@ public final class MockWorkspace {
self.httpClient = httpClient
self.archiver = archiver
self.checksumAlgorithm = customChecksumAlgorithm ?? MockHashAlgorithm()
self.fingerprintStorage = customFingerprintStorage ?? MockPackageFingerprintStorage()
self.mirrors = mirrors ?? DependencyMirrors()
self.fingerprints = customFingerprints ?? MockPackageFingerprintStorage()
self.mirrors = customMirrors ?? DependencyMirrors()
self.identityResolver = DefaultIdentityResolver(locationMapper: self.mirrors.effectiveURL(for:))
self.roots = roots
self.packages = packages
Expand All @@ -73,7 +73,7 @@ public final class MockWorkspace {
identityResolver: self.identityResolver,
checksumAlgorithm: self.checksumAlgorithm,
filesystem: self.fileSystem,
fingerprintStorage: self.fingerprintStorage
fingerprintStorage: self.fingerprints
)
self.registryClient = customRegistryClient ?? self.registry.registryClient
self.toolsVersion = toolsVersion
Expand Down Expand Up @@ -220,7 +220,7 @@ public final class MockWorkspace {
return workspace
}

let workspace = try Workspace(
let workspace = try Workspace._init(
fileSystem: self.fileSystem,
location: .init(
workingDirectory: self.sandbox.appending(component: ".build"),
Expand All @@ -230,7 +230,15 @@ public final class MockWorkspace {
sharedCacheDirectory: self.fileSystem.swiftPMCacheDirectory,
sharedConfigurationDirectory: self.fileSystem.swiftPMConfigDirectory
),
fingerprints: self.fingerprints,
mirrors: self.mirrors,
resolutionConfiguration: ResolutionConfiguration(
updateEnabled: self.resolverUpdateEnabled,
prefetchingEnabled: true,
additionalFileRules: ResolutionConfiguration.default.additionalFileRules,
sharedRepositoriesCacheEnabled: ResolutionConfiguration.default.sharedRepositoriesCacheEnabled,
fingerprintCheckingMode: .strict
),
customToolsVersion: self.toolsVersion,
customManifestLoader: self.manifestLoader,
customRepositoryProvider: self.repositoryProvider,
Expand All @@ -239,10 +247,6 @@ public final class MockWorkspace {
customHTTPClient: self.httpClient,
customArchiver: self.archiver,
customChecksumAlgorithm: self.checksumAlgorithm,
customFingerprintStorage: self.fingerprintStorage,
resolverUpdateEnabled: self.resolverUpdateEnabled,
resolverPrefetchingEnabled: true,
resolverFingerprintCheckingMode: .strict,
delegate: self.delegate
)

Expand Down
Loading

0 comments on commit d2ba4e0

Please sign in to comment.