|
12 | 12 |
|
13 | 13 | import Foundation |
14 | 14 | @_spi(Internal) import SwiftFormat |
15 | | -import SwiftSyntax |
16 | 15 | import SwiftParser |
| 16 | +import SwiftSyntax |
17 | 17 |
|
18 | 18 | class Frontend { |
19 | 19 | /// Represents a file to be processed by the frontend and any file-specific options associated |
@@ -245,36 +245,40 @@ class Frontend { |
245 | 245 |
|
246 | 246 | // Load global configuration file |
247 | 247 | // First URLs are created, then they are queried. First match is loaded |
248 | | - var configLocations: [URL] = [] |
249 | | - |
250 | | - if #available(macOS 13.0, iOS 16.0, *) { |
251 | | - // From "~/Library/Application Support/" directory |
252 | | - configLocations.append(URL.applicationSupportDirectory) |
253 | | - // From $XDG_CONFIG_HOME directory |
254 | | - if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { |
255 | | - configLocations.append(URL(filePath: xdgConfig, directoryHint: .isDirectory)) |
| 248 | + var configLocations: [URL?] = [] |
| 249 | + |
| 250 | + #if os(Windows) |
| 251 | + if let localAppData = ProcessInfo.processInfo.environment["LOCALAPPDATA"] { |
| 252 | + configLocations.append(URL(fileURLWithPath: localAppData)) |
256 | 253 | } |
257 | | - // From "~/.config/" directory |
258 | | - var dotconfig: URL = URL.homeDirectory |
259 | | - dotconfig.append(component: ".config", directoryHint: .isDirectory) |
260 | | - configLocations.append(dotconfig) |
261 | | - } else { |
262 | | - // From "~/Library/Application Support/" directory |
263 | | - var appSupport: URL = FileManager.default.homeDirectoryForCurrentUser |
264 | | - appSupport.appendPathComponent("Library", isDirectory: true) |
265 | | - appSupport.appendPathComponent("Application Support", isDirectory: true) |
266 | | - configLocations.append(appSupport) |
267 | | - // From $XDG_CONFIG_HOME directory |
268 | | - if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { |
269 | | - configLocations.append(URL(fileURLWithPath: xdgConfig)) |
| 254 | + if let programData = ProcessInfo.processInfo.environment["PROGRAMDATA"] { |
| 255 | + configLocations.append(URL(fileURLWithPath: programData)) |
270 | 256 | } |
271 | | - // From "~/.config/" directory |
272 | | - var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser |
273 | | - dotconfig.appendPathComponent(".config") |
274 | | - configLocations.append(dotconfig) |
275 | | - } |
| 257 | + #else |
| 258 | + if let xdgConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] { |
| 259 | + configLocations.append(URL(fileURLWithPath: xdgConfigHome)) |
| 260 | + } |
| 261 | + |
| 262 | + if let libraryUrl = FileManager.default.urls( |
| 263 | + for: .applicationSupportDirectory, in: .userDomainMask |
| 264 | + ).first { |
| 265 | + configLocations.append(libraryUrl) |
| 266 | + } |
| 267 | + |
| 268 | + if let xdgConfigDirs = ProcessInfo.processInfo.environment["XDG_CONFIG_DIRS"] { |
| 269 | + configLocations += xdgConfigDirs.split(separator: ":").map { xdgConfigDir in |
| 270 | + URL(fileURLWithPath: String(xdgConfigDir)) |
| 271 | + } |
| 272 | + } |
| 273 | + |
| 274 | + if let libraryUrl = FileManager.default.urls( |
| 275 | + for: .applicationSupportDirectory, in: .systemDomainMask |
| 276 | + ).first { |
| 277 | + configLocations.append(libraryUrl) |
| 278 | + } |
| 279 | + #endif |
276 | 280 |
|
277 | | - for var location: URL in configLocations { |
| 281 | + for case var location? in configLocations { |
278 | 282 | if #available(macOS 13.0, iOS 16.0, *) { |
279 | 283 | location.append(components: "swift-format", "config.json") |
280 | 284 | } else { |
@@ -307,7 +311,8 @@ class Frontend { |
307 | 311 | // That way they will be printed out, but we'll continue execution on the valid rules. |
308 | 312 | let invalidRules = configuration.rules.filter { !RuleRegistry.rules.keys.contains($0.key) } |
309 | 313 | for rule in invalidRules { |
310 | | - diagnosticsEngine.emitWarning("Configuration contains an unrecognized rule: \(rule.key)", location: nil) |
| 314 | + diagnosticsEngine.emitWarning( |
| 315 | + "Configuration contains an unrecognized rule: \(rule.key)", location: nil) |
311 | 316 | } |
312 | 317 | } |
313 | 318 | } |
0 commit comments