#!/usr/bin/env bash # Builds the launcher APK (MoonBit page + Java shell, android/build.sh) and publishes it as a # Gitea release: build-, holding ultra-launcher.apk and version.json, made a # draft first and published once both files are up, so the latest release never lacks them. # Runs in .gitea/workflows/launcher.yml's "apk" job (node:22-bookworm container, as root, # no docker). The installed app updates itself from the latest release (updater/ in MoonBit). set -euo pipefail cd "$(dirname "$0")/.." # Minutes since the epoch: always above every earlier build's versionCode (the Qt builds used # the same scale) and always rising. export VERSION_CODE=$(( $(date +%s) / 60 )) COMMIT="${GITHUB_SHA:-$(git rev-parse HEAD)}" echo "== tools" apt-get update -qq apt-get install -y -qq --no-install-recommends openjdk-17-jdk-headless unzip zip curl ca-certificates git >/dev/null curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash export PATH="$HOME/.moon/bin:$PATH" moon update echo "== Android SDK (build-tools and the platform jar only)" export ANDROID_SDK_ROOT=/opt/android-sdk mkdir -p "$ANDROID_SDK_ROOT/cmdline-tools" curl -fsSL -o /tmp/cmdline.zip https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip unzip -q /tmp/cmdline.zip -d "$ANDROID_SDK_ROOT/cmdline-tools" mv "$ANDROID_SDK_ROOT/cmdline-tools/cmdline-tools" "$ANDROID_SDK_ROOT/cmdline-tools/latest" yes | "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" --licenses >/dev/null || true "$ANDROID_SDK_ROOT/cmdline-tools/latest/bin/sdkmanager" --install "build-tools;35.0.0" >/dev/null # Only android.jar is needed from the platform: unzipping the whole thing (sdkmanager) is slow. curl -fsSL -o /tmp/platform.zip https://dl.google.com/android/repository/platform-35_r02.zip mkdir -p "$ANDROID_SDK_ROOT/platforms" && unzip -q /tmp/platform.zip 'android-35/android.jar' -d "$ANDROID_SDK_ROOT/platforms" echo "== build (versionCode $VERSION_CODE)" android/build.sh ultra-launcher.apk echo "== release build-$VERSION_CODE" server="${GITHUB_SERVER_URL:-https://git.site.latha.org}" repo="${GITHUB_REPOSITORY:-experiments/launcher}" api="${server%/}/api/v1/repos/$repo" token="${GITEA_TOKEN:-${GITHUB_TOKEN:-}}" test -n "$token" || { echo "no GITEA_TOKEN/GITHUB_TOKEN in the environment"; exit 1; } auth=(-H "Authorization: token $token") tag="build-$VERSION_CODE" id=$(curl -fsS "${auth[@]}" -H "Content-Type: application/json" -X POST "$api/releases" \ -d "{\"tag_name\": \"$tag\", \"name\": \"ultra Home build $VERSION_CODE\", \"target_commitish\": \"$COMMIT\", \"draft\": true, \"body\": \"MoonBit launcher built from $COMMIT.\"}" \ | python3 -c 'import sys, json; print(json.load(sys.stdin)["id"])') curl -fsS "${auth[@]}" -F "attachment=@ultra-launcher.apk" "$api/releases/$id/assets?name=ultra-launcher.apk" >/dev/null curl -fsS "${auth[@]}" -F "attachment=@$(dirname ultra-launcher.apk)/version.json" "$api/releases/$id/assets?name=version.json" >/dev/null curl -fsS "${auth[@]}" -H "Content-Type: application/json" -X PATCH "$api/releases/$id" -d '{"draft": false}' >/dev/null echo "published $tag to $server/$repo"