-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.bat
More file actions
207 lines (170 loc) · 4.39 KB
/
init.bat
File metadata and controls
207 lines (170 loc) · 4.39 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
@echo off
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion
:: ============================================
:: Opencode 初始化配置脚本 (Windows)
:: ============================================
:: 配置文件路径
set "CONFIG_FILE=.opencode\opencode.json"
set "SKILLS_DIR=.opencode\skills"
:: 打印分隔线
:print_line
echo ========================================
goto :eof
:: 打印成功信息
:print_success
echo [√] %~1
goto :eof
:: 打印警告信息
:print_warning
echo [!] %~1
goto :eof
:: 打印错误信息
:print_error
echo [X] %~1
goto :eof
:: 打印信息
:print_info
echo %~1
goto :eof
:: 检查是否在项目根目录
:check_directory
if not exist "%CONFIG_FILE%" (
call :print_error "未找到配置文件: %CONFIG_FILE%"
call :print_error "请确保在项目根目录下运行此脚本"
exit /b 1
)
goto :eof
:: 步骤1: 更新 opencode
:upgrade_opencode
echo.
call :print_info "[步骤 1/4] 正在更新 opencode..."
echo 运行命令: opencode upgrade
opencode upgrade
if errorlevel 1 (
call :print_warning "更新失败,跳过此步骤继续执行"
) else (
call :print_success "更新完成"
)
goto :eof
:: 步骤2: 配置 API Key
:configure_api_key
echo.
call :print_info "[步骤 2/4] 配置 API Key"
echo 请输入 API Key 或环境变量名:
call :print_info "提示:"
echo - 直接输入 API Key: sk-xxxxxxxxxxxx
echo - 使用环境变量: %%ZP_KEY%% 或 ZP_KEY
echo.
set "user_input="
set /p "user_input=请输入: "
if "!user_input!"=="" (
call :print_error "输入不能为空"
exit /b 1
)
:: 检查是否以 % 开头(Windows 环境变量格式)
set "first_char=!user_input:~0,1!"
if "!first_char!"=="%%" (
:: 去掉 % 前缀,获取环境变量值
set "var_name=!user_input:~1!"
set "api_key=!%var_name%!"
if "!api_key!"=="" (
call :print_error "环境变量 !var_name! 不存在或为空"
exit /b 1
)
call :print_success "检测到环境变量: !var_name!"
set "API_KEY=!api_key!"
goto :eof
)
:: 检查输入是否是环境变量名(不带 %)
set "api_key=!%user_input%!"
if not "!api_key!"=="" (
call :print_success "检测到环境变量: !user_input!"
set "API_KEY=!api_key!"
goto :eof
)
:: 直接作为 API Key 使用
call :print_info "使用输入值作为 API Key"
set "API_KEY=!user_input!"
goto :eof
:: 步骤3: 替换配置文件
:replace_config
echo.
call :print_info "[步骤 3/4] 替换配置文件"
:: 创建临时文件
set "TEMP_FILE=%CONFIG_FILE%.tmp"
set "count=0"
:: 逐行读取并替换
(
for /f "usebackq delims=" %%a in ("%CONFIG_FILE%") do (
set "line=%%a"
set "new_line=!line:zp_api_key=%API_KEY%!"
if not "!line!"=="!new_line!" (
set /a "count+=1"
)
echo !new_line!
)
) > "%TEMP_FILE%"
:: 替换原文件
move /y "%TEMP_FILE%" "%CONFIG_FILE%" >nul
if !count! equ 0 (
call :print_warning "配置文件中未找到 zp_api_key"
) else (
call :print_success "已替换 %CONFIG_FILE% 中的 !count! 处 zp_api_key"
)
goto :eof
:: 步骤4: 精简 Skills
:cleanup_skills
echo.
call :print_info "[步骤 4/4] 精简 Skills"
if not exist "%SKILLS_DIR%" (
call :print_warning "skills 目录不存在: %SKILLS_DIR%"
goto :eof
)
set "confirm="
set /p "confirm=是否删除多余的 skills 目录?(y/n): "
if not "!confirm!"=="y" if not "!confirm!"=="Y" (
call :print_info "跳过精简 skills"
goto :eof
)
:: 获取所有子目录并删除
set "deleted_count=0"
set "kept_count=0"
for /d %%d in ("%SKILLS_DIR%\*") do (
set "dir_name=%%~nxd"
:: 检查是否在保留列表中
set "keep=false"
if "!dir_name!"=="opencode_guide" set "keep=true"
if "!dir_name!"=="skill-creator" set "keep=true"
if "!keep!"=="true" (
set /a "kept_count+=1"
) else (
rd /s /q "%%d"
set /a "deleted_count+=1"
)
)
call :print_success "保留: opencode_guide, skill-creator"
call :print_success "已删除 !deleted_count! 个目录"
goto :eof
:: 主函数
:main
call :print_line
echo Opencode 初始化配置脚本 (Windows)
call :print_line
:: 检查当前目录
call :check_directory
if errorlevel 1 exit /b 1
:: 执行各步骤
call :upgrade_opencode
call :configure_api_key
if errorlevel 1 exit /b 1
call :replace_config
call :cleanup_skills
:: 完成
echo.
call :print_line
call :print_success "配置完成!"
call :print_line
echo.
endlocal
pause