|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Prepares an Amp orb (Debian 12) to build, test, and analyse this repository. |
| 3 | +# Idempotent: a warm run skips installs that are already in place. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +cd "$(dirname "$0")/.." |
| 7 | + |
| 8 | +step() { |
| 9 | + local name="$1" |
| 10 | + shift |
| 11 | + local start=$SECONDS |
| 12 | + echo "==> $name" |
| 13 | + "$@" |
| 14 | + echo "<== $name ($((SECONDS - start))s)" |
| 15 | +} |
| 16 | + |
| 17 | +# PHP 8.4 runs everything: the library itself (platform pinned to 7.4.6 in |
| 18 | +# composer.json), and tools/phplrt, whose grammar compiler requires 8.4. |
| 19 | +PHP_VERSION=8.4 |
| 20 | +PHP_PACKAGES=( |
| 21 | + "php$PHP_VERSION-cli" |
| 22 | + "php$PHP_VERSION-mbstring" |
| 23 | + "php$PHP_VERSION-xml" |
| 24 | + "php$PHP_VERSION-curl" |
| 25 | + "php$PHP_VERSION-zip" |
| 26 | +) |
| 27 | + |
| 28 | +install_php() { |
| 29 | + local missing=() |
| 30 | + for pkg in "${PHP_PACKAGES[@]}"; do |
| 31 | + dpkg-query -W -f='${Status}' "$pkg" 2>/dev/null | grep -q 'install ok installed' || missing+=("$pkg") |
| 32 | + done |
| 33 | + if [ ${#missing[@]} -eq 0 ]; then |
| 34 | + echo "PHP $PHP_VERSION packages already installed" |
| 35 | + return |
| 36 | + fi |
| 37 | + |
| 38 | + if [ ! -f /etc/apt/sources.list.d/php-sury.list ]; then |
| 39 | + sudo install -d -m 0755 /etc/apt/keyrings |
| 40 | + curl -fsSL https://packages.sury.org/php/apt.gpg | sudo tee /etc/apt/keyrings/php-sury.gpg >/dev/null |
| 41 | + echo "deb [signed-by=/etc/apt/keyrings/php-sury.gpg] https://packages.sury.org/php/ $(. /etc/os-release && echo "$VERSION_CODENAME") main" \ |
| 42 | + | sudo tee /etc/apt/sources.list.d/php-sury.list >/dev/null |
| 43 | + fi |
| 44 | + sudo apt-get update -qq |
| 45 | + sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends "${missing[@]}" |
| 46 | +} |
| 47 | + |
| 48 | +install_composer() { |
| 49 | + if command -v composer >/dev/null 2>&1; then |
| 50 | + echo "Composer already installed: $(composer --version 2>/dev/null)" |
| 51 | + return |
| 52 | + fi |
| 53 | + local tmp |
| 54 | + tmp="$(mktemp -d)" |
| 55 | + curl -fsSL https://getcomposer.org/installer -o "$tmp/composer-setup.php" |
| 56 | + php "$tmp/composer-setup.php" --quiet --install-dir="$tmp" --filename=composer |
| 57 | + sudo install -m 0755 "$tmp/composer" /usr/local/bin/composer |
| 58 | + rm -rf "$tmp" |
| 59 | +} |
| 60 | + |
| 61 | +# composer.lock is git-ignored, so the first run resolves dependencies like CI |
| 62 | +# does; later runs reuse the restored lock and vendor directory. |
| 63 | +install_dependencies() { |
| 64 | + composer install --no-interaction --no-progress |
| 65 | +} |
| 66 | + |
| 67 | +# Coding standard used by `make cs` / `make cs-fix`. |
| 68 | +install_build_cs() { |
| 69 | + if [ ! -d build-cs/.git ]; then |
| 70 | + git clone --quiet --branch 2.x https://github.com/phpstan/build-cs.git build-cs |
| 71 | + fi |
| 72 | + composer install --no-interaction --no-progress --working-dir build-cs |
| 73 | +} |
| 74 | + |
| 75 | +# Grammar toolchain used by FuzzyTest and GrammarSyncTest (`make grammars-install`). |
| 76 | +install_grammars() { |
| 77 | + composer install --no-interaction --no-progress --working-dir tools/phplrt |
| 78 | +} |
| 79 | + |
| 80 | +step "Install PHP $PHP_VERSION" install_php |
| 81 | +step "Install Composer" install_composer |
| 82 | +step "Install dependencies" install_dependencies |
| 83 | +step "Install build-cs" install_build_cs |
| 84 | +step "Install grammar toolchain" install_grammars |
0 commit comments