Halleck45/ast-metrics

GitHub: ast-metrics/ast-metrics

一款轻量级离线代码质量与架构分析工具,通过 AST 解析为多语言项目提供复杂度、耦合度、巴士因子等指标度量及 CI/CD 集成。

Stars: 150 | Forks: 16

AST Metrics

无需服务器。无需账号。单一可执行文件。
AST Metrics 可以分析你的代码库(复杂度、架构、耦合度、巴士因子等),并且可以在任何地方运行。
将其直接放入任何 CI 中。完全离线可用。无需安装,没有 SaaS,数据也不会离开你的本机。


CI GitHub Release License: MIT GitHub all releases Go Report Card codecov AST-Metrics report

文档 | 贡献指南 | Twitter

banner

分析你的项目

粘贴一个 GitHub URL。获取完整报告。无需安装。

或查看实时示例:

spf13/cobra fatih/color gorilla/mux guzzle/psr7 thephpleague/flysystem



## 开始使用 打开你的终端并运行以下命令: ``` curl -s https://raw.githubusercontent.com/Halleck45/ast-metrics/main/scripts/download.sh|bash ./ast-metrics analyze --report-html= /path/to/your/code ``` ## 你能得到什么 | | | |---|---| | **架构分析** | 社区发现、耦合度、不稳定性 — 尽早发现设计偏离 | | **代码指标** | 圈复杂度、可维护性指数、代码行数 | | **活动指标** | 提交历史、巴士因子 — 了解代码归属于谁 | | **Linter** | 对耦合度、复杂度、每个方法的代码行数强制执行阈值限制 | | **CI/CD 就绪** | GitHub Actions、GitLab CI、任何流水线 — 出现违规时以非零状态退出 | | **多种报告格式** | HTML 仪表盘、JSON、Markdown、SARIF、OpenMetrics | | **MCP server** | 通过 Model Context Protocol 为 AI 编程助手赋予架构感知能力 | [在文档中阅读更多内容](https://ast-metrics.dev/) ## 检查你的代码 运行: ``` # 创建一个 .ast-metrics.yaml config 文件 ast-metrics init # 将 ruleset 添加到您的 config 文件中 ast-metrics ruleset add architecture ast-metrics ruleset add volume ast-metrics ruleset list # see the list of available rulesets # 运行 linter ast-metrics lint ``` 你可以在 YAML 配置中声明阈值(*每个方法的代码行数、耦合度、可维护性等*)。 示例: ``` requirements: rules: architecture: coupling: forbidden: - from: Controller to: Repository - from: Repository to: Service max_afferent_coupling: 10 max_efferent_coupling: 10 min_maintainability: 70 volume: max_loc: 1000 max_logical_loc: 600 max_loc_by_method: 30 max_logical_loc_by_method: 20 complexity: max_cyclomatic: 10 golang: no_package_name_in_method: true max_nesting: 4 max_file_size: 1000 max_files_per_package: 50 slice_prealloc: true ignored_error: true context_missing: true context_ignored: true ``` 这使得**轻松实现大规模的架构与质量保障**成为可能。 运行 `ast-metrics ruleset list` 查看可用的规则集列表。然后使用 `ast-metrics ruleset add ` 将规则集应用到你的项目中。 ## 在 CI 中使用 使用专门的 CI 命令运行 lint 并一次性生成所有报告: ``` ast-metrics ci [options] /path/to/your/code ``` 注意事项: - 该命令首先运行 linter,然后生成 HTML、Markdown、JSON、OpenMetrics 和 SARIF 报告。 - 如果发现任何 lint 违规,命令将以非零状态退出,但仍会生成报告。 - 先前的别名 `analyze --ci` 已废弃并会显示警告。请迁移至 `ast-metrics ci`。 ## Github Action 在你的项目中创建一个 `.github/workflows/ast-metrics.yml` 文件,内容如下: ``` name: "AST Metrics" on: pull_request: permissions: contents: read pull-requests: write # optional: allows the action to comment on the pull request jobs: ast-metrics: runs-on: ubuntu-latest steps: - uses: halleck45/action-ast-metrics@v2 ``` 在每次 pull request 时,该 Action 会运行 `ast-metrics review` 并将结果发布在检查摘要中;如果权限允许,还会作为一条更新的评论发布。在 `push` 事件中,它会转而运行完整的分析。有关所有选项(`fail-on`、`sarif`、`html-artifact` 等),请参阅 [action-ast-metrics](https://github.com/Halleck45/action-ast-metrics)。 ## 面向 AI 助手的 MCP Server AI 编程助手(Claude Code、Cursor、Copilot 等)只能线性地读取代码,缺乏架构层面的全局观。AST Metrics 可以充当 [MCP server](https://modelcontextprotocol.io/),让它们能够按需获取复杂度、耦合度、依赖关系图、社区发现、风险评分以及测试质量等信息 —— 而无需阅读每一个文件。 ``` ast-metrics mcp . ``` 这将启动一个 stdio MCP server,暴露 8 个工具: | 工具 | 用途 | |---|---| | `analyze_project` | 高层概览:语言、复杂度、可维护性、主要风险 | | `get_file_metrics` | 特定文件的详细指标 | | `find_risky_code` | 风险评分最高的文件/类 | | `find_complex_code` | 复杂度超过阈值的函数/类 | | `get_dependencies` | 组件周围的依赖子图 | | `get_coupling` | 组件的传入/传出耦合度 | | `get_communities` | 架构社区发现及相关指标 | | `get_test_quality` | 测试隔离性、可追溯性、上帝测试、孤儿类 | 配置完成后,只需像平常一样与你的 AI 助手对话即可。例如: *"重构风险最高的文件有哪些?"* · *"给我看看 UserService 类的依赖关系 —— 如果我修改它会破坏什么?"* · *"有没有没有测试且复杂的类?"* · *"我需要处理 src/billing/invoice.go,有什么我应该了解的吗?"* 要将其与 Claude Code 或任何兼容 MCP 的助手一起使用,请在项目根目录添加一个 `.mcp.json` 文件: ``` { "mcpServers": { "ast-metrics": { "command": "ast-metrics", "args": ["mcp", "."] } } } ``` ## 支持的语言 + ✅ **Golang** `任意版本` + ✅ **Python** `Python 2, Python 3` + ✅ **Rust** `任意版本` + ✅ **PHP** `<= PHP 8.5` + ✅ **Java** `任意版本` + ✅ **C#** `任意版本` + ✅ **TypeScript** `任意版本` + 🕛 **Flutter** + 🕛 **C++** + 🕛 **Ruby** ## 许可证 AST Metrics 是开源软件,[基于 MIT 许可证授权](LICENSE) ## 支持本项目 如果 AST Metrics 为你节省了时间,给个 Star 会带来极大的帮助 —— 这有助于其他开发者发现这个工具。 [![Star History Chart](https://api.star-history.com/svg?repos=Halleck45/ast-metrics&type=Date)](https://star-history.com/#Halleck45/ast-metrics&Date)
标签:EVTX分析, LNA, SOC Prime, 云安全监控, 代码分析, 凭证管理, 开发工具, 日志审计, 架构分析, 聊天机器人, 静态分析