Repository navigation
Expand file tree
/
Copy pathdevflow_cli.py
More file actions
268 lines (249 loc) · 8.13 KB
/
Copy pathdevflow_cli.py
File metadata and controls
268 lines (249 loc) · 8.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python3
"""
DevFlow CLI - 统一命令行入口(模块化版本,支持 pip 安装)
用法: devflow <command> [options]
python devflow_cli.py <command> [options]
"""
import os
import subprocess
import sys
from pathlib import Path
# 定位 scripts 目录:优先仓库内(开发模式),其次安装目录(pip 安装后)
_REPO_SCRIPTS = Path(__file__).resolve().parent / "scripts"
def _candidate_installed_dirs() -> list[Path]:
"""pip 安装后数据文件可能的位置(wheel 的 data 目录)"""
candidates = []
# 常规 site-packages 安装:<prefix>/share/devflow/scripts
candidates.append(Path(sys.prefix) / "share" / "devflow" / "scripts")
# user 安装(pip install --user):~/.local/share/devflow/scripts
user_base = os.environ.get("PYTHONUSERBASE")
if user_base:
candidates.append(Path(user_base) / "share" / "devflow" / "scripts")
else:
candidates.append(Path.home() / ".local" / "share" / "devflow" / "scripts")
# 某些系统 data 目录不同
try:
import sysconfig
candidates.append(Path(sysconfig.get_path("data")) / "share" / "devflow" / "scripts")
except Exception:
pass
return candidates
def _scripts_dir() -> Path:
if _REPO_SCRIPTS.exists():
return _REPO_SCRIPTS
for d in _candidate_installed_dirs():
if d.exists():
return d
return _REPO_SCRIPTS # 返回默认,运行时再报错
SCRIPTS_DIR = _scripts_dir()
COMMANDS = {
"review": {
"desc": "代码审查 (安全/逻辑/性能)",
"script": "review.py",
"example": "devflow review --mode security --dir ."
},
"explain": {
"desc": "代码结构分析 (代码→人话需配合 AI)",
"script": "explain.py",
"example": "devflow explain --file main.py"
},
"testgen": {
"desc": "测试模板生成",
"script": "testgen.py",
"example": "devflow testgen --file calculator.py --framework pytest"
},
"docgen": {
"desc": "文档生成",
"script": "docgen.py",
"example": "devflow docgen --mode all --file main.py"
},
"smell": {
"desc": "代码坏味道检测",
"script": "smell.py",
"example": "devflow smell --dir ."
},
"migrate": {
"desc": "语法/语言规则转换",
"script": "migrate.py",
"example": "devflow migrate --from py2 --to py3 --dir src/"
},
"env": {
"desc": "环境检查",
"script": "env-check.py",
"example": "devflow env --dir ."
},
"git": {
"desc": "Git 辅助 (Commit/PR)",
"script": "git-helper.py",
"example": "devflow git --mode commit"
},
"clean": {
"desc": "清理调试标记",
"script": "clean-debug.py",
"example": "devflow clean --dir src/"
},
"regex": {
"desc": "正则表达式工具",
"script": "regex.py",
"example": "devflow regex --pattern '\\d+' --text 'abc123'"
},
"boundary": {
"desc": "边界用例推演",
"script": "boundary.py",
"example": "devflow boundary --file main.py"
},
"racecheck": {
"desc": "竞态条件分析",
"script": "racecheck.py",
"example": "devflow racecheck --file async.py"
},
"config": {
"desc": "配置文件生成",
"script": "config-gen.py",
"example": "devflow config --type docker"
},
"a11y": {
"desc": "无障碍审查",
"script": "a11y.py",
"example": "devflow a11y --file index.html"
},
"db": {
"desc": "数据库查询优化",
"script": "db-optimize.py",
"example": "devflow db --file query.sql"
},
"full": {
"desc": "全流程运行",
"script": "devflow.py",
"example": "devflow full --dir ."
},
"selftest": {
"desc": "自测",
"script": "selftest.py",
"example": "devflow selftest"
},
"setup": {
"desc": "一键接入 AI 平台 (claude/codex/opencode/cursor/windsurf/trae/copilot)",
"script": "setup.py",
"example": "devflow setup install --target <项目目录>"
},
"todo": {
"desc": "TODO/FIXME 标记扫描",
"script": "todo-scan.py",
"example": "devflow todo --dir ."
},
"complexity": {
"desc": "代码复杂度分析",
"script": "complexity.py",
"example": "devflow complexity --dir src/ --threshold 10"
},
"gitstats": {
"desc": "Git 提交统计分析",
"script": "git-stats.py",
"example": "devflow gitstats --dir ."
},
"workflow": {
"desc": "阶段化工作流引擎 (init/plan/code/verify/review)",
"script": "workflow.py",
"example": "devflow workflow init --dir ."
},
"agents": {
"desc": "多 Agent 协作 (pm/architect/developer/qa/reviewer)",
"script": "agents.py",
"example": "devflow agents --team qa,reviewer --dir ."
},
"doctor": {
"desc": "项目健康诊断",
"script": "doctor.py",
"example": "devflow doctor --dir ."
},
"audit": {
"desc": "深度安全审计",
"script": "security-audit.py",
"example": "devflow audit --dir ."
},
"pr": {
"desc": "PR 合并检查",
"script": "pr-tool.py",
"example": "devflow pr --diff pr.diff"
},
"tdd": {
"desc": "TDD 实践检查",
"script": "tdd-check.py",
"example": "devflow tdd --dir ."
},
"map": {
"desc": "代码地图",
"script": "code-map.py",
"example": "devflow map --dir ."
},
"memory": {
"desc": "项目记忆管理",
"script": "memory.py",
"example": "devflow memory add '决策内容'"
},
"refactor": {
"desc": "重构建议",
"script": "refactor.py",
"example": "devflow refactor --dir ."
},
"release": {
"desc": "发布说明生成",
"script": "release-notes.py",
"example": "devflow release --dir ."
},
"onboarding": {
"desc": "新成员项目导览",
"script": "onboarding.py",
"example": "devflow onboarding --dir ."
},
"coverage": {
"desc": "测试覆盖率估算",
"script": "coverage-scan.py",
"example": "devflow coverage --dir ."
}
}
def show_help():
"""显示帮助信息"""
print("DevFlow CLI - 一站式全链路开发辅助工具\n")
print("用法: devflow <command> [options]\n")
print("可用命令:")
for cmd, info in COMMANDS.items():
print(f" {cmd:<12} {info['desc']}")
print("\n示例:")
for cmd, info in list(COMMANDS.items())[:5]:
print(f" {info['example']}")
print("\n更多帮助: devflow <command> --help")
def main(argv=None):
argv = list(sys.argv[1:] if argv is None else argv)
if not argv or argv[0] in ("-h", "--help", "help"):
show_help()
return 0
command = argv[0]
if command not in COMMANDS:
print(f"错误: 未知命令 '{command}'")
print(f"运行 'devflow help' 查看可用命令")
return 2
# setup 命令特殊处理:setup.py 位于 share/devflow/(scripts 的上一级)
if command == "setup":
setup_script = SCRIPTS_DIR.parent / "setup.py"
if not setup_script.exists():
setup_script = _REPO_SCRIPTS.parent / "setup.py"
if not setup_script.exists():
print(f"错误: 找不到 setup.py,请在 DevFlow 仓库目录下运行,或重新 pip install .")
return 2
cmd = [sys.executable, str(setup_script)] + argv[1:]
result = subprocess.run(cmd)
return result.returncode
script = SCRIPTS_DIR / COMMANDS[command]["script"]
args = argv[1:]
if not script.exists():
print(f"错误: 脚本不存在 {script}")
print("若已通过 pip 安装,请确认 scripts/ 已随包安装;"
"或在仓库目录下运行。")
return 2
cmd = [sys.executable, str(script)] + args
result = subprocess.run(cmd)
return result.returncode
if __name__ == "__main__":
sys.exit(main())