30 lines
1.2 KiB
Bash
Executable File
30 lines
1.2 KiB
Bash
Executable File
#!/usr/bin/env sh
|
|
# latest-version.sh — 查詢套件最新且穩定的版本號。
|
|
# 用法: latest-version.sh <ecosystem> <name>
|
|
# ecosystem: nodejs | python | dotnet
|
|
# 輸出: 版本號一行;查不到 exit 4
|
|
set -u
|
|
eco="${1:?ecosystem required (nodejs|python|dotnet)}"
|
|
name="${2:?package name required}"
|
|
|
|
case "$eco" in
|
|
nodejs)
|
|
v=$(curl -sSf "https://registry.npmjs.org/$name/latest" 2>/dev/null \
|
|
| python3 2>/dev/null -c 'import json,sys;print(json.load(sys.stdin).get("version",""))') ;;
|
|
python)
|
|
v=$(curl -sSf "https://pypi.org/pypi/$name/json" 2>/dev/null \
|
|
| python3 2>/dev/null -c 'import json,sys;print(json.load(sys.stdin)["info"]["version"])') ;;
|
|
dotnet)
|
|
lname=$(printf '%s' "$name" | tr '[:upper:]' '[:lower:]')
|
|
v=$(curl -sSf "https://api.nuget.org/v3-flatcontainer/$lname/index.json" 2>/dev/null \
|
|
| python3 2>/dev/null -c '
|
|
import json,sys,re
|
|
vs=json.load(sys.stdin).get("versions",[])
|
|
stable=[x for x in vs if re.match(r"^[0-9]+(\.[0-9]+)*$",x)]
|
|
print((stable or vs)[-1] if (stable or vs) else "")') ;;
|
|
*) echo "unknown ecosystem: $eco" >&2; exit 2 ;;
|
|
esac
|
|
|
|
[ -n "${v:-}" ] || { echo "not found: $eco/$name" >&2; exit 4; }
|
|
printf '%s\n' "$v"
|