depermaid 是一个 Swift Package Manager 插件,用于生成 Mermaid 图表,以可视化您的 Swift 包中的依赖关系。
使用 depermaid 可以让您持续可视化项目依赖关系的最新状态。 它提供了一种以可视方式面对项目结构的方式,从而深入了解您的依赖关系。 建议在每次更新 Package.swift
文件的内容时运行 depermaid,并将生成的图表反映在您的 README 或文档中。
//swift-tools-version:5.9
将以下行添加到 Package.swift
文件中的 dependencies
部分
dependencies: [
.package(url: "https://github.com/daikimat/depermaid.git", from: "1.1.0")
],
在与 Package.swift
文件相同的目录中运行以下命令
$ swift package plugin depermaid
生成的 Mermaid 图表说明了 Swift 包中模块之间的依赖关系
flowchart LR
AnimalClient
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
您可以根据您的具体使用场景自定义命令选项
运行以下命令以在生成的 Mermaid 图表中包含测试目标
$ swift package plugin depermaid --test
flowchart LR
AnimalClient
AnimalClientTests{{AnimalClientTests}}-->AnimalClient
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
运行以下命令以在生成的 Mermaid 图表中包含可执行目标
$ swift package plugin depermaid --executable
flowchart LR
AnimalClient
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
ExecutableExample([ExecutableExample])-->Dog
运行以下命令以在生成的 Mermaid 图表中包含产品
$ swift package plugin depermaid --product
flowchart LR
AnimalClient-->LifeCore[[LifeCore]]
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
运行以下命令以在生成的 Mermaid 图表中包含测试目标、可执行目标和产品
$ swift package plugin depermaid --test --executable --product
flowchart LR
AnimalClient-->LifeCore[[LifeCore]]
AnimalClientTests{{AnimalClientTests}}-->AnimalClient
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
ExecutableExample([ExecutableExample])-->Dog
您可以选择使用 --direction 参数来定义生成的 Mermaid 图表的朝向。 可用的朝向有 TD、TB(与 TD 相同)、BT、RL、LR。 默认情况下,它设置为 LR。"
$ swift package plugin depermaid --direction TD --test --executable --product
flowchart TD
AnimalClient-->LifeCore[[LifeCore]]
AnimalClientTests{{AnimalClientTests}}-->AnimalClient
Cat-->AnimalClient
Dog-->AnimalClient
Example-->Cat
Example-->Dog
ExecutableExample([ExecutableExample])-->Dog
对于具有深度直接和传递依赖关系层次结构的包,生成的图可能显得复杂,如下所示
flowchart LR
A-->B
A-->C
A-->D
A-->E
B-->C
B-->D
B-->E
C-->D
C-->E
D
E
要简化图表并仅关注基本依赖关系,请运行以下命令
$ swift package plugin depermaid --minimal
生成的 Mermaid 图表将通过省略重复箭头来显示更清晰的表示
flowchart LR
A-->B
B-->C
C-->D
C-->E
D
E
为了演示 Depermaid 的功能,在 ./Example/ExampleDepermaid.xcodeproj
中提供了一个示例项目。
成功构建和运行后,Depermaid 将分析示例项目中的 Swift 包依赖关系,并生成 Mermaid 格式的图形。 该图将自动反映在此 README 文件中,演示 Depermaid 集成以可视化 Swift 包依赖关系。 在查看生成的图时,请参考此 README 文件。
在构建过程中自动更新 README 文件的机制是使用添加到 Xcode 项目的构建阶段的 自定义脚本 实现的。
按照以下步骤构建并运行示例项目
打开 Xcode 项目
open ./Example/ExampleDepermaid.xcodeproj
根据需要更改 ./Example/Package.swift
文件。
从 Xcode 构建并运行项目。
构建过程完成后,请查看此 README 文件以查看反映对 Swift 包依赖关系所做更改的更新图形。
Depermaid 在 MIT 许可证 下发布。