Skip to content

Commit

Permalink
Infer Swift version from Package.swift swift-tools-version (#180)
Browse files Browse the repository at this point in the history
  • Loading branch information
calda authored Jul 28, 2022
1 parent b53ed2a commit 1bf9613
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion Plugins/FormatSwift/Plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,17 @@ extension AirbnbSwiftFormatPlugin: CommandPlugin {
})
}

// When running on a SPM package we infer the minimum Swift version from the
// `swift-tools-version` in `Package.swift`
let additionalArguments = [
"--swift-version",
"\(context.package.toolsVersion.major).\(context.package.toolsVersion.minor)",
] + argumentExtractor.remainingArguments

try performCommand(
context: context,
inputPaths: inputPaths,
additionalArguments: argumentExtractor.remainingArguments)
additionalArguments: additionalArguments)
}

// MARK: Private
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ $ swift package format --exclude Tests
# Alternatively you can explicitly list the set of paths and/or SPM targets:
$ swift package format --paths Sources Tests Package.swift
$ swift package format --targets AirbnbSwiftFormatTool

# The plugin infers your package's minimum Swift version from the `swift-tools-version`
# in your `Package.swift`, but you can provide a custom value with `--swift-version`:
$ swift package format --swift-version 5.3
```

</details>
Expand Down
7 changes: 7 additions & 0 deletions Sources/AirbnbSwiftFormatTool/AirbnbSwiftFormatTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ struct AirbnbSwiftFormatTool: ParsableCommand {
@Option(help: "The absolute path to the SwiftLint config file")
var swiftLintConfig = Bundle.module.path(forResource: "swiftlint", ofType: "yml")!

@Option(help: "The project's minimum Swift version")
var swiftVersion: String?

mutating func run() throws {
try swiftFormat.run()
swiftFormat.waitUntilExit()
Expand Down Expand Up @@ -83,6 +86,10 @@ struct AirbnbSwiftFormatTool: ParsableCommand {
arguments += ["--lint"]
}

if let swiftVersion = swiftVersion {
arguments += ["--swiftversion", swiftVersion]
}

let swiftFormat = Process()
swiftFormat.launchPath = swiftFormatPath
swiftFormat.arguments = arguments
Expand Down

0 comments on commit 1bf9613

Please sign in to comment.