mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
3a32d6b479
also slightly speed up writing to the cache. For example, on the Lyft codebase with 1,500 Swift files: ```bash $ time swiftlint lint --quiet swiftlint --quiet 3.53s user 0.27s system 388% cpu 0.979 total $ rm -rf ~/Library/Caches/SwiftLint && time swiftlint lint --quiet swiftlint --quiet 35.20s user 1.22s system 371% cpu 9.806 total $ time swiftlint lint --quiet swiftlint lint --quiet 0.90s user 0.13s system 218% cpu 0.472 total $ rm -rf ~/Library/Caches/SwiftLint && time swiftlint lint --quiet swiftlint lint --quiet 31.78s user 1.18s system 360% cpu 9.146 total ```
82 lines
2.7 KiB
Swift
82 lines
2.7 KiB
Swift
//
|
|
// Configuration+Cache.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by JP Simard on 5/22/17.
|
|
// Copyright © 2017 Realm. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
extension Configuration {
|
|
|
|
// MARK: Caching Configurations By Path (In-Memory)
|
|
|
|
private static var cachedConfigurationsByPath = [String: Configuration]()
|
|
private static var cachedConfigurationsByPathLock = NSLock()
|
|
|
|
internal func setCached(atPath path: String) {
|
|
Configuration.cachedConfigurationsByPathLock.lock()
|
|
Configuration.cachedConfigurationsByPath[path] = self
|
|
Configuration.cachedConfigurationsByPathLock.unlock()
|
|
}
|
|
|
|
internal static func getCached(atPath path: String) -> Configuration? {
|
|
cachedConfigurationsByPathLock.lock()
|
|
defer { cachedConfigurationsByPathLock.unlock() }
|
|
return cachedConfigurationsByPath[path]
|
|
}
|
|
|
|
public func withPrecomputedCacheDescription() -> Configuration {
|
|
var result = self
|
|
result.computedCacheDescription = result.cacheDescription
|
|
return result
|
|
}
|
|
|
|
// MARK: SwiftLint Cache (On-Disk)
|
|
|
|
internal var cacheDescription: String {
|
|
if let computedCacheDescription = computedCacheDescription {
|
|
return computedCacheDescription
|
|
}
|
|
|
|
let cacheRulesDescriptions: [String: Any] = rules.reduce([:]) { accu, element in
|
|
var accu = accu
|
|
accu[type(of: element).description.identifier] = element.cacheDescription
|
|
return accu
|
|
}
|
|
let dict: [String: Any] = [
|
|
"root": rootPath ?? FileManager.default.currentDirectoryPath,
|
|
"rules": cacheRulesDescriptions
|
|
]
|
|
if let jsonData = try? JSONSerialization.data(withJSONObject: dict),
|
|
let jsonString = String(data: jsonData, encoding: .utf8) {
|
|
return jsonString
|
|
}
|
|
queuedFatalError("Could not serialize configuration for cache")
|
|
}
|
|
|
|
internal var cacheURL: URL {
|
|
let baseURL: URL
|
|
if let path = cachePath {
|
|
baseURL = URL(fileURLWithPath: path)
|
|
} else {
|
|
#if os(Linux)
|
|
baseURL = URL(fileURLWithPath: "/var/tmp/")
|
|
#else
|
|
baseURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
|
|
#endif
|
|
}
|
|
let folder = baseURL.appendingPathComponent("SwiftLint/\(Version.current.value)")
|
|
|
|
do {
|
|
try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true, attributes: nil)
|
|
} catch {
|
|
queuedPrintError("Error while creating cache: " + error.localizedDescription)
|
|
}
|
|
|
|
return folder.appendingPathComponent("cache.json")
|
|
}
|
|
}
|