-
Notifications
You must be signed in to change notification settings - Fork 114
Expand file tree
/
Copy pathofd.sh
More file actions
executable file
·519 lines (474 loc) · 15.6 KB
/
Copy pathofd.sh
File metadata and controls
executable file
·519 lines (474 loc) · 15.6 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#!/usr/bin/env bash
#
# OFD - Open Filament Database CLI Wrapper
# Cross-platform setup and execution script for Linux/macOS
#
# Usage: ./ofd.sh <command> [options]
#
set -e
# Configuration
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$SCRIPT_DIR/.venv"
WEBUI_DIR="$SCRIPT_DIR/webui"
REQUIREMENTS_FILE="$SCRIPT_DIR/requirements.txt"
SETUP_MARKER="$VENV_DIR/.ofd_setup_complete"
WEBUI_SETUP_MARKER="$WEBUI_DIR/node_modules/.ofd_webui_setup"
DOCS_URL="https://github.com/OpenFilamentCollective/open-filament-database/blob/main/docs/installing-software.md"
# Colors for output (if terminal supports it)
if [ -t 1 ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
NC=''
fi
# Helper functions
info() { echo -e "${BLUE}[INFO]${NC} $1"; }
success() { echo -e "${GREEN}[OK]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1" >&2; }
# Detect Python command (python3 preferred, then python)
detect_python() {
if command -v python3 &>/dev/null; then
PYTHON_CMD="python3"
elif command -v python &>/dev/null; then
# Verify it's Python 3
if python --version 2>&1 | grep -q "Python 3"; then
PYTHON_CMD="python"
else
return 1
fi
else
return 1
fi
return 0
}
# Check Python version is >= 3.10
check_python_version() {
local version=$($PYTHON_CMD -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
local major=$(echo "$version" | cut -d. -f1)
local minor=$(echo "$version" | cut -d. -f2)
if [ "$major" -lt 3 ] || ([ "$major" -eq 3 ] && [ "$minor" -lt 10 ]); then
return 1
fi
return 0
}
# Detect npm
detect_npm() {
command -v npm &>/dev/null
}
# Detect package manager for auto-install
detect_package_manager() {
# Check for NixOS or nix package manager first
if [ -n "$IN_NIX_SHELL" ] || [ -f /etc/NIXOS ]; then
echo "nix"
elif command -v nix-env &>/dev/null; then
echo "nix"
elif command -v apt-get &>/dev/null; then
echo "apt"
elif command -v dnf &>/dev/null; then
echo "dnf"
elif command -v pacman &>/dev/null; then
echo "pacman"
elif command -v zypper &>/dev/null; then
echo "zypper"
elif command -v brew &>/dev/null; then
echo "brew"
else
echo "none"
fi
}
# Try to install Python
try_install_python() {
local pm=$(detect_package_manager)
case "$pm" in
apt)
info "Attempting to install Python via apt..."
sudo apt-get update && sudo apt-get install -y python3 python3-venv python3-pip
;;
dnf)
info "Attempting to install Python via dnf..."
sudo dnf install -y python3 python3-pip
;;
pacman)
info "Attempting to install Python via pacman..."
sudo pacman -Sy --noconfirm python python-pip
;;
zypper)
info "Attempting to install Python via zypper..."
sudo zypper install -y python3 python3-pip python3-venv
;;
nix)
if [ -n "$IN_NIX_SHELL" ]; then
warn "Already in a nix-shell. Please add python3 to your shell.nix"
return 1
fi
info "Attempting to install Python via nix-env..."
nix-env -iA nixpkgs.python3
;;
brew)
info "Attempting to install Python via Homebrew..."
brew install python
;;
*)
return 1
;;
esac
}
# Try to install Node.js
try_install_nodejs() {
local pm=$(detect_package_manager)
case "$pm" in
apt)
info "Attempting to install Node.js via apt..."
sudo apt-get update && sudo apt-get install -y nodejs npm
;;
dnf)
info "Attempting to install Node.js via dnf..."
sudo dnf install -y nodejs npm
;;
pacman)
info "Attempting to install Node.js via pacman..."
sudo pacman -Sy --noconfirm nodejs npm
;;
zypper)
info "Attempting to install Node.js via zypper..."
sudo zypper install -y nodejs npm
;;
nix)
if [ -n "$IN_NIX_SHELL" ]; then
warn "Already in a nix-shell. Please add nodejs to your shell.nix"
return 1
fi
info "Attempting to install Node.js via nix-env..."
nix-env -iA nixpkgs.nodejs
;;
brew)
info "Attempting to install Node.js via Homebrew..."
brew install node
;;
*)
return 1
;;
esac
}
# Setup Python virtual environment
setup_venv() {
if [ ! -d "$VENV_DIR" ]; then
info "Creating Python virtual environment..."
$PYTHON_CMD -m venv "$VENV_DIR"
fi
}
# Activate virtual environment
activate_venv() {
source "$VENV_DIR/bin/activate"
PYTHON_CMD="python"
}
# Install Python dependencies
install_python_deps() {
info "Installing Python dependencies..."
pip install --upgrade pip
pip install -r "$REQUIREMENTS_FILE"
# Install the ofd package in development mode
pip install -e "$SCRIPT_DIR"
}
# Install Node.js dependencies (only when needed for webui)
install_node_deps() {
if [ -d "$WEBUI_DIR" ]; then
info "Installing Node.js dependencies for WebUI..."
(cd "$WEBUI_DIR" && npm ci)
touch "$WEBUI_SETUP_MARKER"
success "Node.js dependencies installed"
fi
}
# Check if Python setup is complete
is_setup_complete() {
[ -f "$SETUP_MARKER" ] && [ -d "$VENV_DIR/bin" ]
}
# Check if dependencies need updating (requirements.txt or pyproject.toml changed since last install)
deps_need_update() {
[ -f "$SETUP_MARKER" ] || return 1
if [ -f "$REQUIREMENTS_FILE" ] && [ "$REQUIREMENTS_FILE" -nt "$SETUP_MARKER" ]; then
return 0
fi
if [ -f "$SCRIPT_DIR/pyproject.toml" ] && [ "$SCRIPT_DIR/pyproject.toml" -nt "$SETUP_MARKER" ]; then
return 0
fi
return 1
}
# Check if WebUI setup is complete
is_webui_setup_complete() {
[ -f "$WEBUI_SETUP_MARKER" ] && [ -d "$WEBUI_DIR/node_modules" ]
}
# Mark setup as complete
mark_setup_complete() {
touch "$SETUP_MARKER"
}
# Setup Python environment only
run_python_setup() {
# Check/install Python
if ! detect_python; then
warn "Python 3 not found."
echo -n "Would you like to try installing Python? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
if try_install_python; then
detect_python || { error "Failed to install Python. See: $DOCS_URL"; exit 1; }
else
error "Could not auto-install Python. Please install manually."
error "See: $DOCS_URL"
exit 1
fi
else
error "Python 3 is required."
error "Please install Python manually from: https://www.python.org/"
error "Or see: $DOCS_URL"
exit 1
fi
fi
# Check Python version
if ! check_python_version; then
error "Python 3.10+ is required. Current: $($PYTHON_CMD --version)"
error "Please upgrade Python. See: $DOCS_URL"
exit 1
fi
# Setup Python environment
setup_venv
activate_venv
install_python_deps
mark_setup_complete
success "Python environment ready"
info "To enable tab completion: eval \"\$(./ofd.sh _completion)\""
}
# Setup WebUI (Node.js) - called only when webui command is used
setup_webui() {
# Check/install Node.js
if ! detect_npm; then
warn "Node.js/npm not found."
echo -n "Would you like to try installing Node.js? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
if try_install_nodejs; then
detect_npm && success "Node.js installed: $(node --version)"
else
error "Could not auto-install Node.js."
error "Please install Node.js manually from: https://nodejs.org/"
error "Or see: $DOCS_URL"
exit 1
fi
else
error "Node.js is required for the WebUI."
error "Please install Node.js from: https://nodejs.org/"
error "Or see: $DOCS_URL"
exit 1
fi
fi
# Install Node.js dependencies
install_node_deps
}
# Run full setup (for explicit 'setup' command)
run_setup() {
echo "========================================"
echo "OFD - Setup"
echo "========================================"
echo
# Python setup
if ! detect_python; then
warn "Python 3 not found."
echo -n "Would you like to try installing Python? [y/N] "
read -r response
if [[ "$response" =~ ^[Yy]$ ]]; then
if try_install_python; then
detect_python || { error "Failed to install Python. See: $DOCS_URL"; exit 1; }
else
error "Could not auto-install Python. Please install manually."
error "See: $DOCS_URL"
exit 1
fi
else
error "Python 3 is required."
error "Please install Python manually from: https://www.python.org/"
error "Or see: $DOCS_URL"
exit 1
fi
fi
success "Python found: $($PYTHON_CMD --version)"
# Check Python version
if ! check_python_version; then
error "Python 3.10+ is required. Current: $($PYTHON_CMD --version)"
error "Please upgrade Python. See: $DOCS_URL"
exit 1
fi
# Setup Python environment
setup_venv
activate_venv
install_python_deps
mark_setup_complete
success "Python dependencies installed"
# Check Node.js status (informational only)
if ! detect_npm; then
warn "Node.js/npm not found. WebUI dependencies will be installed when you run './ofd.sh webui'"
else
success "Node.js found: $(node --version)"
info "WebUI dependencies will be installed when you first run './ofd.sh webui'"
fi
echo
success "Setup complete! You can now use: ./ofd.sh <command>"
info "To enable tab completion: eval \"\$(./ofd.sh _completion)\""
echo
}
# Show help
show_help() {
echo "OFD - Open Filament Database CLI"
echo
echo "Usage: ./ofd.sh <command> [options]"
echo
echo "Wrapper Commands:"
echo " setup Run first-time setup (install Python dependencies)"
echo " _completion Output bash completion script (use with eval)"
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.sh # Start WebUI dev server (default)"
echo " ./ofd.sh setup # Run setup manually"
echo " ./ofd.sh validate # Validate all data"
echo " ./ofd.sh webui # Start WebUI dev server"
echo " ./ofd.sh 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"
}
# Main entry point
main() {
local skip_setup=false
local args=()
local needs_webui=false
# Parse wrapper-specific arguments and detect webui command
for arg in "$@"; do
case "$arg" in
--no-setup)
skip_setup=true
;;
setup)
run_setup
exit 0
;;
_completion)
# Output bash completion function for sourcing
# Usage: eval "$(./ofd.sh _completion)"
sed -n '/^# BEGIN_COMPLETION$/,/^# END_COMPLETION$/p' "$0" | grep -v '^# \(BEGIN\|END\)_COMPLETION$'
exit 0
;;
-h|--help)
if [ ${#args[@]} -eq 0 ]; then
show_help
exit 0
else
args+=("$arg")
fi
;;
webui)
needs_webui=true
args+=("$arg")
;;
*)
args+=("$arg")
;;
esac
done
# Auto-setup Python on first run (unless --no-setup)
if [ "$skip_setup" = false ] && ! is_setup_complete; then
info "First run detected. Setting up Python environment..."
run_python_setup
elif [ "$skip_setup" = false ] && deps_need_update; then
info "Dependencies have changed. Updating..."
activate_venv
install_python_deps
mark_setup_complete
success "Dependencies updated"
fi
# Setup WebUI if webui command is used and not yet setup
if [ "$needs_webui" = true ] && ! is_webui_setup_complete; then
info "WebUI first run. Setting up Node.js dependencies..."
setup_webui
fi
# Activate venv and run OFD
if [ -f "$VENV_DIR/bin/activate" ]; then
activate_venv
else
# Fallback if venv doesn't exist but --no-setup was used
if ! detect_python; then
error "Python not found and setup was skipped. Run: ./ofd.sh setup"
exit 1
fi
fi
# If no arguments, default to webui
if [ ${#args[@]} -eq 0 ]; then
# Setup WebUI if not yet setup
if ! is_webui_setup_complete; then
info "WebUI first run. Setting up Node.js dependencies..."
setup_webui
fi
exec "$PYTHON_CMD" -m ofd webui
fi
# Run OFD CLI
exec "$PYTHON_CMD" -m ofd "${args[@]}"
}
main "$@"
exit $?
# BEGIN_COMPLETION
_ofd_sh_completions() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
# Top-level commands
if [ "$COMP_CWORD" -eq 1 ]; then
COMPREPLY=($(compgen -W "validate build serve script webui setup -h --help -V --version" -- "$cur"))
return
fi
local cmd="${COMP_WORDS[1]}"
case "$cmd" in
validate)
COMPREPLY=($(compgen -W "--json-files --logos --folder-names --store-ids --gtin --json --progress --data-dir --stores-dir -h --help" -- "$cur"))
;;
build)
COMPREPLY=($(compgen -W "-o --output-dir -d --data-dir -s --stores-dir -v --version --skip-json --skip-sqlite --skip-csv --skip-api --skip-html -h --help" -- "$cur"))
;;
serve)
COMPREPLY=($(compgen -W "-d --directory -p --port --host -h --help" -- "$cur"))
;;
script)
if [ "$COMP_CWORD" -eq 2 ]; then
local script_names
script_names=$("${VENV_DIR:-$(dirname "${BASH_SOURCE[0]}")/.venv}/bin/python" -c "
import ofd.scripts
from ofd.base import _script_registry
print(' '.join(sorted(_script_registry.keys())))
" 2>/dev/null)
COMPREPLY=($(compgen -W "$script_names --list -l -h --help" -- "$cur"))
else
COMPREPLY=($(compgen -W "--dry-run --json --progress -h --help" -- "$cur"))
fi
;;
webui)
COMPREPLY=($(compgen -W "-p --port --host --open --install -h --help" -- "$cur"))
;;
esac
}
complete -F _ofd_sh_completions ./ofd.sh
complete -F _ofd_sh_completions ofd.sh
# END_COMPLETION