-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.bat
More file actions
202 lines (177 loc) · 6.8 KB
/
Copy pathrun.bat
File metadata and controls
202 lines (177 loc) · 6.8 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
@echo off
setlocal enabledelayedexpansion
REM =========================================================
REM Base directory
REM =========================================================
set "RootDir=%~dp0"
if "%RootDir:~-1%"=="\" set "RootDir=%RootDir:~0,-1%"
REM =========================================================
REM Defaults
REM =========================================================
set "APP_NAME=Shiny App"
set "APP_ID=ShinyApp"
set "LTC_PORT=3402"
set "LTC_HOST=127.0.0.1"
REM =========================================================
REM Read metadata from app_meta.cfg if present
REM =========================================================
if exist "%RootDir%\app_meta.cfg" (
for /f "usebackq tokens=1,* delims==" %%A in ("%RootDir%\app_meta.cfg") do (
if /I "%%A"=="APP_NAME" set "APP_NAME=%%B"
if /I "%%A"=="APP_ID" set "APP_ID=%%B"
if /I "%%A"=="PREFERRED_PORT" set "LTC_PORT=%%B"
if /I "%%A"=="HOST" set "LTC_HOST=%%B"
)
)
set "APP_ID_SAFE=%APP_ID: =_%"
REM =========================================================
REM Bundled Pandoc
REM =========================================================
set "PANDOC_DIR=%RootDir%\pandoc"
if exist "%PANDOC_DIR%\pandoc.exe" (
set "PATH=%PANDOC_DIR%;%PATH%"
set "RSTUDIO_PANDOC=%PANDOC_DIR%"
)
REM =========================================================
REM App folder
REM =========================================================
set "AppDir=%RootDir%\app"
if not exist "%AppDir%" (
echo ERROR: App folder not found at "%AppDir%".
pause
exit /b 1
)
REM =========================================================
REM Logging: local logs first, fallback to TEMP
REM =========================================================
set "LogDir=%RootDir%\logs"
if not exist "%LogDir%" mkdir "%LogDir%" >nul 2>&1
set "LogTest=%LogDir%\__write_test.tmp"
break > "%LogTest%" 2>nul
if exist "%LogTest%" (
del "%LogTest%" >nul 2>&1
) else (
set "LogDir=%TEMP%\%APP_ID_SAFE%_logs"
if not exist "%LogDir%" mkdir "%LogDir%" >nul 2>&1
)
for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "stamp=%%I"
set "LOG=%LogDir%\app_%stamp%.log"
REM =========================================================
REM Delete logs older than 7 days
REM =========================================================
powershell -NoProfile -Command ^
"Get-ChildItem -Path '%LogDir%' -Filter '*.log' -ErrorAction SilentlyContinue | " ^
"Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | " ^
"Remove-Item -Force -ErrorAction SilentlyContinue" >nul 2>&1
(
echo [START] %date% %time%
echo APP_NAME="%APP_NAME%"
echo APP_ID="%APP_ID%"
echo RootDir="%RootDir%"
echo AppDir="%AppDir%"
echo LogDir="%LogDir%"
echo LTC_HOST="%LTC_HOST%"
echo LTC_PORT="%LTC_PORT%"
) > "%LOG%" 2>&1
if not exist "%LOG%" (
echo [FATAL] Could not create log file.
pause
exit /b 1
)
REM =========================================================
REM R detection
REM =========================================================
set "RPathFound=false"
set "RS="
set "R="
for %%P in (
"%RootDir%\R\bin\Rscript.exe"
"%RootDir%\R\bin\x64\Rscript.exe"
"%RootDir%\R-Portable\bin\Rscript.exe"
"%RootDir%\R-Portable\bin\x64\Rscript.exe"
"%RootDir%\R-Portable\App\R-Portable\bin\Rscript.exe"
"%RootDir%\R-Portable\App\R-Portable\bin\x64\Rscript.exe"
) do (
if exist "%%~fP" (
set "RS=%%~fP"
set "RPathFound=true"
goto :prep
)
)
if exist "%RootDir%\R\bin\x64\R.exe" (
set "R=%RootDir%\R\bin\x64\R.exe"
set "RPathFound=true"
goto :prep
)
for %%d in ("C:\Program Files\R" "C:\Program Files (x86)\R") do (
for /d %%i in (%%d\R-*) do (
if exist "%%i\bin\Rscript.exe" (
set "RS=%%i\bin\Rscript.exe"
set "RPathFound=true"
goto :prep
) else (
if exist "%%i\bin\x64\R.exe" (
set "R=%%i\bin\x64\R.exe"
set "RPathFound=true"
goto :prep
)
)
)
)
if not "%RPathFound%"=="true" (
echo R was not detected automatically. >> "%LOG%"
set /p "R=Enter full path to R.exe: "
if not exist "%R%" (
echo The specified path does not exist.
echo [ERROR] Invalid R path provided: "%R%" >> "%LOG%"
pause
exit /b 1
)
)
:prep
set "APP_NAME=%APP_NAME%"
set "APP_ID=%APP_ID%"
set "LTC_PORT=%LTC_PORT%"
set "LTC_HOST=%LTC_HOST%"
REM =========================================================
REM Startup splash page
REM =========================================================
set "SPLASH_TEMPLATE=%RootDir%\build\splash.html"
set "SPLASH_TMP=%TEMP%\sb_splash_%APP_ID_SAFE%.html"
set "SB_LAUNCH_URL=http://sharebridge-%APP_ID_SAFE%.localhost:%LTC_PORT%"
if exist "%SPLASH_TEMPLATE%" (
set "SB_SPLASH_TEMPLATE=%SPLASH_TEMPLATE%"
set "SB_SPLASH_TMP=%SPLASH_TMP%"
set "SB_APP_NAME=%APP_NAME%"
powershell -NoProfile -Command ^
"(Get-Content -LiteralPath $env:SB_SPLASH_TEMPLATE -Raw) -replace '__LAUNCH_URL__', $env:SB_LAUNCH_URL -replace '__APP_NAME__', $env:SB_APP_NAME | Set-Content -LiteralPath $env:SB_SPLASH_TMP -Encoding UTF8"
if exist "%SPLASH_TMP%" start "" "%SPLASH_TMP%"
)
REM =========================================================
REM Run app
REM =========================================================
if defined RS (
echo Launching with Rscript... >> "%LOG%"
"%RS%" --vanilla "%RootDir%\run.R" "%AppDir%" >> "%LOG%" 2>&1
) else (
echo Launching with R.exe... >> "%LOG%"
"%R%" --no-save --slave -f "%RootDir%\run.R" --args "%AppDir%" >> "%LOG%" 2>&1
)
set "RC=%errorlevel%"
echo [END] %date% %time% (exitcode=%RC%) >> "%LOG%"
REM =========================================================
REM Crash landing page
REM =========================================================
if not "%RC%"=="0" (
set "SB_CRASH_LOG=%LOG%"
set "SB_CRASH_APP=%APP_NAME%"
set "SB_CRASH_OUT=%TEMP%\sb_crash_%APP_ID_SAFE%.html"
powershell -NoProfile -Command ^
"$n = $env:SB_CRASH_APP; $lf = $env:SB_CRASH_LOG; $out = $env:SB_CRASH_OUT;" ^
"$lines = if (Test-Path $lf) { (Get-Content $lf -Tail 35) -join \"`n\" } else { 'Log not available.' };" ^
"$lines = $lines -replace '&','&' -replace '<','<' -replace '>','>';" ^
"$h = '<!DOCTYPE html><html><head><meta charset=UTF-8><title>' + $n + ' - Error</title><style>body{font-family:Segoe UI,sans-serif;max-width:720px;margin:40px auto;padding:24px;background:#f8fafc;color:#1e293b}h2{color:#dc2626;margin-bottom:8px}.log{background:#0f172a;color:#e2e8f0;padding:20px;border-radius:10px;font:12px/1.6 Consolas,monospace;overflow:auto;white-space:pre-wrap;margin:12px 0}.meta{font-size:13px;color:#64748b;margin-top:12px}</style></head><body><h2>' + $n + ' did not start</h2><p>The app exited with an error. Check the last log lines below:</p><div class=log>' + $lines + '</div><p class=meta>Full log: ' + $lf + '</p></body></html>';" ^
"[System.IO.File]::WriteAllText($out, $h, [System.Text.Encoding]::UTF8);" ^
"Start-Process $out"
)
exit /b %RC%