-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.cmd
268 lines (231 loc) · 7.55 KB
/
install.cmd
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
@echo off
setlocal enabledelayedexpansion
:: Install Codex on Windows
:: Variables
if defined VERSION (
:: Remove trailing spaces
for /l %%v in (1,1,100) do if "!VERSION:~-1!"==" " set VERSION=v!VERSION:~0,-1!
) else (
set VERSION=latest
)
if defined CIRDL (
for /l %%v in (1,1,100) do if "!CIRDL:~-1!"==" " set CIRDL=!CIRDL:~0,-1!
) else (
set CIRDL=false
)
if defined INSTALL_DIR (
for /l %%v in (1,1,100) do if "!INSTALL_DIR:~-1!"==" " set INSTALL_DIR=!INSTALL_DIR:~0,-1!
) else (
set "INSTALL_DIR=%LOCALAPPDATA%\Codex"
)
set CODEX_ARCHIVE_PREFIX=codex
set CIRDL_ARCHIVE_PREFIX=cirdl
set CODEX_BINARY_PREFIX=codex
set CIRDL_BINARY_PREFIX=cirdl
if defined WINDOWS_LIBS (
for /l %%v in (1,1,100) do if "!WINDOWS_LIBS:~-1!"==" " set WINDOWS_LIBS=!WINDOWS_LIBS:~0,-1!
) else (
set WINDOWS_LIBS=true
)
if defined BASE_URL (
for /l %%v in (1,1,100) do if "!BASE_URL:~-1!"==" " set BASE_URL=!BASE_URL:~0,-1!
) else (
set BASE_URL=https://github.com/codex-storage/nim-codex
)
set API_BASE_URL=https://api.github.com/repos/codex-storage/nim-codex
if defined TEMP_DIR (
for /l %%v in (1,1,100) do if "!TEMP_DIR:~-1!"==" " set TEMP_DIR=!TEMP_DIR:~0,-1!
) else (
set TEMP_DIR=.
)
:: Colors
for /f %%a in ('echo prompt $E^| cmd') do set "ESC=%%a"
:: Help
if "%1" == "help" (
echo %ESC%[93mUsage:%ESC%[%m
set SCRIPT_NAME=%~n0%~x0
set URL=https://get.codex.storage/!SCRIPT_NAME!
set "COMMAND=curl -sO !URL!"
echo !COMMAND! ^&^& !SCRIPT_NAME!
echo !COMMAND! ^&^& set VERSION=0.1.7 ^& set CIRDL=true ^& !SCRIPT_NAME!
echo !COMMAND! ^&^& set VERSION=0.1.7 ^& set WINDOWS_LIBS=false ^& !SCRIPT_NAME!
echo !COMMAND! ^&^& set VERSION=0.1.7 ^& set "INSTALL_DIR=C:\Program Files\Codex" ^& !SCRIPT_NAME!
echo.
echo %ESC%[93mVariables:%ESC%[%m
echo - VERSION=0.1.7 - codex and cird version to install
echo - CIRDL=true - install cirdl
echo - "INSTALL_DIR=C:\Program Files\Codex" - directory to install binaries
echo - WINDOWS_LIBS=false - download and install archive without the libs
echo - BASE_URL=http://localhost:8080 - custom base URL for binaries downloading
exit /b 0
)
goto :run
:: Show
:show_start
echo.
echo %~1
echo.
exit /b 0
:show_progress
echo - %~1
exit /b 0
:: Not used yet
:: :show_pass
:: echo - %~1
:: exit /b 0
:show_fail
echo.
echo %ESC%[91mError: %~2%ESC%[%m
echo.
exit /b 1
:show_end
echo.
echo %ESC%[92m%~1%ESC%[%m
echo.
exit /b 0
:: Run
:run
:: Start
call :show_start "Installing Codex..."
:: Version
set message="Computing version"
call :show_progress %message%
if "%VERSION%" == "latest" (
for /f delims^=^"^ tokens^=4 %%v in ('curl -Ls %API_BASE_URL%/releases/latest ^| find "tag_name"') do set VERSION=%%v
)
:: Archives and binaries
set message="Computing archives and binaries names"
call :show_progress %message%
::: Set variables
if "%CIRDL%" == "true" (
set "ARCHIVES=%CODEX_ARCHIVE_PREFIX% %CIRDL_ARCHIVE_PREFIX%"
set "BINARIES=%CODEX_BINARY_PREFIX% %CIRDL_BINARY_PREFIX%"
) else (
set ARCHIVES=%CODEX_ARCHIVE_PREFIX%
set BINARIES=%CODEX_BINARY_PREFIX%
)
:: Get the current OS
set message="Checking the current OS"
call :show_progress %message%
set OS=windows
for /f "tokens=4-5 delims=. " %%i in ('ver') do set OS_VER=%%i.%%j
for /f "skip=1 tokens=*" %%a in ('wmic os get caption ^| findstr /r /v "^$"') do set "OS_NAME=%%a"
::: Not supported
if not "%OS_VER%" == "10.0" (
call :show_fail %message% "Unsupported OS - %OS_NAME%"
goto :delete
)
:: Get the current architecture
set message="Checking the current architecture"
call :show_progress %message%
if "%PROCESSOR_ARCHITECTURE%" == "AMD64" (set ARCHITECTURE=amd64) else (set ARCHITECTURE=arm64)
::: Not supported
if not "%ARCHITECTURE%" == "amd64" (
call :show_fail %message% "Unsupported architecture - %PROCESSOR_ARCHITECTURE%"
goto :delete
)
:: Archive and binaries names
if "%WINDOWS_LIBS%" == "true" (
set ARCHIVE_SUFFIX=%VERSION%-%OS%-%ARCHITECTURE%-libs.zip
set BINARY_SUFFIX=%VERSION%-%OS%-%ARCHITECTURE%
) else (
set ARCHIVE_SUFFIX=%VERSION%-%OS%-%ARCHITECTURE%.zip
set BINARY_SUFFIX=%VERSION%-%OS%-%ARCHITECTURE%
)
:: Download
for %%f in (%ARCHIVES%) do (
set ARCHIVE=%%f
set ARCHIVE_NAME=!ARCHIVE!-%ARCHIVE_SUFFIX%
for %%f in (!ARCHIVE_NAME! !ARCHIVE_NAME!.sha256) do (
set ARCHIVE=%%f
echo %BASE_URL% | find /i "https://github.com/" >nul
if !errorlevel! equ 0 (
set DOWNLOAD_URL=%BASE_URL%/releases/download/%VERSION%/!ARCHIVE!
) else (
set DOWNLOAD_URL=%BASE_URL%/%VERSION%/!ARCHIVE!
)
set message="Downloading !ARCHIVE!"
call :show_progress !message!
@rem we can't rely on http_code - https://github.com/curl/curl/issues/13845
@rem for /f "delims=" %%s in ('curl --write-out %%{http_code} --connect-timeout 5 --retry 5 -sL !DOWNLOAD_URL! -o !TEMP_DIR!\!ARCHIVE!') do set http_code=%%s
curl --fail --connect-timeout 10 --retry 10 -sL !DOWNLOAD_URL! -o !TEMP_DIR!\!ARCHIVE!
if !errorlevel! neq 0 (
call :show_fail !message! "Failed to download !DOWNLOAD_URL!"
goto :delete
)
)
)
:: Checksum
for %%f in (%ARCHIVES%) do (
set ARCHIVE=%%f
set ARCHIVE_NAME=!ARCHIVE!-%ARCHIVE_SUFFIX%
set message="Verifying checksum for !ARCHIVE_NAME!"
call :show_progress !message!
for /f "delims=" %%f in ('certUtil -hashfile !ARCHIVE_NAME! SHA256 ^| find /v ":"') do set "ACTUAL_HASH=%%f"
for /f "tokens=1" %%f in (!ARCHIVE_NAME!.sha256) do set EXPECTED_HASH=%%f
if not "!ACTUAL_HASH!" == "!EXPECTED_HASH!" (
call :show_fail !message! "Checksum verification failed for !ARCHIVE_NAME!. Expected: !EXPECTED_HASH!, Got: !ACTUAL_HASH!"
goto :delete
)
)
:: Create directory
set message="Creating installation directory %INSTALL_DIR%"
call :show_progress !message!
if not exist %INSTALL_DIR% mkdir %INSTALL_DIR%
if not !errorlevel! == 0 (
call :show_fail !message! "Failed to create %INSTALL_DIR%"
goto :delete
)
:: Extract
for %%f in (%ARCHIVES%) do (
set ARCHIVE=%%f
set ARCHIVE_NAME=!ARCHIVE!-%ARCHIVE_SUFFIX%
set message="Extracting !ARCHIVE_NAME! to %INSTALL_DIR%"
call :show_progress !message!
tar -xf !ARCHIVE_NAME! -C %INSTALL_DIR%
if not !errorlevel! == 0 (
call :show_fail !message! "Failed to extract !ARCHIVE_NAME!"
goto :delete
)
)
:: Rename
for %%f in (%BINARIES%) do (
set BINARY=%%f
set BINARY_NAME=!BINARY!-%BINARY_SUFFIX%
set message="Renaming !BINARY_NAME!.exe to !BINARY!.exe"
call :show_progress !message!
move /Y "%INSTALL_DIR%\!BINARY_NAME!.exe" "%INSTALL_DIR%\!BINARY!.exe" >nul
if not !errorlevel! == 0 (
call :show_fail !message! "Failed to rename %INSTALL_DIR%\!BINARY_NAME!.exe to %INSTALL_DIR%\!BINARY!.exe"
)
)
:: Cleanup
set message="Cleanup"
call :show_progress %message%
for %%f in (%ARCHIVES%) do (
set ARCHIVE=%%f
set ARCHIVE_NAME=!ARCHIVE!-!ARCHIVE_SUFFIX!
del /Q %TEMP_DIR%\!ARCHIVE_NAME!*
if !errorlevel! neq 0 (
call :show_fail !message! "Failed to delete %TEMP_DIR%\!ARCHIVE_NAME!*"
goto :delete
)
)
:: End
:end
call :show_end "Setup completed successfully!"
:: Set PATH
echo %PATH% | find /i "%INSTALL_DIR%" >nul
if not %errorlevel% equ 0 (
echo %ESC%[93m Update current session PATH:%ESC%[%m
echo set "PATH=%%PATH%%%INSTALL_DIR%;"
echo.
echo %ESC%[93m Update PATH permanently and add %INSTALL_DIR%:%ESC%[%m
echo - Control Panel -- System -- Advanced System settings -- Environment Variables
echo - Alternatively, type 'environment variables' into the Windows Search box
@rem we can't rely on setx - https://ss64.com/nt/path.html
@rem setx PATH "%PATH%%INSTALL_DIR;" >nul 2>&1
)
:: Self delete
:delete
goto 2>nul & del "%~f0"