Skip to content

Commit

Permalink
Introduce CachePolicy and replace CacheMode with an array of CachePolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesyo committed Nov 12, 2024
1 parent c22ed6f commit 45b6089
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 88 deletions.
54 changes: 22 additions & 32 deletions Sources/ScipioKit/Producer/FrameworkProducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -96,14 +96,10 @@ struct FrameworkProducer {
)

let targetsToBuild: OrderedSet<CacheSystem.CacheTarget>
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
Expand All @@ -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
Expand Down Expand Up @@ -200,16 +196,13 @@ struct FrameworkProducer {
) async -> Set<CacheSystem.CacheTarget> {
let cacheStorages: [any CacheStorage]

switch cacheMode {
case .storages(let configs):
guard !configs.isEmpty else { return [] }
guard !cachePolicies.isEmpty else { return [] }

let storagesWithConsumer = configs.compactMap { config in
config.actors.contains(.consumer) ? config.storage : nil
}
guard !storagesWithConsumer.isEmpty else { return [] }
cacheStorages = storagesWithConsumer
let storagesWithConsumer = cachePolicies.compactMap { cachePolicy in
cachePolicy.actors.contains(.consumer) ? cachePolicy.storage : nil
}
guard !storagesWithConsumer.isEmpty else { return [] }
cacheStorages = storagesWithConsumer

var remainingTargets = availableTargets
var restored: Set<CacheSystem.CacheTarget> = []
Expand Down Expand Up @@ -357,16 +350,13 @@ struct FrameworkProducer {
}

private func cacheFrameworksIfNeeded(_ targets: Set<CacheSystem.CacheTarget>, 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)
}
}

Expand Down
42 changes: 19 additions & 23 deletions Sources/ScipioKit/Runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public struct Runner {
descriptionPackage: descriptionPackage,
buildOptions: buildOptions,
buildOptionsMatrix: buildOptionsMatrix,
cacheMode: options.cacheMode,
cachePolicies: options.cachePolicies,
overwrite: options.overwrite,
outputDir: outputDir
)
Expand Down Expand Up @@ -208,35 +208,31 @@ extension Runner {
public var buildOptionsMatrix: [String: TargetBuildOptions]
}

public enum CacheMode: Sendable {
public struct StorageConfig: Sendable {
public let storage: any CacheStorage
public let actors: Set<CacheActorKind>

public init(storage: any CacheStorage, actors: Set<CacheActorKind>) {
self.storage = storage
self.actors = actors
}
}

public struct CachePolicy: Sendable {
public enum CacheActorKind: Sendable {
// Save built product to cacheStorage
case producer
// Consume stored caches
case consumer
}

case storages([StorageConfig])
public let storage: any CacheStorage
public let actors: Set<CacheActorKind>

public static let disabled: Self = .storages([])
public init(storage: any CacheStorage, actors: Set<CacheActorKind>) {
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 {
Expand All @@ -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]?
Expand All @@ -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
Expand All @@ -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
Expand Down
20 changes: 10 additions & 10 deletions Sources/scipio/CommandType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
}
Expand All @@ -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
}
}

Expand Down
13 changes: 5 additions & 8 deletions Sources/scipio/PrepareCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
2 changes: 1 addition & 1 deletion Tests/ScipioKitTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ final class IntegrationTests: XCTestCase {
),
buildOptionsMatrix: buildOptionsMatrix,
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .disabled,
cachePolicies: [],
overwrite: true,
verbose: false
)
Expand Down
27 changes: 13 additions & 14 deletions Tests/ScipioKitTests/RunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ final class RunnerTests: XCTestCase {
options: .init(
baseBuildOptions: .init(enableLibraryEvolution: true),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .project
cachePolicies: [.project]
)
)
do {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -441,7 +440,7 @@ final class RunnerTests: XCTestCase {
frameworkType: .dynamic
),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .project,
cachePolicies: [.project],
overwrite: false,
verbose: false)
)
Expand All @@ -468,7 +467,7 @@ final class RunnerTests: XCTestCase {
frameworkType: .dynamic
),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .project,
cachePolicies: [.project],
overwrite: false,
verbose: false)
)
Expand Down Expand Up @@ -539,7 +538,7 @@ final class RunnerTests: XCTestCase {
enableLibraryEvolution: true
),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .project,
cachePolicies: [.project],
overwrite: false,
verbose: false)
)
Expand Down Expand Up @@ -575,7 +574,7 @@ final class RunnerTests: XCTestCase {
),
],
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .project,
cachePolicies: [.project],
overwrite: false,
verbose: false)
)
Expand Down Expand Up @@ -613,7 +612,7 @@ final class RunnerTests: XCTestCase {
isSimulatorSupported: true
),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .disabled
cachePolicies: []
)
)

Expand Down Expand Up @@ -656,7 +655,7 @@ final class RunnerTests: XCTestCase {
frameworkType: .mergeable
),
shouldOnlyUseVersionsFromResolvedFile: true,
cacheMode: .disabled
cachePolicies: []
)
)

Expand Down

0 comments on commit 45b6089

Please sign in to comment.