Merge remote-tracking branch 'origin/master'

This commit is contained in:
Him188 2020-11-13 14:32:34 +08:00
commit e406cd58cd
4 changed files with 44 additions and 39 deletions

View File

@ -102,29 +102,31 @@ public object BuiltInCommands {
@Handler
public suspend fun CommandSender.handle() {
kotlin.runCatching {
closingLock.withLock {
sendMessage("Stopping mirai-console")
kotlin.runCatching {
runIgnoreException<CancellationException> { MiraiConsole.job.cancelAndJoin() }
}.fold(
onSuccess = {
runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") }
},
onFailure = {
if (it is CancellationException) return@fold
@OptIn(ConsoleInternalApi::class)
MiraiConsole.mainLogger.error("Exception in stop", it)
runIgnoreException<EventCancelledException> {
sendMessage(
it.localizedMessage ?: it.message ?: it.toString()
)
GlobalScope.launch {
kotlin.runCatching {
closingLock.withLock {
if (!MiraiConsole.isActive) return@withLock
sendMessage("Stopping mirai-console")
kotlin.runCatching {
MiraiConsole.job.cancelAndJoin()
}.fold(
onSuccess = {
runIgnoreException<EventCancelledException> { sendMessage("mirai-console stopped successfully.") }
},
onFailure = {
@OptIn(ConsoleInternalApi::class)
MiraiConsole.mainLogger.error("Exception in stop", it)
runIgnoreException<EventCancelledException> {
sendMessage(
it.localizedMessage ?: it.message ?: it.toString()
)
}
}
}
)
}
}.exceptionOrNull()?.let(MiraiConsole.mainLogger::error)
exitProcess(0)
)
}
}.exceptionOrNull()?.let(MiraiConsole.mainLogger::error)
exitProcess(0)
}
}
}

View File

@ -94,21 +94,16 @@ internal constructor(
*/
val rule: String,
) {
init {
kotlin.runCatching {
parseRangeRequirement(rule)
}.onFailure {
throw java.lang.IllegalArgumentException("Syntax error: $rule", it)
}
}
@Transient
private val impl = SemVersionInternal.parseRangeRequirement(rule)
internal val impl = kotlin.runCatching {
SemVersionInternal.parseRangeRequirement(rule)
}.getOrElse {
throw java.lang.IllegalArgumentException("Syntax error: $rule", it)
}
/** 在 [version] 满足此要求时返回 true */
public fun test(version: SemVersion): Boolean {
return impl.test(version)
}
public fun test(version: SemVersion): Boolean = impl.test(version)
/**
* 序列化为字符串, [rule]. 从字符串反序列化, [parseRangeRequirement].

View File

@ -43,6 +43,16 @@ internal class TestSemVersion {
assert("1.0.0-rc.1".sem() < "1.0.0".sem())
}
@Test
internal fun testRequirementCopy() {
fun SemVersion.Requirement.check(a: SemVersion.Requirement.() -> SemVersion.Requirement) {
assert(a().impl !== this.impl)
}
SemVersion.parseRangeRequirement("1.0").check { copy() }
SemVersion.parseRangeRequirement("1.0").check { copy("2.0") }
SemVersion.parseRangeRequirement("1.0").check { copy("1.0") }
}
@Test
internal fun testRequirement() {
fun SemVersion.Requirement.assert(version: String): SemVersion.Requirement {

View File

@ -9,10 +9,7 @@
package net.mamoe.mirai.console.terminal
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import net.mamoe.mirai.console.MiraiConsole
import net.mamoe.mirai.console.command.*
import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
@ -30,12 +27,13 @@ internal fun startupConsoleThread() {
if (terminal is NoConsole) return
MiraiConsole.launch(CoroutineName("Input Cancelling Daemon")) {
while (true) {
while (isActive) {
delay(2000)
}
}.invokeOnCompletion {
runCatching<Unit> {
terminal.close()
// 应该仅关闭用户输入
terminal.reader().shutdown()
ConsoleInputImpl.thread.shutdownNow()
runCatching {
ConsoleInputImpl.executingCoroutine?.cancel(EndOfFileException())