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 0006fbb
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 98 deletions.
70 changes: 28 additions & 42 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,15 +108,14 @@ struct FrameworkProducer {
cacheSystem: cacheSystem
)

let storagesWithConsumer = configs.compactMap { config in
config.actors.contains(.consumer) ? config.storage : nil
}
let storagesWithConsumer = cachePolicies.storages(for: .consumer)
if storagesWithConsumer.isEmpty {
// no-op
targetsToBuild = allTargets.subtracting(valid)
} else {
let restored = await restoreAllAvailableCachesIfNeeded(
availableTargets: targets.subtracting(valid),
to: storagesWithConsumer,
cacheSystem: cacheSystem
)
targetsToBuild = allTargets
Expand Down Expand Up @@ -196,29 +191,17 @@ struct FrameworkProducer {

private func restoreAllAvailableCachesIfNeeded(
availableTargets: Set<CacheSystem.CacheTarget>,
to storages: [any CacheStorage],
cacheSystem: CacheSystem
) async -> Set<CacheSystem.CacheTarget> {
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<CacheSystem.CacheTarget> = []

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)
Expand Down Expand Up @@ -357,16 +340,9 @@ struct FrameworkProducer {
}

private func cacheFrameworksIfNeeded(_ targets: Set<CacheSystem.CacheTarget>, cacheSystem: CacheSystem) async {
switch cacheMode {
case .storages(let configs):
guard !configs.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.storages(for: .producer)
if !storagesWithProducer.isEmpty {
await cacheSystem.cacheFrameworks(targets, to: storagesWithProducer)
}
}

Expand All @@ -378,3 +354,13 @@ struct FrameworkProducer {
}
}
}

extension [Runner.Options.CachePolicy] {
fileprivate func storages(for actor: Runner.Options.CachePolicy.CacheActorKind) -> [any CacheStorage] {
reduce(into: []) { result, cachePolicy in
if cachePolicy.actors.contains(actor) {
result.append(cachePolicy.storage)
}
}
}
}
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
Loading

0 comments on commit 0006fbb

Please sign in to comment.