name: architecture-analyzer description: 分析和可视化软件架构模式、依赖关系和模块边界,用于迁移规划 allowed-tools: [“Bash”, “Read”, “Write”, “Grep”, “Glob”, “Edit”]
架构分析器技能
分析和可视化软件架构模式和依赖关系,以支持迁移规划、模块边界识别和架构决策制定。
目的
实现全面的架构分析,用于:
- 组件依赖关系映射
- 分层架构检测
- 耦合和内聚度量
- 架构违规检测
- 模块边界识别
- 依赖关系图生成
能力
1. 组件依赖关系映射
- 提取模块/包之间的依赖关系
- 映射服务间通信
- 识别外部系统集成
- 跟踪组件间数据流
- 生成依赖关系矩阵
2. 分层架构检测
- 识别架构层(表示层、业务层、数据层)
- 检测层违规
- 映射横切关注点
- 分析层依赖关系
- 验证架构模式
3. 耦合/内聚度量
- 计算传入耦合(Ca)
- 计算传出耦合(Ce)
- 计算不稳定性指数(I = Ce / (Ca + Ce))
- 测量模块内聚性
- 识别高度耦合的组件
4. 架构违规检测
- 检测模块间的循环依赖
- 识别层绕过
- 查找UI层直接访问数据库
- 检查抽象使用是否恰当
- 验证依赖规则
5. 模块边界识别
- 检测逻辑模块分组
- 识别有界上下文
- 映射共享内核区域
- 分析模块接口
- 建议分解边界
6. 依赖关系图生成
- 生成DOT格式图
- 创建Mermaid图
- 导出到PlantUML
- 生成交互式可视化
- 支持多种粒度级别
工具集成
当可用时,此技能可以利用以下外部工具:
| 工具 | 用途 | 集成方法 |
|---|---|---|
| Structure101 | 架构可视化 | 导出分析 |
| Lattix | 依赖关系分析 | CLI / API |
| NDepend | .NET架构分析 | CLI |
| JDepend | Java包依赖关系 | CLI |
| Madge | JavaScript/TypeScript | CLI |
| deptree | Python依赖关系 | CLI |
| go-arch-lint | Go架构 | CLI |
| ast-grep | 模式匹配 | MCP服务器 |
使用方法
基本分析
# 调用技能进行架构分析
# 技能将分析结构和依赖关系
# 预期输入:
# - targetPath:代码库根路径
# - analysisDepth:'module' | 'package' | 'class' | 'function'
# - outputFormat:'json' | 'dot' | 'mermaid' | 'plantuml'
# - includeMetrics:布尔值
分析工作流程
-
发现阶段
- 识别项目结构
- 检测构建配置
- 映射源目录
-
提取阶段
- 解析导入/require语句
- 提取模块依赖关系
- 识别外部依赖
-
分析阶段
- 计算耦合度量
- 检测架构模式
- 识别违规
- 映射边界
-
可视化阶段
- 生成依赖关系图
- 创建架构图
- 生成度量报告
输出模式
{
"analysisId": "string",
"timestamp": "ISO8601",
"target": {
"path": "string",
"language": "string",
"moduleCount": "number",
"totalFiles": "number"
},
"architecture": {
"pattern": "string (layered|modular|monolithic|microservices)",
"layers": [
{
"name": "string",
"modules": ["string"],
"allowedDependencies": ["string"]
}
],
"boundedContexts": [
{
"name": "string",
"modules": ["string"],
"interfaces": ["string"]
}
]
},
"modules": [
{
"name": "string",
"path": "string",
"files": "number",
"linesOfCode": "number",
"dependencies": ["string"],
"dependents": ["string"],
"metrics": {
"afferentCoupling": "number",
"efferentCoupling": "number",
"instability": "number",
"cohesion": "number"
}
}
],
"dependencies": [
{
"from": "string",
"to": "string",
"type": "import|call|inherit|implement",
"count": "number"
}
],
"violations": [
{
"type": "circular|layer-bypass|abstraction-leak",
"severity": "high|medium|low",
"from": "string",
"to": "string",
"description": "string",
"recommendation": "string"
}
],
"metrics": {
"averageCoupling": "number",
"maxCoupling": "number",
"cyclomaticComplexity": "number",
"circularDependencies": "number",
"layerViolations": "number"
},
"graphs": {
"dot": "string (file path)",
"mermaid": "string (file path)",
"plantuml": "string (file path)"
}
}
与迁移流程集成
此技能与以下代码迁移/现代化流程集成:
- legacy-codebase-assessment:架构理解
- monolith-to-microservices:服务边界识别
- migration-planning-roadmap:基于依赖关系的规划
- code-refactoring:耦合减少目标
配置
技能配置文件
在项目根目录创建.architecture-analyzer.json:
{
"analysisDepth": "module",
"excludePaths": [
"node_modules",
"vendor",
"dist",
"build",
".git",
"__tests__"
],
"modulePatterns": {
"javascript": "src/*",
"java": "src/main/java/**",
"python": "src/*"
},
"layers": {
"enabled": true,
"definitions": [
{
"name": "presentation",
"patterns": ["**/ui/**", "**/views/**", "**/controllers/**"],
"allowedDependencies": ["business", "shared"]
},
{
"name": "business",
"patterns": ["**/services/**", "**/domain/**"],
"allowedDependencies": ["data", "shared"]
},
{
"name": "data",
"patterns": ["**/repositories/**", "**/dao/**"],
"allowedDependencies": ["shared"]
},
{
"name": "shared",
"patterns": ["**/common/**", "**/utils/**"],
"allowedDependencies": []
}
]
},
"rules": {
"maxCoupling": 10,
"maxModuleSize": 5000,
"forbiddenDependencies": [
{"from": "presentation", "to": "data"}
]
},
"visualization": {
"formats": ["mermaid", "dot"],
"groupBy": "layer",
"showMetrics": true
}
}
MCP服务器集成
当ast-grep MCP服务器可用于模式检测时:
// 示例架构模式检测
{
"tool": "ast_grep_search",
"arguments": {
"pattern": "import { $_ } from '../data/$_'",
"language": "typescript",
"path": "./src/ui"
}
}
架构模式
分层架构
┌─────────────────────────────────┐
│ 表示层 │
│ (UI, 控制器, 视图) │
└──────────────┬──────────────────┘
│
┌──────────────▼──────────────────┐
│ 业务层 │
│ (服务, 领域, 逻辑) │
└──────────────┬──────────────────┘
│
┌──────────────▼──────────────────┐
│ 数据层 │
│ (存储库, DAO, ORM) │
└─────────────────────────────────┘
模块化单体架构
┌─────────────────────────────────────────────┐
│ 应用程序外壳 │
├───────────┬───────────┬───────────┬─────────┤
│ 模块 A │ 模块 B │ 模块 C │ 共享 │
│ ┌─────┐ │ ┌─────┐ │ ┌─────┐ │ ┌─────┐ │
│ │ UI │ │ │ UI │ │ │ UI │ │ │工具 │ │
│ ├─────┤ │ ├─────┤ │ ├─────┤ │ └─────┘ │
│ │逻辑 │ │ │逻辑 │ │ │逻辑 │ │ │
│ ├─────┤ │ ├─────┤ │ ├─────┤ │ │
│ │数据 │ │ │数据 │ │ │数据 │ │ │
│ └─────┘ │ └─────┘ │ └─────┘ │ │
└───────────┴───────────┴───────────┴─────────┘
微服务(目标)
┌─────────┐ ┌─────────┐ ┌─────────┐
│服务 A │ │服务 B │ │服务 C │
│ ┌───┐ │ │ ┌───┐ │ │ ┌───┐ │
│ │API│ │ │ │API│ │ │ │API│ │
│ └─┬─┘ │ │ └─┬─┘ │ │ └─┬─┘ │
│ │ │ │ │ │ │ │ │
│ ┌─▼─┐ │ │ ┌─▼─┐ │ │ ┌─▼─┐ │
│ │DB │ │ │ │DB │ │ │ │DB │ │
│ └───┘ │ │ └───┘ │ │ └───┘ │
└─────────┘ └─────────┘ └─────────┘
│ │ │
└────────────┼────────────┘
│
┌──────▼──────┐
│ 事件总线 │
└─────────────┘
度量参考
耦合度量
| 度量 | 公式 | 良好 | 警告 | 差 |
|---|---|---|---|---|
| 传入耦合(Ca) | 传入依赖关系 | < 10 | 10-20 | > 20 |
| 传出耦合(Ce) | 传出依赖关系 | < 10 | 10-20 | > 20 |
| 不稳定性(I) | Ce / (Ca + Ce) | 0-0.3 或 0.7-1.0 | 0.3-0.7 | - |
| 抽象性(A) | 抽象类 / 总数 | 上下文相关 | - | - |
与主序列的距离
D = |A + I - 1|
- D = 0:理想(在主序列上)
- D > 0.3:警告
- D > 0.5:有问题
最佳实践
- 定期分析:将架构分析作为CI/CD的一部分运行
- 定义边界:明确定义模块和层边界
- 强制执行规则:使用架构适应度函数
- 记录决策:使用ADR进行架构变更
- 跟踪度量:监控耦合趋势随时间变化
- 可视化:保持架构图最新
相关技能
static-code-analyzer:代码级分析domain-model-extractor:DDD边界识别technical-debt-quantifier:架构债务评估
相关代理
legacy-system-archaeologist:使用此技能进行架构发现microservices-decomposer:使用此技能进行边界识别ddd-analyst:使用此技能进行上下文映射migration-readiness-assessor:使用此技能进行架构评估