Files
SwiftLint/Source/SwiftLintCore/Extensions/Configuration+Cache.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

101 lines
3.8 KiB
Swift

#if canImport(CryptoSwift)
import CryptoSwift
#endif
import Foundation
extension Configuration {
// MARK: Caching Configurations By Identifier (In-Memory)
private static var cachedConfigurationsByIdentifier = [String: Configuration]()
private static var cachedConfigurationsByIdentifierLock = NSLock()
/// Since the cache is stored in a static var, this function is used to reset the cache during tests
internal static func resetCache() {
Self.cachedConfigurationsByIdentifierLock.lock()
Self.cachedConfigurationsByIdentifier = [:]
Self.cachedConfigurationsByIdentifierLock.unlock()
}
internal func setCached(forIdentifier identifier: String) {
Self.cachedConfigurationsByIdentifierLock.lock()
Self.cachedConfigurationsByIdentifier[identifier] = self
Self.cachedConfigurationsByIdentifierLock.unlock()
}
internal static func getCached(forIdentifier identifier: String) -> Configuration? {
cachedConfigurationsByIdentifierLock.lock()
defer { cachedConfigurationsByIdentifierLock.unlock() }
return cachedConfigurationsByIdentifier[identifier]
}
/// Returns a copy of the current `Configuration` with its `computedCacheDescription` property set to the value of
/// `cacheDescription`, which is expensive to compute.
///
/// - returns: A new `Configuration` value.
public func withPrecomputedCacheDescription() -> Configuration {
var result = self
result.computedCacheDescription = result.cacheDescription
return result
}
// MARK: Nested Config Is Self Cache
private static var nestedConfigIsSelfByIdentifier = [String: Bool]()
private static var nestedConfigIsSelfByIdentifierLock = NSLock()
internal static func setIsNestedConfigurationSelf(forIdentifier identifier: String, value: Bool) {
Self.nestedConfigIsSelfByIdentifierLock.lock()
Self.nestedConfigIsSelfByIdentifier[identifier] = value
Self.nestedConfigIsSelfByIdentifierLock.unlock()
}
internal static func getIsNestedConfigurationSelf(forIdentifier identifier: String) -> Bool {
Self.nestedConfigIsSelfByIdentifierLock.lock()
defer { Self.nestedConfigIsSelfByIdentifierLock.unlock() }
return Self.nestedConfigIsSelfByIdentifier[identifier] ?? false
}
// MARK: SwiftLint Cache (On-Disk)
internal var cacheDescription: String {
if let computedCacheDescription {
return computedCacheDescription
}
let cacheRulesDescriptions = rules
.map { rule in [type(of: rule).description.identifier, rule.cacheDescription] }
.sorted { $0[0] < $1[0] }
let jsonObject: [Any] = [rootDirectory, cacheRulesDescriptions]
if let jsonData = try? JSONSerialization.data(withJSONObject: jsonObject) {
return jsonData.sha256().toHexString()
}
queuedFatalError("Could not serialize configuration for cache")
}
internal var cacheURL: URL {
let baseURL: URL
if let path = cachePath {
baseURL = URL(fileURLWithPath: path, isDirectory: true)
} else {
#if os(Linux)
baseURL = URL(fileURLWithPath: "/var/tmp/", isDirectory: true)
#else
baseURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
#endif
}
let versionedDirectory = [
"SwiftLint",
Version.current.value,
ExecutableInfo.buildID
].compactMap({ $0 }).joined(separator: "/")
let folder = baseURL.appendingPathComponent(versionedDirectory)
do {
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
} catch {
queuedPrintError("Error while creating cache: " + error.localizedDescription)
}
return folder
}
}