184 lines
6.2 KiB
Bash
Executable File
184 lines
6.2 KiB
Bash
Executable File
#!/bin/sh
|
|
# Build and publish MG Utility releases.
|
|
#
|
|
# Without flags: publish the next Dev build for the latest stable line.
|
|
# With --stable: publish the next Stable release, then its first Dev build.
|
|
set -eu
|
|
|
|
ROOT=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
|
STABLE=0
|
|
DRY_RUN=0
|
|
|
|
for arg in "$@"; do
|
|
case "$arg" in
|
|
--stable) STABLE=1 ;;
|
|
--dry-run) DRY_RUN=1 ;;
|
|
--help|-h)
|
|
printf '%s\n' 'Usage: ./scripts/release.sh [--stable] [--dry-run]'
|
|
printf '%s\n' ' (default) publish the next Dev build'
|
|
printf '%s\n' ' --stable publish the next Stable build and its first Dev build'
|
|
printf '%s\n' ' --dry-run calculate and print versions without building or publishing'
|
|
exit 0
|
|
;;
|
|
*) echo "Unknown option: $arg" >&2; exit 2 ;;
|
|
esac
|
|
done
|
|
|
|
API=${GITEA_API_URL:-https://git.molberg.cloud/api/v1/repos/alex/mg-utility}
|
|
|
|
if [ "$DRY_RUN" -eq 0 ] && [ -z "${GITEA_TOKEN:-}" ]; then
|
|
echo "Use scripts/release-dev.sh to obtain GITEA_TOKEN securely from Vault" >&2
|
|
exit 2
|
|
fi
|
|
|
|
api_curl() {
|
|
case "${GITEA_TOKEN:-}" in
|
|
*[!a-zA-Z0-9]*) echo "Invalid Gitea token format" >&2; return 2 ;;
|
|
esac
|
|
printf 'header = "Authorization: token %s"\n' "$GITEA_TOKEN" |
|
|
curl --config - -fsS -A 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36' "$@"
|
|
}
|
|
|
|
RELEASES_JSON=$(mktemp)
|
|
PLAN_JSON=$(mktemp)
|
|
trap 'rm -f "$RELEASES_JSON" "$PLAN_JSON"' EXIT HUP INT TERM
|
|
|
|
if [ -n "${GITEA_TOKEN:-}" ]; then
|
|
api_curl "$API/releases?limit=50" > "$RELEASES_JSON"
|
|
else
|
|
printf '%s\n' '[]' > "$RELEASES_JSON"
|
|
fi
|
|
|
|
BASE_VERSION=$(sed -n 's/.*android:versionName="\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' "$ROOT/AndroidManifest.xml" | head -n 1)
|
|
BASE_CODE=$(sed -n 's/.*android:versionCode="\([0-9][0-9]*\)".*/\1/p' "$ROOT/AndroidManifest.xml" | head -n 1)
|
|
|
|
python3 - "$RELEASES_JSON" "$PLAN_JSON" "$BASE_VERSION" "$BASE_CODE" "$STABLE" <<'PY'
|
|
import json
|
|
import re
|
|
import sys
|
|
|
|
releases_path, plan_path, base_version, base_code, stable_flag = sys.argv[1:]
|
|
stable_flag = int(stable_flag)
|
|
semver = re.compile(r"^(\d+)\.(\d+)\.(\d+)$")
|
|
devver = re.compile(r"^(\d+)\.(\d+)\.(\d+)\.(\d+)$")
|
|
|
|
with open(releases_path, encoding="utf-8") as handle:
|
|
releases = json.load(handle)
|
|
|
|
stable_versions = []
|
|
dev_versions = []
|
|
version_codes = [int(base_code or 0)]
|
|
for release in releases:
|
|
try:
|
|
metadata = json.loads(release.get("body", ""))
|
|
code = int(metadata.get("versionCode", 0))
|
|
if code > 0:
|
|
version_codes.append(code)
|
|
name = str(metadata.get("versionName", ""))
|
|
channel = metadata.get("channel")
|
|
match = semver.fullmatch(name)
|
|
if channel == "stable" and match:
|
|
stable_versions.append(tuple(map(int, match.groups())))
|
|
match = devver.fullmatch(name)
|
|
if channel == "dev" and match:
|
|
dev_versions.append(tuple(map(int, match.groups())))
|
|
except (TypeError, ValueError, json.JSONDecodeError):
|
|
continue
|
|
|
|
base_match = semver.fullmatch(base_version or "")
|
|
if not stable_versions:
|
|
if not base_match:
|
|
raise SystemExit("AndroidManifest.xml must contain a base X.Y.Z versionName")
|
|
latest_stable = tuple(map(int, base_match.groups()))
|
|
else:
|
|
latest_stable = max(stable_versions)
|
|
|
|
if stable_flag:
|
|
major, minor, patch = latest_stable
|
|
stable_version = (major, minor, patch + 1)
|
|
stable_name = ".".join(map(str, stable_version))
|
|
dev_name = stable_name + ".1"
|
|
stable_code = max(version_codes) + 1
|
|
dev_code = stable_code + 1
|
|
plan = [
|
|
{"channel": "stable", "versionName": stable_name, "versionCode": stable_code},
|
|
{"channel": "dev", "versionName": dev_name, "versionCode": dev_code},
|
|
]
|
|
else:
|
|
major, minor, patch = latest_stable
|
|
counter = max((v[3] for v in dev_versions if v[:3] == latest_stable), default=0) + 1
|
|
dev_name = f"{major}.{minor}.{patch}.{counter}"
|
|
plan = [{"channel": "dev", "versionName": dev_name, "versionCode": max(version_codes) + 1}]
|
|
|
|
with open(plan_path, "w", encoding="utf-8") as handle:
|
|
json.dump(plan, handle)
|
|
PY
|
|
|
|
python3 - "$PLAN_JSON" <<'PY'
|
|
import json
|
|
import sys
|
|
for item in json.load(open(sys.argv[1], encoding="utf-8")):
|
|
print(f"{item['channel']} {item['versionName']} {item['versionCode']}")
|
|
PY
|
|
|
|
[ "$DRY_RUN" -eq 0 ] || exit 0
|
|
|
|
publish() {
|
|
channel=$1
|
|
apk=$2
|
|
version_code=$3
|
|
version_name=$4
|
|
branch=${5:-main}
|
|
tag="${channel}-v${version_name}-c${version_code}"
|
|
sha=$(sha256sum "$apk" | cut -d' ' -f1)
|
|
body=$(printf '{"schema":1,"channel":"%s","versionCode":%s,"versionName":"%s","branch":"%s","sha256":"%s"}' \
|
|
"$channel" "$version_code" "$version_name" "$branch" "$sha")
|
|
response=$(mktemp)
|
|
body_json=$(python3 -c 'import json,sys; print(json.dumps(sys.argv[1]))' "$body")
|
|
api_curl -X POST "$API/releases" -H 'Content-Type: application/json' \
|
|
--data "{\"tag_name\":\"$tag\",\"target_commitish\":\"$branch\",\"name\":\"MG Utility $version_name\",\"body\":$body_json,\"draft\":false,\"prerelease\":$([ "$channel" = dev ] && echo true || echo false)}" > "$response"
|
|
release_id=$(python3 - "$response" <<'PY'
|
|
import json
|
|
import sys
|
|
with open(sys.argv[1], encoding="utf-8") as handle:
|
|
print(json.load(handle)["id"])
|
|
PY
|
|
)
|
|
rm -f "$response"
|
|
curl_status=0
|
|
api_curl -X POST \
|
|
"$API/releases/$release_id/assets?name=MG-Utility.apk" \
|
|
-F "attachment=@$apk;type=application/vnd.android.package-archive" >/dev/null || curl_status=$?
|
|
[ "$curl_status" -eq 0 ] || return "$curl_status"
|
|
printf 'Published %s release %s (%s)\n' "$channel" "$tag" "$sha"
|
|
}
|
|
|
|
publish_plan_item() {
|
|
channel=$1
|
|
version_name=$2
|
|
version_code=$3
|
|
UPDATE_VERSION_CODE="$version_code" UPDATE_VERSION_NAME="$version_name" UPDATE_BRANCH=main \
|
|
"$ROOT/build.sh" "$channel"
|
|
publish "$channel" "$ROOT/build/MG-Utility.apk" "$version_code" "$version_name" main
|
|
}
|
|
|
|
if [ "$STABLE" -eq 1 ]; then
|
|
stable=$(python3 - "$PLAN_JSON" <<'PY'
|
|
import json,sys
|
|
for item in json.load(open(sys.argv[1], encoding="utf-8")):
|
|
if item["channel"] == "stable": print(item["versionName"], item["versionCode"])
|
|
PY
|
|
)
|
|
set -- $stable
|
|
publish_plan_item stable "$1" "$2"
|
|
fi
|
|
|
|
dev=$(python3 - "$PLAN_JSON" <<'PY'
|
|
import json,sys
|
|
for item in json.load(open(sys.argv[1], encoding="utf-8")):
|
|
if item["channel"] == "dev": print(item["versionName"], item["versionCode"])
|
|
PY
|
|
)
|
|
set -- $dev
|
|
publish_plan_item dev "$1" "$2"
|