Skip to content

Commit 43697f5

Browse files
committed
use/make global config if local one isn't present
1 parent 79cc59f commit 43697f5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

Package.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@ let package = Package(
1414
],
1515

1616
products: [
17-
.library(
18-
name: "SwiftFormatterPluginDefaults",
19-
targets: [
20-
"SwiftFormatterPluginDefaults"
21-
]
22-
),
23-
2417
.plugin(
2518
name: "SwiftFormatterPlugin",
2619
targets: [

Plugins/SwiftFormatterPlugin/SwiftFormatterPlugin.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ struct FormatterPlugin: CommandPlugin {
1414
let swiftFormatExec = URL(fileURLWithPath: swiftFormatTool.path.string)
1515

1616
var baseArguments: [String] = []
17-
// let fm = FileManager.default
18-
// let configFile = context.package.directory.appending(".swift-format.json").string
19-
// if fm.fileExists(atPath: configFile) {
20-
// baseArguments.append(contentsOf: ["--config", "\(configFile)"])
21-
// }
17+
18+
let configFile = context.package.directory.appending(".swiftformat").string
19+
if !FileManager.default.fileExists(atPath: configFile) {
20+
baseArguments.append(contentsOf: ["--config", defaultConfigPath])
21+
}
2222

2323

2424
for target in context.package.targets {
@@ -41,4 +41,19 @@ struct FormatterPlugin: CommandPlugin {
4141
}
4242
}
4343
}
44+
45+
var defaultConfigPath: String {
46+
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.
51+
52+
"""
53+
54+
try? defaultConfig.write(toFile: path, atomically: true, encoding: .utf8)
55+
}
56+
57+
return path
58+
}
4459
}

0 commit comments

Comments
 (0)