Repository navigation
Expand file tree
/
Copy pathdev
More file actions
executable file
·336 lines (311 loc) · 13.3 KB
/
Copy pathdev
File metadata and controls
executable file
·336 lines (311 loc) · 13.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
#!/bin/sh
# Flipster developer entrypoint.
#
# Docker is this project's ONLY prerequisite (D10 / NFR12). Every command below runs inside a
# container: you do not need PHP, Composer, PECL, or Make on your machine. Podman works too.
#
# This is a plain POSIX shell script rather than a Makefile precisely because `make` would
# itself be a second host prerequisite. The Makefile that ships alongside is convenience only
# and simply delegates here.
set -eu
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
PHP_VERSION=${PHP_VERSION:-8.1}
BUILD_TARGET=${BUILD_TARGET:-base}
IMAGE_REPO=flipster-php
CACHE_VOLUME=flipster-composer-cache
# Created by Compose from the `name:` in docker-compose.yml. Plain `run` containers join it when it
# exists so that service hostnames such as `redis` resolve from every ./dev command, not just the ones
# that happen to go through Compose.
COMPOSE_NETWORK=flipster_default
SUPPORTED_VERSIONS="8.1 8.2 8.3 8.4 8.5"
usage() {
cat <<'EOF'
Flipster dev commands — all run inside a container.
./dev composer <args> run Composer (e.g. ./dev composer install)
./dev install shorthand for `composer install`
./dev test [args] run PHPUnit (unit suite; no infrastructure needed)
./dev test:integration run the integration suite, starting Redis via Compose
./dev down stop any Compose services this project started
./dev coverage run PHPUnit with coverage (pcov)
./dev analyse run PHPStan
./dev baseline assert the PHP 8.1 syntax floor is actually enforced
./dev cs check code style (no changes written)
./dev cs:fix apply code style fixes
./dev check cs + analyse + test, i.e. what CI runs
./dev consumer-check install the package in a scratch project and verify it as a dependency
./dev example <name> run a documented example (in-memory | flagd)
./dev shell interactive shell in the container
./dev run <cmd...> run an arbitrary command in the container
./dev php <ver> <cmd...> run a command on another PHP version
e.g. ./dev php 8.4 test
./dev build [--xdebug] (re)build the toolchain image
./dev versions list the PHP versions CI covers
./dev doctor report the detected engine and mount strategy
./dev clean remove vendor/, build artefacts, images and cache volume
Environment overrides:
PHP_VERSION PHP version to use (default 8.1 — our supported floor)
CONTAINER_ENGINE container engine binary (default: docker, then podman)
EOF
}
# --- container engine detection ---------------------------------------------------------------
if [ -n "${CONTAINER_ENGINE:-}" ]; then
ENGINE=$CONTAINER_ENGINE
# Validate the override as strictly as auto-detection validates its candidates. Without this a
# typo in CONTAINER_ENGINE surfaces much later as "No such file or directory" from inside some
# unrelated subcommand, which reads like a broken toolchain rather than a bad variable.
if ! command -v "$ENGINE" >/dev/null 2>&1; then
printf 'error: CONTAINER_ENGINE is set to "%s", which is not an executable command.\n' \
"$ENGINE" >&2
printf ' Unset it to auto-detect docker or podman.\n' >&2
exit 1
fi
elif command -v docker >/dev/null 2>&1; then
ENGINE=docker
elif command -v podman >/dev/null 2>&1; then
ENGINE=podman
else
cat >&2 <<'EOF'
error: no container engine found.
Flipster's whole toolchain runs in containers, so Docker is the only thing you need to install:
https://docs.docker.com/get-docker/
Podman is also supported. If your engine lives somewhere unusual, set CONTAINER_ENGINE.
EOF
exit 1
fi
engine_is_podman() {
case "$("$ENGINE" --version 2>/dev/null)" in
*[Pp]odman*) return 0 ;;
*) return 1 ;;
esac
}
# Whether we must force the container to run as the host UID/GID.
#
# This exists so containerised commands do not leave root-owned files in the working tree
# (NFR14). It is deliberately conditional, because forcing a UID is *harmful* in the
# environments that already handle this:
#
# macOS / Windows - the engine runs in a VM whose file sharing already maps writes back to
# the invoking user; forcing a UID can instead break tool caches.
# rootless Podman - the container's root is already mapped to the host user.
# rootful Linux - nothing maps anything; without --user, files land owned by root.
needs_user_flag() {
[ "$(uname -s)" = Linux ] || return 1
if engine_is_podman \
&& "$ENGINE" info --format '{{.Host.Security.Rootless}}' 2>/dev/null | grep -qi true; then
return 1
fi
return 0
}
# SELinux relabelling is meaningful on Linux and meaningless (or unsupported) elsewhere.
mount_opt() {
[ "$(uname -s)" = Linux ] && printf ':z'
}
# A short digest of the Dockerfile, baked into the image tag.
#
# Without this, `ensure_image` only builds when an image is *absent*, so editing the Dockerfile leaves
# every already-built version silently stale — and a stale image fails in ways that look like a code
# bug rather than a toolchain one. Including the digest means a Dockerfile change yields a new tag,
# which no longer exists, which triggers a rebuild. Automatically, and per PHP version.
dockerfile_digest() {
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$ROOT/Dockerfile" | cut -c1-8
elif command -v sha256sum >/dev/null 2>&1; then
sha256sum "$ROOT/Dockerfile" | cut -c1-8
else
# cksum is in POSIX, so there is always a fallback.
cksum "$ROOT/Dockerfile" | awk '{print $1}'
fi
}
image_tag() {
if [ "$BUILD_TARGET" = base ]; then
printf '%s:%s-%s' "$IMAGE_REPO" "$PHP_VERSION" "$(dockerfile_digest)"
else
printf '%s:%s-%s-%s' "$IMAGE_REPO" "$PHP_VERSION" "$BUILD_TARGET" "$(dockerfile_digest)"
fi
}
build_image() {
printf '==> building %s (php %s, target %s)\n' "$(image_tag)" "$PHP_VERSION" "$BUILD_TARGET" >&2
"$ENGINE" build \
--build-arg "PHP_VERSION=${PHP_VERSION}" \
--target "$BUILD_TARGET" \
--tag "$(image_tag)" \
"$ROOT"
}
ensure_image() {
"$ENGINE" image inspect "$(image_tag)" >/dev/null 2>&1 || build_image
}
# Runs a command in the container.
#
# POSIX sh has no arrays, so engine flags are appended after the caller's command and then the
# command is rotated to the end. That keeps every path correctly quoted, which matters because a
# checkout path may well contain spaces.
run_in_container() {
ensure_image
ucount=$#
set -- "$@" --rm -i
if [ -t 0 ] && [ -t 1 ]; then set -- "$@" -t; fi
set -- "$@" \
--volume "${ROOT}:/app$(mount_opt)" \
--volume "${CACHE_VOLUME}:/composer" \
--workdir /app
# Join the Compose network when it is up, so `redis` resolves by service name here too.
#
# Without this, `./dev test --testsuite integration` silently skips every integration test even though
# Redis is running — because a plain container is not on Compose's network and cannot resolve the
# hostname. The skip message then advises starting Redis, which is already started, and the user is sent
# in a circle. Fixing the network is better than documenting the trap.
if "$ENGINE" network inspect "$COMPOSE_NETWORK" >/dev/null 2>&1; then
set -- "$@" --network "$COMPOSE_NETWORK"
fi
if needs_user_flag; then
set -- "$@" --user "$(id -u):$(id -g)" --env HOME=/tmp
fi
set -- "$@" "$(image_tag)"
i=0
while [ "$i" -lt "$ucount" ]; do
first=$1
shift
set -- "$@" "$first"
i=$((i + 1))
done
"$ENGINE" run "$@"
}
# Compose is used ONLY for genuinely multi-service work: the Redis-backed Ganesha tests and the
# flagd example. Everyday commands use a plain `run` (see run_in_container) because that behaves
# identically across engines and Compose implementations.
#
# The tradeoff is real and worth stating: a plain `run` container is not attached to the Compose
# network, so it cannot reach `redis` by service name. Rather than paper over that with a
# host-gateway hostname that differs per engine, the integration path goes through Compose so
# service discovery just works.
compose() {
# The cache volume is declared `external` in docker-compose.yml, so it must exist first.
"$ENGINE" volume create "$CACHE_VOLUME" >/dev/null 2>&1 || true
"$ENGINE" compose --file "$ROOT/docker-compose.yml" "$@"
}
ensure_deps() {
if [ ! -f "$ROOT/vendor/autoload.php" ]; then
printf '==> vendor/ is missing; installing dependencies first\n' >&2
run_in_container composer install --no-interaction --no-progress
fi
}
# --- command dispatch -------------------------------------------------------------------------
cmd=${1:-help}
[ $# -gt 0 ] && shift || true
case "$cmd" in
php)
[ $# -ge 1 ] || { echo "usage: ./dev php <version> <command...>" >&2; exit 2; }
PHP_VERSION=$1
shift
export PHP_VERSION
exec "$ROOT/dev" "$@"
;;
build)
if [ "${1:-}" = "--xdebug" ]; then BUILD_TARGET=xdebug; shift; fi
build_image
;;
composer) run_in_container composer "$@" ;;
install) run_in_container composer install --no-interaction --no-progress "$@" ;;
test) ensure_deps; run_in_container vendor/bin/phpunit --display-skipped "$@" ;;
coverage)
ensure_deps
run_in_container vendor/bin/phpunit \
--coverage-text --coverage-clover=.build/clover.xml "$@"
;;
analyse|analyze) ensure_deps; run_in_container vendor/bin/phpstan analyse --no-progress --memory-limit=512M "$@" ;;
baseline)
# Proves the PHP 8.1 guard is real (NFR1b): analysing the 8.2-syntax fixture MUST fail.
# `set -e` would abort on the expected failure, hence the explicit `if`.
ensure_deps
printf '==> asserting PHPStan rejects PHP 8.2+ syntax at our 8.1 floor\n' >&2
if run_in_container vendor/bin/phpstan analyse --no-progress --memory-limit=512M \
--configuration tools/baseline-fixture/phpstan.neon >/dev/null 2>&1; then
printf 'FAIL: PHPStan accepted `readonly class` despite phpVersion 80100.\n' >&2
printf ' The 8.1 baseline guard is not protecting anything. See NFR1b.\n' >&2
exit 1
fi
printf '==> ok: 8.2+ syntax is rejected, the floor is enforced\n' >&2
;;
cs) ensure_deps; run_in_container vendor/bin/php-cs-fixer fix --dry-run --diff "$@" ;;
cs:fix|fix) ensure_deps; run_in_container vendor/bin/php-cs-fixer fix "$@" ;;
check)
ensure_deps
run_in_container vendor/bin/php-cs-fixer fix --dry-run --diff
run_in_container vendor/bin/phpstan analyse --no-progress --memory-limit=512M
"$ROOT/dev" baseline
run_in_container vendor/bin/phpunit
;;
test:integration|integration)
# Runs inside the Compose network so `redis` resolves by service name.
ensure_deps
printf '==> starting the integration profile (redis)\n' >&2
compose --profile integration up -d redis
compose --profile tools --profile integration run --rm --no-deps php \
vendor/bin/phpunit --testsuite integration "$@"
;;
down)
compose --profile integration --profile examples --profile tools down --remove-orphans
;;
example)
# Runs a file from examples/, bringing up whatever it needs. The point of these being executable is that
# the README quotes them, so a broken example is a failing command rather than a wrong document.
ensure_deps
name=${1:-}
[ -n "$name" ] || { echo "usage: ./dev example <in-memory|flagd>" >&2; exit 2; }
shift
case "$name" in
in-memory)
run_in_container php "examples/in-memory.php" "$@"
;;
flagd)
printf '==> starting flagd and redis\n' >&2
compose --profile examples up -d flagd
compose --profile integration up -d redis
compose --profile tools --profile examples --profile integration run --rm --no-deps php \
php examples/flagd.php "$@"
;;
*)
echo "unknown example: $name (expected in-memory or flagd)" >&2
exit 2
;;
esac
;;
consumer-check)
# Proves the package works as a *dependency*: the toolkit is usable with no test framework, and no
# dev-only package leaks. Neither is observable from inside this repo, where all dev deps exist.
run_in_container sh tools/verify-consumer-install.sh
;;
shell|sh) run_in_container sh "$@" ;;
run) run_in_container "$@" ;;
versions) printf '%s\n' $SUPPORTED_VERSIONS ;;
doctor)
printf 'engine : %s (%s)\n' "$ENGINE" "$("$ENGINE" --version 2>&1 | head -1)"
printf 'host os : %s\n' "$(uname -s)"
printf 'php version : %s\n' "$PHP_VERSION"
printf 'image : %s\n' "$(image_tag)"
if needs_user_flag; then
printf 'user mapping : --user %s:%s (rootful Linux engine)\n' "$(id -u)" "$(id -g)"
else
printf 'user mapping : none needed (engine maps writes to the host user)\n'
fi
printf 'mount option : %s\n' "$(mount_opt)none"
;;
clean)
rm -rf "$ROOT/vendor" "$ROOT/.build"
"$ENGINE" volume rm "$CACHE_VOLUME" >/dev/null 2>&1 || true
# Matched by prefix rather than enumerated, since tags now carry a Dockerfile digest and any
# number of stale ones may have accumulated.
"$ENGINE" images --format '{{.Repository}}:{{.Tag}}' 2>/dev/null \
| grep "$IMAGE_REPO:" \
| while read -r image; do
"$ENGINE" image rm "$image" >/dev/null 2>&1 || true
done
printf '==> cleaned\n' >&2
;;
help|-h|--help) usage ;;
*)
printf 'unknown command: %s\n\n' "$cmd" >&2
usage >&2
exit 2
;;
esac