IDE: Fix mirai version parsing

This commit is contained in:
Him188 2021-04-08 01:12:39 +08:00
parent d75b6a061c
commit 2d12d8bbce
2 changed files with 34 additions and 14 deletions

View File

@ -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<MiraiVersion>? {
val xml = runInterruptible {
private suspend fun getMiraiVersionList(): Set<MiraiVersion> {
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("""<version>\s*(.*?)\s*</version>""").findAll(xml).mapNotNull { it.groupValues[1] }.toSet()
}
fun CoroutineScope.getMiraiVersionListAsync(): Deferred<Set<MiraiVersion>> {
return async(CoroutineName("getMiraiVersionListAsync")) {
getMiraiVersionList()?: setOf("+")
getMiraiVersionList()
}
}
}

View File

@ -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
}
}