TargetDependencyChecker

一个简单的命令行工具,用于分析 Swift Package Manager 项目中定义的 target 之间的(本地)依赖关系,并捕获大型 target 图中可能出现的任何非依赖导入错误。

要求

Swift 5.5

用法

克隆仓库并使用 swift build 构建,然后使用 swift run TargetDependencyChecker <options> 运行。

$ swift run TargetDependencyChecker check --help

OVERVIEW: 
Inspects a Swift Package Manager project and produce warnings about targets that include other targets that are not declared as dependencies in the Package.swift manifest.

USAGE: TargetDependencyChecker check [--package-path <package-path>] [--output-type <output-type>] [--warn-once-per-framework] [--warn-indirect-dependencies] [--no-color] [--print-full-paths] [--ignore-includes <ignore-includes>]

OPTIONS:
  -p, --package-path <package-path>
                          Specifies the path for the directory containing a Package.swift manifest for a Swift Package Manager project.
                          If not specified, defaults to the current working directory.
  -t, --output-type <output-type>
                          Specifies the format of the output.
                          Defaults to 'terminal' if not provided.

                              terminal
                                  Prints output of conversion in a format proper for terminal's standard output.
                              
                              xcode
                                  Prints output with leading file/line numbers as warnings that Xcode can detect when used as a build phase.

  -o, --warn-once-per-framework
                          When specified, omits warnings of violations for frameworks that where already reported in previous files in the same target.
  -i, --warn-indirect-dependencies
                          When specified, warns when importing a target that is not a direct dependency into another target.
  -c, --no-color          When specified along with --output-type terminal, produces a non-colorized output in stdout.
  -f, --print-full-paths  When specified along with --output-type terminal, prints the full path of each file in the diagnostics.
  --ignore-includes <ignore-includes>
                          Ignores all includes in the string separated by commas provided to this argument.
  -h, --help              Show help information.

示例

包含了一个小型测试包,可用于测试该程序

$ swift run TargetDependencyChecker check -iop ./TestPackage/

将输出

请注意,TargetDependencyChecker 的某些参数可能与 swift run 识别的参数重叠,在这种情况下,您可以使用 swift build --show-bin-path 找到构建的二进制文件的路径,并直接从那里执行 TargetDependencyChecker

GraphViz 生成

可以使用 TargetDependencyChecker graph 生成包含包的依赖树的 Graphviz 文件。

$ swift run TargetDependencyChecker graph --help

OVERVIEW: 
Produces a GraphViz .dot file describing the internal dependency graph of a Swift Package Manager project.

USAGE: TargetDependencyChecker graph [--package-path <package-path>] [--output <output>] [--include-indirect] [--include-tests] [--include-folder-hierarchy]

OPTIONS:
  -p, --package-path <package-path>
                          Specifies the path for the directory containing a Package.swift manifest for a Swift Package Manager project.
                          If not specified, defaults to the current working directory.
  -o, --output <output>   A .dot file to write the results to, relative to the current working directory. If not provided, prints the result to stdout, instead.
  -i, --include-indirect  Include indirect dependencies via `import` statements on target's files.
  -t, --include-tests     Include test targets in the graph.
  -f, --include-folder-hierarchy
                          Include information about the target's folder hierarchy as clusters in the graph.
  -h, --help              Show help information.

您还可以使用示例项目来生成演示图文件

$ swift run TargetDependencyChecker graph -p ./TestPackage/

将生成

digraph {
    graph [rankdir=LR]

    0 [label="Core"]
    1 [label="IndirectCore"]
    2 [label="IndirectCoreRoot"]
    3 [label="TestPackage"]

    0 -> 1
    1 -> 2
    3 -> 0 [label="@ /Sources/TestPackage/TestPackage.swift", color=red]
}