-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_pace_ui.bat
301 lines (242 loc) · 8.2 KB
/
start_pace_ui.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
cls
setlocal
@echo off
SET VERSION=2.0.0
REM Determine if this a git repository
IF EXIST ".git" (
ECHO You are in a Git repository, please follow ReadMe Instruction to run app in Rstudio
ECHO Press any key to terminate...
PAUSE > nul
exit
)
REM Determine if this is offline mode
SET "OFFLINE_FOLDERS=R-4.4.0 R config"
SET offline=TRUE
FOR %%F IN (%OFFLINE_FOLDERS%) DO (
IF NOT EXIST "%%F\" (
SET offline=FALSE
)
)
IF %offline%==TRUE (
ECHO All folders exist. No instllation needed.
SET R_PATH=.\R-4.4.0\bin\Rscript.exe
SET SHINY_DIR=.
GOTO app
) ELSE (
ECHO You cannot run it with offline mode.
GOTO prompt
)
:prompt
SET UPGRADE=FALSE
ECHO Online installation is needed, please specify installation folder:
SET root_dir=
SET /P root_dir="App Directory: "
REM Remove surrounding quotes from root_dir
SET "root_dir=%root_dir:"=%"
ECHO "You have entered: %root_dir%"
SETLOCAL ENABLEDELAYEDEXPANSION
IF NOT "%root_dir%"=="" (
SET "APP_DIR=!root_dir!\PACE-HRH-UI"
ECHO "Setting app in !APP_DIR!"
)
SETLOCAL DISABLEDELAYEDEXPANSION
IF "%root_dir%"=="" (
ECHO You must enter a root directory path.
GOTO prompt
) ELSE IF NOT EXIST "%root_dir%" (
MKDIR "%root_dir%"
GOTO continue
) ELSE IF EXIST "%root_dir%\PACE-HRH-UI" (
GOTO askUpgrade
) ELSE (
GOTO continue
)
:askUpgrade
ECHO.
ECHO Do you want to overwrite and update code in this folder?
ECHO Type Y to update and continue, or N to skip.
ECHO If you want to update the code, please make sure to save 'pace_results' directory in %APP_DIR% in case of failure
ECHO.
SET /p confirmation=Your choice:
IF /i "%confirmation%"=="Y" (
ECHO "Start cleaning files..."
SET UPGRADE=TRUE
GOTO deleteFiles
) ELSE IF /i "%confirmation%"=="N" (
GOTO prompt
) ELSE (
echo "Invalid input. Please enter Y or N."
GOTO askUpgrade
)
:continue
REM Download PACE-HRH-UI Release
REM Prompt the user for the version number
set /p "VERSION_INPUT=Enter PACE-HRH-UI Release version number (default is %VERSION%): "
REM Check if user has provided a version number as input
if not "%VERSION_INPUT%"=="" (
set "VERSION=%VERSION_INPUT%"
)
SET CODE_URL=https://github.com/InstituteforDiseaseModeling/PACE-HRH-UI/archive/refs/tags/%VERSION%.zip
SET DOWNLOAD_PATH="%root_dir%\pace-hrh-ui.zip"
IF NOT EXIST "%APP_DIR%" (
ECHO "App will be created in: %APP_DIR%"
MKDIR "%APP_DIR%"
)
ECHO "Setup will download and extract source code to %APP_DIR%"
IF NOT EXIST %DOWNLOAD_PATH% (
curl -L -o %DOWNLOAD_PATH% "%CODE_URL%"
)
IF EXIST %DOWNLOAD_PATH% (
tar -xf %DOWNLOAD_PATH% -C "%APP_DIR%"
) ELSE (
ECHO Source code Download failed
ECHO Please make sure the release version exists and try again!
PAUSE > nul
EXIT /b 1
)
IF %ERRORLEVEL% NEQ 0 (
ECHO Error: Failed to extract %DOWNLOAD_PATH%
ECHO Please make sure the release version exists and try again!
PAUSE > nul
DEL /F %DOWNLOAD_PATH%
EXIT /b 1
)
REM SET the working directory
PUSHD "%APP_DIR%"
REM Change to the first directory found (extracted from zip)
for /d %%G in (*) do (
SET WORKING_DIR=%%G
ECHO Working directory in "%%G"
GOTO :breakLoop
)
:breakLoop
:restore_results
if /i "%UPGRADE%"=="TRUE" (
ECHO Restore pace_results
IF EXIST "%TEMP%\tmp_pace_hrh_ui\pace_results" (
MOVE "%TEMP%\tmp_pace_hrh_ui\pace_results" "%WORKING_DIR%" > NUL
)
)
:license
ECHO Please read the following license terms:
TYPE "%WORKING_DIR%\LICENSE"
ECHO.
ECHO Do you accept the license terms?
ECHO Type "accept" to accept and continue, or "cancel" to exit.
ECHO.
:askChoice
SET /p userChoice=Your choice (accept/cancel):
REM Convert the input to lowercase for comparison
IF /i "%userChoice%"=="accept" GOTO accepted
IF /i "%userChoice%"=="cancel" GOTO cancelled
ECHO Invalid choice. Please type "accept" to accept the terms or "cancel" to decline.
GOTO askChoice
:accepted
ECHO You have accepted the terms.
POPD
SET R_PATH="%APP_DIR%\%WORKING_DIR%\R-4.4.0\bin\Rscript.exe"
SET SHINY_DIR="%APP_DIR%\%WORKING_DIR%"
ECHO %R_PATH%
ECHO "%SHINY_DIR%"
REM Check if R is installed
SET DOWNLOAD_R_PATH="%root_dir%\R-4.4.0-win.exe"
ECHO %DOWNLOAD_R_PATH%
IF EXIST "%R_PATH%" (
ECHO R is already installed.
) ELSE (
IF NOT EXIST %DOWNLOAD_R_PATH% (
ECHO Downloading R 4.4.0...
REM Replace the URL with the direct download link to the R installer you want to use
curl -o %DOWNLOAD_R_PATH% https://cran.r-project.org/bin/windows/base/old/4.4.0/R-4.4.0-win.exe
)
ECHO Installing R...
REM Run the installer silently
%DOWNLOAD_R_PATH% /VERYSILENT /NORESTART /DIR="%APP_DIR%\%WORKING_DIR%\R-4.4.0"
ECHO R 4.4.0 installation complete.
REM Install pandoc
SET DOWNLOAD_PANDOC_PATH=pandoc-3.2-windows-x86_64.msi
ECHO Downloading Pandoc...
curl -o %DOWNLOAD_PANDOC_PATH% https://github.com/jgm/pandoc/releases/download/3.2/pandoc-3.2-windows-x86_64.msi
ECHO Installing Pandoc
CMD /c "msiexec /i %DOWNLOAD_PANDOC_PATH% /quiet /norestart"
DEL %DOWNLOAD_PANDOC_PATH%
ECHO Install packages...
REM Run the R script in silent mode
%R_PATH% --verbose --vanilla "%APP_DIR%\%WORKING_DIR%\install_packages.R"
)
REM Install a woking version
curl -L -o pacehrh_1.1.0.zip https://github.com/InstituteforDiseaseModeling/PACE-HRH/releases/download/1.1.0/pacehrh_1.1.0.zip
%R_PATH% -e "if (!require('pacehrh', character.only=TRUE)) install.packages('pacehrh_1.1.0.zip', repos = NULL, type = 'source');library(pacehrh)"
REM remove installation files
DEL %DOWNLOAD_R_PATH%
DEL %DOWNLOAD_PATH%
REM Ask User to start again with Offline mode
IF %offline%==FALSE (
ECHO Please go to %SHINY_DIR% and run the start_pace_ui.bat again
ECHO Press any key to terminate ...
PAUSE > nul
exit
)
:app
REM Start The shinyapps in port 8888 and open the browser
SET PORT=8888
ECHO Starting Shiny app on port %PORT%...
ECHO Using %R_PATH% ON %SHINY_DIR%
START /MIN "" %R_PATH% -e "Sys.setenv(PATH = paste(Sys.getenv('PATH'), ';', Sys.getenv('LocalAppData'), '\\Pandoc', sep='')); shiny::runApp(appDir='%SHINY_DIR%', port=%PORT%, launch.browser=TRUE)"
ECHO Your default browser will be opened for PACE-HRH-UI in a few seconds ...
GOTO end
:cancelled
ECHO You have cancelled the operation. Goodbye!
ECHO Press any key to terminate ...
PAUSE > nul
exit
:end
REM Prompt the user to close the Shiny app
ECHO Press any key to terminate the PACE-HRH app...
PAUSE > nul
REM Find and terminate the Rscript process that runs the Shiny app
ECHO Terminating the Shiny app...
TASKKILL /FI "IMAGENAME eq %R_PATH%" /f 2>nul
ECHO PACE-HRH app terminated.
exit
:deleteFiles
REM Delete old files and move 'pace_results' directory
ECHO Deleting directories in "%APP_DIR%" except 'pace_results'
ECHO Searching for 'pace_results' directories under "%APP_DIR%"
REM Use /r for recursive directory traversal
FOR /d /r "%APP_DIR%" %%i in (*) do (
REM Check if the current directory is named 'pace_results'
if "%%~nxi"=="pace_results" (
ECHO 'pace_results' found at: "%%i"
RD /Q /S "%TEMP%\tmp_pace_hrh_ui" 2>nul
MD "%TEMP%\tmp_pace_hrh_ui" 2>NUL
MOVE /Y "%%i" "%TEMP%\tmp_pace_hrh_ui" || (
ECHO Failed to move 'pace_results' to %TEMP%\tmp_pace_hrh_ui\%RELATIVE_PATH%
ECHO Please close your application and try again.
ECHO If there is a permission issue, please contact your IT administrator.
ECHO Alternatively, manually save "%%i" and restore files after upgrade.
PAUSE > nul
exit /b 1
)
)
)
FOR /d %%i in ("%APP_DIR%\*") do (
REM Check if the directory name is 'pace_results'
ECHO Deleting "%%i"
rd /s /q "%%i" || (
ECHO There was an error deleting the folder "%%i".
ECHO If there is a permission issue, please contact your IT administrator.
ECHO If the problem is not resolved, you will need to delete "%APP_DIR%" manually.
ECHO Manually deleting "%APP_DIR%" will result in loss of your previous results.
PAUSE > nul
exit /b 1
)
)
REM Check the exit code of delete section to be sure
IF %ERRORLEVEL% NEQ 0 (
ECHO Cleanup failed to delete existing files
ECHO Please rerun the script, if the problem exists, you will need to delete "%APP_DIR%" manually.
PAUSE > nul
exit /b 1
)
GOTO continue