Skip to content

Commit

Permalink
Merge pull request #869 from simple-robot/optimize-bot-api
Browse files Browse the repository at this point in the history
为BotManager和BotManagers提供更多辅助的获取API
  • Loading branch information
ForteScarlet authored Jul 9, 2024
2 parents 44a07fc + 84bfef6 commit de95a9c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 42 deletions.
45 changes: 13 additions & 32 deletions buildSrc/src/main/kotlin/P.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,22 @@
@file:Suppress("unused")

import love.forte.gradle.common.core.project.ProjectDetail
import love.forte.gradle.common.core.project.Version
import love.forte.gradle.common.core.project.minus
import love.forte.gradle.common.core.project.version
import love.forte.gradle.common.core.property.systemProp
import org.gradle.api.Project

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

println("IsSnapshot from system.property: $snapProp")
println("IsSnapshot from system.env: $snapEnv")
logger.info("IsSnapshot from system.property: {}", snapProp)
logger.info("IsSnapshot from system.env: {}", snapEnv)

return snapProp || snapEnv
snapProp || snapEnv
}

fun isSnapshot(): Boolean = _isSnapshot


/**
* Project versions.
Expand All @@ -52,9 +51,9 @@ sealed class P(override val group: String) : ProjectDetail() {
*/
companion object {
const val VERSION = "4.1.1"
const val VERSION = "4.2.0"
const val SNAPSHOT_VERSION = "$VERSION-SNAPSHOT"
const val NEXT_VERSION = "4.1.1"
const val NEXT_VERSION = "4.2.0"
const val NEXT_SNAPSHOT_VERSION = "$NEXT_VERSION-SNAPSHOT"

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

final override val version: Version
val versionWithoutSnapshot: Version

init {
val mainVersion = version(4, 1, 0)

fun initVersionWithoutSnapshot(status: Version?): Version = if (status == null) {
mainVersion
} else {
mainVersion - status.copy()
}

versionWithoutSnapshot = initVersionWithoutSnapshot(null)

version = if (isSnapshot()) {
versionWithoutSnapshot - Version.SNAPSHOT
} else {
versionWithoutSnapshot
}

println("version=$version, versionWithoutSnapshot=$versionWithoutSnapshot")
final override val version: String = if (isSnapshot()) {
NEXT_SNAPSHOT_VERSION
} else {
VERSION
}


override val description: String get() = DESCRIPTION
override val developers: List<Developer> = developers {
developer {
Expand Down
23 changes: 17 additions & 6 deletions buildSrc/src/main/kotlin/simbot.changelog-generator.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
/*
* Copyright (c) 2022-2023 ForteScarlet.
* Copyright (c) 2022-2024. ForteScarlet.
*
* This file is part of Simple Robot.
* Project https://github.com/simple-robot/simpler-robot
* Email ForteScarlet@163.com
*
* 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.
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
*
* 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.
* This program 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.
*
* This program 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
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* 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/>.
*/

import changelog.generateChangelog
Expand All @@ -33,7 +44,7 @@ import changelog.generateChangelog
tasks.create("createChangelog") {
group = "documentation"
doFirst {
generateChangelog("v${P.Simbot.versionWithoutSnapshot}")
generateChangelog("v${P.VERSION}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ reactor = "3.6.2"
# simbots
suspendTransform = "0.9.0"
suspendReversal = "0.2.0"
gradleCommon = "0.4.0"
gradleCommon = "0.6.0"
# tests
mockk = "1.13.11"
# detekt
Expand Down
4 changes: 4 additions & 0 deletions simbot-api/api/simbot-api.api
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ public class love/forte/simbot/bot/BotException : java/lang/RuntimeException {
public abstract interface class love/forte/simbot/bot/BotManager : love/forte/simbot/ability/CompletionAware, love/forte/simbot/ability/LifecycleAware, love/forte/simbot/bot/AutoConfigurableBotPlugin {
public abstract fun all ()Lkotlin/sequences/Sequence;
public fun all (Llove/forte/simbot/common/id/ID;)Lkotlin/sequences/Sequence;
public fun allToList ()Ljava/util/List;
public fun allToList (Llove/forte/simbot/common/id/ID;)Ljava/util/List;
public fun asFuture ()Ljava/util/concurrent/CompletableFuture;
public abstract fun cancel (Ljava/lang/Throwable;)V
public static synthetic fun cancel$default (Llove/forte/simbot/bot/BotManager;Ljava/lang/Throwable;ILjava/lang/Object;)V
Expand All @@ -390,6 +392,8 @@ public final class love/forte/simbot/bot/BotManagerUtil {

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

public abstract interface class love/forte/simbot/bot/BotPlugin : love/forte/simbot/plugin/Plugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ public interface BotManager : AutoConfigurableBotPlugin, LifecycleAware, Complet
*/
public fun all(id: ID): Sequence<Bot> = all().filter { bot -> bot.id == id }

/**
* 将 [all] 收集为 [List] 并返回。
*
* @since 4.2.0
*/
public fun allToList(): List<Bot> = all().toList()

/**
* 将 [all] 收集为 [List] 并返回。
*
* @since 4.2.0
*/
public fun allToList(id: ID): List<Bot> = all(id).toList()

/**
* 根据一个指定的 [id] 获取匹配的bot。
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
package love.forte.simbot.bot

import love.forte.simbot.common.collection.toImmutable
import love.forte.simbot.common.id.ID
import kotlin.jvm.JvmMultifileClass
import kotlin.jvm.JvmName

//region BotManagers

/**
* 用于表示一组 [BotManager]。
*
Expand All @@ -41,6 +40,27 @@ public interface BotManagers : Collection<BotManager> {
* 以序列的形式获取当前 [BotManager] 中所有的 [Bot]。
*/
public fun allBots(): Sequence<Bot> = asSequence().flatMap { it.all() }

/**
* 尝试获取第一个 [BotManager] 中的第一个 [Bot]。
*
* @throws NoSuchElementException 如果无法获取
* @since 4.2.0
*/
public fun firstBot(): Bot =
(firstOrNull() ?: throw NoSuchElementException("BotManagers is empty"))
.all().firstOrNull() ?: throw NoSuchElementException("Bot is empty")

/**
* 根据指定ID寻找第一个匹配的 [Bot]。
*
* @throws NoSuchElementException 找不到匹配的bot
* @throws ConflictBotException 从某个BotManager中获取时存在多个相同id的bot
* @since 4.2.0
*/
public fun firstBot(id: ID): Bot =
asSequence().mapNotNull { it.find(id) }
.first()
}

/**
Expand Down Expand Up @@ -81,4 +101,3 @@ private class CollectionBotManagers(private val collections: Collection<BotManag
return collections.hashCode()
}
}
//endregion

0 comments on commit de95a9c

Please sign in to comment.