Files
SwiftLint/Source/SwiftLintFramework/Extensions/Configuration+Cache.swift
T
JP Simard c0f9f2175b Add support for native custom rules (#4039)
This change makes it possible to add native custom rules when building
SwiftLint via Bazel (possible as of
https://github.com/realm/SwiftLint/pull/4038).

First, add a local bazel repository where custom rules will be defined
to your project's `WORKSPACE`:

```python
local_repository(
    name = "swiftlint_extra_rules",
    path = "swiftlint_extra_rules",
)
```

Then in the extra rules directory, add an empty `WORKSPACE` and a
`BUILD` file with the following contents:

```python
filegroup(
    name = "extra_rules",
    srcs = glob(["*.swift"]),
    visibility = ["//visibility:public"],
)
```

To add a rule (for example, `MyPrivateRule`) add the following two
files:

```swift
// ExtraRules.swift
func extraRules() -> [Rule.Type] {
    [
        MyPrivateRule.self,
    ]
}
```

```swift
// MyPrivateRule.swift
import SourceKittenFramework
import SwiftSyntax

struct MyPrivateRule: ConfigurationProviderRule {
    var configuration = SeverityConfiguration(.error)

    init() {}

    static let description = RuleDescription(
        identifier: "my_private_rule",
        name: "My Private Rule",
        description: "This is my private rule.",
        kind: .idiomatic
    )

    func validate(file: SwiftLintFile) -> [StyleViolation] {
        // Perform validation here...
    }
}
```

Then you can reference the rule in your configuration or source files as
though they were built in to the official SwiftLint repo.

This means that you have access to SwiftLintFramework's internal API.
We make no guarantees as to the stability of these internal APIs,
although if you end up using something that gets removed please reach
out and we'll make a best effort to maintain some level of support.

This PR also improves the linter cache on macOS to make it correctly
invalidate previous results when custom native rules are edited. This
even works when doing local development of SwiftLint, where previous it
was necessary to use `--no-cache` when working on SwiftLint, now the
cache should always work.

Co-authored-by: Keith Smiley <keithbsmiley@gmail.com>
2022-07-26 13:56:22 -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]
}
// MARK: SwiftLint Cache (On-Disk)
internal var cacheDescription: String {
if let computedCacheDescription = 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
}
}