-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup_claude_code.py
More file actions
executable file
·221 lines (186 loc) · 6.64 KB
/
setup_claude_code.py
File metadata and controls
executable file
·221 lines (186 loc) · 6.64 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
#!/usr/bin/env python3
"""
Claude Code Integration Setup Script for CCDebugger
快速設置 CCDebugger 與 Claude Code 的整合
"""
import os
import json
import sys
from pathlib import Path
import subprocess
def setup_claude_code_integration():
"""設置 Claude Code 整合"""
print("🚀 CCDebugger Claude Code Integration Setup")
print("-" * 50)
# 1. 檢查 CCDebugger 是否已安裝
print("\n1. 檢查 CCDebugger 安裝...")
try:
import claudecode_debugger
print("✅ CCDebugger 已安裝")
except ImportError:
print("❌ CCDebugger 未安裝")
if input("是否要安裝 CCDebugger? (y/n): ").lower() == 'y':
subprocess.run([sys.executable, '-m', 'pip', 'install', '-e', '.'])
print("✅ CCDebugger 安裝完成")
else:
print("請先安裝 CCDebugger: pip install claudecode-debugger")
return
# 2. 創建 Claude 配置目錄
print("\n2. 設置配置文件...")
claude_dir = Path.home() / '.claude'
claude_dir.mkdir(exist_ok=True)
# 3. 創建配置文件
config_file = claude_dir / 'ccdebug.json'
default_config = {
"autoAnalyze": True,
"defaultLanguage": "zh",
"enableSuggestions": True,
"saveHistory": True,
"maxHistorySize": 20,
"monitorMode": False,
"contextLines": 10,
"outputFormat": "structured",
"templates": {
"python": {
"enabled": True,
"customPrompts": []
},
"javascript": {
"enabled": True,
"customPrompts": []
}
}
}
if config_file.exists():
print(f"配置文件已存在: {config_file}")
if input("是否要覆蓋現有配置? (y/n): ").lower() != 'y':
with open(config_file) as f:
existing_config = json.load(f)
# 合併配置
for key, value in default_config.items():
if key not in existing_config:
existing_config[key] = value
default_config = existing_config
# 語言選擇
print("\n3. 選擇預設語言:")
print(" 1. 中文 (zh)")
print(" 2. English (en)")
choice = input("請選擇 (1/2) [預設: 1]: ").strip() or '1'
default_config['defaultLanguage'] = 'zh' if choice == '1' else 'en'
# 自動分析
print("\n4. 是否啟用自動錯誤分析?")
auto_analyze = input("啟用自動分析? (y/n) [預設: y]: ").strip().lower()
default_config['autoAnalyze'] = auto_analyze != 'n'
# 監控模式
print("\n5. 是否啟用監控模式?")
print(" 監控模式會持續監控所有命令輸出")
monitor_mode = input("啟用監控模式? (y/n) [預設: n]: ").strip().lower()
default_config['monitorMode'] = monitor_mode == 'y'
# 保存配置
with open(config_file, 'w') as f:
json.dump(default_config, f, indent=2, ensure_ascii=False)
print(f"\n✅ 配置已保存到: {config_file}")
# 4. 創建快捷命令(可選)
print("\n6. 創建快捷命令...")
# 創建 ccdebug 包裝腳本
wrapper_script = """#!/usr/bin/env python3
# CCDebugger Claude Code Wrapper
import sys
from claude_code_ccdebug import CCDebugCommand
if __name__ == "__main__":
command = " ".join(sys.argv)
ccdebug = CCDebugCommand()
result = ccdebug.execute(command)
print(result)
"""
bin_dir = Path.home() / '.local' / 'bin'
bin_dir.mkdir(exist_ok=True, parents=True)
ccdebug_script = bin_dir / 'ccdebug-claude'
with open(ccdebug_script, 'w') as f:
f.write(wrapper_script)
ccdebug_script.chmod(0o755)
# 5. 測試整合
print("\n7. 測試整合...")
test_code = """
from claude_code_integration import claude_code_hook
# 測試錯誤
test_output = '''
Traceback (most recent call last):
File "test.py", line 1, in <module>
undefined_function()
NameError: name 'undefined_function' is not defined
'''
result = claude_code_hook(test_output, "python test.py")
if result:
print("✅ 整合測試成功!")
print("\\n預覽:")
print(result[:200] + "...")
else:
print("❌ 整合測試失敗")
"""
try:
exec(test_code)
except Exception as e:
print(f"❌ 測試失敗: {e}")
# 6. 使用說明
print("\n" + "="*50)
print("🎉 設置完成!")
print("\n使用方法:")
print("1. 在 Claude Code 中正常執行命令")
print("2. 遇到錯誤時,CCDebugger 會自動分析")
print("3. 使用 'ccdebug --last' 分析最後的錯誤")
print("4. 使用 'ccdebug --help' 查看更多選項")
if default_config['monitorMode']:
print("\n監控模式已啟用,所有錯誤都會被自動分析。")
print(f"\n配置文件位置: {config_file}")
print("修改配置後會立即生效。")
# 7. 創建示例代碼
if input("\n是否創建示例代碼? (y/n): ").lower() == 'y':
example_file = Path.cwd() / 'claude_code_example.py'
example_code = '''#!/usr/bin/env python3
"""
CCDebugger Claude Code Integration Example
這是一個展示 CCDebugger 如何在 Claude Code 中工作的示例
"""
# 示例 1: 基本錯誤
def test_basic_error():
"""測試基本的 NameError"""
print("測試 1: NameError")
undefined_variable # 這會觸發 CCDebugger
# 示例 2: 類型錯誤
def test_type_error():
"""測試 TypeError"""
print("測試 2: TypeError")
"string" + 123 # 這會觸發 CCDebugger
# 示例 3: 屬性錯誤
def test_attribute_error():
"""測試 AttributeError"""
print("測試 3: AttributeError")
data = None
data.process() # 這會觸發 CCDebugger
# 示例 4: 索引錯誤
def test_index_error():
"""測試 IndexError"""
print("測試 4: IndexError")
items = [1, 2, 3]
print(items[10]) # 這會觸發 CCDebugger
if __name__ == "__main__":
print("CCDebugger Claude Code 整合示例")
print("-" * 40)
print("運行任何一個測試函數都會觸發 CCDebugger 自動分析")
print("例如: test_basic_error()")
print()
print("提示: CCDebugger 會自動偵測錯誤並提供解決方案!")
'''
with open(example_file, 'w') as f:
f.write(example_code)
print(f"\n✅ 示例代碼已創建: {example_file}")
print("在 Claude Code 中運行示例來體驗 CCDebugger!")
if __name__ == "__main__":
try:
setup_claude_code_integration()
except KeyboardInterrupt:
print("\n\n設置已取消。")
except Exception as e:
print(f"\n❌ 設置過程中出錯: {e}")
sys.exit(1)