Skip to content

Commit afd0ad9

Browse files
committed
Write default config options to temp directory.
1 parent 71e7d8b commit afd0ad9

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

Plugins/SwiftFormatterPlugin/SwiftFormatterPlugin.swift

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ struct FormatterPlugin: CommandPlugin {
1313
let swiftFormatTool = try context.tool(named: "swiftformat")
1414
let swiftFormatExec = URL(fileURLWithPath: swiftFormatTool.path.string)
1515

16-
var baseArguments: [String] = []
16+
let cacheFile = context.pluginWorkDirectory.appending("swift-format-cache").string
17+
var baseArguments: [String] = ["--quiet", "--cache", cacheFile]
1718

1819
let configFile = context.package.directory.appending(".swiftformat").string
19-
if !FileManager.default.fileExists(atPath: configFile), let defaultPath = defaultConfigPath {
20+
if !FileManager.default.fileExists(atPath: configFile), let defaultPath = defaultConfigPath(context: context) {
2021
baseArguments.append(contentsOf: ["--config", defaultPath])
2122
}
2223

23-
24+
let swiftVersionFile = context.package.directory.appending(".swift-version").string
25+
if !FileManager.default.fileExists(atPath: swiftVersionFile) {
26+
let tools = context.package.toolsVersion
27+
let version = "\(tools.major).\(tools.minor)"
28+
baseArguments.append(contentsOf: ["--swiftversion", version])
29+
}
30+
2431
for target in context.package.targets {
2532
guard let target = target as? SourceModuleTarget else { continue }
2633

@@ -34,31 +41,35 @@ struct FormatterPlugin: CommandPlugin {
3441
process.waitUntilExit()
3542

3643
if process.terminationReason == .exit && process.terminationStatus == 0 {
37-
print("Formatted the source code in \(target.directory).")
44+
Diagnostics.remark("Formatted the source code in \(target.directory).")
3845
} else {
3946
let problem = "\(process.terminationReason):\(process.terminationStatus)"
4047
Diagnostics.error("swift-format invocation failed: \(problem)")
4148
}
4249
}
4350
}
4451

45-
var defaultConfigPath: String? {
52+
func defaultConfigPath(context: PluginContext) -> String? {
4653
let path = ("~/.swiftformat" as NSString).expandingTildeInPath
47-
if !FileManager.default.fileExists(atPath: path) {
48-
let defaultConfig = """
49-
# Place your default swift-format configuration options here.
50-
# See https://github.com/nicklockwood/SwiftFormat#config-file for more details.
54+
if FileManager.default.fileExists(atPath: path) {
55+
return path
56+
}
57+
58+
let defaultPath = context.pluginWorkDirectory.appending("default-swift-format-config").string
59+
let defaultConfig = """
60+
# Default Elegant Chaos swift-format configuration options.
61+
# See https://github.com/nicklockwood/SwiftFormat#config-file for more details.
62+
# To override these, place a .swiftformat file into your source directory, or at ~/.swiftformat.
5163
52-
"""
64+
--ifdef no-indent
65+
"""
5366

54-
do {
55-
try defaultConfig.write(toFile: path, atomically: true, encoding: .utf8)
56-
} catch {
57-
print(error)
58-
return nil
59-
}
67+
do {
68+
try defaultConfig.write(toFile: defaultPath, atomically: true, encoding: .utf8)
69+
return defaultPath
70+
} catch {
71+
print(error)
72+
return nil
6073
}
61-
62-
return path
6374
}
6475
}

0 commit comments

Comments
 (0)