SwiftLint 的 Swift Package Manager 插件,同时支持 Swift Packages 和 Xcode 项目。此包提供了插件命令和一个在构建之前运行的构建工具命令。对于 Packages,插件命令可以从命令行和 Xcode 中执行。
此包包含一个 binaryTarget
,用于来自 https://github.com/realm/SwiftLint/ 的 SwiftLint 的当前发布版本 0.57.0,它被所有插件命令和构建工具命令使用。无需安装 SwiftLint 或配置 PATH。
重要提示
你需要在你的 package 或项目的根目录下放置一个 swiftlint.yml
或 .swiftlint.yml
文件。 如果找不到配置文件,命令将会失败。
有关 SwiftLint 规则的更多信息,请参阅:https://realm.github.io/SwiftLint/rule-directory.html
SwiftLintPlugin
添加为你的 Package 的依赖项。 dependencies: [
.package(url: "https://github.com/GayleDunham/SwiftLintPlugin.git", branch: "main"),
],
SwiftLintBuildTool
插件添加到你的主要 target,package 中的所有文件都将被 linter 评估。 targets: [
.target(
name: "YOUR_TARGET",
dependencies: [],
plugins: [ .plugin(name: "SwiftLintBuildTool", package: "SwiftLintPlugin") ]
),
https://github.com/GayleDunham/SwiftLintPlugin
添加到项目中。SwiftLintBuildTool
插件添加到你想要被 linter 评估的 targets 中。SwiftLintBuildTool
是一个 pre-build 命令,它针对 Target 的源目录运行 SwiftLint。你必须在 Package 或 Project 的根目录下放置一个 swiftlint.yml
或 .swiftlint.yml
文件。
命令针对一个或多个选定的 Targets 运行
SwiftLintFix
: 尽可能更正违规行为SwiftLintLinter
: 打印 lint 警告和错误SwiftLintRules
: 显示规则列表及其标识符SwiftLintVersion
: 显示 SwiftLint 的当前版本在 Swift Package 的顶层目录中执行以下任何命令。
% swift package swiftlint-fix
% swift package swiftlint-lint
% swift package swiftlint-rules
% swift package swiftlint-version
注意
对于 CI 系统,您应该指定 -skipPackagePluginValidation 以跳过 Xcode 中出现的验证提示。
xcodebuild \
-scheme "YOUR_PROJECT" \
-destination "platform=OS X,arch=x86_64" \
-skipPackagePluginValidation \
clean build
SwiftLint 规则目录参考 - https://realm.github.io/SwiftLint/rule-directory.html
官方 raywenderlich.com SwiftLint 策略 - https://github.com/kodecocodes/swift-style-guide/blob/main/SWIFTLINT.markdown
com.raywenderlich.swiftlint.yml - 官方 Kodeco 配置文件 - https://github.com/kodecocodes/swift-style-guide/blob/main/com.raywenderlich.swiftlint.yml
官方 Kodeco Swift 风格指南 - https://github.com/kodecocodes/swift-style-guide
SwiftLee 博客:SwiftLint 有价值的可选规则来改进你的代码 - https://www.avanderlee.com/optimization/swiftlint-optin-rules/
Swift API 设计指南 - https://swiftlang.cn/documentation/api-design-guidelines/
Google Swift 风格指南 - https://ggdocs.cn/swift/
LinkedIn Swift 风格指南 - https://github.com/linkedin/swift-style-guide
Airbnb Swift 风格指南 - https://github.com/airbnb/swift
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 14 中 Swift Package 插件的 Xcode 集成 - https://blog.eidinger.info/xcode-integration-of-swift-package-plugins-in-xcode-14
Swift package manager command plugins 初学者指南 - https://theswiftdev.com/beginners-guide-to-swift-package-manager-command-plugins/
如何在你的 iOS 应用中使用 Xcode 插件 - https://betterprogramming.pub/how-to-use-xcode-plugins-in-your-ios-app-13574261f210