|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +ENGINE="$1" |
| 4 | + |
| 5 | +get_default_version() { |
| 6 | + case "$1" in |
| 7 | + "evert/phpdoc-md") |
| 8 | + echo "7.4" |
| 9 | + ;; |
| 10 | + "clean/phpdoc-md") |
| 11 | + echo "8.1" |
| 12 | + ;; |
| 13 | + *) |
| 14 | + echo "ERROR: unknown engine" >&2 |
| 15 | + return 1 |
| 16 | + ;; |
| 17 | + esac |
| 18 | +} |
| 19 | + |
| 20 | +get_constraint_from_composer_json() { |
| 21 | + if [ ! -f "composer.json" ]; then |
| 22 | + return 0 |
| 23 | + fi |
| 24 | + |
| 25 | + python3 - <<'PY' |
| 26 | +import json |
| 27 | +
|
| 28 | +try: |
| 29 | + with open("composer.json", "r", encoding="utf-8") as file: |
| 30 | + data = json.load(file) |
| 31 | +except Exception: |
| 32 | + raise SystemExit(0) |
| 33 | +
|
| 34 | +require = data.get("require", {}) |
| 35 | +if isinstance(require, dict): |
| 36 | + php_constraint = require.get("php") |
| 37 | + if isinstance(php_constraint, str) and php_constraint.strip(): |
| 38 | + print(php_constraint.strip()) |
| 39 | + raise SystemExit(0) |
| 40 | +
|
| 41 | +config = data.get("config", {}) |
| 42 | +if isinstance(config, dict): |
| 43 | + platform = config.get("platform", {}) |
| 44 | + if isinstance(platform, dict): |
| 45 | + php_constraint = platform.get("php") |
| 46 | + if isinstance(php_constraint, str) and php_constraint.strip(): |
| 47 | + print(php_constraint.strip()) |
| 48 | +PY |
| 49 | +} |
| 50 | + |
| 51 | +get_constraint_from_composer_lock() { |
| 52 | + if [ ! -f "composer.lock" ]; then |
| 53 | + return 0 |
| 54 | + fi |
| 55 | + |
| 56 | + python3 - <<'PY' |
| 57 | +import json |
| 58 | +
|
| 59 | +try: |
| 60 | + with open("composer.lock", "r", encoding="utf-8") as file: |
| 61 | + data = json.load(file) |
| 62 | +except Exception: |
| 63 | + raise SystemExit(0) |
| 64 | +
|
| 65 | +for key in ("platform", "platform-overrides"): |
| 66 | + values = data.get(key, {}) |
| 67 | + if isinstance(values, dict): |
| 68 | + php_constraint = values.get("php") |
| 69 | + if isinstance(php_constraint, str) and php_constraint.strip(): |
| 70 | + print(php_constraint.strip()) |
| 71 | + raise SystemExit(0) |
| 72 | +PY |
| 73 | +} |
| 74 | + |
| 75 | +normalize_constraint_to_version() { |
| 76 | + local php_constraint |
| 77 | + php_constraint="$1" |
| 78 | + |
| 79 | + python3 - "$php_constraint" <<'PY' |
| 80 | +import re |
| 81 | +import sys |
| 82 | +
|
| 83 | +constraint = sys.argv[1] |
| 84 | +match = re.search(r'(\d+)\.(\d+)', constraint) |
| 85 | +if match: |
| 86 | + print(f"{int(match.group(1))}.{int(match.group(2))}") |
| 87 | +PY |
| 88 | +} |
| 89 | + |
| 90 | +DEFAULT_VERSION=$(get_default_version "$ENGINE") || exit 1 |
| 91 | +DETECTED_CONSTRAINT=$(get_constraint_from_composer_json) |
| 92 | + |
| 93 | +if [ -z "$DETECTED_CONSTRAINT" ]; then |
| 94 | + DETECTED_CONSTRAINT=$(get_constraint_from_composer_lock) |
| 95 | +fi |
| 96 | + |
| 97 | +if [ -n "$DETECTED_CONSTRAINT" ]; then |
| 98 | + DETECTED_VERSION=$(normalize_constraint_to_version "$DETECTED_CONSTRAINT") |
| 99 | +fi |
| 100 | + |
| 101 | +if [ -n "$DETECTED_VERSION" ]; then |
| 102 | + echo "$DETECTED_VERSION" |
| 103 | +else |
| 104 | + echo "$DEFAULT_VERSION" |
| 105 | +fi |
0 commit comments