Files
SwiftLint/Source/SwiftLintCore/Models/LinterCache.swift
T
JP Simard 86d60400c1 Move core SwiftLint functionality to new SwiftLintCore module
Over the years, SwiftLintFramework had become a fairly massive monolith,
containing over 400 source files with both core infrastructure and
rules.

Architecturally, the rules should rely on the core infrastructure but
not the other way around. There are two exceptions to this:
`custom_rules` and `superfluous_disable_command` which need special
integration with the linter infrastructure.

Now the time has come to formalize this architecture and one way to do
that is to move the core SwiftLint functionality out of
SwiftLintFramework and into a new SwiftLintCore module that the rules
can depend on.

Beyond enforcing architectural patterns, this also has the advantage of
speeding up incremental compilation by skipping rebuilding the core
functionality when iterating on rules.

Because the core functionality is always useful when building rules, I'm
opting to import SwiftLintCore in SwiftLintFramework as `@_exported` so
that it's implicitly available to all files in SwiftLintFramework
without needing to import it directly.

In a follow-up I'll also split the built-in rules and the extra rules
into their own modules. More modularization is possible from there, but
not planned.

The bulk of this PR just moves files from `Source/SwiftLintFramework/*`
to `Source/SwiftLintCore/*`. There are some other changes that can't be
split up into their own PRs:

* Change jazzy to document the SwiftLintCore module instead of
  SwiftLintFramework.
* Change imports in unit tests to reflect where code was moved to.
* Update `sourcery` make rule to reflect where code was moved to.
* Create a new `coreRules` array and register those rules with the
  registry. This allows the `custom_rules` and
  `superfluous_disable_command` rule implementations to remain internal
  to the SwiftLintCore module, preventing more implementation details
  from leaking across architectural layers.
* Move `RuleRegistry.registerAllRulesOnce()` out of the type declaration
  and up one level so it can access rules defined downstream from
  SwiftLintCore.
2023-04-26 21:10:19 -04:00

154 lines
5.5 KiB
Swift

import Foundation
private enum LinterCacheError: Error {
case noLocation
}
private struct FileCacheEntry: Codable {
let violations: [StyleViolation]
let lastModification: Date
let swiftVersion: SwiftVersion
}
private struct FileCache: Codable {
var entries: [String: FileCacheEntry]
static var empty: FileCache { return Self(entries: [:]) }
}
/// A persisted cache for storing and retrieving linter results.
public final class LinterCache {
private typealias Encoder = PropertyListEncoder
private typealias Decoder = PropertyListDecoder
private static let fileExtension = "plist"
private typealias Cache = [String: FileCache]
private var lazyReadCache: Cache
private let readCacheLock = NSLock()
private var writeCache = Cache()
private let writeCacheLock = NSLock()
internal let fileManager: LintableFileManager
private let location: URL?
private let swiftVersion: SwiftVersion
internal init(fileManager: LintableFileManager = FileManager.default, swiftVersion: SwiftVersion = .current) {
location = nil
self.fileManager = fileManager
self.lazyReadCache = Cache()
self.swiftVersion = swiftVersion
}
/// Creates a `LinterCache` by specifying a SwiftLint configuration and a file manager.
///
/// - parameter configuration: The SwiftLint configuration for which this cache will be used.
/// - parameter fileManager: The file manager to use to read lintable file information.
public init(configuration: Configuration, fileManager: LintableFileManager = FileManager.default) {
location = configuration.cacheURL
lazyReadCache = Cache()
self.fileManager = fileManager
self.swiftVersion = .current
}
private init(cache: Cache, location: URL?, fileManager: LintableFileManager, swiftVersion: SwiftVersion) {
self.lazyReadCache = cache
self.location = location
self.fileManager = fileManager
self.swiftVersion = swiftVersion
}
internal func cache(violations: [StyleViolation], forFile file: String, configuration: Configuration) {
guard let lastModification = fileManager.modificationDate(forFileAtPath: file) else {
return
}
let configurationDescription = configuration.cacheDescription
writeCacheLock.lock()
var filesCache = writeCache[configurationDescription] ?? .empty
filesCache.entries[file] = FileCacheEntry(violations: violations, lastModification: lastModification,
swiftVersion: swiftVersion)
writeCache[configurationDescription] = filesCache
writeCacheLock.unlock()
}
internal func violations(forFile file: String, configuration: Configuration) -> [StyleViolation]? {
guard let lastModification = fileManager.modificationDate(forFileAtPath: file),
let entry = fileCache(cacheDescription: configuration.cacheDescription).entries[file],
entry.lastModification == lastModification,
entry.swiftVersion == swiftVersion
else {
return nil
}
return entry.violations
}
/// Persists the cache to disk.
///
/// - throws: Throws if the linter cache doesn't have a `location` value, if the cache couldn't be serialized, or if
/// the contents couldn't be written to disk.
public func save() throws {
guard let url = location else {
throw LinterCacheError.noLocation
}
writeCacheLock.lock()
defer {
writeCacheLock.unlock()
}
guard writeCache.isNotEmpty else {
return
}
readCacheLock.lock()
let readCache = lazyReadCache
readCacheLock.unlock()
let encoder = Encoder()
for (description, writeFileCache) in writeCache where writeFileCache.entries.isNotEmpty {
let fileCacheEntries = readCache[description]?.entries.merging(writeFileCache.entries) { _, write in write }
let fileCache = fileCacheEntries.map(FileCache.init) ?? writeFileCache
let data = try encoder.encode(fileCache)
let file = url.appendingPathComponent(description).appendingPathExtension(Self.fileExtension)
try data.write(to: file, options: .atomic)
}
}
internal func flushed() -> LinterCache {
return Self(cache: mergeCaches(), location: location, fileManager: fileManager, swiftVersion: swiftVersion)
}
private func fileCache(cacheDescription: String) -> FileCache {
readCacheLock.lock()
defer {
readCacheLock.unlock()
}
if let fileCache = lazyReadCache[cacheDescription] {
return fileCache
}
guard let location else {
return .empty
}
let file = location.appendingPathComponent(cacheDescription).appendingPathExtension(Self.fileExtension)
let data = try? Data(contentsOf: file)
let fileCache = data.flatMap { try? Decoder().decode(FileCache.self, from: $0) } ?? .empty
lazyReadCache[cacheDescription] = fileCache
return fileCache
}
private func mergeCaches() -> Cache {
readCacheLock.lock()
writeCacheLock.lock()
defer {
readCacheLock.unlock()
writeCacheLock.unlock()
}
return lazyReadCache.merging(writeCache) { read, write in
FileCache(entries: read.entries.merging(write.entries) { _, write in write })
}
}
}