From 05734608099aac415a05d09e1d1d3118fae8e9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimondas=20Rimkevi=C4=8Dius=20=28aka=20MekDrop=29?= Date: Sat, 28 Mar 2026 06:44:56 +0200 Subject: [PATCH 1/2] feat: resolve needed php version from composer metadata --- bin/resolve-php-version.sh | 105 +++++++++++++++++++++++++++++++++++++ bin/setup.sh | 22 +++----- 2 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 bin/resolve-php-version.sh diff --git a/bin/resolve-php-version.sh b/bin/resolve-php-version.sh new file mode 100644 index 00000000..5aeacc26 --- /dev/null +++ b/bin/resolve-php-version.sh @@ -0,0 +1,105 @@ +#!/usr/bin/env bash + +ENGINE="$1" + +get_default_version() { + case "$1" in + "evert/phpdoc-md") + echo "7.4" + ;; + "clean/phpdoc-md") + echo "8.1" + ;; + *) + echo "ERROR: unknown engine" >&2 + return 1 + ;; + esac +} + +get_constraint_from_composer_json() { + if [ ! -f "composer.json" ]; then + return 0 + fi + + python3 - <<'PY' +import json + +try: + with open("composer.json", "r", encoding="utf-8") as file: + data = json.load(file) +except Exception: + raise SystemExit(0) + +require = data.get("require", {}) +if isinstance(require, dict): + php_constraint = require.get("php") + if isinstance(php_constraint, str) and php_constraint.strip(): + print(php_constraint.strip()) + raise SystemExit(0) + +config = data.get("config", {}) +if isinstance(config, dict): + platform = config.get("platform", {}) + if isinstance(platform, dict): + php_constraint = platform.get("php") + if isinstance(php_constraint, str) and php_constraint.strip(): + print(php_constraint.strip()) +PY +} + +get_constraint_from_composer_lock() { + if [ ! -f "composer.lock" ]; then + return 0 + fi + + python3 - <<'PY' +import json + +try: + with open("composer.lock", "r", encoding="utf-8") as file: + data = json.load(file) +except Exception: + raise SystemExit(0) + +for key in ("platform", "platform-overrides"): + values = data.get(key, {}) + if isinstance(values, dict): + php_constraint = values.get("php") + if isinstance(php_constraint, str) and php_constraint.strip(): + print(php_constraint.strip()) + raise SystemExit(0) +PY +} + +normalize_constraint_to_version() { + local php_constraint + php_constraint="$1" + + python3 - "$php_constraint" <<'PY' +import re +import sys + +constraint = sys.argv[1] +match = re.search(r'(\d+)\.(\d+)', constraint) +if match: + print(f"{int(match.group(1))}.{int(match.group(2))}") +PY +} + +DEFAULT_VERSION=$(get_default_version "$ENGINE") || exit 1 +DETECTED_CONSTRAINT=$(get_constraint_from_composer_json) + +if [ -z "$DETECTED_CONSTRAINT" ]; then + DETECTED_CONSTRAINT=$(get_constraint_from_composer_lock) +fi + +if [ -n "$DETECTED_CONSTRAINT" ]; then + DETECTED_VERSION=$(normalize_constraint_to_version "$DETECTED_CONSTRAINT") +fi + +if [ -n "$DETECTED_VERSION" ]; then + echo "$DETECTED_VERSION" +else + echo "$DEFAULT_VERSION" +fi diff --git a/bin/setup.sh b/bin/setup.sh index a24f4623..b5b31a5e 100644 --- a/bin/setup.sh +++ b/bin/setup.sh @@ -8,23 +8,15 @@ NEW_WIKI_CHECKOUT_PATH="${RUNNER_TEMP}/new-wiki-${ACTION_SUFFIX}" TMP_BRANCH_MAP_FILE="${RUNNER_TEMP}/branches-map-${ACTION_SUFFIX}" ACTION_BIN_PATH="${GITHUB_ACTION_PATH}/bin" -if [ "$ENGINE" == "evert/phpdoc-md" ]; then - NEEDED_PHP_VERSION=7.4 -elif [ "$ENGINE" == "clean/phpdoc-md" ]; then - NEEDED_PHP_VERSION=8.1 -else - echo 'ERROR: unknown engine' +NEEDED_PHP_VERSION=$(bash "$ACTION_BIN_PATH/resolve-php-version.sh" "$ENGINE") || exit 1 - exit 1 -fi; - -echo "OLD_WIKI_CHECKOUT_PATH=$OLD_WIKI_CHECKOUT_PATH" >> $GITHUB_ENV -echo "NEW_WIKI_CHECKOUT_PATH=$NEW_WIKI_CHECKOUT_PATH" >> $GITHUB_ENV -echo "NEEDED_PHP_VERSION=$NEEDED_PHP_VERSION" >> $GITHUB_ENV -echo "ACTION_BIN_PATH=$ACTION_BIN_PATH" >> $GITHUB_ENV -echo "TMP_BRANCH_MAP_FILE=$TMP_BRANCH_MAP_FILE" >> $GITHUB_ENV +echo "OLD_WIKI_CHECKOUT_PATH=$OLD_WIKI_CHECKOUT_PATH" >> "$GITHUB_ENV" +echo "NEW_WIKI_CHECKOUT_PATH=$NEW_WIKI_CHECKOUT_PATH" >> "$GITHUB_ENV" +echo "NEEDED_PHP_VERSION=$NEEDED_PHP_VERSION" >> "$GITHUB_ENV" +echo "ACTION_BIN_PATH=$ACTION_BIN_PATH" >> "$GITHUB_ENV" +echo "TMP_BRANCH_MAP_FILE=$TMP_BRANCH_MAP_FILE" >> "$GITHUB_ENV" rm -rf "$OLD_WIKI_CHECKOUT_PATH" || true rm -rf "$NEW_WIKI_CHECKOUT_PATH" || true -mkdir -p "$NEW_WIKI_CHECKOUT_PATH" \ No newline at end of file +mkdir -p "$NEW_WIKI_CHECKOUT_PATH" From 6a4da36bacb67b9520938ccd8a6c4ba9254d27cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimondas=20Rimkevi=C4=8Dius=20=28aka=20MekDrop=29?= Date: Sat, 28 Mar 2026 06:53:11 +0200 Subject: [PATCH 2/2] refactor: replace python usage in php resolver with node --- bin/resolve-php-version.sh | 95 +++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/bin/resolve-php-version.sh b/bin/resolve-php-version.sh index 5aeacc26..00bfd353 100644 --- a/bin/resolve-php-version.sh +++ b/bin/resolve-php-version.sh @@ -2,6 +2,10 @@ ENGINE="$1" +has_node() { + command -v node > /dev/null 2>&1 +} + get_default_version() { case "$1" in "evert/phpdoc-md") @@ -18,73 +22,60 @@ get_default_version() { } get_constraint_from_composer_json() { - if [ ! -f "composer.json" ]; then + if [ ! -f "composer.json" ] || ! has_node; then return 0 fi - python3 - <<'PY' -import json - -try: - with open("composer.json", "r", encoding="utf-8") as file: - data = json.load(file) -except Exception: - raise SystemExit(0) - -require = data.get("require", {}) -if isinstance(require, dict): - php_constraint = require.get("php") - if isinstance(php_constraint, str) and php_constraint.strip(): - print(php_constraint.strip()) - raise SystemExit(0) - -config = data.get("config", {}) -if isinstance(config, dict): - platform = config.get("platform", {}) - if isinstance(platform, dict): - php_constraint = platform.get("php") - if isinstance(php_constraint, str) and php_constraint.strip(): - print(php_constraint.strip()) -PY + node -e " +const fs = require('fs'); +try { + const data = JSON.parse(fs.readFileSync('composer.json', 'utf8')); + const requirePhp = data?.require?.php; + if (typeof requirePhp === 'string' && requirePhp.trim() !== '') { + process.stdout.write(requirePhp.trim()); + process.exit(0); + } + const platformPhp = data?.config?.platform?.php; + if (typeof platformPhp === 'string' && platformPhp.trim() !== '') { + process.stdout.write(platformPhp.trim()); + } +} catch (_) {} +" } get_constraint_from_composer_lock() { - if [ ! -f "composer.lock" ]; then + if [ ! -f "composer.lock" ] || ! has_node; then return 0 fi - python3 - <<'PY' -import json - -try: - with open("composer.lock", "r", encoding="utf-8") as file: - data = json.load(file) -except Exception: - raise SystemExit(0) - -for key in ("platform", "platform-overrides"): - values = data.get(key, {}) - if isinstance(values, dict): - php_constraint = values.get("php") - if isinstance(php_constraint, str) and php_constraint.strip(): - print(php_constraint.strip()) - raise SystemExit(0) -PY + node -e " +const fs = require('fs'); +try { + const data = JSON.parse(fs.readFileSync('composer.lock', 'utf8')); + const platformPhp = data?.platform?.php; + if (typeof platformPhp === 'string' && platformPhp.trim() !== '') { + process.stdout.write(platformPhp.trim()); + process.exit(0); + } + const overriddenPhp = data?.['platform-overrides']?.php; + if (typeof overriddenPhp === 'string' && overriddenPhp.trim() !== '') { + process.stdout.write(overriddenPhp.trim()); + } +} catch (_) {} +" } normalize_constraint_to_version() { local php_constraint php_constraint="$1" - python3 - "$php_constraint" <<'PY' -import re -import sys - -constraint = sys.argv[1] -match = re.search(r'(\d+)\.(\d+)', constraint) -if match: - print(f"{int(match.group(1))}.{int(match.group(2))}") -PY + if [[ "$php_constraint" =~ ([0-9]+)\.([0-9]+) ]]; then + local major + local minor + major="${BASH_REMATCH[1]}" + minor="${BASH_REMATCH[2]}" + echo "$((10#$major)).$((10#$minor))" + fi } DEFAULT_VERSION=$(get_default_version "$ENGINE") || exit 1