Build Status SPM Latest Version License

Swift 类型采用报告器 (STAR)

使用简单的命令行界面,生成关于指定 Swift 类型在你的 iOS 代码库中被使用频率的报告。

安装

$ cd star
$ make install

用法

$ star --types Avatar Button Color Pill --files ./

Avatar used 27 times.
Button used 167 times.
Color used 2711 times.
Pill used 9 times.

要报告位于单独模块中的类型,请指定 --moduleName。 这将确保像 Thumbprint.Button() 这样的引用也被捕获。

$ star --types Avatar Button Color Pill --files ./ --module Thumbprint

Avatar used 30 times.
Button used 182 times.
Color used 2786 times.
Pill used 11 times.

选项

USAGE: star [--types <types> ...] [--module <module>] [--format <format>] [--files <files> ...] [--includeTypeInheritance] [--verbose]

OPTIONS:
  -t, --types <types>     List of types on which to report
  -m, --module <module>   Name of module containing types, to ensure types referenced by <module name>.<type name> are counted
  -f, --format <format>   Output format (humanReadable|json) (default: humanReadable)
  --files <files>         Paths in which to look for Swift source
  --includeTypeInheritance
                          Include subclass and protocol conformance declarations in usage counts
  -v, --verbose           Print additional information about source as it is parsed
  -h, --help              Show help information.

工作原理

STAR 使用 SwiftSyntax 遍历 AST 并查找对指定标识符的引用。 由于 STAR 在无类型 AST 上运行,因此当将引用链接到其标识符需要完全类型检查时,使用情况报告可能包含不完善的信息。

报告器尝试提供尽可能有用的信息,因此某些类型的引用被有意过滤掉。 例如,代码行

let foo: UIView = UIView()

从技术上讲,对于 UIView 标识符,在 AST 中包含两个节点:一个在类型注释中,另一个在构造函数调用中。 但是,对于大多数商业用途而言,最好只将此行代码计为 UIView 的一次使用。 因此,类型注释被 STAR 忽略。

其他一些被有意忽略的引用的示例包括代码注释、类/结构体/扩展等声明,以及组件内的内部类(例如,MyComponent.SomeInnerClass 将不匹配 MyComponent,也不匹配 SomeInnerClass)。

卸载

$ cd star
$ make uninstall

导入到另一个 Swift 包中

除了命令行可执行文件 star 之外,STAR 的核心功能也可以通过 Swift Package Manager 作为静态库 STARLib 使用。 要在你的 Swift 包中使用 STARLib,请将以下内容添加到你的 Package.swift

let package = Package(
    ...
    dependencies: [
        ...
        .package(name: "SwiftTypeAdoptionReporter", url: "https://github.com/thumbtack/star.git", <version (e.g., `.upToNextMinor(from: "3.0.0")`)>),
    ],
    targets: [
        .target(
            ...
            dependencies: [
                ...
                .product(name: "STARLib", package: "SwiftTypeAdoptionReporter"),
            ]
        ),
    ]
)

贡献

如果您有使 STAR 更有用的想法,请打开一个 issue 或提交一个 pull request! 请参阅下文以获取有关本地构建/测试的说明。

$ git clone git@github.com/thumbtack/star.git
$ cd star
$ open -a Xcode .

本地构建和运行

$ swift run star ...

传入 --verbose 参数将打印出额外的有用的调试信息。

运行测试套件

从命令行

$ swift test

使用 Xcode

  1. 创建 Xcode 项目

    $ swift package generate-xcodeproj
    
  2. 打开 SwiftTypeAdoptionReporter.xcodeproj

  3. 在 Xcode 中,Product -> Test