Skip to content

Commit de95a9c

Browse files
authored
Merge pull request #869 from simple-robot/optimize-bot-api
为BotManager和BotManagers提供更多辅助的获取API
2 parents 44a07fc + 84bfef6 commit de95a9c

File tree

6 files changed

+71
-42
lines changed

6 files changed

+71
-42
lines changed

buildSrc/src/main/kotlin/P.kt

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,22 @@
2424
@file:Suppress("unused")
2525

2626
import love.forte.gradle.common.core.project.ProjectDetail
27-
import love.forte.gradle.common.core.project.Version
28-
import love.forte.gradle.common.core.project.minus
29-
import love.forte.gradle.common.core.project.version
3027
import love.forte.gradle.common.core.property.systemProp
3128
import org.gradle.api.Project
3229

33-
inline fun isSnapshot(b: () -> Unit = {}): Boolean {
34-
b()
30+
@Suppress("ObjectPropertyName")
31+
private val _isSnapshot: Boolean by lazy {
3532
val snapProp = System.getProperty("isSnapshot")?.toBoolean() ?: false
3633
val snapEnv = System.getenv(Env.IS_SNAPSHOT)?.toBoolean() ?: false
3734

38-
println("IsSnapshot from system.property: $snapProp")
39-
println("IsSnapshot from system.env: $snapEnv")
35+
logger.info("IsSnapshot from system.property: {}", snapProp)
36+
logger.info("IsSnapshot from system.env: {}", snapEnv)
4037

41-
return snapProp || snapEnv
38+
snapProp || snapEnv
4239
}
4340

41+
fun isSnapshot(): Boolean = _isSnapshot
42+
4443

4544
/**
4645
* Project versions.
@@ -52,9 +51,9 @@ sealed class P(override val group: String) : ProjectDetail() {
5251
5352
*/
5453
companion object {
55-
const val VERSION = "4.1.1"
54+
const val VERSION = "4.2.0"
5655
const val SNAPSHOT_VERSION = "$VERSION-SNAPSHOT"
57-
const val NEXT_VERSION = "4.1.1"
56+
const val NEXT_VERSION = "4.2.0"
5857
const val NEXT_SNAPSHOT_VERSION = "$NEXT_VERSION-SNAPSHOT"
5958

6059
const val GROUP = "love.forte.simbot"
@@ -87,30 +86,12 @@ sealed class P(override val group: String) : ProjectDetail() {
8786
object SimbotExtension : P(GROUP_EXTENSION)
8887
object SimbotBenchmark : P(GROUP_BENCHMARK)
8988

90-
final override val version: Version
91-
val versionWithoutSnapshot: Version
92-
93-
init {
94-
val mainVersion = version(4, 1, 0)
95-
96-
fun initVersionWithoutSnapshot(status: Version?): Version = if (status == null) {
97-
mainVersion
98-
} else {
99-
mainVersion - status.copy()
100-
}
101-
102-
versionWithoutSnapshot = initVersionWithoutSnapshot(null)
103-
104-
version = if (isSnapshot()) {
105-
versionWithoutSnapshot - Version.SNAPSHOT
106-
} else {
107-
versionWithoutSnapshot
108-
}
109-
110-
println("version=$version, versionWithoutSnapshot=$versionWithoutSnapshot")
89+
final override val version: String = if (isSnapshot()) {
90+
NEXT_SNAPSHOT_VERSION
91+
} else {
92+
VERSION
11193
}
11294

113-
11495
override val description: String get() = DESCRIPTION
11596
override val developers: List<Developer> = developers {
11697
developer {

buildSrc/src/main/kotlin/simbot.changelog-generator.gradle.kts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
/*
2-
* Copyright (c) 2022-2023 ForteScarlet.
2+
* Copyright (c) 2022-2024. ForteScarlet.
33
*
4-
* This file is part of Simple Robot.
4+
* Project https://github.com/simple-robot/simpler-robot
5+
56
*
6-
* Simple Robot is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
78
*
8-
* Simple Robot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
921
*
10-
* You should have received a copy of the GNU Lesser General Public License along with Simple Robot. If not, see <https://www.gnu.org/licenses/>.
1122
*/
1223

1324
import changelog.generateChangelog
@@ -33,7 +44,7 @@ import changelog.generateChangelog
3344
tasks.create("createChangelog") {
3445
group = "documentation"
3546
doFirst {
36-
generateChangelog("v${P.Simbot.versionWithoutSnapshot}")
47+
generateChangelog("v${P.VERSION}")
3748
}
3849
}
3950

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ reactor = "3.6.2"
1919
# simbots
2020
suspendTransform = "0.9.0"
2121
suspendReversal = "0.2.0"
22-
gradleCommon = "0.4.0"
22+
gradleCommon = "0.6.0"
2323
# tests
2424
mockk = "1.13.11"
2525
# detekt

simbot-api/api/simbot-api.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ public class love/forte/simbot/bot/BotException : java/lang/RuntimeException {
368368
public abstract interface class love/forte/simbot/bot/BotManager : love/forte/simbot/ability/CompletionAware, love/forte/simbot/ability/LifecycleAware, love/forte/simbot/bot/AutoConfigurableBotPlugin {
369369
public abstract fun all ()Lkotlin/sequences/Sequence;
370370
public fun all (Llove/forte/simbot/common/id/ID;)Lkotlin/sequences/Sequence;
371+
public fun allToList ()Ljava/util/List;
372+
public fun allToList (Llove/forte/simbot/common/id/ID;)Ljava/util/List;
371373
public fun asFuture ()Ljava/util/concurrent/CompletableFuture;
372374
public abstract fun cancel (Ljava/lang/Throwable;)V
373375
public static synthetic fun cancel$default (Llove/forte/simbot/bot/BotManager;Ljava/lang/Throwable;ILjava/lang/Object;)V
@@ -390,6 +392,8 @@ public final class love/forte/simbot/bot/BotManagerUtil {
390392

391393
public abstract interface class love/forte/simbot/bot/BotManagers : java/util/Collection, kotlin/jvm/internal/markers/KMappedMarker {
392394
public fun allBots ()Lkotlin/sequences/Sequence;
395+
public fun firstBot ()Llove/forte/simbot/bot/Bot;
396+
public fun firstBot (Llove/forte/simbot/common/id/ID;)Llove/forte/simbot/bot/Bot;
393397
}
394398

395399
public abstract interface class love/forte/simbot/bot/BotPlugin : love/forte/simbot/plugin/Plugin {

simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/BotManager.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ public interface BotManager : AutoConfigurableBotPlugin, LifecycleAware, Complet
5757
*/
5858
public fun all(id: ID): Sequence<Bot> = all().filter { bot -> bot.id == id }
5959

60+
/**
61+
* 将 [all] 收集为 [List] 并返回。
62+
*
63+
* @since 4.2.0
64+
*/
65+
public fun allToList(): List<Bot> = all().toList()
66+
67+
/**
68+
* 将 [all] 收集为 [List] 并返回。
69+
*
70+
* @since 4.2.0
71+
*/
72+
public fun allToList(id: ID): List<Bot> = all(id).toList()
73+
6074
/**
6175
* 根据一个指定的 [id] 获取匹配的bot。
6276
*

simbot-api/src/commonMain/kotlin/love/forte/simbot/bot/BotManagers.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@
2727
package love.forte.simbot.bot
2828

2929
import love.forte.simbot.common.collection.toImmutable
30+
import love.forte.simbot.common.id.ID
3031
import kotlin.jvm.JvmMultifileClass
3132
import kotlin.jvm.JvmName
3233

33-
//region BotManagers
34-
3534
/**
3635
* 用于表示一组 [BotManager]。
3736
*
@@ -41,6 +40,27 @@ public interface BotManagers : Collection<BotManager> {
4140
* 以序列的形式获取当前 [BotManager] 中所有的 [Bot]。
4241
*/
4342
public fun allBots(): Sequence<Bot> = asSequence().flatMap { it.all() }
43+
44+
/**
45+
* 尝试获取第一个 [BotManager] 中的第一个 [Bot]。
46+
*
47+
* @throws NoSuchElementException 如果无法获取
48+
* @since 4.2.0
49+
*/
50+
public fun firstBot(): Bot =
51+
(firstOrNull() ?: throw NoSuchElementException("BotManagers is empty"))
52+
.all().firstOrNull() ?: throw NoSuchElementException("Bot is empty")
53+
54+
/**
55+
* 根据指定ID寻找第一个匹配的 [Bot]。
56+
*
57+
* @throws NoSuchElementException 找不到匹配的bot
58+
* @throws ConflictBotException 从某个BotManager中获取时存在多个相同id的bot
59+
* @since 4.2.0
60+
*/
61+
public fun firstBot(id: ID): Bot =
62+
asSequence().mapNotNull { it.find(id) }
63+
.first()
4464
}
4565

4666
/**
@@ -81,4 +101,3 @@ private class CollectionBotManagers(private val collections: Collection<BotManag
81101
return collections.hashCode()
82102
}
83103
}
84-
//endregion

0 commit comments

Comments
 (0)