gayledunham/swiftlintplugin
A Swift Package Manager plugin for SwiftLint, supporting both Swift Packages and Xcode Projects.
Requirements
- Recommended: Xcode 16 or later, but you can use Xcode 14 or 15 by specifying branch
swift-5.7
- Recommended: Swift 6.0 or later, but you can use Swift 5.7 or later by specifying branch
swift-5.7
- Required: You must have a SwiftLint configuration file (
swiftlint.ymlor.swiftlint.yml) in the root folder of your package or project.
Installation
This package contains a binaryTarget for the current released version 0.63.2 of SwiftLint from realm/SwiftLint, used by all the plugin commands and the build tool command. No installation of SwiftLint or configuring of PATH is required.
For more information on SwiftLint rules, see the Rule Directory Reference.
Adding Linting to a Swift Package
- Add
SwiftLintPluginas a dependency of your package.
dependencies: [
.package(url: "https://github.com/GayleDunham/SwiftLintPlugin.git", branch: "main"),
],- Optionally, add the
SwiftLintBuildToolplugin to your main target. All files in the package will be evaluated by the linter.
targets: [
.target(
name: "YOUR_TARGET",
dependencies: [],
plugins: [ .plugin(name: "SwiftLintBuildTool", package: "SwiftLintPlugin") ]
),Adding Linting to an Xcode Project
TL;DR
- Add the package
https://github.com/GayleDunham/SwiftLintPluginto the project. - Optionally, add the
SwiftLintBuildToolplugin to the targets you want to be evaluated by the linter.
Add the Package to Your Project
- In the Project Navigator, select the first item (the project).
- In the Project Settings editor, select the project.
- Select the Package Dependencies tab.
- Click the + under the "Add package here" text.
[Edit the Project Package Settings]
- Paste
https://github.com/GayleDunham/SwiftLintPluginin the search box. - Click Add Package.
[Paste the link and Add Package]
Optionally: Add the Build Tool to Your Target
- Select the target.
- Select the Build Phases tab.
- Expand Run Build Tool Plug-in and click the +.
[Edit the Target Build Settings]
- Select SwiftLintBuildTool and click Add.
[Select the Build Tool and Add]
Features
Build Tool
The SwiftLintBuildTool is a pre-build command that runs SwiftLint against a target's sources directory. All lint issues are displayed in the Xcode Issue Navigator and code editor.
Commands
Commands are run against one or more selected targets. Output appears in the Report Navigator, labeled with the command name (e.g., "SwiftLintFix").
SwiftLintFix: Modifies your files to correct lint violations if possibleSwiftLintLinter: Prints lint warnings and errorsSwiftLintRules: Displays the list of rules and their identifiersSwiftLintVersion: Displays the current version of SwiftLint
Running Plugin Commands from Xcode
- From the Project Navigator: Right-click on the first item (the project or package) in the Project Navigator, then click the command to run.
[Run Command from Project Navigator]
- From the Menu: Select the first item (the project or package) in the Project Navigator. Then in the menu bar, select File > Packages and the command to run.
[Run Command from Menu]
Command Line Usage for Swift Packages
In the top-level directory of the Swift Package, execute any of the following commands.
swift package swiftlint-fix
swift package swiftlint-lint
swift package swiftlint-rules
swift package swiftlint-versionxcodebuild Usage and CI Systems
xcodebuild \
-scheme "YOUR_PROJECT" \
-destination "platform=macOS" \
-skipPackagePluginValidation \
clean build[!NOTE] For CI systems, specify
-skipPackagePluginValidationto skip the validation prompt that occurs in Xcode.
References
SwiftLint
- SwiftLint Rule Directory Reference - https://realm.github.io/SwiftLint/rule-directory.html
Example SwiftLint Configuration Files from Industry Leaders
- The Official raywenderlich.com SwiftLint Policy - https://github.com/kodecocodes/swift-style-guide/blob/main/SWIFTLINT.markdown
- com.raywenderlich.swiftlint.yml - The Official Kodeco Configuration file - https://github.com/kodecocodes/swift-style-guide/blob/main/com.raywenderlich.swiftlint.yml
- The Official Kodeco Swift Style Guide - https://github.com/kodecocodes/swift-style-guide
- SwiftLee Blog: SwiftLint valuable opt-in rules to improve your code - https://www.avanderlee.com/optimization/swiftlint-optin-rules/
- Swift API Design Guidelines - https://www.swift.org/documentation/api-design-guidelines/
- Google Swift Style Guide - https://google.github.io/swift/
- LinkedIn Swift Style Guide - https://github.com/linkedin/swift-style-guide
- Airbnb Swift Style Guide - https://github.com/airbnb/swift
Swift Package Plugins
- WWDC 2022 Meet Swift Package plugins - https://developer.apple.com/videos/play/wwdc2022/110359/
- WWDC 2022 Create Swift Package plugins - https://developer.apple.com/videos/play/wwdc2022/110401/
- Xcode integration of Swift Package Plugins in Xcode 14 - https://blog.eidinger.info/xcode-integration-of-swift-package-plugins-in-xcode-14
- Beginner's guide to Swift package manager command plugins - https://theswiftdev.com/beginners-guide-to-swift-package-manager-command-plugins/
- How to Use Xcode Plugins in Your iOS App - https://betterprogramming.pub/how-to-use-xcode-plugins-in-your-ios-app-13574261f210
Package Metadata
Repository: gayledunham/swiftlintplugin
Default branch: main
README: README.md