Rewrite the launcher in MoonBit (old code moves to legacy/) (#1)
Launcher / test (push) Successful in 1m20s
Launcher / apk (push) Successful in 37s

This commit was merged in pull request #1.
This commit is contained in:
nandi committed 2026-11-23 01:57:10 +00:00
1 parent 7d2bf5996f
commit 2fa7bd745b
150 files changed
+8319 -462

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
import {
"experiments/launcher/jsonx",
}
+45
View File
@@ -0,0 +1,45 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/launcher/updater"
import {
"moonbitlang/core/debug",
}
// Values
pub let apk_asset : String
pub let check_every_ms : Int64
pub fn decide(Int32, Int32, Int32, manual~ : Bool) -> Decision
pub fn parse_release(String) -> Release?
pub fn parse_version(String) -> Int32?
pub let release_api : String
pub fn should_check(Int64, Int64, manual~ : Bool) -> Bool
pub fn tag_version(String) -> Int32?
pub let version_asset : String
// Errors
// Types and methods
#derive(Eq, @debug.Debug)
pub(all) enum Decision {
UpToDate(Int32)
Install(Int32)
Declined(Int32)
}
pub(all) struct Release {
tag : String
version_url : String?
apk_url : String?
}
// Type aliases
// Traits
+110
View File
@@ -0,0 +1,110 @@
///|
/// Self-update logic, pure. The shell does the network and the install; this decides
/// what the releases it fetched mean.
///
/// CI publishes a Gitea release `build-<versionCode>` holding `ultra-launcher.apk` and
/// `version.json` (`{"versionCode": N, "commit": SHA}`), with N also the APK's
/// versionCode. The latest-release API names the download URLs of both assets.
///|
pub let release_api : String = "https://git.site.latha.org/api/v1/repos/experiments/launcher/releases/latest"
///|
pub let apk_asset : String = "ultra-launcher.apk"
///|
pub let version_asset : String = "version.json"
///|
/// How often the home screen checks by itself (6 hours).
pub let check_every_ms : Int64 = 6L * 60L * 60L * 1000L
///|
pub(all) struct Release {
tag : String
version_url : String?
apk_url : String?
}
///|
/// The latest-release JSON from the Gitea API.
pub fn parse_release(text : String) -> Release? {
let json = @jsonx.parse(text)
guard json is Some(json) else { return None }
let mut version_url = None
let mut apk_url = None
for a in @jsonx.arr(json, "assets") {
let name = @jsonx.str(a, "name")
let url = @jsonx.str(a, "browser_download_url")
if url != "" {
if name == version_asset {
version_url = Some(url)
} else if name == apk_asset {
apk_url = Some(url)
}
}
}
Some({ tag: @jsonx.str(json, "tag_name"), version_url, apk_url, })
}
///|
/// The versionCode in a `version.json`.
pub fn parse_version(text : String) -> Int32? {
guard @jsonx.parse(text) is Some(json) else { return None }
match @jsonx.get(json, "versionCode") {
Some(Number(n, ..)) => Some(n.to_int())
_ => None
}
}
///|
/// The versionCode a release tag `build-<N>` stands for.
pub fn tag_version(tag : String) -> Int32? {
guard tag.has_prefix("build-") else { return None }
let rest = tag.clamped_view(start=6)
if rest.length() == 0 {
return None
}
let mut n = 0
for c in rest {
if c < '0' || c > '9' {
return None
}
n = n * 10 + (c.to_int() - '0'.to_int())
}
Some(n)
}
///|
#derive(Eq, Debug)
pub(all) enum Decision {
UpToDate(Int32)
/// A newer build exists and should be installed.
Install(Int32)
/// The user already cancelled this build's install; not offered again unless they ask.
Declined(Int32)
}
///|
/// `latest` is the newest published versionCode, `installed` the running one, `declined`
/// the last build whose install the user cancelled (-1: none).
pub fn decide(
latest : Int32,
installed : Int32,
declined : Int32,
manual~ : Bool,
) -> Decision {
if latest <= installed {
UpToDate(installed)
} else if !manual && latest == declined {
Declined(latest)
} else {
Install(latest)
}
}
///|
/// An automatic check runs at most every `check_every_ms`; a manual one always runs.
pub fn should_check(now : Int64, last : Int64, manual~ : Bool) -> Bool {
manual || now - last >= check_every_ms
}
+59
View File
@@ -0,0 +1,59 @@
///|
let sample : String =
#|{"tag_name":"build-29000123","assets":[
#|{"name":"version.json","browser_download_url":"https://x/v.json"},
#|{"name":"ultra-launcher.apk","browser_download_url":"https://x/a.apk"}]}
///|
test "release assets by name" {
let r = parse_release(sample).unwrap()
assert_eq(r.tag, "build-29000123")
assert_eq(r.version_url, Some("https://x/v.json"))
assert_eq(r.apk_url, Some("https://x/a.apk"))
}
///|
test "bad or partial release json" {
assert_eq(parse_release("nope").map(fn(r) { r.tag }), None)
let r = parse_release("{\"tag_name\":\"t\"}").unwrap()
assert_eq(r.apk_url, None)
}
///|
test "version.json" {
assert_eq(
parse_version("{\"versionCode\": 29000123, \"commit\": \"abc\"}"),
Some(29000123),
)
assert_eq(parse_version("{}"), None)
assert_eq(parse_version("x"), None)
}
///|
test "tags compare as build numbers" {
assert_eq(tag_version("build-29000123"), Some(29000123))
assert_eq(tag_version("build-"), None)
assert_eq(tag_version("v1.2.3"), None)
assert_eq(tag_version("build-12x"), None)
assert_true(
tag_version("build-10").unwrap() < tag_version("build-9000").unwrap(),
)
}
///|
test "deciding" {
assert_eq(decide(5, 5, -1, manual=false), UpToDate(5))
assert_eq(decide(4, 5, -1, manual=true), UpToDate(5))
assert_eq(decide(6, 5, -1, manual=false), Install(6))
assert_eq(decide(6, 5, 6, manual=false), Declined(6))
assert_eq(decide(6, 5, 6, manual=true), Install(6))
assert_eq(decide(7, 5, 6, manual=false), Install(7))
}
///|
test "throttling the automatic check" {
let h = 60L * 60L * 1000L
assert_true(should_check(7L * h, 0L, manual=false))
assert_true(!should_check(5L * h, 0L, manual=false))
assert_true(should_check(1L, 0L, manual=true))
}