-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun
More file actions
executable file
·691 lines (609 loc) · 19.3 KB
/
run
File metadata and controls
executable file
·691 lines (609 loc) · 19.3 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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env bash
# DevNote: This script is kinda hard to re-write in a compiled language due to
# the way how a spawned process vs shell script modifies its environment. Using
# other forms of configs like yaml won't have the flexibility a shell script +
# source conf offers. Therefore bash is the best tool for this job of handling
# parent shell environment.
# -----------------------------[ CONFIGURATION ]----------------------------- #
init_default_config() {
# Default: project dir name
PROJECT_NAME=""
# Default: automatic detection
# Available values:
# none | rust | python | python-uv | django | fastapi | flask | html | nodejs | reactjs | nextjs | elixir | angular | flutter | astal-gtk
PROJECT_TYPE=""
# Services: add any valid systemd services here
ENABLED_SERVICES=(
# postgresql
# docker
# mongod
# nginx
)
# Default: automatic detection
# Available values: none | {custom url}
URL=""
# Default: xdg-open
# Available values: {custom command with arguments ending with newtab flag}
BROWSER=""
# Settings: [ true | false ]
AUTORUN_COMMANDS=true
}
# ---------------------------[ CUSTOM OVERRIDES ]--------------------------- #
red='\033[1;31m'
rst='\033[0;0m'
# =========================================================================== #
# █░█ ▀█▀ █ █░░ █▀
# █▄█ ░█░ █ █▄▄ ▄█
# log(str:message)
log() {
local green='\033[1;32m'
local reset='\033[0;0m'
echo -e "[${green}$(date +'%H:%M:%S')${reset}] $1"
}
# code_block(str:message)
code_block() {
local green='\033[1;32m'
local bg_gray='\033[48;5;236m'
local reset='\033[0m'
local pad=" "
# Read input (supports \n)
local content
content=$(printf "%b" "$1")
# Split into lines
IFS=$'\n' read -r -d '' -a lines <<<"$content"$'\0'
# Calculate max line width
local max=0
for line in "${lines[@]}"; do
((${#line} > max)) && max=${#line}
done
local width=$((max + ${#pad} * 2))
# Top padding
echo ""
printf "${bg_gray}${green}%*s${reset}\n" "$width" ""
# Content
for line in "${lines[@]}"; do
printf "${bg_gray}${green}%s%-*s%s${reset}\n" \
"$pad" "$max" "$line" "$pad"
done
# Bottom padding
printf "${bg_gray}${green}%*s${reset}\n" "$width" ""
echo ""
}
# open_browser(str:new_tab_url str:browser_cmd?)
open_browser() {
# CHECK: Valid URL
local url="$1"
if [[ -z "$url" ]]; then
return
fi
# CHECK: Valid browser
local browser="$2"
if [[ -z "$browser" ]]; then
browser="xdg-open"
else
command -v "${browser%% *}" >/dev/null || {
log "❌ Error: ${browser%% *} not found"
exit 1
}
fi
log "🌐 Launching browser"
(
# Wait for the server to start
local timeout=20
while ! curl -sf "$url" >/dev/null; do
sleep 1
((timeout--)) || { exit 1; }
done
# Launch the browser
$browser "$url" >/dev/null &
) &
}
ensure_window() {
local name="$1"
if tmux has-session -t "$PROJECT_NAME" 2>/dev/null; then
tmux new-window -t "$PROJECT_NAME" -n "$name"
else
tmux new-session -d -s "$PROJECT_NAME" -n "$name"
fi
}
# create_window(window_name:str command?:str)
create_window() {
local name="$1"
local cmd="$2"
# CHECK: Valid Name
if [[ -z "$name" ]]; then
log "🚫 Error: Missing window name"
return
fi
# ACTION: Window without command
if [[ -z "$cmd" ]]; then
ensure_window "$name"
return
fi
# ACTION: Create window only if valid command
if command -v "${cmd%% *}" >/dev/null; then
ensure_window "$name"
# "clear" Enter fixes send-keys doubling text due to shell race condition
tmux send-keys -t "$PROJECT_NAME:$name" "clear" Enter "$cmd"
if [ "$AUTORUN_COMMANDS" == true ]; then
tmux send-keys -t "$PROJECT_NAME:$name" Enter
fi
fi
}
# create_temp_window(str:window_name str:command)
create_temp_window() {
local name="$1"
local cmd="$2"
if command -v "${cmd%% *}" >/dev/null; then
tmux new-window -t "$PROJECT_NAME" -n "$name" "$cmd"
fi
}
# █▄▄ █▀█ █▀█ █░█░█ █▀ █▀▀ █▀█
# █▄█ █▀▄ █▄█ ▀▄▀▄▀ ▄█ ██▄ █▀▄
get_server_url() {
local project_type="$1"
case "$project_type" in
"none") ;; # Do nothing
"python") ;;
"python-uv") ;;
"fastapi") ;;
"flask") echo "http://localhost:5000" ;;
"html")
if command -v live-server >/dev/null; then
echo "http://localhost:8080"
fi
;;
"django") echo "http://localhost:8000" ;;
"nodejs") ;;
"reactjs") echo "http://localhost:3000" ;;
"nextjs") echo "http://localhost:3000" ;;
"angular") echo "http://localhost:4200" ;;
"flutter") ;;
"astal-gtk") ;;
"elixir") echo "http://localhost:4000" ;;
"rust") ;;
*) log "🚧 Warning: No server url for ${project_type}" ;;
esac
}
# █░░ ▄▀█ █▄█ █▀█ █░█ ▀█▀ █▀
# █▄▄ █▀█ ░█░ █▄█ █▄█ ░█░ ▄█
# run_server_command(str:project_type)
run_server_command() {
local project_type="$1"
case "$project_type" in
"none") ;; # Do nothing
"python") echo "python main.py" ;;
"python-uv") echo "uv run main.py" ;;
"fastapi") echo "uvicorn main:app --reload" ;;
"flask") echo "python main.py" ;;
"html")
if command -v live-server >/dev/null; then
echo "live-server --no-browser"
fi
;;
"django") echo "python manage.py runserver" ;;
"nodejs") echo "npm start" ;;
"reactjs") echo "npm start" ;;
"nextjs") echo "npm run dev" ;;
"angular") echo "ng serve" ;;
"flutter") echo "flutter run" ;;
"astal-gtk") echo "ags run" ;;
"elixir") echo "iex -S mix phx.server" ;;
"rust") echo "cargo run" ;;
*) log "🚧 Warning: No server setup for ${project_type}" ;;
esac
}
setup_base_layout() {
create_window "Cmd" "$(run_server_command "$PROJECT_TYPE")"
create_window "Editor" "nvim"
}
setup_fastapi_layout() {
setup_base_layout
create_window "Test" "clear && pytest"
}
setup_django_layout() {
setup_base_layout
create_window "Test" "python manage.py test"
}
setup_rust_layout() {
setup_base_layout
create_window "Test" "cargo test"
}
# select_layout(str:project_type)
setup_layout() {
local project_type="$1"
case "$project_type" in
"none") setup_base_layout ;;
"python") setup_base_layout ;;
"python-uv") setup_base_layout ;;
"django") setup_django_layout ;;
"fastapi") setup_fastapi_layout ;;
"flask") setup_base_layout ;;
"html") setup_base_layout ;;
"nodejs") setup_base_layout ;;
"reactjs") setup_base_layout ;;
"nextjs") setup_base_layout ;;
"angular") setup_base_layout ;;
"flutter") setup_base_layout ;;
"astal-gtk") setup_base_layout ;;
"elixir") setup_base_layout ;;
"rust") setup_rust_layout ;;
*)
log "🚧 Unknown technology: ${project_type}. Using default layout..."
setup_base_layout
;;
esac
}
# █▀ █▀▀ █▀█ █░█ █ █▀▀ █▀▀ █▀
# ▄█ ██▄ █▀▄ ▀▄▀ █ █▄▄ ██▄ ▄█
init_services() {
for service in "${ENABLED_SERVICES[@]}"; do
if ! systemctl is-active --quiet "$service"; then
log "🔧 Starting $service..."
sudo systemctl start "$service"
fi
done
}
# █▀▀ █▄░█ █░█ █▀ █▀▀ ▀█▀ █░█ █▀█
# ██▄ █░▀█ ▀▄▀ ▄█ ██▄ ░█░ █▄█ █▀▀
# setup_base_env(str:dependency_dir str:dependency_cmd)
setup_base_env() {
local dependency_dir="$1"
local dependency_cmd="$2"
if [[ ! -d $dependency_dir ]]; then
log "🚀 Setting up dependencies..."
command -v "${dependency_cmd%% *}" >/dev/null || {
log "❌ Error: ${dependency_cmd%% *} not found"
exit 1
}
$dependency_cmd
fi
}
setup_python_env() {
local dependency_dir="${1:-venv/}"
local dependency_cmd="${2:-python -m venv venv}"
if [[ ! -d $dependency_dir ]]; then
setup_base_env "$dependency_dir" "$dependency_cmd"
log "📦 Installing requirements..."
# shellcheck disable=SC1090
source "./$dependency_dir/bin/activate"
if [[ -f "requirements.txt" ]]; then
pip install -r requirements.txt
else
log "🚧 Warning: No requirements.txt found"
fi
else
# shellcheck disable=SC1090
source "./$dependency_dir/bin/activate"
fi
}
setup_node_env() {
setup_base_env "node_modules/" "npm install"
}
setup_elixir_env() {
setup_base_env "deps/" "mix setup"
}
# select_env(str:project_type)
setup_env() {
local project_type="$1"
case "$project_type" in
"none") ;; # Do nothing
"python") setup_python_env ;;
"python-uv") ;; # uv manages its own environment
"django") setup_python_env ;;
"fastapi") setup_python_env ;;
"flask") setup_python_env ;;
"html") ;;
"nodejs") setup_node_env ;;
"reactjs") setup_node_env ;;
"nextjs") setup_node_env ;;
"angular") setup_node_env ;;
"flutter") ;;
"astal-gtk") setup_node_env ;;
"elixir") setup_elixir_env ;;
"rust") ;; # Rust sets up its own environment with cargo
*) log "🚧 Unknown technology: ${project_type}" ;;
esac
}
# █▀▀ █░█ █▀▀ █▀▀ █▄▀ █▀
# █▄▄ █▀█ ██▄ █▄▄ █░█ ▄█
precheck_alembic() {
# CHECK: alembic is installed
if ! command -v alembic >/dev/null; then
log "❌ Error: alembic.ini found but alembic is not installed"
return
fi
# Capture both stdout + stderr
local alembic_output
alembic_output="$(alembic check 2>&1)"
local alembic_status=$?
if [[ $alembic_status == 0 ]]; then
# CASE: Revision Created | DB Migrated
return
fi
case "$alembic_output" in
*"ERROR [alembic.util.messaging] Target database is not up to date."*)
# CASE: Revision Created | DB Not Migrated
log "📎 - Migrating DB to latest alembic revision..."
alembic upgrade head
;;
*) ;;
esac
}
postcheck_alembic() {
# CHECK: alembic is installed
if ! command -v alembic >/dev/null; then
log "❌ Error: alembic.ini found but alembic is not installed"
return
fi
# Capture both stdout + stderr
local alembic_output
alembic_output="$(alembic check 2>&1)"
local alembic_status=$?
if [[ $alembic_status == 0 ]]; then
# CASE: Revision Created | DB Migrated
return
fi
case "$alembic_output" in
*"ERROR [alembic.util.messaging] New upgrade operations detected"*)
# CASE: Model Changed | Revision Not Created
case "$alembic_output" in
*"INFO [alembic.autogenerate.compare] Detected added table"*)
table_name=$(grep -oP "Detected added table '\K[^']+" <<<"$alembic_output")
log "📎 - New table detected. You probably want:"
code_block "alembic revision --autogenerate -m \"add $table_name\"\nalembic upgrade head"
;;
*)
log "📎 - Model changes detected. Consider:"
code_block "alembic revision --autogenerate -m \"schema changes\"\nalembic upgrade head"
;;
esac
return 1
;;
*"ERROR [alembic.util.messaging] Target database is not up to date."*)
# CASE: Revision Created | DB Not Migrated
log "📎 - Looks like you forgot to migrate DB"
alembic upgrade head
log "📎 - DB migrated to latest revision"
;;
*)
# CASE: Unrecognized Error
log "❌ Alembic failed with an unrecognized error"
log "$alembic_output"
return 1
;;
esac
}
precheck_django() {
# Run migrations if needed
if python manage.py showmigrations --plan | grep '\[ \]'; then
log "📎 - Running migrations to database..."
python manage.py migrate
fi
}
postcheck_django() {
# Capture both stdout + stderr
local django_output
django_output="$(python manage.py migrate 2>&1)"
case "$django_output" in
*"Your models in app(s):"*"have changes that are not yet reflected in a migration"*)
# CASE: Model Changed | Revision Not Created
app_names=$(grep -oP "Your models in app\(s\): '\K[^']+" <<<"$django_output")
log "📎 - Model changes detected in: ${red}$app_names${rst}. Consider:"
code_block "python manage.py makemigrations\npython manage.py migrate"
;;
*)
# CASE: Revision Created | DB Migrated
return 0
;;
esac
}
# █▀█ █▀█ █▀▀ █▀▀ █░░ █ █▀▀ █░█ ▀█▀
# █▀▀ █▀▄ ██▄ █▀░ █▄▄ █ █▄█ █▀█ ░█░
preflight_fastapi() {
if [[ -f "alembic.ini" ]]; then
log "✈️ Preflight: alembic"
precheck_alembic
fi
}
preflight_django() {
log "✈️ Preflight: django"
precheck_django
}
# preflight(str:project_type)
preflight_check() {
local project_type="$1"
case "$project_type" in
"none") ;;
"python") ;;
"python-uv") ;;
"django") preflight_django ;;
"fastapi") preflight_fastapi ;;
"flask") ;;
"html") ;;
"nodejs") ;;
"react") ;;
"nextjs") ;;
"angular") ;;
"flutter") ;;
"astal-gtk") ;;
"elixir") ;;
"rust") ;;
*) ;;
esac
}
# █▀█ █▀█ █▀ ▀█▀ █▀▀ █░░ █ █▀▀ █░█ ▀█▀
# █▀▀ █▄█ ▄█ ░█░ █▀░ █▄▄ █ █▄█ █▀█ ░█░
postflight_fastapi() {
if [[ -f "alembic.ini" ]]; then
log "✈️ Postflight: alembic"
postcheck_alembic
fi
}
postflight_django() {
log "✈️ Postflight: django"
postcheck_django
}
# postflight_check(str:project_type)
postflight_check() {
local project_type="$1"
case "$project_type" in
"none") ;;
"python") ;;
"python-uv") ;;
"django") postflight_django ;;
"fastapi") postflight_fastapi ;;
"flask") ;;
"html") ;;
"nodejs") ;;
"react") ;;
"nextjs") ;;
"angular") ;;
"flutter") ;;
"astal-gtk") ;;
"elixir") ;;
"rust") ;;
*) ;;
esac
}
# █▀▄ █▀▀ ▀█▀ █▀▀ █▀▀ ▀█▀ █ █▀█ █▄░█
# █▄▀ ██▄ ░█░ ██▄ █▄▄ ░█░ █ █▄█ █░▀█
detect_project_type() {
# python
if [[ -f "main.py" ]]; then
if grep -q "from fastapi import FastAPI" main.py; then
echo "fastapi" && return
elif grep -q "from flask import Flask" main.py; then
echo "flask" && return
fi
if [[ -f "pyproject.toml" ]]; then
echo "python-uv" && return
fi
echo "python" && return
fi
# django
if [[ -f "manage.py" ]]; then
echo "django" && return
fi
# nodejs
if [[ -f "package.json" ]]; then
if [[ -f "angular.json" ]]; then
echo "angular" && return
elif [[ -f "next.config.js" ]] || [[ -f "next.config.ts" ]] || [[ -f "next.config.mjs" ]]; then
echo "nextjs" && return
elif grep -q "react" package.json; then
echo "reactjs" && return
elif [[ -d "@girs" ]]; then
echo "astal-gtk" && return
fi
echo "nodejs" && return
fi
# flutter
if [[ -f "pubspec.yaml" ]]; then
echo "flutter" && return
fi
# elixir
if [[ -f "mix.exs" ]]; then
echo "elixir" && return
fi
# rust
if [[ -f "Cargo.toml" ]]; then
echo "rust" && return
fi
# html
if [[ -f "index.html" ]]; then
echo "html" && return
fi
# none
echo "none" && return
}
# █▀▄▀█ ▄▀█ █ █▄░█
# █░▀░█ █▀█ █ █░▀█
load_user_overrides() {
# INIT: Load global config if available
config_file="${XDG_CONFIG_HOME:-$HOME/.config}/run/config.conf"
# shellcheck disable=SC1090
[[ -f "$config_file" ]] && source "$config_file"
# INIT: Load custom overrides if available
OVERRIDE_FILE=".runrc"
# shellcheck disable=SC1090
[[ -f "$OVERRIDE_FILE" ]] && source "$OVERRIDE_FILE"
}
autoconfigure() {
# FALLBACK: PROJECT_NAME={project dir name}
if [[ -z "$PROJECT_NAME" ]]; then
PROJECT_NAME="$(basename "$PWD")"
# SANITIZE: Replace dots with underscores
PROJECT_NAME="${PROJECT_NAME//./_}"
# SANITIZE: Remove invalid characters
PROJECT_NAME="${PROJECT_NAME//[^a-zA-Z0-9 _-]/}"
# EDGE_CASE: No valid characters at all
if [[ -z "$PROJECT_NAME" ]]; then
PROJECT_NAME="invalid_project_name"
fi
fi
# FALLBACK: PROJECT_TYPE={automatic detection}
if [[ -z "$PROJECT_TYPE" ]]; then
PROJECT_TYPE="$(detect_project_type)"
log "🔍 Detected: $PROJECT_TYPE"
fi
# FALLBACK: NEW_TAB_URL={automatic detection}
if [[ -z "$URL" ]]; then
URL=$(get_server_url "$PROJECT_TYPE")
fi
}
init_environment() {
# SETUP: ENV or custom
if declare -f setup_env_custom > /dev/null; then
setup_env_custom
else
setup_env "$PROJECT_TYPE"
fi
}
exec_output() {
# ACTION: OPEN BROWSER
if [[ "$URL" != "none" ]]; then
open_browser "$URL" "$BROWSER"
fi
}
exec_tmux_devsetup() {
# REQUIRE: tmux dependency for LAYOUT
if ! command -v tmux >/dev/null; then
# FALLBACK: RUN SERVER (Blocking)
cmd=$(run_server_command "$PROJECT_TYPE")
if [[ -z "$cmd" ]]; then
log "🚫 Error: No server found for this type"
exit 1
fi
log "ℹ️ Running server without tmux..."
eval "$cmd"
exit 0
fi
# ACTION: Attach session if exists
if tmux has-session -t "$PROJECT_NAME" 2>/dev/null; then
log "Session '$PROJECT_NAME' already exists. Attaching..."
tmux attach -t "$PROJECT_NAME"
exit 0
fi
# SETUP: LAYOUT or custom
if declare -f setup_layout_custom > /dev/null; then
setup_layout_custom
else
setup_layout "$PROJECT_TYPE"
fi
# ACTION: POST INIT HOOK
if declare -f setup_post_init_hook > /dev/null; then
setup_post_init_hook
fi
tmux attach -t "$PROJECT_NAME:1"
}
init_default_config
load_user_overrides
autoconfigure
init_services
init_environment
preflight_check "$PROJECT_TYPE"
exec_output
exec_tmux_devsetup
postflight_check "$PROJECT_TYPE"
log "👋 Goodbye!"