-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathofd.bat
More file actions
428 lines (383 loc) · 11.5 KB
/
ofd.bat
File metadata and controls
428 lines (383 loc) · 11.5 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
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
@echo off
setlocal enabledelayedexpansion
:: OFD - Open Filament Database CLI Wrapper
:: Cross-platform setup and execution script for Windows
::
:: Usage: ofd <command> [options]
:: Configuration
set "SCRIPT_DIR=%~dp0"
set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"
set "VENV_DIR=%SCRIPT_DIR%\.venv"
set "WEBUI_DIR=%SCRIPT_DIR%\webui"
set "REQUIREMENTS_FILE=%SCRIPT_DIR%\requirements.txt"
set "SETUP_MARKER=%VENV_DIR%\.ofd_setup_complete"
set "WEBUI_SETUP_MARKER=%WEBUI_DIR%\node_modules\.ofd_webui_setup"
set "DOCS_URL=https://github.com/OpenFilamentCollective/open-filament-database/blob/main/docs/installing-software.md"
:: Parse first argument
set "FIRST_ARG=%~1"
set "NEEDS_WEBUI=0"
if "%FIRST_ARG%"=="setup" goto :run_setup
if "%FIRST_ARG%"=="--no-setup" (
set "SKIP_SETUP=1"
shift
goto :parse_args
)
if "%FIRST_ARG%"=="-h" goto :show_help
if "%FIRST_ARG%"=="--help" goto :show_help
if "%FIRST_ARG%"=="" goto :default_webui
:parse_args
:: Collect all arguments and check for webui
set "ARGS="
:collect_args
if "%~1"=="" goto :done_args
if "%~1"=="webui" set "NEEDS_WEBUI=1"
set "ARGS=%ARGS% %1"
shift
goto :collect_args
:done_args
:: Check if Python setup is needed
if not defined SKIP_SETUP (
if not exist "%SETUP_MARKER%" (
echo [INFO] First run detected. Setting up Python environment...
call :run_python_setup
if errorlevel 1 exit /b 1
) else (
call :deps_need_update
if not errorlevel 1 (
echo [INFO] Dependencies have changed. Updating...
call "%VENV_DIR%\Scripts\activate.bat"
pip install -q --upgrade pip
if errorlevel 1 (
echo [ERROR] Failed to upgrade pip
exit /b 1
)
pip install -q -r "%REQUIREMENTS_FILE%"
if errorlevel 1 (
echo [ERROR] Failed to install requirements
exit /b 1
)
pip install -q -e "%SCRIPT_DIR%"
if errorlevel 1 (
echo [ERROR] Failed to install package
exit /b 1
)
echo. > "%SETUP_MARKER%"
echo [OK] Dependencies updated
)
)
)
:: Check if WebUI setup is needed
if "%NEEDS_WEBUI%"=="1" (
if not exist "%WEBUI_SETUP_MARKER%" (
echo [INFO] WebUI first run. Setting up Node.js dependencies...
call :setup_webui
if errorlevel 1 exit /b 1
)
)
:: Activate venv and run
if exist "%VENV_DIR%\Scripts\activate.bat" (
call "%VENV_DIR%\Scripts\activate.bat"
set "PYTHON_CMD=python"
) else (
echo [ERROR] Virtual environment not found. Run: ofd setup
exit /b 1
)
:: Run OFD CLI
%PYTHON_CMD% -m ofd %ARGS%
exit /b %ERRORLEVEL%
:: ============================================
:: Functions
:: ============================================
:default_webui
:: Default behavior: run webui
set "NEEDS_WEBUI=1"
set "ARGS=webui"
goto :done_args
:show_help
echo OFD - Open Filament Database CLI
echo.
echo Usage: ofd ^<command^> [options]
echo.
echo Wrapper Commands:
echo setup Run first-time setup (install Python dependencies)
echo --no-setup Skip auto-setup check
echo.
echo OFD Commands (passed to Python CLI):
echo validate Validate data files against schemas
echo build Build database exports (JSON, SQLite, CSV, API)
echo serve Start development server with CORS
echo script Run utility scripts
echo webui Start the WebUI development server
echo.
echo Examples:
echo ofd # Start WebUI dev server (default)
echo ofd setup # Run setup manually
echo ofd validate # Validate all data
echo ofd webui # Start WebUI dev server
echo ofd build # Build all exports
echo.
echo Note: Running without arguments starts the WebUI
echo Node.js dependencies are only installed when you first use 'webui'
echo.
echo Documentation: %DOCS_URL%
exit /b 0
:run_setup
echo ========================================
echo OFD - Setup
echo ========================================
echo.
:: Check Python
call :detect_python
if errorlevel 1 (
echo [WARN] Python 3.10+ not found.
call :try_install_python
call :detect_python
if errorlevel 1 (
echo [ERROR] Could not install Python automatically.
echo Please install Python from: https://apps.microsoft.com/detail/9pnrbtzxmb4z
echo Or see: %DOCS_URL%
exit /b 1
)
)
echo [OK] Python found: %PYTHON_CMD%
:: Check Python version
call :check_python_version
if errorlevel 1 (
echo [ERROR] Python 3.10+ is required.
echo Please upgrade Python. See: %DOCS_URL%
exit /b 1
)
:: Create virtual environment
if not exist "%VENV_DIR%" (
echo [INFO] Creating Python virtual environment...
%PYTHON_CMD% -m venv "%VENV_DIR%"
)
:: Activate and install dependencies
call "%VENV_DIR%\Scripts\activate.bat"
echo [INFO] Installing Python dependencies...
pip install -q --upgrade pip
if errorlevel 1 (
echo [ERROR] Failed to upgrade pip
exit /b 1
)
pip install -q -r "%REQUIREMENTS_FILE%"
if errorlevel 1 (
echo [ERROR] Failed to install requirements
exit /b 1
)
pip install -q -e "%SCRIPT_DIR%"
if errorlevel 1 (
echo [ERROR] Failed to install package
exit /b 1
)
echo [OK] Python dependencies installed
:: Mark setup complete
echo. > "%SETUP_MARKER%"
:: Check Node.js status (informational only)
call :detect_npm
if errorlevel 1 (
echo [WARN] Node.js/npm not found. WebUI dependencies will be installed when you run 'ofd webui'
) else (
for /f "tokens=*" %%v in ('node --version') do echo [OK] Node.js found: %%v
echo [INFO] WebUI dependencies will be installed when you first run 'ofd webui'
)
echo.
echo [OK] Setup complete! You can now use: ofd ^<command^>
echo.
exit /b 0
:run_python_setup
:: Check Python
call :detect_python
if errorlevel 1 (
echo [WARN] Python 3.10+ not found.
call :try_install_python
call :detect_python
if errorlevel 1 (
echo [ERROR] Could not install Python automatically.
echo Please install Python from: https://apps.microsoft.com/detail/9pnrbtzxmb4z
echo Or see: %DOCS_URL%
exit /b 1
)
)
:: Check Python version
call :check_python_version
if errorlevel 1 (
echo [ERROR] Python 3.10+ is required.
echo Please upgrade Python. See: %DOCS_URL%
exit /b 1
)
:: Create virtual environment
if not exist "%VENV_DIR%" (
echo [INFO] Creating Python virtual environment...
%PYTHON_CMD% -m venv "%VENV_DIR%"
)
:: Activate and install dependencies
call "%VENV_DIR%\Scripts\activate.bat"
echo [INFO] Installing Python dependencies...
pip install -q --upgrade pip
if errorlevel 1 (
echo [ERROR] Failed to upgrade pip
exit /b 1
)
pip install -q -r "%REQUIREMENTS_FILE%"
if errorlevel 1 (
echo [ERROR] Failed to install requirements
exit /b 1
)
pip install -q -e "%SCRIPT_DIR%"
if errorlevel 1 (
echo [ERROR] Failed to install package
exit /b 1
)
:: Mark setup complete
echo. > "%SETUP_MARKER%"
echo [OK] Python environment ready
exit /b 0
:setup_webui
:: Check Node.js
call :detect_npm
if errorlevel 1 (
echo [WARN] Node.js/npm not found.
call :try_install_nodejs
call :detect_npm
if errorlevel 1 (
echo [ERROR] Could not install Node.js automatically.
echo Please install Node.js manually from: https://nodejs.org/
echo Or see: %DOCS_URL%
exit /b 1
)
)
:: Install Node.js dependencies
echo [INFO] Installing Node.js dependencies for WebUI...
pushd "%WEBUI_DIR%"
call npm ci
if errorlevel 1 (
popd
echo [ERROR] Failed to install Node.js dependencies
exit /b 1
)
popd
:: Mark WebUI setup complete
echo. > "%WEBUI_SETUP_MARKER%"
echo [OK] Node.js dependencies installed
exit /b 0
:try_install_python
:: Try to install Python using Windows package managers
echo [INFO] Attempting to install Python...
:: Try winget first (built into Windows 11 and Windows 10 22H2+)
where winget >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using winget to install Python...
winget install -e --id Python.Python.3.12 --accept-package-agreements --accept-source-agreements
if not errorlevel 1 (
echo [OK] Python installed via winget
:: Refresh PATH
call :refresh_path
exit /b 0
)
)
:: Try chocolatey
where choco >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using Chocolatey to install Python...
choco install python -y
if not errorlevel 1 (
echo [OK] Python installed via Chocolatey
call :refresh_path
exit /b 0
)
)
:: Try scoop
where scoop >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using Scoop to install Python...
scoop install python
if not errorlevel 1 (
echo [OK] Python installed via Scoop
exit /b 0
)
)
echo [WARN] No supported package manager found (winget, choco, scoop)
exit /b 1
:try_install_nodejs
:: Try to install Node.js using Windows package managers
echo [INFO] Attempting to install Node.js...
:: Try winget first
where winget >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using winget to install Node.js...
winget install -e --id OpenJS.NodeJS.LTS --accept-package-agreements --accept-source-agreements
if not errorlevel 1 (
echo [OK] Node.js installed via winget
call :refresh_path
exit /b 0
)
)
:: Try chocolatey
where choco >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using Chocolatey to install Node.js...
choco install nodejs-lts -y
if not errorlevel 1 (
echo [OK] Node.js installed via Chocolatey
call :refresh_path
exit /b 0
)
)
:: Try scoop
where scoop >nul 2>&1
if not errorlevel 1 (
echo [INFO] Using Scoop to install Node.js...
scoop install nodejs-lts
if not errorlevel 1 (
echo [OK] Node.js installed via Scoop
exit /b 0
)
)
echo [WARN] No supported package manager found (winget, choco, scoop)
exit /b 1
:refresh_path
:: Refresh PATH environment variable to pick up newly installed programs
:: This reads the current PATH from the registry
for /f "tokens=2*" %%a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul') do set "SYSPATH=%%b"
for /f "tokens=2*" %%a in ('reg query "HKCU\Environment" /v Path 2^>nul') do set "USERPATH=%%b"
set "PATH=%SYSPATH%;%USERPATH%"
exit /b 0
:detect_python
:: Try python first (Windows usually has python, not python3)
where python >nul 2>&1
if not errorlevel 1 (
:: Verify it's Python 3
for /f "tokens=*" %%v in ('python --version 2^>^&1') do set "PY_VER=%%v"
echo !PY_VER! | findstr /C:"Python 3" >nul
if not errorlevel 1 (
set "PYTHON_CMD=python"
exit /b 0
)
)
:: Try python3 as fallback
where python3 >nul 2>&1
if not errorlevel 1 (
set "PYTHON_CMD=python3"
exit /b 0
)
exit /b 1
:check_python_version
:: Check if Python version is >= 3.10
for /f "tokens=2 delims= " %%v in ('!PYTHON_CMD! --version 2^>^&1') do set "PY_VERSION=%%v"
for /f "tokens=1,2 delims=." %%a in ("!PY_VERSION!") do (
set "PY_MAJOR=%%a"
set "PY_MINOR=%%b"
)
if !PY_MAJOR! LSS 3 exit /b 1
if !PY_MAJOR! EQU 3 if !PY_MINOR! LSS 10 exit /b 1
exit /b 0
:deps_need_update
:: Check if requirements.txt or pyproject.toml are newer than setup marker
:: Uses PowerShell for reliable file timestamp comparison
if not exist "%SETUP_MARKER%" exit /b 1
powershell -NoProfile -Command "$m=(Get-Item '%SETUP_MARKER%').LastWriteTime; if (((Test-Path '%REQUIREMENTS_FILE%') -and (Get-Item '%REQUIREMENTS_FILE%').LastWriteTime -gt $m) -or ((Test-Path '%SCRIPT_DIR%\pyproject.toml') -and (Get-Item '%SCRIPT_DIR%\pyproject.toml').LastWriteTime -gt $m)) { exit 0 } else { exit 1 }"
exit /b %ERRORLEVEL%
:detect_npm
where npm >nul 2>&1
exit /b %ERRORLEVEL%