Skip to content

Commit 1bf9613

Browse files
authored
Infer Swift version from Package.swift swift-tools-version (#180)
1 parent b53ed2a commit 1bf9613

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

Plugins/FormatSwift/Plugin.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,17 @@ extension AirbnbSwiftFormatPlugin: CommandPlugin {
8484
})
8585
}
8686

87+
// When running on a SPM package we infer the minimum Swift version from the
88+
// `swift-tools-version` in `Package.swift`
89+
let additionalArguments = [
90+
"--swift-version",
91+
"\(context.package.toolsVersion.major).\(context.package.toolsVersion.minor)",
92+
] + argumentExtractor.remainingArguments
93+
8794
try performCommand(
8895
context: context,
8996
inputPaths: inputPaths,
90-
additionalArguments: argumentExtractor.remainingArguments)
97+
additionalArguments: additionalArguments)
9198
}
9299

93100
// MARK: Private

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ $ swift package format --exclude Tests
5959
# Alternatively you can explicitly list the set of paths and/or SPM targets:
6060
$ swift package format --paths Sources Tests Package.swift
6161
$ swift package format --targets AirbnbSwiftFormatTool
62+
63+
# The plugin infers your package's minimum Swift version from the `swift-tools-version`
64+
# in your `Package.swift`, but you can provide a custom value with `--swift-version`:
65+
$ swift package format --swift-version 5.3
6266
```
6367

6468
</details>

Sources/AirbnbSwiftFormatTool/AirbnbSwiftFormatTool.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct AirbnbSwiftFormatTool: ParsableCommand {
3737
@Option(help: "The absolute path to the SwiftLint config file")
3838
var swiftLintConfig = Bundle.module.path(forResource: "swiftlint", ofType: "yml")!
3939

40+
@Option(help: "The project's minimum Swift version")
41+
var swiftVersion: String?
42+
4043
mutating func run() throws {
4144
try swiftFormat.run()
4245
swiftFormat.waitUntilExit()
@@ -83,6 +86,10 @@ struct AirbnbSwiftFormatTool: ParsableCommand {
8386
arguments += ["--lint"]
8487
}
8588

89+
if let swiftVersion = swiftVersion {
90+
arguments += ["--swiftversion", swiftVersion]
91+
}
92+
8693
let swiftFormat = Process()
8794
swiftFormat.launchPath = swiftFormatPath
8895
swiftFormat.arguments = arguments

0 commit comments

Comments
 (0)