Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Revision 621 | Zur aktuellen Revision | 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"
99
    echo
100
    echo "${YELLOW}Binaries:${NC}"
101
    echo "  ${GREEN}sail bin ...${NC}   Run Composer binary scripts from the vendor/bin directory"
102
    echo
103
    echo "${YELLOW}Customization:${NC}"
104
    echo "  ${GREEN}sail artisan sail:publish${NC}   Publish the Sail configuration files"
105
    echo "  ${GREEN}sail build --no-cache${NC}       Rebuild all of the Sail containers"
106
 
107
    exit 1
108
}
109
 
110
# Proxy the "help" command...
111
if [ $# -gt 0 ]; then
112
    if [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "-help" ] || [ "$1" == "--help" ]; then
113
        display_help
114
    fi
115
else
116
    display_help
117
fi
118
 
119
# Source the ".env" file so Laravel's environment variables are available...
120
if [ ! -z "$APP_ENV" ] && [ -f ./.env.$APP_ENV ]; then
121
  source ./.env.$APP_ENV;
122
elif [ -f ./.env ]; then
123
  source ./.env;
124
fi
125
 
126
# Define environment variables...
127
export APP_PORT=${APP_PORT:-80}
128
export APP_SERVICE=${APP_SERVICE:-"laravel.test"}
129
export DB_PORT=${DB_PORT:-3306}
130
export WWWUSER=${WWWUSER:-$UID}
131
export WWWGROUP=${WWWGROUP:-$(id -g)}
132
 
133
export SAIL_FILES=${SAIL_FILES:-""}
134
export SAIL_SHARE_DASHBOARD=${SAIL_SHARE_DASHBOARD:-4040}
135
export SAIL_SHARE_SERVER_HOST=${SAIL_SHARE_SERVER_HOST:-"laravel-sail.site"}
136
export SAIL_SHARE_SERVER_PORT=${SAIL_SHARE_SERVER_PORT:-8080}
137
export SAIL_SHARE_SUBDOMAIN=${SAIL_SHARE_SUBDOMAIN:-""}
399 lars 138
export SAIL_SHARE_DOMAIN=${SAIL_SHARE_DOMAIN:-""}
148 lars 139
 
140
# Function that outputs Sail is not running...
141
function sail_is_not_running {
142
    echo "${BOLD}Sail is not running.${NC}" >&2
143
    echo "" >&2
144
    echo "${BOLD}You may Sail using the following commands:${NC} './vendor/bin/sail up' or './vendor/bin/sail up -d'" >&2
145
 
146
    exit 1
147
}
148
 
149
# Define Docker Compose command prefix...
150
docker compose &> /dev/null
151
if [ $? == 0 ]; then
152
    DOCKER_COMPOSE=(docker compose)
153
else
154
    DOCKER_COMPOSE=(docker-compose)
155
fi
156
 
157
if [ -n "$SAIL_FILES" ]; then
158
    # Convert SAIL_FILES to an array...
159
    IFS=':' read -ra SAIL_FILES <<< "$SAIL_FILES"
160
 
161
    for FILE in "${SAIL_FILES[@]}"; do
162
        if [ -f "$FILE" ]; then
163
            DOCKER_COMPOSE+=(-f "$FILE")
164
        else
165
            echo "${BOLD}Unable to find Docker Compose file: '${FILE}'${NC}" >&2
166
 
167
            exit 1
168
        fi
169
    done
170
fi
171
 
172
EXEC="yes"
173
 
174
if [ -z "$SAIL_SKIP_CHECKS" ]; then
175
    # Ensure that Docker is running...
176
    if ! docker info > /dev/null 2>&1; then
177
        echo "${BOLD}Docker is not running.${NC}" >&2
178
 
179
        exit 1
180
    fi
181
 
182
    # Determine if Sail is currently up...
183
    if "${DOCKER_COMPOSE[@]}" ps "$APP_SERVICE" 2>&1 | grep 'Exit\|exited'; then
184
        echo "${BOLD}Shutting down old Sail processes...${NC}" >&2
185
 
186
        "${DOCKER_COMPOSE[@]}" down > /dev/null 2>&1
187
 
188
        EXEC="no"
189
    elif [ -z "$("${DOCKER_COMPOSE[@]}" ps -q)" ]; then
190
        EXEC="no"
191
    fi
192
fi
193
 
194
ARGS=()
195
 
196
# Proxy PHP commands to the "php" binary on the application container...
197
if [ "$1" == "php" ]; then
198
    shift 1
199
 
200
    if [ "$EXEC" == "yes" ]; then
201
        ARGS+=(exec -u sail)
202
        [ ! -t 0 ] && ARGS+=(-T)
203
        ARGS+=("$APP_SERVICE" "php" "$@")
204
    else
205
        sail_is_not_running
206
    fi
207
 
208
# Proxy vendor binary commands on the application container...
209
elif [ "$1" == "bin" ]; then
210
    shift 1
211
 
212
    if [ "$EXEC" == "yes" ]; then
213
        ARGS+=(exec -u sail)
214
        [ ! -t 0 ] && ARGS+=(-T)
215
        ARGS+=("$APP_SERVICE" ./vendor/bin/"$@")
216
    else
217
        sail_is_not_running
218
    fi
219
 
220
# Proxy docker-compose commands to the docker-compose binary on the application container...
221
elif [ "$1" == "docker-compose" ]; then
222
    shift 1
223
 
224
    if [ "$EXEC" == "yes" ]; then
225
        ARGS+=(exec -u sail)
226
        [ ! -t 0 ] && ARGS+=(-T)
227
        ARGS+=("$APP_SERVICE" "${DOCKER_COMPOSE[@]}")
228
    else
229
        sail_is_not_running
230
    fi
231
 
232
# Proxy Composer commands to the "composer" binary on the application container...
233
elif [ "$1" == "composer" ]; then
234
    shift 1
235
 
236
    if [ "$EXEC" == "yes" ]; then
237
        ARGS+=(exec -u sail)
238
        [ ! -t 0 ] && ARGS+=(-T)
239
        ARGS+=("$APP_SERVICE" "composer" "$@")
240
    else
241
        sail_is_not_running
242
    fi
243
 
244
# Proxy Artisan commands to the "artisan" binary on the application container...
245
elif [ "$1" == "artisan" ] || [ "$1" == "art" ]; then
246
    shift 1
247
 
248
    if [ "$EXEC" == "yes" ]; then
249
        ARGS+=(exec -u sail)
250
        [ ! -t 0 ] && ARGS+=(-T)
251
        ARGS+=("$APP_SERVICE" php artisan "$@")
252
    else
253
        sail_is_not_running
254
    fi
255
 
256
# Proxy the "debug" command to the "php artisan" binary on the application container with xdebug enabled...
257
elif [ "$1" == "debug" ]; then
258
    shift 1
259
 
260
    if [ "$EXEC" == "yes" ]; then
261
        ARGS+=(exec -u sail -e XDEBUG_SESSION=1)
262
        [ ! -t 0 ] && ARGS+=(-T)
263
        ARGS+=("$APP_SERVICE" php artisan "$@")
264
    else
265
        sail_is_not_running
266
    fi
267
 
268
# Proxy the "test" command to the "php artisan test" Artisan command...
269
elif [ "$1" == "test" ]; then
270
    shift 1
271
 
272
    if [ "$EXEC" == "yes" ]; then
273
        ARGS+=(exec -u sail)
274
        [ ! -t 0 ] && ARGS+=(-T)
275
        ARGS+=("$APP_SERVICE" php artisan test "$@")
276
    else
277
        sail_is_not_running
278
    fi
279
 
280
# Proxy the "phpunit" command to "php vendor/bin/phpunit"...
281
elif [ "$1" == "phpunit" ]; then
282
    shift 1
283
 
284
    if [ "$EXEC" == "yes" ]; then
285
        ARGS+=(exec -u sail)
286
        [ ! -t 0 ] && ARGS+=(-T)
287
        ARGS+=("$APP_SERVICE" php vendor/bin/phpunit "$@")
288
    else
289
        sail_is_not_running
290
    fi
291
 
399 lars 292
# Proxy the "pest" command to "php vendor/bin/pest"...
293
elif [ "$1" == "pest" ]; then
294
    shift 1
295
 
296
    if [ "$EXEC" == "yes" ]; then
297
        ARGS+=(exec -u sail)
298
        [ ! -t 0 ] && ARGS+=(-T)
299
        ARGS+=("$APP_SERVICE" php vendor/bin/pest "$@")
300
    else
301
        sail_is_not_running
302
    fi
303
 
148 lars 304
# Proxy the "pint" command to "php vendor/bin/pint"...
305
elif [ "$1" == "pint" ]; then
306
    shift 1
307
 
308
    if [ "$EXEC" == "yes" ]; then
309
        ARGS+=(exec -u sail)
310
        [ ! -t 0 ] && ARGS+=(-T)
311
        ARGS+=("$APP_SERVICE" php vendor/bin/pint "$@")
312
    else
313
        sail_is_not_running
314
    fi
315
 
316
# Proxy the "dusk" command to the "php artisan dusk" Artisan command...
317
elif [ "$1" == "dusk" ]; then
318
    shift 1
319
 
320
    if [ "$EXEC" == "yes" ]; then
321
        ARGS+=(exec -u sail)
322
        [ ! -t 0 ] && ARGS+=(-T)
323
        ARGS+=(-e "APP_URL=http://${APP_SERVICE}")
324
        ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
325
        ARGS+=("$APP_SERVICE" php artisan dusk "$@")
326
    else
327
        sail_is_not_running
328
    fi
329
 
330
# Proxy the "dusk:fails" command to the "php artisan dusk:fails" Artisan command...
331
elif [ "$1" == "dusk:fails" ]; then
332
    shift 1
333
 
334
    if [ "$EXEC" == "yes" ]; then
335
        ARGS+=(exec -u sail)
336
        [ ! -t 0 ] && ARGS+=(-T)
337
        ARGS+=(-e "APP_URL=http://${APP_SERVICE}")
338
        ARGS+=(-e "DUSK_DRIVER_URL=http://selenium:4444/wd/hub")
339
        ARGS+=("$APP_SERVICE" php artisan dusk:fails "$@")
340
    else
341
        sail_is_not_running
342
    fi
343
 
344
# Initiate a Laravel Tinker session within the application container...
345
elif [ "$1" == "tinker" ] ; then
346
    shift 1
347
 
348
    if [ "$EXEC" == "yes" ]; then
349
        ARGS+=(exec -u sail)
350
        [ ! -t 0 ] && ARGS+=(-T)
351
        ARGS+=("$APP_SERVICE" php artisan tinker)
352
    else
353
        sail_is_not_running
354
    fi
355
 
356
# Proxy Node commands to the "node" binary on the application container...
357
elif [ "$1" == "node" ]; then
358
    shift 1
359
 
360
    if [ "$EXEC" == "yes" ]; then
361
        ARGS+=(exec -u sail)
362
        [ ! -t 0 ] && ARGS+=(-T)
363
        ARGS+=("$APP_SERVICE" node "$@")
364
    else
365
        sail_is_not_running
366
    fi
367
 
368
# Proxy NPM commands to the "npm" binary on the application container...
369
elif [ "$1" == "npm" ]; then
370
    shift 1
371
 
372
    if [ "$EXEC" == "yes" ]; then
373
        ARGS+=(exec -u sail)
374
        [ ! -t 0 ] && ARGS+=(-T)
375
        ARGS+=("$APP_SERVICE" npm "$@")
376
    else
377
        sail_is_not_running
378
    fi
379
 
380
# Proxy NPX commands to the "npx" binary on the application container...
381
elif [ "$1" == "npx" ]; then
382
    shift 1
383
 
384
    if [ "$EXEC" == "yes" ]; then
385
        ARGS+=(exec -u sail)
386
        [ ! -t 0 ] && ARGS+=(-T)
387
        ARGS+=("$APP_SERVICE" npx "$@")
388
    else
389
        sail_is_not_running
390
    fi
391
 
392
# Proxy YARN commands to the "yarn" binary on the application container...
393
elif [ "$1" == "yarn" ]; then
394
    shift 1
395
 
396
    if [ "$EXEC" == "yes" ]; then
397
        ARGS+=(exec -u sail)
398
        [ ! -t 0 ] && ARGS+=(-T)
399
        ARGS+=("$APP_SERVICE" yarn "$@")
400
    else
401
        sail_is_not_running
402
    fi
403
 
404
# Initiate a MySQL CLI terminal session within the "mysql" container...
405
elif [ "$1" == "mysql" ]; then
406
    shift 1
407
 
408
    if [ "$EXEC" == "yes" ]; then
409
        ARGS+=(exec)
410
        [ ! -t 0 ] && ARGS+=(-T)
411
        ARGS+=(mysql bash -c)
412
        ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}")
413
    else
414
        sail_is_not_running
415
    fi
416
 
417
# Initiate a MySQL CLI terminal session within the "mariadb" container...
418
elif [ "$1" == "mariadb" ]; then
419
    shift 1
420
 
421
    if [ "$EXEC" == "yes" ]; then
422
        ARGS+=(exec)
423
        [ ! -t 0 ] && ARGS+=(-T)
424
        ARGS+=(mariadb bash -c)
425
        ARGS+=("MYSQL_PWD=\${MYSQL_PASSWORD} mysql -u \${MYSQL_USER} \${MYSQL_DATABASE}")
426
    else
427
        sail_is_not_running
428
    fi
429
 
430
# Initiate a PostgreSQL CLI terminal session within the "pgsql" container...
431
elif [ "$1" == "psql" ]; then
432
    shift 1
433
 
434
    if [ "$EXEC" == "yes" ]; then
435
        ARGS+=(exec)
436
        [ ! -t 0 ] && ARGS+=(-T)
437
        ARGS+=(pgsql bash -c)
438
        ARGS+=("PGPASSWORD=\${PGPASSWORD} psql -U \${POSTGRES_USER} \${POSTGRES_DB}")
439
    else
440
        sail_is_not_running
441
    fi
442
 
443
# Initiate a Bash shell within the application container...
444
elif [ "$1" == "shell" ] || [ "$1" == "bash" ]; then
445
    shift 1
446
 
447
    if [ "$EXEC" == "yes" ]; then
448
        ARGS+=(exec -u sail)
449
        [ ! -t 0 ] && ARGS+=(-T)
450
        ARGS+=("$APP_SERVICE" bash "$@")
451
    else
452
        sail_is_not_running
453
    fi
454
 
455
# Initiate a root user Bash shell within the application container...
456
elif [ "$1" == "root-shell" ] || [ "$1" == "root-bash" ]; then
457
    shift 1
458
 
459
    if [ "$EXEC" == "yes" ]; then
460
        ARGS+=(exec)
461
        [ ! -t 0 ] && ARGS+=(-T)
462
        ARGS+=("$APP_SERVICE" bash "$@")
463
    else
464
        sail_is_not_running
465
    fi
466
 
467
# Initiate a Redis CLI terminal session within the "redis" container...
468
elif [ "$1" == "redis" ] ; then
469
    shift 1
470
 
471
    if [ "$EXEC" == "yes" ]; then
472
        ARGS+=(exec)
473
        [ ! -t 0 ] && ARGS+=(-T)
474
        ARGS+=(redis redis-cli)
475
    else
476
        sail_is_not_running
477
    fi
478
 
479
# Share the site...
480
elif [ "$1" == "share" ]; then
481
    shift 1
482
 
483
    if [ "$EXEC" == "yes" ]; then
484
        docker run --init --rm -p "$SAIL_SHARE_DASHBOARD":4040 -t beyondcodegmbh/expose-server:latest share http://host.docker.internal:"$APP_PORT" \
485
            --server-host="$SAIL_SHARE_SERVER_HOST" \
486
            --server-port="$SAIL_SHARE_SERVER_PORT" \
487
            --auth="$SAIL_SHARE_TOKEN" \
488
            --subdomain="$SAIL_SHARE_SUBDOMAIN" \
399 lars 489
            --domain="$SAIL_SHARE_DOMAIN" \
148 lars 490
            "$@"
491
 
492
        exit
493
    else
494
        sail_is_not_running
495
    fi
496
 
497
# Pass unknown commands to the "docker-compose" binary...
498
else
499
    ARGS+=("$@")
500
fi
501
 
502
# Run Docker Compose with the defined arguments...
503
"${DOCKER_COMPOSE[@]}" "${ARGS[@]}"