StringsLint

一个确保本地化字符串完整且永不被使用的工具。

StringsLint 会钩入你的源文件,特别是它会扫描

Test Status

如果你发现了一个 bug有一个功能请求,请提交一个 issue。如果你想要贡献代码,请提交一个 pull request。

安装

使用 Mint

mint install dral3x/stringslint

使用 Homebrew

brew tap dral3x/dral3x
brew install stringslint

使用 CocoaPods

只需将以下行添加到您的 Podfile

pod 'StringsLint'

这将在您下次执行 pod install 时下载 StringsLint 二进制文件和依赖项到 Pods/ 中,并允许您通过脚本构建阶段中的 ${PODS_ROOT}/StringsLint/stringslint 调用它。

这是安装特定 StringsLint 版本的推荐方法,因为它支持安装一个固定的版本,而不仅仅是最新版本(Homebrew 和 Mint 就是这种情况)。

请注意,这会将 StringsLint 二进制文件、其依赖项的二进制文件以及 Swift 二进制库分发添加到 Pods/ 目录中,因此不建议将此目录签入到 git 等 SCM 中。

从源代码编译

您可以克隆此项目并运行 make install(Xcode 10.0 或更高版本)从源代码构建。

用法

Xcode

将 StringsLint 集成到 Xcode scheme 中,以在 IDE 中显示警告和错误。 只需添加一个新的 “Run Script Phase”,内容为:

if which stringslint >/dev/null; then
  stringslint
else
  echo "warning: StringsLint not installed, download from https://github.com/dral3x/StringsLint"
fi

或者,如果您通过 CocoaPods 安装了 StringsLint,则脚本应如下所示:

"${PODS_ROOT}/StringsLint/stringslint"

命令行

$ stringslint help
OVERVIEW: A tool to enforce Swift style and conventions.

USAGE: stringslint <subcommand>

OPTIONS:
  --version               Show the version.
  -h, --help              Show help information.

SUBCOMMANDS:
  lint (default)          Print lint warnings and errors (default command)
  version                 Display the current version of StringsLint

  See 'stringslint help <subcommand>' for detailed help.

在包含要 lint 的文件的目录中运行 stringslint。 将递归搜索目录。

GitHub Action

在您的仓库中创建一个新的 workflow 文件(例如:.github/workflows/stringslint.yml),内容如下:

name: StringsLint

on:
  pull_request:
    paths:
      - '.github/workflows/stringslint.yml'
      - '.stringslint.yml'
      - '**/*.swift'
      - '**/*.strings'
      - '**/*.stringsdict'

jobs:
  StringsLint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: GitHub Action for StringsLint
        uses: dral3x/action-stringslint@1.1.9

更多详情请见 https://github.com/dral3x/action-stringslint

规则

包含了一些基本但重要的规则。 有关更多信息,请参见 Rules.md

您还可以检查 Sources/StringsLintFramework/Rules/Lint 目录以查看它们的实现。

每个规则都可以发出违规。 您可以配置这些违规的严重性(可接受的值为 nonewarningerror)。

当发出严重性为 error 的违规时,Xcode 构建将失败。

配置

通过从您将运行 StringsLint 的目录中添加一个 .stringslint.yml 文件来配置 StringsLint。 您可以配置包含和排除的文件路径,扩展一些解析器的功能,甚至可以关闭每个规则的规则或特定文件

included: # paths to include during linting. `--path` is ignored if present.
  - Source
excluded: # paths to ignore during linting. Takes precedence over `included`.
  - Carthage
  - Pods
  - Source/ExcludedFolder
  - Source/ExcludedFile.swift
  - Source/*/ExcludedFile.swift # Exclude files with a wildcard

# Customize parsers
objc_parser:
  implicit_macros:
    - SPKLocalizedString # detect this custom macro

xib_parser:
  key_paths:
    - textLocalized # keyPath used to localized UI elements

swift_parser:
  swiftui_implicit: false # disable implicit detection of SwiftUI localized strings

# Customize specific rules
missing:
  severity: error
  ignored:
    - Demo title

partial:
  severity: warning

unused:
  severity: note
  ignored:
    - NSAppleMusicUsageDescription # used by iOS directly

missing_comment:
  severity: none

许可证

MIT 许可。