|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +ENGINE="$1" |
| 4 | + |
| 5 | +has_node() { |
| 6 | + command -v node > /dev/null 2>&1 |
| 7 | +} |
| 8 | + |
| 9 | +get_default_version() { |
| 10 | + case "$1" in |
| 11 | + "evert/phpdoc-md") |
| 12 | + echo "7.4" |
| 13 | + ;; |
| 14 | + "clean/phpdoc-md") |
| 15 | + echo "8.1" |
| 16 | + ;; |
| 17 | + *) |
| 18 | + echo "ERROR: unknown engine" >&2 |
| 19 | + return 1 |
| 20 | + ;; |
| 21 | + esac |
| 22 | +} |
| 23 | + |
| 24 | +get_constraint_from_composer_json() { |
| 25 | + if [ ! -f "composer.json" ] || ! has_node; then |
| 26 | + return 0 |
| 27 | + fi |
| 28 | + |
| 29 | + node -e " |
| 30 | +const fs = require('fs'); |
| 31 | +try { |
| 32 | + const data = JSON.parse(fs.readFileSync('composer.json', 'utf8')); |
| 33 | + const requirePhp = data?.require?.php; |
| 34 | + if (typeof requirePhp === 'string' && requirePhp.trim() !== '') { |
| 35 | + process.stdout.write(requirePhp.trim()); |
| 36 | + process.exit(0); |
| 37 | + } |
| 38 | + const platformPhp = data?.config?.platform?.php; |
| 39 | + if (typeof platformPhp === 'string' && platformPhp.trim() !== '') { |
| 40 | + process.stdout.write(platformPhp.trim()); |
| 41 | + } |
| 42 | +} catch (_) {} |
| 43 | +" |
| 44 | +} |
| 45 | + |
| 46 | +get_constraint_from_composer_lock() { |
| 47 | + if [ ! -f "composer.lock" ] || ! has_node; then |
| 48 | + return 0 |
| 49 | + fi |
| 50 | + |
| 51 | + node -e " |
| 52 | +const fs = require('fs'); |
| 53 | +try { |
| 54 | + const data = JSON.parse(fs.readFileSync('composer.lock', 'utf8')); |
| 55 | + const platformPhp = data?.platform?.php; |
| 56 | + if (typeof platformPhp === 'string' && platformPhp.trim() !== '') { |
| 57 | + process.stdout.write(platformPhp.trim()); |
| 58 | + process.exit(0); |
| 59 | + } |
| 60 | + const overriddenPhp = data?.['platform-overrides']?.php; |
| 61 | + if (typeof overriddenPhp === 'string' && overriddenPhp.trim() !== '') { |
| 62 | + process.stdout.write(overriddenPhp.trim()); |
| 63 | + } |
| 64 | +} catch (_) {} |
| 65 | +" |
| 66 | +} |
| 67 | + |
| 68 | +normalize_constraint_to_version() { |
| 69 | + local php_constraint |
| 70 | + php_constraint="$1" |
| 71 | + |
| 72 | + if [[ "$php_constraint" =~ ([0-9]+)\.([0-9]+) ]]; then |
| 73 | + local major |
| 74 | + local minor |
| 75 | + major="${BASH_REMATCH[1]}" |
| 76 | + minor="${BASH_REMATCH[2]}" |
| 77 | + echo "$((10#$major)).$((10#$minor))" |
| 78 | + fi |
| 79 | +} |
| 80 | + |
| 81 | +DEFAULT_VERSION=$(get_default_version "$ENGINE") || exit 1 |
| 82 | +DETECTED_CONSTRAINT=$(get_constraint_from_composer_json) |
| 83 | + |
| 84 | +if [ -z "$DETECTED_CONSTRAINT" ]; then |
| 85 | + DETECTED_CONSTRAINT=$(get_constraint_from_composer_lock) |
| 86 | +fi |
| 87 | + |
| 88 | +if [ -n "$DETECTED_CONSTRAINT" ]; then |
| 89 | + DETECTED_VERSION=$(normalize_constraint_to_version "$DETECTED_CONSTRAINT") |
| 90 | +fi |
| 91 | + |
| 92 | +if [ -n "$DETECTED_VERSION" ]; then |
| 93 | + echo "$DETECTED_VERSION" |
| 94 | +else |
| 95 | + echo "$DEFAULT_VERSION" |
| 96 | +fi |
0 commit comments