通过一个简单的命令行界面,生成关于指定的 Swift 类型在您的 iOS 代码库中被使用频率的报告。
我将维护这个分支,而不是原始的 thumbtack/star,因为我已经不在 Thumbtack 工作了,并且我是该项目的主要贡献者。
$ 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()
技术上在 AST 中包含 UIView
标识符的两个节点:一个在类型注解中,一个在构造函数调用中。 但是,对于大多数商业用途,最好只将该行计为 UIView
的一次使用。 因此,类型注解会被 STAR 忽略。
一些其他有意忽略的引用示例包括代码注释、类/结构体/扩展/等等声明,以及组件内的内部类(例如,MyComponent.SomeInnerClass
将既不匹配 MyComponent
,也不匹配 SomeInnerClass
)。
$ cd star
$ make uninstall
除了命令行可执行文件 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/kevinmbeaulieu/star.git
$ cd star
$ xed .
$ swift run star ...
传入 --verbose
参数将打印出可以用于调试的附加信息。
$ swift test
xed .