diff --git a/Sources/ScipioKit/Producer/FrameworkProducer.swift b/Sources/ScipioKit/Producer/FrameworkProducer.swift index c94a594..68d7939 100644 --- a/Sources/ScipioKit/Producer/FrameworkProducer.swift +++ b/Sources/ScipioKit/Producer/FrameworkProducer.swift @@ -10,15 +10,15 @@ struct FrameworkProducer { private let descriptionPackage: DescriptionPackage private let baseBuildOptions: BuildOptions private let buildOptionsMatrix: [String: BuildOptions] - private let cacheMode: Runner.Options.CacheMode + private let cachePolicies: [Runner.Options.CachePolicy] private let overwrite: Bool private let outputDir: URL private let fileSystem: any FileSystem private let toolchainEnvironment: [String: String]? private var shouldGenerateVersionFile: Bool { - // cacheMode is not disabled - if case .storages(let configs) = cacheMode, configs.isEmpty { + // cache is not disabled + guard !cachePolicies.isEmpty else { return false } @@ -33,7 +33,7 @@ struct FrameworkProducer { descriptionPackage: DescriptionPackage, buildOptions: BuildOptions, buildOptionsMatrix: [String: BuildOptions], - cacheMode: Runner.Options.CacheMode, + cachePolicies: [Runner.Options.CachePolicy], overwrite: Bool, outputDir: URL, toolchainEnvironment: [String: String]? = nil, @@ -42,7 +42,7 @@ struct FrameworkProducer { self.descriptionPackage = descriptionPackage self.baseBuildOptions = buildOptions self.buildOptionsMatrix = buildOptionsMatrix - self.cacheMode = cacheMode + self.cachePolicies = cachePolicies self.overwrite = overwrite self.outputDir = outputDir self.toolchainEnvironment = toolchainEnvironment @@ -96,14 +96,10 @@ struct FrameworkProducer { ) let targetsToBuild: OrderedSet - switch cacheMode { - case .storages(let configs): - if configs.isEmpty { - // no-op because cache is disabled - targetsToBuild = allTargets - break - } - + if cachePolicies.isEmpty { + // no-op because cache is disabled + targetsToBuild = allTargets + } else { let targets = Set(allTargets) // Validate the existing frameworks in `outputDir` before restoration @@ -112,8 +108,8 @@ struct FrameworkProducer { cacheSystem: cacheSystem ) - let storagesWithConsumer = configs.compactMap { config in - config.actors.contains(.consumer) ? config.storage : nil + let storagesWithConsumer = cachePolicies.compactMap { cachePolicy in + cachePolicy.actors.contains(.consumer) ? cachePolicy.storage : nil } if storagesWithConsumer.isEmpty { // no-op @@ -121,6 +117,7 @@ struct FrameworkProducer { } else { let restored = await restoreAllAvailableCachesIfNeeded( availableTargets: targets.subtracting(valid), + to: storagesWithConsumer, cacheSystem: cacheSystem ) targetsToBuild = allTargets @@ -196,29 +193,17 @@ struct FrameworkProducer { private func restoreAllAvailableCachesIfNeeded( availableTargets: Set, + to storages: [any CacheStorage], cacheSystem: CacheSystem ) async -> Set { - let cacheStorages: [any CacheStorage] - - switch cacheMode { - case .storages(let configs): - guard !configs.isEmpty else { return [] } - - let storagesWithConsumer = configs.compactMap { config in - config.actors.contains(.consumer) ? config.storage : nil - } - guard !storagesWithConsumer.isEmpty else { return [] } - cacheStorages = storagesWithConsumer - } - var remainingTargets = availableTargets var restored: Set = [] - for index in cacheStorages.indices { - let storage = cacheStorages[index] + for index in storages.indices { + let storage = storages[index] let logSuffix = "[\(index)] \(type(of: storage))" - if index == cacheStorages.startIndex { + if index == storages.startIndex { logger.info( "▶️ Starting restoration with cache storage: \(logSuffix)", metadata: .color(.green) @@ -357,16 +342,13 @@ struct FrameworkProducer { } private func cacheFrameworksIfNeeded(_ targets: Set, cacheSystem: CacheSystem) async { - switch cacheMode { - case .storages(let configs): - guard !configs.isEmpty else { return } + guard !cachePolicies.isEmpty else { return } - let storagesWithProducer = configs.compactMap { config in - config.actors.contains(.producer) ? config.storage : nil - } - if !storagesWithProducer.isEmpty { - await cacheSystem.cacheFrameworks(targets, to: storagesWithProducer) - } + let storagesWithProducer = cachePolicies.compactMap { cachePolicy in + cachePolicy.actors.contains(.producer) ? cachePolicy.storage : nil + } + if !storagesWithProducer.isEmpty { + await cacheSystem.cacheFrameworks(targets, to: storagesWithProducer) } } diff --git a/Sources/ScipioKit/Runner.swift b/Sources/ScipioKit/Runner.swift index 027be6a..4b99ce9 100644 --- a/Sources/ScipioKit/Runner.swift +++ b/Sources/ScipioKit/Runner.swift @@ -97,7 +97,7 @@ public struct Runner { descriptionPackage: descriptionPackage, buildOptions: buildOptions, buildOptionsMatrix: buildOptionsMatrix, - cacheMode: options.cacheMode, + cachePolicies: options.cachePolicies, overwrite: options.overwrite, outputDir: outputDir ) @@ -208,17 +208,7 @@ extension Runner { public var buildOptionsMatrix: [String: TargetBuildOptions] } - public enum CacheMode: Sendable { - public struct StorageConfig: Sendable { - public let storage: any CacheStorage - public let actors: Set - - public init(storage: any CacheStorage, actors: Set) { - self.storage = storage - self.actors = actors - } - } - + public struct CachePolicy: Sendable { public enum CacheActorKind: Sendable { // Save built product to cacheStorage case producer @@ -226,17 +216,23 @@ extension Runner { case consumer } - case storages([StorageConfig]) + public let storage: any CacheStorage + public let actors: Set - public static let disabled: Self = .storages([]) + public init(storage: any CacheStorage, actors: Set) { + self.storage = storage + self.actors = actors + } - public static let project: Self = .storages([ - .init(storage: ProjectCacheStorage(), actors: [.producer]), - ]) + public static let project = Self( + storage: ProjectCacheStorage(), + actors: [.producer] + ) - public static func storage(_ config: StorageConfig) -> Self { - .storages([config]) - } + public static let localDisk = Self( + storage: LocalDiskCacheStorage(), + actors: [.producer, .consumer] + ) } public enum PlatformSpecifier: Equatable { @@ -255,7 +251,7 @@ extension Runner { public var buildOptionsContainer: BuildOptionsContainer public var shouldOnlyUseVersionsFromResolvedFile: Bool - public var cacheMode: CacheMode + public var cachePolicies: [CachePolicy] public var overwrite: Bool public var verbose: Bool public var toolchainEnvironment: [String: String]? @@ -264,7 +260,7 @@ extension Runner { baseBuildOptions: BuildOptions = .init(), buildOptionsMatrix: [String: TargetBuildOptions] = [:], shouldOnlyUseVersionsFromResolvedFile: Bool = false, - cacheMode: CacheMode = .project, + cachePolicies: [CachePolicy] = [.project], overwrite: Bool = false, verbose: Bool = false, toolchainEnvironment: [String: String]? = nil @@ -274,7 +270,7 @@ extension Runner { buildOptionsMatrix: buildOptionsMatrix ) self.shouldOnlyUseVersionsFromResolvedFile = shouldOnlyUseVersionsFromResolvedFile - self.cacheMode = cacheMode + self.cachePolicies = cachePolicies self.overwrite = overwrite self.verbose = verbose self.toolchainEnvironment = toolchainEnvironment diff --git a/Sources/scipio/CommandType.swift b/Sources/scipio/CommandType.swift index 3c31d9c..1869a92 100644 --- a/Sources/scipio/CommandType.swift +++ b/Sources/scipio/CommandType.swift @@ -3,7 +3,7 @@ import ScipioKit enum CommandType { case create(platformSpecifier: Runner.Options.PlatformSpecifier) - case prepare(cacheMode: Runner.Options.CacheMode) + case prepare(cachePolicies: [Runner.Options.CachePolicy]) var mode: Runner.Mode { switch self { @@ -23,12 +23,12 @@ enum CommandType { } } - var cacheMode: Runner.Options.CacheMode { + var cachePolicies: [Runner.Options.CachePolicy] { switch self { case .create: - return .disabled - case .prepare(let cacheMode): - return cacheMode + return [] + case .prepare(let cachePolicies): + return cachePolicies } } } @@ -51,19 +51,19 @@ extension Runner { let runnerOptions = Runner.Options( baseBuildOptions: baseBuildOptions, shouldOnlyUseVersionsFromResolvedFile: buildOptions.shouldOnlyUseVersionsFromResolvedFile, - cacheMode: Self.cacheMode(from: commandType), + cachePolicies: Self.cachePolicies(from: commandType), overwrite: buildOptions.overwrite, verbose: globalOptions.verbose ) self.init(mode: commandType.mode, options: runnerOptions) } - private static func cacheMode(from commandType: CommandType) -> Runner.Options.CacheMode { + private static func cachePolicies(from commandType: CommandType) -> [Runner.Options.CachePolicy] { switch commandType { case .create: - return .disabled - case .prepare(let cacheMode): - return cacheMode + return [] + case .prepare(let cachePolicies): + return cachePolicies } } diff --git a/Sources/scipio/PrepareCommands.swift b/Sources/scipio/PrepareCommands.swift index f05d629..199cff2 100644 --- a/Sources/scipio/PrepareCommands.swift +++ b/Sources/scipio/PrepareCommands.swift @@ -31,21 +31,18 @@ extension Scipio { let logLevel: Logger.Level = globalOptions.verbose ? .trace : .info LoggingSystem.bootstrap(logLevel: logLevel) - let runnerCacheMode: Runner.Options.CacheMode + let runnerCachePolicies: [Runner.Options.CachePolicy] switch cachePolicy { case .disabled: - runnerCacheMode = .disabled + runnerCachePolicies = [] case .project: - runnerCacheMode = .project + runnerCachePolicies = [.project] case .local: - runnerCacheMode = .storage(.init( - storage: LocalDiskCacheStorage(), - actors: [.consumer, .producer] - )) + runnerCachePolicies = [.localDisk] } let runner = Runner( - commandType: .prepare(cacheMode: runnerCacheMode), + commandType: .prepare(cachePolicies: runnerCachePolicies), buildOptions: buildOptions, globalOptions: globalOptions ) diff --git a/Tests/ScipioKitTests/IntegrationTests.swift b/Tests/ScipioKitTests/IntegrationTests.swift index 0be120a..a8f8022 100644 --- a/Tests/ScipioKitTests/IntegrationTests.swift +++ b/Tests/ScipioKitTests/IntegrationTests.swift @@ -108,7 +108,7 @@ final class IntegrationTests: XCTestCase { ), buildOptionsMatrix: buildOptionsMatrix, shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .disabled, + cachePolicies: [], overwrite: true, verbose: false ) diff --git a/Tests/ScipioKitTests/RunnerTests.swift b/Tests/ScipioKitTests/RunnerTests.swift index 3753434..93d41b8 100644 --- a/Tests/ScipioKitTests/RunnerTests.swift +++ b/Tests/ScipioKitTests/RunnerTests.swift @@ -306,7 +306,7 @@ final class RunnerTests: XCTestCase { options: .init( baseBuildOptions: .init(enableLibraryEvolution: true), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .project + cachePolicies: [.project] ) ) do { @@ -336,10 +336,9 @@ final class RunnerTests: XCTestCase { mode: .prepareDependencies, options: .init( shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .storage(.init( - storage: storage, - actors: [.consumer, .producer] - )) + cachePolicies: [ + .init(storage: storage, actors: [.consumer, .producer]), + ] ) ) do { @@ -373,7 +372,7 @@ final class RunnerTests: XCTestCase { try fileManager.removeItem(at: storageDir) } - func testCacheModeMultipleStorages() async throws { + func testMultipleCachePolicies() async throws { let storage1CacheDir = tempDir.appending(path: "storage1", directoryHint: .isDirectory) let storage1 = LocalDiskCacheStorage(cacheDirectory: .custom(storage1CacheDir)) let storage1Dir = storage1CacheDir.appendingPathComponent("Scipio") @@ -386,10 +385,10 @@ final class RunnerTests: XCTestCase { mode: .prepareDependencies, options: .init( shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .storages([ + cachePolicies: [ .init(storage: storage1, actors: [.consumer, .producer]), .init(storage: storage2, actors: [.consumer, .producer]), - ]) + ] ) ) do { @@ -441,7 +440,7 @@ final class RunnerTests: XCTestCase { frameworkType: .dynamic ), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .project, + cachePolicies: [.project], overwrite: false, verbose: false) ) @@ -468,7 +467,7 @@ final class RunnerTests: XCTestCase { frameworkType: .dynamic ), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .project, + cachePolicies: [.project], overwrite: false, verbose: false) ) @@ -539,7 +538,7 @@ final class RunnerTests: XCTestCase { enableLibraryEvolution: true ), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .project, + cachePolicies: [.project], overwrite: false, verbose: false) ) @@ -575,7 +574,7 @@ final class RunnerTests: XCTestCase { ), ], shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .project, + cachePolicies: [.project], overwrite: false, verbose: false) ) @@ -613,7 +612,7 @@ final class RunnerTests: XCTestCase { isSimulatorSupported: true ), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .disabled + cachePolicies: [] ) ) @@ -656,7 +655,7 @@ final class RunnerTests: XCTestCase { frameworkType: .mergeable ), shouldOnlyUseVersionsFromResolvedFile: true, - cacheMode: .disabled + cachePolicies: [] ) )