Skip to content

Commit 6a4da36

Browse files
committed
refactor: replace python usage in php resolver with node
1 parent 0573460 commit 6a4da36

1 file changed

Lines changed: 43 additions & 52 deletions

File tree

bin/resolve-php-version.sh

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
ENGINE="$1"
44

5+
has_node() {
6+
command -v node > /dev/null 2>&1
7+
}
8+
59
get_default_version() {
610
case "$1" in
711
"evert/phpdoc-md")
@@ -18,73 +22,60 @@ get_default_version() {
1822
}
1923

2024
get_constraint_from_composer_json() {
21-
if [ ! -f "composer.json" ]; then
25+
if [ ! -f "composer.json" ] || ! has_node; then
2226
return 0
2327
fi
2428

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
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+
"
4944
}
5045

5146
get_constraint_from_composer_lock() {
52-
if [ ! -f "composer.lock" ]; then
47+
if [ ! -f "composer.lock" ] || ! has_node; then
5348
return 0
5449
fi
5550

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
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+
"
7366
}
7467

7568
normalize_constraint_to_version() {
7669
local php_constraint
7770
php_constraint="$1"
7871

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
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
8879
}
8980

9081
DEFAULT_VERSION=$(get_default_version "$ENGINE") || exit 1

0 commit comments

Comments
 (0)