Skip to content

Commit

Permalink
[config] Search in OS config directories
Browse files Browse the repository at this point in the history
Searches for:
- ~/Library/Application Support/swift-format/config.json
- $XDG_CONFIG_HOME/swift-format/config.json
- ~/.config/swift-format/config.json
  • Loading branch information
WuerfelDev committed Jun 5, 2024
1 parent 4f19acc commit 4eefa53
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion Sources/swift-format/Frontend/Frontend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ class Frontend {
/// it was provided, or by searching in paths inferred by `swiftFilePath` if one exists, or the
/// default configuration otherwise. If an error occurred when reading the configuration, a
/// diagnostic is emitted and `nil` is returned. If neither `pathOrString` nor `swiftFilePath`
/// were provided, a default `Configuration()` will be returned.
/// were provided, a configuration is searched at the current working directory or upwards the
/// path. Next the configuration is searched for at the OS default config locations as
/// swift-format/config.json. Finally the default `Configuration()` will be returned.
private func configuration(
fromPathOrString pathOrString: String?,
orInferredFromSwiftFileAt swiftFileURL: URL?
Expand Down Expand Up @@ -241,6 +243,57 @@ class Frontend {
}
}

// Load global configuration file
// First URLs are created, then they are queried. First match is loaded
var configLocations: [URL] = []

if #available(macOS 13.0, iOS 16.0, *) {
// From "~/Library/Application Support/" directory
configLocations.append(URL.applicationSupportDirectory)
// From $XDG_CONFIG_HOME directory
if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
configLocations.append(URL(filePath: xdgConfig, directoryHint: .isDirectory))
}
// From "~/.config/" directory
var dotconfig: URL = URL.homeDirectory
dotconfig.append(component: ".config", directoryHint: .isDirectory)
configLocations.append(dotconfig)
} else {
// From "~/Library/Application Support/" directory
var appSupport: URL = FileManager.default.homeDirectoryForCurrentUser
appSupport.appendPathComponent("Library", isDirectory: true)
appSupport.appendPathComponent("Application Support", isDirectory: true)
configLocations.append(appSupport)
// From $XDG_CONFIG_HOME directory
if let xdgConfig: String = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"] {
configLocations.append(URL(fileURLWithPath: xdgConfig))
}
// From "~/.config/" directory
var dotconfig: URL = FileManager.default.homeDirectoryForCurrentUser
dotconfig.appendPathComponent(".config")
configLocations.append(dotconfig)
}

for var location: URL in configLocations {
if #available(macOS 13.0, iOS 16.0, *) {
location.append(components: "swift-format", "config.json")
} else {
location.appendPathComponent("swift-format", isDirectory: true)
location.appendPathComponent("config.json", isDirectory: false)
}
if FileManager.default.fileExists(atPath: location.path) {
do {
let configuration = try configurationLoader.configuration(at: location)
self.checkForUnrecognizedRules(in: configuration)
return configuration
} catch {
diagnosticsEngine.emitError(
"Unable to read configuration for \(location.path): \(error.localizedDescription)")
return nil
}
}
}

// An explicit configuration has not been given, and one cannot be found.
// Return the default configuration.
return Configuration()
Expand Down

0 comments on commit 4eefa53

Please sign in to comment.