SwiftLintPlugin

Twitter: @lukeeep_

一个用于 SwiftLint 的 Swift Package 插件,它将在构建时运行 SwiftLint 并在 Xcode 中显示错误和警告。

注意SwiftLint 代码仓库中现在已经有一个官方版本! 虽然这个包仍然会被维护和更新,因为它具有仓库更小,因此作为依赖项下载速度更快的优点。

添加到 Package

首先添加来自此 package 的依赖项

dependencies: [
    // ...
    .package(url: "https://github.com/lukepistrol/SwiftLintPlugin", from: "0.2.2"),
]

然后将其作为插件添加到你的 targets

targets: [
    .target(
        name: "YOUR_TARGET",
        dependencies: [],
        plugins: [
            .plugin(name: "SwiftLint", package: "SwiftLintPlugin")
        ]
    ),
]

添加到 Project

从 Xcode 14 开始,插件也可以在 Xcode Project 的 targets 上工作。 为此,只需在 Xcode 中将此 package 添加到你的 SPM 依赖项中。 之后,打开你的 target 的设置 > Build Phases 并将 SwiftLint 添加到 Run Build Tool Plug-ins,如下所示

Screen Shot 2022-09-02 at 09 33 23

你可能需要启用和信任插件,然后才能在构建期间实际运行它。

修复警告

0.1.0 版本开始,此 package 还包含一个命令插件,可以对任何 target 调用。

  1. 在项目导航器中选择一个项目或 package。
  2. 右键单击并选择 SwiftLintFix
    • 或者,你可以选择 File > Packages > SwiftLintFix
  3. 选择要在其上运行 swiftlint --fix 命令的 target(s)。

Screenshot 2022-10-31 at 12 59 53

在 CI 上运行

重要的是要注意,在任何 CI 提供商(例如 GitHub Actions)上构建 package/项目时,必须将 -skipPackagePluginValidation 标志传递给 xcodebuild 命令。 这将跳过验证提示,该提示在 Xcode 中如下所示

Screenshot 2022-12-13 at 17 48 44

示例

xcodebuild  \
    -scheme "$SCHEME" \
    -destination "$PLATFORM" \
    -skipPackagePluginValidation \ # this is mandatory
    clean build

如果需要禁用 linting(对于发布/应用商店版本),你可以设置 DISABLE_SWIFTLINT 环境变量


Buy Me A Coffee