|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Orb setup for phpstan/phpstan-src: PHP toolchain + composer dependencies. |
| 5 | +# Idempotent: skips whatever the (possibly stale) snapshot already provides. |
| 6 | + |
| 7 | +log() { printf '[setup] %s\n' "$*" >&2; } |
| 8 | + |
| 9 | +# Amp runs setup either as root or as the unprivileged orb user with passwordless sudo. |
| 10 | +as_root() { |
| 11 | + if [ "$(id -u)" -eq 0 ]; then |
| 12 | + "$@" |
| 13 | + else |
| 14 | + sudo "$@" |
| 15 | + fi |
| 16 | +} |
| 17 | + |
| 18 | +PHP_VERSION=8.4 |
| 19 | + |
| 20 | +# --- base utilities ------------------------------------------------------- |
| 21 | +missing_pkgs=() |
| 22 | +command -v curl >/dev/null 2>&1 || missing_pkgs+=(ca-certificates curl) |
| 23 | +command -v unzip >/dev/null 2>&1 || missing_pkgs+=(unzip) |
| 24 | +command -v patch >/dev/null 2>&1 || missing_pkgs+=(patch) |
| 25 | +if [ "${#missing_pkgs[@]}" -gt 0 ]; then |
| 26 | + export DEBIAN_FRONTEND=noninteractive |
| 27 | + as_root apt-get update -qq |
| 28 | + as_root apt-get install -y -qq "${missing_pkgs[@]}" >/dev/null |
| 29 | +fi |
| 30 | + |
| 31 | +# --- PHP ------------------------------------------------------------------- |
| 32 | +need_php=1 |
| 33 | +if command -v php >/dev/null 2>&1 \ |
| 34 | + && php -r 'exit(version_compare(PHP_VERSION, "8.2", "<") ? 1 : 0);' 2>/dev/null \ |
| 35 | + && php -m 2>/dev/null | grep -qix '^ds$' \ |
| 36 | + && php -m 2>/dev/null | grep -qix '^mbstring$'; then |
| 37 | + need_php=0 |
| 38 | + log "PHP $(php -r 'echo PHP_VERSION;') already installed" |
| 39 | +fi |
| 40 | + |
| 41 | +if [ "$need_php" -eq 1 ]; then |
| 42 | + log "Installing PHP ${PHP_VERSION} from deb.sury.org (Debian's own PHP has no ext-ds)" |
| 43 | + export DEBIAN_FRONTEND=noninteractive |
| 44 | + as_root apt-get update -qq |
| 45 | + as_root apt-get install -y -qq ca-certificates curl gnupg >/dev/null |
| 46 | + as_root curl -fsSLo /usr/share/keyrings/deb.sury.org-php.gpg https://packages.sury.org/php/apt.gpg |
| 47 | + printf 'deb [signed-by=/usr/share/keyrings/deb.sury.org-php.gpg] https://packages.sury.org/php/ bookworm main\n' | as_root tee /etc/apt/sources.list.d/php.list > /dev/null |
| 48 | + as_root apt-get update -qq |
| 49 | + as_root apt-get install -y -qq \ |
| 50 | + "php${PHP_VERSION}-cli" \ |
| 51 | + "php${PHP_VERSION}-mbstring" \ |
| 52 | + "php${PHP_VERSION}-ds" \ |
| 53 | + "php${PHP_VERSION}-curl" \ |
| 54 | + "php${PHP_VERSION}-xml" \ |
| 55 | + >/dev/null |
| 56 | + as_root update-alternatives --set php "/usr/bin/php${PHP_VERSION}" >/dev/null 2>&1 || true |
| 57 | + log "Installed PHP $(php -r 'echo PHP_VERSION;')" |
| 58 | +fi |
| 59 | + |
| 60 | +# CI runs with memory_limit=-1; the test runner needs it for big analyses. |
| 61 | +if [ -d "/etc/php/${PHP_VERSION}/cli" ]; then |
| 62 | + printf 'memory_limit=-1\n' | as_root tee "/etc/php/${PHP_VERSION}/cli/conf.d/99-amp-memory.ini" > /dev/null |
| 63 | +fi |
| 64 | + |
| 65 | +# --- Composer --------------------------------------------------------------- |
| 66 | +if ! command -v composer >/dev/null 2>&1; then |
| 67 | + log "Installing Composer" |
| 68 | + as_root curl -fsSS https://getcomposer.org/installer | as_root php -- \ |
| 69 | + --install-dir=/usr/local/bin --filename=composer --quiet |
| 70 | +fi |
| 71 | +log "Using $(composer --version 2>/dev/null | head -1)" |
| 72 | + |
| 73 | +# --- Dependencies ----------------------------------------------------------- |
| 74 | +# composer.json pins config.platform php 8.2.99, so any >= 8.2 runtime installs |
| 75 | +# the same lockfile. Patches are applied by cweagans/composer-patches on install. |
| 76 | +log "composer install (root)" |
| 77 | +composer install --no-interaction --no-progress |
| 78 | +log "composer install (tests: paratest, phpbench, phpunit)" |
| 79 | +composer install --working-dir tests --no-interaction --no-progress |
| 80 | + |
| 81 | +log "setup complete" |
0 commit comments