feat: 支持多APK与渠道变体,增加自动定位的服务管理CLI
This commit is contained in:
1 parent
7984785610
commit
87652364bc
33 files changed
+1110
-42
No files matched your search
@@ -266,7 +266,7 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
val req = HttpRequest.newBuilder(URI.create(base + "/api/v1" + route)).timeout(Duration.ofMinutes(5))
|
||||
.header("Authorization", "Bearer $secret").header("Content-Type", "application/json").method(method, publisher).build()
|
||||
val response = http.send(req, HttpResponse.BodyHandlers.ofByteArray())
|
||||
check(response.statusCode() in 200..299) { "HTTP ${response.statusCode()}: ${String(response.body()).take(500)}" }
|
||||
check(response.statusCode() in 200..299) { ServerErrors.describe(response.statusCode(), route, String(response.body(), Charsets.UTF_8)) }
|
||||
return response.body()
|
||||
}
|
||||
private fun json(base: String, secret: String, method: String, route: String, body: Any? = null) =
|
||||
@@ -301,6 +301,7 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
object : Task.Backgroundable(project, "Remote Compilation", false) {
|
||||
override fun run(indicator: ProgressIndicator) {
|
||||
var secret = ""
|
||||
var buildSucceeded = false
|
||||
try {
|
||||
http = ServerTls.client(cfg.serverCertificate)
|
||||
secret = PasswordSafe.instance.getPassword(credentials(base)) ?: ""
|
||||
@@ -358,6 +359,8 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
setBuildId(activeBuild)
|
||||
append("构建ID:$activeBuild(已保存,断网后可恢复)")
|
||||
val result = watchBuild(base, secret, activeBuild)
|
||||
buildSucceeded = true
|
||||
append("构建已成功,正在选择和下载 APK")
|
||||
val output = downloadApk(base, secret, activeBuild, source, result)
|
||||
check(!cancelRequested) { "已取消安装" }
|
||||
if (cfg.install) {
|
||||
@@ -369,7 +372,7 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
append("远程编译和 APK 下载完成")
|
||||
} catch (e: Exception) {
|
||||
// Never print request headers or the token.
|
||||
append((if (activeBuild.isNotEmpty()) "构建失败(记录已保留,可点击恢复上次构建):" else "同步或启动失败(尚未确认创建构建任务):") + (e.message ?: e.javaClass.simpleName).let { if (secret.isNotEmpty()) it.replace(secret, "***") else it })
|
||||
append((if (buildSucceeded) "构建已成功,APK 下载未完成(可恢复构建后重新选择):" else if (activeBuild.isNotEmpty()) "构建失败(记录已保留,可点击恢复上次构建):" else "同步或启动失败(尚未确认创建构建任务):") + (e.message ?: e.javaClass.simpleName).let { if (secret.isNotEmpty()) it.replace(secret, "***") else it })
|
||||
} finally {
|
||||
activeBuild = ""; busy = false
|
||||
ApplicationManager.getApplication().invokeLater { if (!disposed && !project.isDisposed) run.isEnabled = true }
|
||||
@@ -436,18 +439,32 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
}
|
||||
private fun downloadApk(base: String, secret: String, id: String, source: Path, result: JsonObject): Path {
|
||||
require(id.matches(Regex("[a-f0-9]{32}"))) { "无效构建 ID" }
|
||||
val artifacts = result.getAsJsonArray("artifacts")
|
||||
check(artifacts.size() == 1) { "当前要求恰好一个 APK,实际 ${artifacts.size()} 个" }
|
||||
val artifact = artifacts[0].asJsonObject
|
||||
val expected = artifact.get("hash").asString
|
||||
val size = artifact.get("size").asLong
|
||||
val output = source.resolve(".remotecompile/$id.apk"); Files.createDirectories(output.parent)
|
||||
if (Files.exists(output) && fileHash(output) == expected) { rememberArtifact(source, id, expected); append("复用已下载 APK:$output"); return output }
|
||||
val part = output.resolveSibling("$id.apk.part")
|
||||
val artifacts = ApkArtifacts.read(result)
|
||||
check(artifacts.isNotEmpty()) { "成功构建没有可下载的 APK" }
|
||||
val previous = LastBuildStore.load(source)?.takeIf { it.buildId == id }?.artifactName
|
||||
val artifact = if (artifacts.size == 1) artifacts.single() else {
|
||||
append("构建生成 ${artifacts.size} 个 APK,请选择要下载的完整安装包;其他产物保留在服务端")
|
||||
var chosen: ApkArtifact? = null
|
||||
ApplicationManager.getApplication().invokeAndWait {
|
||||
if (!disposed && !project.isDisposed) {
|
||||
chosen = JOptionPane.showInputDialog(this,
|
||||
"选择要下载的 APK;不同渠道或签名包请按实际用途选择。\n拆分 APK 需成套安装,当前不支持自动安装拆分包。",
|
||||
"选择构建产物", JOptionPane.QUESTION_MESSAGE, null,
|
||||
artifacts.toTypedArray(), artifacts.firstOrNull { it.name == previous } ?: artifacts.first()) as? ApkArtifact
|
||||
}
|
||||
}
|
||||
chosen ?: error("已取消选择 APK;构建已成功,产物仍保留,可点击下载 APK 重新选择")
|
||||
}
|
||||
append("选择 APK:$artifact")
|
||||
val expected = artifact.hash
|
||||
val size = artifact.size
|
||||
val output = source.resolve(".remotecompile/$id-$expected.apk"); Files.createDirectories(output.parent)
|
||||
if (Files.exists(output) && fileHash(output) == expected) { rememberArtifact(source, id, expected, artifact.name); append("复用已下载 APK:$output"); return output }
|
||||
val part = output.resolveSibling("$id-$expected.apk.part")
|
||||
var received = if (Files.exists(part)) Files.size(part) else 0L
|
||||
if (received > size) { Files.delete(part); received = 0 }
|
||||
if (received < size) {
|
||||
val name = URLEncoder.encode(artifact.get("name").asString, Charsets.UTF_8).replace("+", "%20")
|
||||
val name = URLEncoder.encode(artifact.name, Charsets.UTF_8).replace("+", "%20")
|
||||
val builder = HttpRequest.newBuilder(URI.create("$base/api/v1/builds/$id/artifacts/$name"))
|
||||
.header("Authorization", "Bearer $secret").timeout(Duration.ofMinutes(5))
|
||||
if (received > 0) builder.header("Range", "bytes=$received-")
|
||||
@@ -471,24 +488,25 @@ class RemotePanel(private val project: Project) : JPanel(BorderLayout()), Dispos
|
||||
}
|
||||
check(Files.size(part) == size && fileHash(part) == expected) { Files.deleteIfExists(part); "APK 校验失败,已清理损坏分片" }
|
||||
Files.move(part, output, java.nio.file.StandardCopyOption.REPLACE_EXISTING)
|
||||
rememberArtifact(source, id, expected)
|
||||
rememberArtifact(source, id, expected, artifact.name)
|
||||
showProgress("APK 下载完成", 100); append("APK 已保存:$output"); return output
|
||||
}
|
||||
private fun rememberArtifact(source: Path, id: String, expected: String) {
|
||||
LastBuildStore.load(source)?.takeIf { it.buildId == id }?.let { it.artifactHash = expected; LastBuildStore.save(source, it) }
|
||||
private fun rememberArtifact(source: Path, id: String, expected: String, name: String) {
|
||||
LastBuildStore.load(source)?.takeIf { it.buildId == id }?.let { it.artifactHash = expected; it.artifactName = name; LastBuildStore.save(source, it) }
|
||||
}
|
||||
private fun installDownloaded() {
|
||||
if (busy) return
|
||||
val source = project.basePath?.let { Path.of(it) } ?: return
|
||||
val record = try { LastBuildStore.load(source) ?: error("没有已下载构建记录") }
|
||||
catch (e: Exception) { append(e.message ?: "读取记录失败"); return }
|
||||
if (!record.buildId.matches(Regex("[a-f0-9]{32}")) || record.artifactHash.isBlank() || (buildIdField.text.isNotBlank() && buildIdField.text.trim() != record.buildId)) { append("请先下载并校验当前任务的 APK"); return }
|
||||
if (!record.buildId.matches(Regex("[a-f0-9]{32}")) || !record.artifactHash.matches(Regex("[a-f0-9]{64}")) || (buildIdField.text.isNotBlank() && buildIdField.text.trim() != record.buildId)) { append("请先下载并校验当前任务的 APK"); return }
|
||||
val cfg = settings.snapshot(); val explicit = devices.selectedItem as? AdbDevice
|
||||
busy = true; run.isEnabled = false
|
||||
object : Task.Backgroundable(project, "安装已下载 APK", false) {
|
||||
override fun run(indicator: ProgressIndicator) {
|
||||
try {
|
||||
val output = source.resolve(".remotecompile/${record.buildId}.apk")
|
||||
val selectedOutput = source.resolve(".remotecompile/${record.buildId}-${record.artifactHash}.apk")
|
||||
val output = if (Files.isRegularFile(selectedOutput)) selectedOutput else source.resolve(".remotecompile/${record.buildId}.apk")
|
||||
check(Files.isRegularFile(output) && fileHash(output) == record.artifactHash) { "已下载 APK 缺失或校验失败,请重新下载" }
|
||||
val adb = AdbDevices.resolve(source, cfg.adb)
|
||||
val target = AdbDevices.choose(AdbDevices.list(adb), explicit?.serial ?: cfg.device, explicit != null)
|
||||
|
||||
Reference in new issue
Block a user