-
Notifications
You must be signed in to change notification settings - Fork 142
/
rime-install.bat
433 lines (378 loc) · 10.3 KB
/
rime-install.bat
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
@echo off
setlocal enabledelayedexpansion
title Rime package installer
set root_dir=%~dp0
set PATH=%root_dir%;%PATH%
set config_file=%~dp0\rime-install-config.bat
if exist "%config_file%" call "%config_file%"
if not defined rime_dir set rime_dir=%APPDATA%\Rime
if not defined download_cache_dir set download_cache_dir=%TEMP%
if not exist "%download_cache_dir%" mkdir "%download_cache_dir%"
echo.
echo Rime package installer
echo.
echo Working directory: %CD%
echo Package installer directory: %root_dir%
echo Download cache directory: %download_cache_dir%
echo Rime user directory: %rime_dir%
echo.
if defined ProgramFiles(x86) (set arch=64) else (set arch=32)
call :find_7z
call :find_git_bash
call :find_downloader
if not defined use_plum if "%has_git_bash%" == "1" set use_plum=1
:process_arguments
if "%1" == "" set batch_interactive=1
if "%1" == "--select" if "%use_plum" == "1" (
call :install_with_plum %*
exit /b !errorlevel!
)
set /a installed_packages=0
:next
if "%batch_interactive%" == "1" (
set package=
echo. && (set /p package=Enter package name, URL, user/repo or downloaded ZIP to install: )
) else (
set package=%1
shift
)
if "%package%" == "" goto finish
call :install_package
if errorlevel 1 exit /b %errorlevel%
goto next
:install_package
if "%package%" == "7z" (
call :install_7z
exit /b %errorlevel%
) else if "%package%" == "git" (
call :install_git
exit /b %errorlevel%
) else if "%package%" == "plum" (
call :install_with_plum plum
exit /b %errorlevel%
) else if "%package:.zip=%.zip" == "%package%" (
if "https://github.com/%package:https://github.com/=%" == "%package%" (
set user_repo_path=%package:https://github.com/=%
set archive_name=%package:*/archive/=%
for /f "tokens=1 usebackq delims=." %%g in ('!archive_name!') do set branch=%%g
call set package_repo=%%user_repo_path:/archive/!archive_name!=%%
call :download_package
) else (
set package_file=%package%
call :install_zip_package
)
goto :after_install_package
)
:prefer_plum_installer
if "%use_plum%" == "1" (
call :install_with_plum %package%
goto after_install_package
)
:fallback_to_builtin_installer
set branch=
if "https://github.com/%package:https://github.com/=%" == "%package%" (
set user_repo_path=%package:https://github.com/=%
if not "%package:/tree/=%" == "%package%" (
set branch=%package:*/tree/=%
)
if defined branch (
call set package_repo=%%user_repo_path:/tree/!branch!=%%
) else (
set package_repo=!user_repo_path!
)
call :download_package
) else if "%package:-packages.bat=%-packages.bat" == "%package%" (
call "%package%"
call :install_package_group
) else if ":%package::=%" == "%package%" (
call "%package::=%-packages.bat"
call :install_package_group
) else if not "%package:/=%" == "%package%" (
for /f "tokens=1,2 usebackq delims=@" %%g in ('%package%') do (
set package_repo=%%g
set branch=%%h
)
call :download_package
) else (
for /f "tokens=1,2 usebackq delims=@" %%g in ('%package%') do (
set user_repo_path=%%g
set branch=%%h
)
set package_repo=rime/rime-!user_repo_path:rime-=!
call :download_package
)
:after_install_package
if not errorlevel 1 set /a installed_packages+=1
exit /b %errorlevel%
:download_package
if not defined downloader (
set error_message=Downloader not found.
goto error
)
call :install_7z /needed
if errorlevel 1 exit /b %errorlevel%
if not defined branch (
for /f "tokens=2 usebackq delims=:, " %%g in (`
%downloader% https://api.github.com/repos/%package_repo% ^| findstr default_branch
`) do set branch=%%~g
)
set package_url=https://github.com/%package_repo%/archive/%branch%.zip
echo.
echo Downloading %package_url% ...
echo.
set package_file=%download_cache_dir%\%package_repo:*/=%-%branch%.zip
if "%no_update%" == "1" if exist "%package_file%" goto skip_download_package
%downloader% "%package_url%" %save_to% "%package_file%"
if errorlevel 1 (
set error_message=Error downloading %package_url%
goto error
)
:skip_download_package
call :install_zip_package
exit /b %errorlevel%
:install_zip_package
call :install_7z /needed
if errorlevel 1 exit /b %errorlevel%
echo.
echo Unpacking %package_file% ...
echo.
for %%f in (%package_file%) do set package_dir=%%~nf
set unpack_package_dir=%TEMP%\%package_dir%
rem clean up obsolete files in target directory
if exist "%unpack_package_dir%" rmdir /s /q "%unpack_package_dir%"
rem unzip package
7z x "%package_file%" -o"%TEMP%" -y
if errorlevel 1 (
set error_message=Error unpacking package %package_file%
goto error
)
if not exist "%rime_dir%" (
mkdir "%rime_dir%"
if errorlevel 1 (
set error_message=Error creating rime user directory: %rime_dir%
goto error
)
)
rem install files from the unzipped package
pushd "%unpack_package_dir%"
for %%f in (
*.yaml
*.txt
opencc\*.json
opencc\*.ocd
opencc\*.txt
) do (
echo.
echo Installing %%f ...
echo.
set target_file=%rime_dir%\%%f
for %%t in (!target_file!) do set target_dir=%%~dpt
if not exist "!target_dir!" mkdir "!target_dir!"
copy /y "%%f" "!target_file!"
if errorlevel 1 (
popd
set error_message=Error installing files from package %package%
goto error
)
)
popd
exit /b
:install_package_group
if not defined package_list (
set error_message=package_list is undefined in %package%
goto error
)
for %%p in (%package_list%) do (
set package=%%p
call :install_package
if errorlevel 1 exit /b !errorlevel!
)
exit /b
:install_with_plum
call :install_git /needed
if errorlevel 1 exit /b %errorlevel%
set WSLENV=plum_dir:rime_dir
if defined plum_dir if exist "%plum_dir%"/rime-install (
bash "%plum_dir%"/rime-install %*
exit /b !errorlevel!
)
if exist plum/rime-install (
bash plum/rime-install %*
) else if exist rime-install (
bash rime-install %*
) else (
echo Downloading rime-install ...
set script_url=https://raw.githubusercontent.com/rime/plum/master/rime-install
curl -fsSL "!script_url!" -o "%download_cache_dir%"/rime-install
if errorlevel 1 (
set error_message=Error downloading rime-install
goto error
)
bash "%download_cache_dir%"/rime-install %*
)
exit /b %errorlevel%
:install_7z
where /q 7z
if not errorlevel 1 (
if "%1" == "/needed" exit /b
echo.
echo Found 7z
echo.
exit /b
)
rem check for updates at https://www.7-zip.org/download.html
if not defined _7z_version set _7z_version=18.01
if "%arch%" == "64" (set _7z_arch=-x%arch%) else (set _7z_arch=)
set _7z_installer=7z%_7z_version:.=%%_7z_arch%.exe
rem find local 7z installer
where /q %_7z_installer%
if not errorlevel 1 (
echo.
echo Found installer: %_7z_installer%
echo.
set _7z_installer_path=%_7z_installer%
goto run_7z_installer
)
set _7z_installer_path=%download_cache_dir%\%_7z_installer%
if "%no_update%" == "1" if exist "%_7z_installer_path%" goto run_7z_installer
:download_7z_installer
set _7z_download_url=https://www.7-zip.org/a/%_7z_installer%
if not defined downloader (
echo.
echo TODO: please download and install 7z: %_7z_download_url%
echo.
set error_message=Downloader not found.
goto error
)
echo.
echo Downloading installer: %_7z_installer%
echo.
%downloader% "%_7z_download_url%" %save_to% "%_7z_installer_path%"
if errorlevel 1 (
set error_message=Error downloading %_7z_installer%
goto error
)
rem TODO: verify installer
echo.
echo Download complete: %_7z_installer%
echo.
:run_7z_installer
echo.
echo Installing 7z ...
echo.
"%_7z_installer_path%" /S
exit /b
:install_git
where /q git
if not errorlevel 1 (
if "%1" == "/needed" exit /b
echo.
echo Found git
echo.
exit /b
)
rem check for updates at https://github.com/git-for-windows/git/releases/latest
if not defined git_version set git_version=2.17.0
if not defined git_release set git_release=.1
set git_installer=Git-%git_version%%git_release:.1=%-%arch%-bit.exe
rem find local Git installer
where /q %git_installer%
if not errorlevel 1 (
echo.
echo Found installer: %git_installer%
echo.
set git_installer_path=%git_installer%
goto run_git_installer
)
set git_installer_path=%download_cache_dir%\%git_installer%
if "%no_update%" == "1" if exist "%git_installer_path%" goto run_git_installer
:download_git_installer
set git_download_url_prefix=https://github.com/git-for-windows/git/releases/download/
set git_download_url=%git_download_url_prefix%v%git_version%.windows%git_release%/%git_installer%
if not defined downloader (
echo.
echo TODO: please download and install git: %git_download_url%
echo.
set error_message=Downloader not found.
goto error
)
echo.
echo Downloading installer: %git_installer%
echo.
%downloader% "%git_download_url%" %save_to% "%git_installer_path%"
if errorlevel 1 (
set error_message=Error downloading %git_installer%
goto error
)
rem TODO: verify installer
echo.
echo Download complete: %git_installer%
echo.
:run_git_installer
echo.
echo Installing git ...
echo.
"%git_installer_path%" /SILENT
exit /b
:find_7z
set search_path=^
%ProgramFiles%\7-Zip;
if defined ProgramW6432 set search_path=%search_path%^
%ProgramW6432%\7-Zip;
if defined ProgramFiles(x86) set search_path=%search_path%^
%ProgramFiles(x86)%\7-Zip;
set PATH=%search_path%%PATH%
where /q 7z
if %errorlevel% equ 0 set has_7z=1
exit /b
:find_git_bash
set search_path=^
%ProgramFiles%\Git\cmd;^
%ProgramFiles%\Git\mingw%arch%\bin;^
%ProgramFiles%\Git\usr\bin;
rem find 64-bit Git in 32-bit cmd.exe
if defined ProgramW6432 set search_path=%search_path%^
%ProgramW6432%\Git\cmd;^
%ProgramW6432%\Git\mingw%arch%\bin;^
%ProgramW6432%\Git\usr\bin;
rem find user installed 32-bit Git on 64-bit OS
if defined ProgramFiles(x86) set search_path=%search_path%^
%ProgramFiles(x86)%\Git\cmd;^
%ProgramFiles(x86)%\Git\mingw32\bin;^
%ProgramFiles(x86)%\Git\usr\bin;
set PATH=%search_path%%PATH%
where /q git
if %errorlevel% equ 0 set has_git=1
where /q bash
if %errorlevel% equ 0 set has_bash=1
if "%has_git%" == "1" if "%has_bash%" == "1" set has_git_bash=1
exit /b
:find_downloader
where /q curl
if %errorlevel% equ 0 (
set downloader=curl -fsSL
set save_to=-o
goto downloader_found
)
where /q powershell
if %errorlevel% equ 0 (
set downloader=powershell Invoke-WebRequest
set save_to=-OutFile
goto downloader_found
)
:downloader_found
exit /b
:error
echo.
echo Installation failed: %error_message%
echo.
exit /b 1
:finish
echo.
if %installed_packages% equ 0 (
echo No package installed.
) else (
echo Installed %installed_packages% packages.
)
echo.
:exit