Subversion-Projekte lars-tiefland.laravel_shop

Revision

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