From 2d12d8bbceab687088d430a8a908db79c48eabf5 Mon Sep 17 00:00:00 2001 From: Him188 Date: Thu, 8 Apr 2021 01:12:39 +0800 Subject: [PATCH] IDE: Fix mirai version parsing --- .../src/creator/MiraiVersion.kt | 25 +++++++++++++------ .../creator/steps/PluginCoordinatesStep.kt | 23 +++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/tools/intellij-plugin/src/creator/MiraiVersion.kt b/tools/intellij-plugin/src/creator/MiraiVersion.kt index 711ba0c6e..8203488c9 100644 --- a/tools/intellij-plugin/src/creator/MiraiVersion.kt +++ b/tools/intellij-plugin/src/creator/MiraiVersion.kt @@ -12,6 +12,7 @@ package net.mamoe.mirai.console.intellij.creator import kotlinx.coroutines.* import org.jsoup.Jsoup +import org.jsoup.nodes.Document typealias MiraiVersion = String @@ -33,23 +34,33 @@ enum class MiraiVersionKind { private val REGEX_STABLE = Regex("""^\d+\.\d+(?:\.\d+)?$""") - private suspend fun getMiraiVersionList(): Set? { - val xml = runInterruptible { + private suspend fun getMiraiVersionList(): Set { + fun download(url: String): Document { + return Jsoup.connect(url) + .followRedirects(true) + .ignoreContentType(true) + .ignoreHttpErrors(true) + .get() + } + + val document = runInterruptible { // https://maven.aliyun.com/repository/central/net/mamoe/mirai-core/maven-metadata.xml // https://repo.maven.apache.org/maven2/net/mamoe/mirai-core/maven-metadata.xml kotlin.runCatching { - Jsoup.connect("https://maven.aliyun.com/repository/central/net/mamoe/mirai-core/maven-metadata.xml").get() + download("https://maven.aliyun.com/repository/central/net/mamoe/mirai-core/maven-metadata.xml") }.recoverCatching { - Jsoup.connect("https://repo.maven.apache.org/maven2/net/mamoe/mirai-core/maven-metadata.xml").get() - }.getOrNull() - }?.body()?.toString() ?: return null + download("https://repo.maven.apache.org/maven2/net/mamoe/mirai-core/maven-metadata.xml") + }.getOrThrow() + } + + val xml = document.toString() return Regex("""\s*(.*?)\s*""").findAll(xml).mapNotNull { it.groupValues[1] }.toSet() } fun CoroutineScope.getMiraiVersionListAsync(): Deferred> { return async(CoroutineName("getMiraiVersionListAsync")) { - getMiraiVersionList()?: setOf("+") + getMiraiVersionList() } } } diff --git a/tools/intellij-plugin/src/creator/steps/PluginCoordinatesStep.kt b/tools/intellij-plugin/src/creator/steps/PluginCoordinatesStep.kt index b2ba99134..5ed933ef6 100644 --- a/tools/intellij-plugin/src/creator/steps/PluginCoordinatesStep.kt +++ b/tools/intellij-plugin/src/creator/steps/PluginCoordinatesStep.kt @@ -11,7 +11,10 @@ package net.mamoe.mirai.console.intellij.creator.steps import com.intellij.ide.util.projectWizard.ModuleWizardStep -import kotlinx.coroutines.* +import kotlinx.coroutines.CoroutineName +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import net.mamoe.mirai.console.compiler.common.CheckerConstants.PLUGIN_ID_PATTERN import net.mamoe.mirai.console.intellij.creator.MiraiProjectModel import net.mamoe.mirai.console.intellij.creator.MiraiVersionKind @@ -92,12 +95,18 @@ class PluginCoordinatesStep( if (!model.availableMiraiVersionsOrFail.isCompleted) return@launch miraiVersionBox.removeAllItems() val expectingKind = miraiVersionKindBox.selectedItem as? MiraiVersionKind ?: MiraiVersionKind.DEFAULT - model.availableMiraiVersionsOrFail.await() - .sortedDescending() - .filter { v -> - expectingKind.isThatKind(v) - } - .forEach { v -> miraiVersionBox.addItem(v) } + kotlin.runCatching { model.availableMiraiVersionsOrFail.await() } + .fold( + onSuccess = { versions -> + versions.sortedDescending() + .filter { v -> expectingKind.isThatKind(v) } + .forEach { v -> miraiVersionBox.addItem(v) } + }, + onFailure = { e -> + Validation.popup("Failed to download version list, please select a version by yourself. \nCause: ${e.cause ?: e}", miraiVersionBox) + } + ) + miraiVersionBox.isEnabled = true } }