到目前为止,我发现的是这篇博客文章:是时候使用 Swift Package Manager,建议将 SwiftLint 和其他工具与
Package.swift
集成。
我能够向包文件添加依赖项,成功构建和测试,但 SwiftLint 从未警告我有关语法违规的问题。
在 Xcode 项目中使用此构建阶段步骤之前:
if which swiftlint >/dev/null; then
swiftlint
else
echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
fi
文章建议采用Komondor,如果没有PackageConfigs,它本身就没用。这个想法是在提交期间运行 SwiftLint 命令。我尝试添加这两个项目,但无法在合理的时间内使其正常工作。在提交期间,我看到这样的警告:
非法指令:4 $komondor 运行预提交
对于 Swift Package Manager 来说还处于早期阶段,互联网上几乎没有任何信息。
理想情况下,我希望有任何解决方案可以让我们的团队自动化 SwiftLint,并且理想情况下不需要添加 22 个依赖项、配置文件,也不需要动态库。
您现在可以通过Swift Package Manager 插件正式使用 SwiftLint。为此,请添加包依赖项,然后在要 lint 的目标中指定插件
Package.swift
:
let package = Package(
...
dependencies: [
.package(url: "https://github.com/realm/SwiftLint", branch: "main")
],
targets: [
.target(
...
plugins: [.plugin(name: "SwiftLintBuildToolPlugin", package: "SwiftLint")]
)
]
)
截至 2023 年 1 月 13 日,我发现在包依赖项中指定最新版本 (
exact
) 的 0.50.3
版本会导致 SPM 无法解析依赖项。使用 branch
设置为 main
(如上所述)或特定的 revision
似乎都有效。
然后,在
.swiftlint.yml
中,您可以指定要 lint 的目录:
included:
- Sources
- Tests
从 0.47.1 版本开始,可以将 SwiftLint 附加到 Swift Package。 您可以使用
.artifactbundle.zip
神器:
在你的Package.swift
:
(...)
targets: [
.binaryTarget(
name: "SwiftLintBinary",
url: "https://github.com/realm/SwiftLint/releases/download/0.47.1/SwiftLintBinary-macos.artifactbundle.zip",
checksum: "cdc36c26225fba80efc3ac2e67c2e3c3f54937145869ea5dbcaa234e57fc3724"
),
.plugin(
name: "SwiftLintPlugin",
capability: .buildTool(),
dependencies: ["SwiftLintBinary"]
),
.target(
name: "YourPackageName",
dependencies: [],
plugins: ["SwiftLintPlugin"]
),
(...)
]
接下来您必须设置插件配置。在您的“Plugins/SwiftLintPlugin”目录中创建一个“plugin.swift”文件:
import PackagePlugin
@main
struct SwiftLintPlugins: BuildToolPlugin {
func createBuildCommands(context: PluginContext, target: Target) async throws -> [Command] {
return [
.buildCommand(
displayName: "Linting \(target.name)...",
executable: try context.tool(named: "swiftlint").path,
arguments: [
"lint",
"--in-process-sourcekit",
"--path",
target.directory.string // only lint the files in the target directory
],
environment: [:]
)
]
}
}
就是这样。
如果您需要更多背景信息,您还可以在此处查看我的存储库:https://github.com/Filozoff/Kukulka