Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 621 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
#!/usr/bin/env bash
2
 
3
UNAMEOUT="$(uname -s)"
4
 
5
# Verify operating system is supported...
6
case "${UNAMEOUT}" in
7
    Linux*)             MACHINE=linux;;
8
    Darwin*)            MACHINE=mac;;
9
    *)                  MACHINE="UNKNOWN"
10
esac
11
 
12
if [ "$MACHINE" == "UNKNOWN" ]; then
13
    echo "Unsupported operating system [$(uname -s)]. Laravel Sail supports macOS, Linux, and Windows (WSL2)." >&2
14
 
15
    exit 1
16
fi
17
 
18
# Determine if stdout is a terminal...
19
if test -t 1; then
20
    # Determine if colors are supported...
21
    ncolors=$(tput colors)
22
 
23
    if test -n "$ncolors" && test "$ncolors" -ge 8; then
24
        BOLD="$(tput bold)"
25
        YELLOW="$(tput setaf 3)"
26
        GREEN="$(tput setaf 2)"
27
        NC="$(tput sgr0)"
28
    fi
29
fi
30
 
31
# Function that prints the available commands...
32
function display_help {
33
    echo "Laravel Sail"
34
    echo
35
    echo "${YELLOW}Usage:${NC}" >&2
36
    echo "  sail COMMAND [options] [arguments]"
37
    echo
38
    echo "Unknown commands are passed to the docker-compose binary."
39
    echo
40
    echo "${YELLOW}docker-compose Commands:${NC}"
41
    echo "  ${GREEN}sail up${NC}        Start the application"
42
    echo "  ${GREEN}sail up -d${NC}     Start the application in the background"
43
    echo "  ${GREEN}sail stop${NC}      Stop the application"
44
    echo "  ${GREEN}sail restart${NC}   Restart the application"
45
    echo "  ${GREEN}sail ps${NC}        Display the status of all containers"
46
    echo
47
    echo "${YELLOW}Artisan Commands:${NC}"
48
    echo "  ${GREEN}sail artisan ...${NC}          Run an Artisan command"
49
    echo "  ${GREEN}sail artisan queue:work${NC}"
50
    echo
51
    echo "${YELLOW}PHP Commands:${NC}"
52
    echo "  ${GREEN}sail php ...${NC}   Run a snippet of PHP code"
53
    echo "  ${GREEN}sail php -v${NC}"
54
    echo
55
    echo "${YELLOW}Composer Commands:${NC}"
56
    echo "  ${GREEN}sail composer ...${NC}                       Run a Composer command"
57
    echo "  ${GREEN}sail composer require laravel/sanctum${NC}"
58
    echo
59
    echo "${YELLOW}Node Commands:${NC}"
60
    echo "  ${GREEN}sail node ...${NC}         Run a Node command"
61
    echo "  ${GREEN}sail node --version${NC}"
62
    echo
63
    echo "${YELLOW}NPM Commands:${NC}"
64
    echo "  ${GREEN}sail npm ...${NC}        Run a npm command"
65
    echo "  ${GREEN}sail npx${NC}            Run a npx command"
66
    echo "  ${GREEN}sail npm run prod${NC}"
67
    echo
68
    echo "${YELLOW}Yarn Commands:${NC}"
69
    echo "  ${GREEN}sail yarn ...${NC}        Run a Yarn command"
70
    echo "  ${GREEN}sail yarn run prod${NC}"
71
    echo
72
    echo "${YELLOW}Database Commands:${NC}"
73
    echo "  ${GREEN}sail mysql${NC}     Start a MySQL CLI session within the 'mysql' container"
74
    echo "  ${GREEN}sail mariadb${NC}   Start a MySQL CLI session within the 'mariadb' container"
75
    echo "  ${GREEN}sail psql${NC}      Start a PostgreSQL CLI session within the 'pgsql' container"
76
    echo "  ${GREEN}sail redis${NC}     Start a Redis CLI session within the 'redis' container"
77
    echo
78
    echo "${YELLOW}Debugging:${NC}"
79
    echo "  ${GREEN}sail debug ...${NC}          Run an Artisan command in debug mode"
80
    echo "  ${GREEN}sail debug queue:work${NC}"
81
    echo
82
    echo "${YELLOW}Running Tests:${NC}"
83
    echo "  ${GREEN}sail test${NC}          Run the PHPUnit tests via the Artisan test command"
84
    echo "  ${GREEN}sail phpunit ...${NC}   Run PHPUnit"
399 lars 85
    echo "  ${GREEN}sail pest ...${NC}      Run Pest"
148 lars 86
    echo "  ${GREEN}sail pint ...${NC}      Run Pint"
87
    echo "  ${GREEN}sail dusk${NC}          Run the Dusk tests (Requires the laravel/dusk package)"
88
    echo "  ${GREEN}sail dusk:fails${NC}    Re-run previously failed Dusk tests (Requires the laravel/dusk package)"
89
    echo
90
    echo "${YELLOW}Container CLI:${NC}"
91
    echo "  ${GREEN}sail shell${NC}        Start a shell session within the application container"
92
    echo "  ${GREEN}sail bash${NC}         Alias for 'sail shell'"
93
    echo "  ${GREEN}sail root-shell${NC}   Start a root shell session within the application container"
94
    echo "  ${GREEN}sail root-bash${NC}    Alias for 'sail root-shell'"
95
    echo "  ${GREEN}sail tinker${NC}       Start a new Laravel Tinker session"
96
    echo
97
    echo "${YELLOW}Sharing:${NC}"
98
    echo "  ${GREEN}sail share${NC}   Share the application publicly via a temporary URL"
621 lars 99
    echo "  ${GREEN}sail open${NC}    Open the site in your browser"
148 lars 100
    echo
101
    echo "${YELLOW}Binaries:${NC}"
102
    echo "  ${GREEN}sail bin ...${NC}   Run Composer binary scripts from the vendor/bin directory"
103
    echo
104
    echo "${YELLOW}Customization:${NC}"
105
    echo "  ${GREEN}sail artisan sail:publish${NC}   Publish the Sail configuration files"
106
    echo "  ${GREEN}sail build --no-cache${NC}       Rebuild all of the Sail containers"
107
 
108
    exit 1
109
}
110
 
111
# Proxy the "help" command...
112
if [ $# -gt 0 ]; then
113
    if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
114
        display_help
115
    fi
116
else
117
    display_help
118
fi
119
 
120
# Source the ".env" file so Laravel's environment variables are available...
121
if [ ! -z "$APP_ENV" ] && [ -f ./.env.$APP_ENV ]; then
122
  source ./.env.$APP_ENV;
123
elif [ -f ./.env ]; then
124
  source ./.env;
125
fi
126
 
127
# Define environment variables...
128
export APP_PORT=${APP_PORT:-80}
129
export APP_SERVICE=${APP_SERVICE:-"laravel.test"}
130
export DB_PORT=${DB_PORT:-3306}
131
export WWWUSER=${WWWUSER:-$UID}
132
export WWWGROUP=${WWWGROUP:-$(id -g)}
133
 
134
export SAIL_FILES=${SAIL_FILES:-""}
135
export SAIL_SHARE_DASHBOARD=${SAIL_SHARE_DASHBOARD:-4040}
136
export SAIL_SHARE_SERVER_HOST=${SAIL_SHARE_SERVER_HOST:-"laravel-sail.site"}
137
export SAIL_SHARE_SERVER_PORT=${SAIL_SHARE_SERVER_PORT:-8080}
138
export SAIL_SHARE_SUBDOMAIN=${SAIL_SHARE_SUBDOMAIN:-""}
621 lars 139
export SAIL_SHARE_DOMAIN=${SAIL_SHARE_DOMAIN:-"$SAIL_SHARE_SERVER_HOST"}
991 lars 140
export SAIL_SHARE_SERVER=${SAIL_SHARE_SERVER:-""}
148 lars 141
 
142
# Function that outputs Sail is not running...
143
function sail_is_not_running {
144
    echo "${BOLD}Sail is not running.${NC}" >&2
145
    echo "" >&2
146
    echo "${BOLD}You may Sail using the following commands:${NC} './vendor/bin/sail up' or './vendor/bin/sail up -d'" >&2
147
 
148
    exit 1
149
}
150
 
151
# Define Docker Compose command prefix...
152
docker compose &> /dev/null
153
if [ $? == 0 ]; then
154
    DOCKER_COMPOSE=(docker compose)
155
else
156
    DOCKER_COMPOSE=(docker-compose)
157
fi
158
 
159
if [ -n "$SAIL_FILES" ]; then
160
    # Convert SAIL_FILES to an array...
161
    IFS=':' read -ra SAIL_FILES <<< "$SAIL_FILES"
162
 
163
    for FILE in "${SAIL_FILES[@]}"; do
164
        if [ -f "$FILE" ]; then
165
            DOCKER_COMPOSE+=(-f "$FILE")
166
        else
167
            echo "${BOLD}Unable to find Docker Compose file: '${FILE}'${NC}" >&2
168
 
169
            exit 1
170
        fi
171
    done
172
fi
173
 
174
EXEC="yes"
175
 
176
if [ -z "$SAIL_SKIP_CHECKS" ]; then
177
    # Ensure that Docker is running...
178
    if ! docker info > /dev/null 2>&1; then
179
        echo "${BOLD}Docker is not running.${NC}" >&2
180
 
181
        exit 1
182
    fi
183
 
184
    # Determine if Sail is currently up...
185
    if "${DOCKER_COMPOSE[@]}" ps "$APP_SERVICE" 2>&1 | grep 'Exit\|exited'; then
186
        echo "${BOLD}Shutting down old Sail processes...${NC}" >&2
187
 
188
        "${DOCKER_COMPOSE[@]}" down > /dev/null 2>&1
189
 
190
        EXEC="no"
191
    elif [ -z "$("${DOCKER_COMPOSE[@]}" ps -q)" ]; then
192
        EXEC="no"
193
    fi
194
fi
195
 
196
ARGS=()
197
 
198
# Proxy PHP commands to the "php" binary on the application container...
199
if [ "$1" == "php" ]; then
200
    shift 1
201
 
202
    if [ "$EXEC" == "yes" ]; then
203
        ARGS+=(exec -u sail)
204
        [ ! -t 0 ] && ARGS+=(-T)
205
        ARGS+=("$APP_SERVICE" "php" "$@")
206
    else
207
        sail_is_not_running
208
    fi
209
 
210
# Proxy vendor binary commands on the application container...
211
elif [ "$1" == "bin" ]; then
212
    shift 1
213
 
214
    if [ "$EXEC" == "yes" ]; then
215
        ARGS+=(exec -u sail)
216
        [ ! -t 0 ] && ARGS+=(-T)
217
        ARGS+=("$APP_SERVICE" ./vendor/bin/"$@")
218
    else
219
        sail_is_not_running
220
    fi
221
 
222
# Proxy docker-compose commands to the docker-compose binary on the application container...
223
elif [ "$1" == "docker-compose" ]; then
224
    shift 1
225
 
226
    if [ "$EXEC" == "yes" ]; then
227
        ARGS+=(exec -u sail)
228
        [ ! -t 0 ] && ARGS+=(-T)
229
        ARGS+=("$APP_SERVICE" "${DOCKER_COMPOSE[@]}")
230
    else
231
        sail_is_not_running
232
    fi
233
 
234
# Proxy Composer commands to the "composer" binary on the application container...
235
elif [ "$1" == "composer" ]; then
236
    shift 1
237
 
238
    if [ "$EXEC" == "yes" ]; then
239
        ARGS+=(exec -u sail)
240
        [ ! -t 0 ] && ARGS+=(-T)
241
        ARGS+=("$APP_SERVICE" "composer" "$@")
242
    else
243
        sail_is_not_running
244
    fi
245
 
246
# Proxy Artisan commands to the "artisan" binary on the application container...
247
elif [ "$1" == "artisan" ] || [ "$1" == "art" ]; then
248
    shift 1
249
 
250
    if [ "$EXEC" == "yes" ]; then
251
        ARGS+=(exec -u sail)
252
        [ ! -t 0 ] && ARGS+=(-T)
253
        ARGS+=("$APP_SERVICE" php artisan "$@")
254
    else
255
        sail_is_not_running
256
    fi
257
 
258
# Proxy the "debug" command to the "php artisan" binary on the application container with xdebug enabled...
259
elif [ "$1" == "debug" ]; then
260
    shift 1
261
 
262
    if [ "$EXEC" == "yes" ]; then
263
        ARGS+=(exec -u sail -e XDEBUG_SESSION=1)
264
        [ ! -t 0 ] && ARGS+=(-T)
265
        ARGS+=("$APP_SERVICE" php artisan "$@")
266
    else
267
        sail_is_not_running
268
    fi
269
 
270
# Proxy the "test" command to the "php artisan test" Artisan command...
271
elif [ "$1" == "test" ]; then
272
    shift 1
273
 
274
    if [ "$EXEC" == "yes" ]; then
275
        ARGS+=(exec -u sail)
276
        [ ! -t 0 ] && ARGS+=(-T)
277
        ARGS+=("$APP_SERVICE" php artisan test "$@")
278
    else
279
        sail_is_not_running
280
    fi
281
 
282
# Proxy the "phpunit" command to "php vendor/bin/phpunit"...
283
elif [ "$1" == "phpunit" ]; then
284
    shift 1
285
 
286
    if [ "$EXEC" == "yes" ]; then
287
        ARGS+=(exec -u sail)
288
        [ ! -t 0 ] && ARGS+=(-T)
289
        ARGS+=("$APP_SERVICE" php vendor/bin/phpunit "$@")
290
    else
291
        sail_is_not_running
292
    fi
293
 
399 lars 294
# Proxy the "pest" command to "php vendor/bin/pest"...
295
elif [ "$1" == "pest" ]; then
296
    shift 1
297
 
298
    if [ "$EXEC" == "yes" ]; then
299
        ARGS+=(exec -u sail)
300
        [ ! -t 0 ] && ARGS+=(-T)
301
        ARGS+=("$APP_SERVICE" php vendor/bin/pest "$@")
302
    else
303
        sail_is_not_running
304
    fi
305
 
148 lars 306
# Proxy the "pint" command to "php vendor/bin/pint"...
307
elif [ "$1" == "pint" ]; then
308
    shift 1
309
 
310
    if [ "$EXEC" == "yes" ]; then
311
        ARGS+=(exec -u sail)
312
        [ ! -t 0 ] && ARGS+=(-T)
313
        ARGS+=("$APP_SERVICE" php vendor/bin/pint "$@")
314
    else
315
        sail_is_not_running
316
    fi
317
 
318
# Proxy the "dusk" command to the "php artisan dusk" Artisan command...
319
elif [ "$1" == "dusk" ]; then
320
    shift 1
321
 
322
    if [ "$EXEC" == "yes" ]; then
323
        ARGS+=(exec -u sail)
324
        [ ! -t 0 ] && ARGS+=(-T)
325
        ARGS+=(-e "APP_URL=http://${APP_SERVICE}")
326
        ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
327
        ARGS+=("$APP_SERVICE" php artisan dusk "$@")
328
    else
329
        sail_is_not_running
330
    fi
331
 
332
# Proxy the "dusk:fails" command to the "php artisan dusk:fails" Artisan command...
333
elif [ "$1" == "dusk:fails" ]; then
334
    shift 1
335
 
336
    if [ "$EXEC" == "yes" ]; then
337
        ARGS+=(exec -u sail)
338
        [ ! -t 0 ] && ARGS+=(-T)
339
        ARGS+=(-e "APP_URL=http://${APP_SERVICE}")
340
        ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
341
        ARGS+=("$APP_SERVICE" php artisan dusk:fails "$@")
342
    else
343
        sail_is_not_running
344
    fi
345
 
346
# Initiate a Laravel Tinker session within the application container...
347
elif [ "$1" == "tinker" ] ; then
348
    shift 1
349
 
350
    if [ "$EXEC" == "yes" ]; then
351
        ARGS+=(exec -u sail)
352
        [ ! -t 0 ] && ARGS+=(-T)
353
        ARGS+=("$APP_SERVICE" php artisan tinker)
354
    else
355
        sail_is_not_running
356
    fi
357
 
358
# Proxy Node commands to the "node" binary on the application container...
359
elif [ "$1" == "node" ]; then
360
    shift 1
361
 
362
    if [ "$EXEC" == "yes" ]; then
363
        ARGS+=(exec -u sail)
364
        [ ! -t 0 ] && ARGS+=(-T)
365
        ARGS+=("$APP_SERVICE" node "$@")
366
    else
367
        sail_is_not_running
368
    fi
369
 
370
# Proxy NPM commands to the "npm" binary on the application container...
371
elif [ "$1" == "npm" ]; then
372
    shift 1
373
 
374
    if [ "$EXEC" == "yes" ]; then
375
        ARGS+=(exec -u sail)
376
        [ ! -t 0 ] && ARGS+=(-T)
377
        ARGS+=("$APP_SERVICE" npm "$@")
378
    else
379
        sail_is_not_running
380
    fi
381
 
382
# Proxy NPX commands to the "npx" binary on the application container...
383
elif [ "$1" == "npx" ]; then
384
    shift 1
385
 
386
    if [ "$EXEC" == "yes" ]; then
387
        ARGS+=(exec -u sail)
388
        [ ! -t 0 ] && ARGS+=(-T)
389
        ARGS+=("$APP_SERVICE" npx "$@")
390
    else
391
        sail_is_not_running
392
    fi
393
 
394
# Proxy YARN commands to the "yarn" binary on the application container...
395
elif [ "$1" == "yarn" ]; then
396
    shift 1
397
 
398
    if [ "$EXEC" == "yes" ]; then
399
        ARGS+=(exec -u sail)
400
        [ ! -t 0 ] && ARGS+=(-T)
401
        ARGS+=("$APP_SERVICE" yarn "$@")
402
    else
403
        sail_is_not_running
404
    fi
405
 
406
# Initiate a MySQL CLI terminal session within the "mysql" container...
407
elif [ "$1" == "mysql" ]; then
408
    shift 1
409
 
410
    if [ "$EXEC" == "yes" ]; then
411
        ARGS+=(exec)
412
        [ ! -t 0 ] && ARGS+=(-T)
413
        ARGS+=(mysql bash -c)
414
        ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}")
415
    else
416
        sail_is_not_running
417
    fi
418
 
419
# Initiate a MySQL CLI terminal session within the "mariadb" container...
420
elif [ "$1" == "mariadb" ]; then
421
    shift 1
422
 
423
    if [ "$EXEC" == "yes" ]; then
424
        ARGS+=(exec)
425
        [ ! -t 0 ] && ARGS+=(-T)
426
        ARGS+=(mariadb bash -c)
427
        ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}")
428
    else
429
        sail_is_not_running
430
    fi
431
 
432
# Initiate a PostgreSQL CLI terminal session within the "pgsql" container...
433
elif [ "$1" == "psql" ]; then
434
    shift 1
435
 
436
    if [ "$EXEC" == "yes" ]; then
437
        ARGS+=(exec)
438
        [ ! -t 0 ] && ARGS+=(-T)
439
        ARGS+=(pgsql bash -c)
440
        ARGS+=("PGPASSWORD=\${PGPASSWORD} psql -U \${POSTGRES_USER} \${POSTGRES_DB}")
441
    else
442
        sail_is_not_running
443
    fi
444
 
445
# Initiate a Bash shell within the application container...
446
elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
447
    shift 1
448
 
449
    if [ "$EXEC" == "yes" ]; then
450
        ARGS+=(exec -u sail)
451
        [ ! -t 0 ] && ARGS+=(-T)
452
        ARGS+=("$APP_SERVICE" bash "$@")
453
    else
454
        sail_is_not_running
455
    fi
456
 
457
# Initiate a root user Bash shell within the application container...
458
elif [ "$1" == "root-shell" ] || [ "$1" == "root-bash" ]; then
459
    shift 1
460
 
461
    if [ "$EXEC" == "yes" ]; then
462
        ARGS+=(exec)
463
        [ ! -t 0 ] && ARGS+=(-T)
464
        ARGS+=("$APP_SERVICE" bash "$@")
465
    else
466
        sail_is_not_running
467
    fi
468
 
469
# Initiate a Redis CLI terminal session within the "redis" container...
470
elif [ "$1" == "redis" ] ; then
471
    shift 1
472
 
473
    if [ "$EXEC" == "yes" ]; then
474
        ARGS+=(exec)
475
        [ ! -t 0 ] && ARGS+=(-T)
476
        ARGS+=(redis redis-cli)
477
    else
478
        sail_is_not_running
479
    fi
480
 
481
# Share the site...
482
elif [ "$1" == "share" ]; then
483
    shift 1
484
 
485
    if [ "$EXEC" == "yes" ]; then
486
        docker run --init --rm -p "$SAIL_SHARE_DASHBOARD":4040 -t beyondcodegmbh/expose-server:latest share http://host.docker.internal:"$APP_PORT" \
487
            --server-host="$SAIL_SHARE_SERVER_HOST" \
488
            --server-port="$SAIL_SHARE_SERVER_PORT" \
489
            --auth="$SAIL_SHARE_TOKEN" \
991 lars 490
            --server="$SAIL_SHARE_SERVER" \
148 lars 491
            --subdomain="$SAIL_SHARE_SUBDOMAIN" \
399 lars 492
            --domain="$SAIL_SHARE_DOMAIN" \
148 lars 493
            "$@"
494
 
495
        exit
496
    else
497
        sail_is_not_running
498
    fi
499
 
621 lars 500
# Open the site...
501
elif [ "$1" == "open" ]; then
502
    shift 1
503
 
504
    if [ "$EXEC" == "yes" ]; then
505
        open $APP_URL
506
 
507
        exit
508
    else
509
        sail_is_not_running
510
    fi
511
 
148 lars 512
# Pass unknown commands to the "docker-compose" binary...
513
else
514
    ARGS+=("$@")
515
fi
516
 
517
# Run Docker Compose with the defined arguments...
518
"${DOCKER_COMPOSE[@]}" "${ARGS[@]}"