Skip to content

Commit 0573460

Browse files
committed
feat: resolve needed php version from composer metadata
1 parent de707fe commit 0573460

2 files changed

Lines changed: 112 additions & 15 deletions

File tree

bin/resolve-php-version.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

bin/setup.sh

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@ NEW_WIKI_CHECKOUT_PATH="${RUNNER_TEMP}/new-wiki-${ACTION_SUFFIX}"
88
TMP_BRANCH_MAP_FILE="${RUNNER_TEMP}/branches-map-${ACTION_SUFFIX}"
99
ACTION_BIN_PATH="${GITHUB_ACTION_PATH}/bin"
1010

11-
if [ "$ENGINE" == "evert/phpdoc-md" ]; then
12-
NEEDED_PHP_VERSION=7.4
13-
elif [ "$ENGINE" == "clean/phpdoc-md" ]; then
14-
NEEDED_PHP_VERSION=8.1
15-
else
16-
echo 'ERROR: unknown engine'
11+
NEEDED_PHP_VERSION=$(bash "$ACTION_BIN_PATH/resolve-php-version.sh" "$ENGINE") || exit 1
1712

18-
exit 1
19-
fi;
20-
21-
echo "OLD_WIKI_CHECKOUT_PATH=$OLD_WIKI_CHECKOUT_PATH" >> $GITHUB_ENV
22-
echo "NEW_WIKI_CHECKOUT_PATH=$NEW_WIKI_CHECKOUT_PATH" >> $GITHUB_ENV
23-
echo "NEEDED_PHP_VERSION=$NEEDED_PHP_VERSION" >> $GITHUB_ENV
24-
echo "ACTION_BIN_PATH=$ACTION_BIN_PATH" >> $GITHUB_ENV
25-
echo "TMP_BRANCH_MAP_FILE=$TMP_BRANCH_MAP_FILE" >> $GITHUB_ENV
13+
echo "OLD_WIKI_CHECKOUT_PATH=$OLD_WIKI_CHECKOUT_PATH" >> "$GITHUB_ENV"
14+
echo "NEW_WIKI_CHECKOUT_PATH=$NEW_WIKI_CHECKOUT_PATH" >> "$GITHUB_ENV"
15+
echo "NEEDED_PHP_VERSION=$NEEDED_PHP_VERSION" >> "$GITHUB_ENV"
16+
echo "ACTION_BIN_PATH=$ACTION_BIN_PATH" >> "$GITHUB_ENV"
17+
echo "TMP_BRANCH_MAP_FILE=$TMP_BRANCH_MAP_FILE" >> "$GITHUB_ENV"
2618

2719
rm -rf "$OLD_WIKI_CHECKOUT_PATH" || true
2820
rm -rf "$NEW_WIKI_CHECKOUT_PATH" || true
2921

30-
mkdir -p "$NEW_WIKI_CHECKOUT_PATH"
22+
mkdir -p "$NEW_WIKI_CHECKOUT_PATH"

0 commit comments

Comments
 (0)