Tweak the app updater logic and add FOSS build support (#1843)

This commit is contained in:
AntsyLich
2025-03-18 19:36:16 +06:00
committed by GitHub
parent 7028b8673a
commit 28093935d8
11 changed files with 85 additions and 75 deletions

View File

@@ -2,47 +2,28 @@ package tachiyomi.data.release
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import tachiyomi.domain.release.model.Release
/**
* Contains information about the latest release from GitHub.
*/
@Serializable
data class GithubRelease(
@SerialName("tag_name") val version: String,
@SerialName("body") val info: String,
@SerialName("html_url") val releaseLink: String,
@SerialName("assets") val assets: List<GitHubAssets>,
@SerialName("tag_name")
val version: String,
@SerialName("body")
val info: String,
@SerialName("html_url")
val releaseLink: String,
@SerialName("assets")
val assets: List<GitHubAsset>,
)
/**
* Assets class containing download url.
* Asset class containing asset name and download url.
*/
@Serializable
data class GitHubAssets(@SerialName("browser_download_url") val downloadLink: String)
/**
* Regular expression that matches a mention to a valid GitHub username, like it's
* done in GitHub Flavored Markdown. It follows these constraints:
*
* - Alphanumeric with single hyphens (no consecutive hyphens)
* - Cannot begin or end with a hyphen
* - Max length of 39 characters
*
* Reference: https://stackoverflow.com/a/30281147
*/
val gitHubUsernameMentionRegex =
"""\B@([a-z0-9](?:-(?=[a-z0-9])|[a-z0-9]){0,38}(?<=[a-z0-9]))""".toRegex(
RegexOption.IGNORE_CASE,
)
val releaseMapper: (GithubRelease) -> Release = {
Release(
it.version,
it.info.replace(gitHubUsernameMentionRegex) { mention ->
"[${mention.value}](https://github.com/${mention.value.substring(1)})"
},
it.releaseLink,
it.assets.map(GitHubAssets::downloadLink),
)
}
data class GitHubAsset(
val name: String,
@SerialName("browser_download_url")
val downloadLink: String,
)

View File

@@ -1,10 +1,12 @@
package tachiyomi.data.release
import android.os.Build
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.network.awaitSuccess
import eu.kanade.tachiyomi.network.parseAs
import kotlinx.serialization.json.Json
import tachiyomi.domain.release.interactor.GetApplicationRelease
import tachiyomi.domain.release.model.Release
import tachiyomi.domain.release.service.ReleaseService
@@ -13,13 +15,53 @@ class ReleaseServiceImpl(
private val json: Json,
) : ReleaseService {
override suspend fun latest(repository: String): Release {
return with(json) {
override suspend fun latest(arguments: GetApplicationRelease.Arguments): Release? {
val release = with(json) {
networkService.client
.newCall(GET("https://api.github.com/repos/$repository/releases/latest"))
.newCall(GET("https://api.github.com/repos/${arguments.repository}/releases/latest"))
.awaitSuccess()
.parseAs<GithubRelease>()
.let(releaseMapper)
}
val downloadLink = getDownloadLink(release = release, isFoss = arguments.isFoss) ?: return null
return Release(
version = release.version,
info = release.info.replace(gitHubUsernameMentionRegex) { mention ->
"[${mention.value}](https://github.com/${mention.value.substring(1)})"
},
releaseLink = release.releaseLink,
downloadLink = downloadLink,
)
}
private fun getDownloadLink(release: GithubRelease, isFoss: Boolean): String? {
val map = release.assets.associate { asset ->
BUILD_TYPES.find { "-$it" in asset.name } to asset.downloadLink
}
return if (!isFoss) {
map[Build.SUPPORTED_ABIS[0]] ?: map[null]
} else {
map[FOSS]
}
}
companion object {
private const val FOSS = "foss"
private val BUILD_TYPES = listOf(FOSS, "arm64-v8a", "armeabi-v7a", "x86_64", "x86")
/**
* Regular expression that matches a mention to a valid GitHub username, like it's
* done in GitHub Flavored Markdown. It follows these constraints:
*
* - Alphanumeric with single hyphens (no consecutive hyphens)
* - Cannot begin or end with a hyphen
* - Max length of 39 characters
*
* Reference: https://stackoverflow.com/a/30281147
*/
private val gitHubUsernameMentionRegex = """\B@([a-z0-9](?:-(?=[a-z0-9])|[a-z0-9]){0,38}(?<=[a-z0-9]))"""
.toRegex(RegexOption.IGNORE_CASE)
}
}