mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-07-01 12:57:24 +00:00
Compare commits
13 Commits
v0.5.0
...
add-compat
Author | SHA1 | Date | |
---|---|---|---|
a65a57f9e2
|
|||
19cc1b9358
|
|||
ff396425a7
|
|||
580d9122e5
|
|||
2a607f1129
|
|||
2d6da7c1ae
|
|||
6235c5f51a
|
|||
255a02c93c
|
|||
dce28be9c7
|
|||
673c6d8392
|
|||
d586ca378e
|
|||
3ba4364a07
|
|||
eda0e522cd
|
26
.github/workflows/build-and-test.yml
vendored
26
.github/workflows/build-and-test.yml
vendored
@ -13,7 +13,7 @@ permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
build:
|
||||
build-and-test:
|
||||
timeout-minutes: 8
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@ -32,3 +32,27 @@ jobs:
|
||||
with:
|
||||
gradle-version: 'wrapper'
|
||||
arguments: test
|
||||
compatibility-check:
|
||||
timeout-minutes: 30
|
||||
continue-on-error: true
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ ubuntu-latest ]
|
||||
java-version: [ 11, 12, 13, 14, 15, 16, 17, 18 ]
|
||||
|
||||
runs-on: ${{ matrix.os }}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- name: Set up JDK
|
||||
uses: actions/setup-java@v3
|
||||
with:
|
||||
java-version: ${{ matrix.java-version }}
|
||||
distribution: 'adopt-hotspot'
|
||||
cache: 'gradle'
|
||||
- name: Grant execute permission for gradlew
|
||||
run: chmod +x gradlew
|
||||
- name: Build and test
|
||||
uses: gradle/gradle-build-action@v2.2.1
|
||||
with:
|
||||
gradle-version: 'wrapper'
|
||||
arguments: test
|
||||
|
@ -1,5 +1,5 @@
|
||||
plugins {
|
||||
kotlin("jvm") version "1.6.10" apply false
|
||||
kotlin("jvm") version "1.7.10" apply false
|
||||
id("org.jetbrains.kotlinx.kover") version "0.5.1" apply false
|
||||
}
|
||||
|
||||
|
@ -56,3 +56,8 @@ application {
|
||||
tasks.jar.configure {
|
||||
exclude("**/logback-test.xml")
|
||||
}
|
||||
|
||||
tasks.withType<AbstractArchiveTask>().configureEach {
|
||||
isPreserveFileTimestamps = false
|
||||
isReproducibleFileOrder = true
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ import java.net.URL
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import java.util.function.Supplier
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
private val log = KotlinLogging.logger { }
|
||||
|
||||
@ -102,9 +104,9 @@ private fun createDefaultRepositoryId(): String {
|
||||
* 必须提供 `pathSupplier` 或 `fileSupplier` 其中一个, 才能正常提供路径.
|
||||
*/
|
||||
internal enum class AppPaths(
|
||||
private val pathSupplier: () -> String = { fileSupplier.invoke().canonicalPath },
|
||||
private val pathSupplier: PathSupplier,
|
||||
private val initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer,
|
||||
private val fileSupplier: () -> File = { File(pathSupplier()) }
|
||||
private val fileSupplier: FileSupplier,
|
||||
) {
|
||||
/**
|
||||
* 数据根目录.
|
||||
@ -113,7 +115,7 @@ internal enum class AppPaths(
|
||||
*
|
||||
* 提示: 结尾不带 `/`.
|
||||
*/
|
||||
DATA_ROOT(fileSupplier = {
|
||||
DATA_ROOT(fileSupplier = FileSupplier {
|
||||
File(
|
||||
System.getProperty(PathConst.PROP_DATA_PATH) ?: System.getenv(PathConst.ENV_DATA_PATH)
|
||||
?: System.getProperty("user.dir") ?: "."
|
||||
@ -125,7 +127,7 @@ internal enum class AppPaths(
|
||||
}
|
||||
}),
|
||||
|
||||
CONFIG_APPLICATION({ "$DATA_ROOT/config.json" }, {
|
||||
CONFIG_APPLICATION(PathSupplier { "$DATA_ROOT/config.json" }, {
|
||||
if (!file.exists()) {
|
||||
file.bufferedWriter(StandardCharsets.UTF_8).use {
|
||||
GsonConst.appConfigGson.toJson(
|
||||
@ -141,7 +143,7 @@ internal enum class AppPaths(
|
||||
}
|
||||
}
|
||||
}),
|
||||
CONFIG_BOT({ "$DATA_ROOT/bot.json" }, {
|
||||
CONFIG_BOT(PathSupplier { "$DATA_ROOT/bot.json" }, {
|
||||
if (!file.exists()) {
|
||||
file.bufferedWriter(StandardCharsets.UTF_8).use {
|
||||
GsonConst.botConfigGson.toJson(
|
||||
@ -167,10 +169,25 @@ internal enum class AppPaths(
|
||||
TEMP({ "$DATA_ROOT/tmp/" })
|
||||
;
|
||||
|
||||
val file: File
|
||||
get() = fileSupplier.invoke()
|
||||
val path: String
|
||||
get() = pathSupplier.invoke()
|
||||
constructor(pathSupplier: PathSupplier, initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer) : this(
|
||||
fileSupplier = FileSupplier { File(pathSupplier.path).canonicalFile },
|
||||
pathSupplier = pathSupplier,
|
||||
initializer = initializer
|
||||
)
|
||||
|
||||
constructor(fileSupplier: FileSupplier, initializer: AppPaths.() -> Unit = AppPaths::defaultInitializer) : this(
|
||||
fileSupplier = fileSupplier,
|
||||
pathSupplier = PathSupplier { fileSupplier.file.canonicalPath },
|
||||
initializer = initializer
|
||||
)
|
||||
|
||||
constructor(pathSupplier: () -> String) : this(
|
||||
fileSupplier = FileSupplier { File(pathSupplier.invoke()).canonicalFile },
|
||||
pathSupplier = PathSupplier { pathSupplier.invoke() }
|
||||
)
|
||||
|
||||
val file: File by fileSupplier
|
||||
val path: String by pathSupplier
|
||||
|
||||
private val initialized = AtomicBoolean(false)
|
||||
|
||||
@ -206,6 +223,20 @@ internal enum class AppPaths(
|
||||
const val ENV_DATA_PATH = "BOT_DATA_PATH"
|
||||
}
|
||||
|
||||
private class FileSupplier(private val supplier: Supplier<File>) {
|
||||
operator fun getValue(appPaths: AppPaths, property: KProperty<*>): File = supplier.get()
|
||||
|
||||
val file: File
|
||||
get() = supplier.get()
|
||||
}
|
||||
|
||||
private class PathSupplier(private val supplier: Supplier<String>) {
|
||||
operator fun getValue(appPaths: AppPaths, property: KProperty<*>): String = supplier.get()
|
||||
|
||||
val path: String
|
||||
get() = supplier.get()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -182,7 +182,7 @@ internal class Launcher(private val config: AppConfig = Const.config) : AutoClos
|
||||
it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL
|
||||
|| it.url == MavenRepositoryExtensionFinder.MAVEN_CENTRAL_URL.trimEnd('/')
|
||||
}) {
|
||||
add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = config.proxy.toAetherProxy()))
|
||||
add(MavenRepositoryExtensionFinder.getMavenCentralRepository(proxy = proxyConfig.toAetherProxy()))
|
||||
}
|
||||
}.toList()
|
||||
val extensionPackageFinders = setOf(
|
||||
|
@ -413,14 +413,14 @@ internal class ExtensionClassLoader(urls: Array<URL>, dependencyLoader: ClassLoa
|
||||
// 以免使用了不来自扩展包的机器人扩展.
|
||||
|
||||
override fun getResources(name: String?): Enumeration<URL> {
|
||||
if (BotExtensionFactory::class.java.equals(name)) {
|
||||
if ("META-INF/services/${BotExtensionFactory::class.java.name}" == name) {
|
||||
return findResources(name)
|
||||
}
|
||||
return super.getResources(name)
|
||||
}
|
||||
|
||||
override fun getResource(name: String?): URL? {
|
||||
if (BotExtensionFactory::class.java.equals(name)) {
|
||||
if ("META-INF/services/${BotExtensionFactory::class.java}" == name) {
|
||||
return findResource(name)
|
||||
}
|
||||
return super.getResource(name)
|
||||
|
@ -74,7 +74,7 @@ fun <T : AutoCloseable> T.registerShutdownHook(): T {
|
||||
private object UtilsInternal {
|
||||
|
||||
val autoCloseableSet = mutableSetOf<AutoCloseable>()
|
||||
private val log = KotlinLogging.logger { }
|
||||
private val log = KotlinLogging.logger(UtilsInternal::class.java.name)
|
||||
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(Thread(this::doCloseResources, "Shutdown-AutoCloseable"))
|
||||
|
@ -24,7 +24,7 @@
|
||||
<appender name="FILE_OUT" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<file>${DATA_LOGS}/latest.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<fileNamePattern>data/logs/%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
<fileNamePattern>${DATA_LOGS}/%d{yyyy-MM-dd}.log.gz</fileNamePattern>
|
||||
<maxHistory>30</maxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder>
|
||||
|
@ -21,6 +21,17 @@ import kotlin.test.*
|
||||
|
||||
internal class AppPathsTest {
|
||||
|
||||
@Test
|
||||
fun `Consistency check`() {
|
||||
for (path in AppPaths.values()) {
|
||||
assertEquals(
|
||||
File(path.path).canonicalPath,
|
||||
path.file.canonicalPath,
|
||||
"路径 File 与 Path 不一致: ${path.name}"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Data root path priority`() {
|
||||
System.setProperty(AppPaths.PathConst.PROP_DATA_PATH, "fromSystemProperties")
|
||||
|
@ -1,6 +1,7 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
java
|
||||
jacoco
|
||||
`maven-publish`
|
||||
signing
|
||||
}
|
||||
@ -29,6 +30,16 @@ java {
|
||||
|
||||
tasks.test {
|
||||
useJUnitPlatform()
|
||||
finalizedBy(tasks.jacocoTestReport)
|
||||
}
|
||||
|
||||
tasks.jacocoTestReport {
|
||||
dependsOn(tasks.test)
|
||||
}
|
||||
|
||||
tasks.withType<AbstractArchiveTask>().configureEach {
|
||||
isPreserveFileTimestamps = false
|
||||
isReproducibleFileOrder = true
|
||||
}
|
||||
|
||||
publishing {
|
||||
|
@ -36,6 +36,11 @@ java {
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
}
|
||||
|
||||
tasks.withType<AbstractArchiveTask>().configureEach {
|
||||
isPreserveFileTimestamps = false
|
||||
isReproducibleFileOrder = true
|
||||
}
|
||||
|
||||
tasks.getByName<Test>("test") {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
@ -28,6 +28,13 @@ data class BotAccount(
|
||||
|
||||
/**
|
||||
* 机器人配置.
|
||||
*
|
||||
* 使用 Gson 解析时, 请添加以下类型适配器:
|
||||
* - [net.lamgc.scalabot.config.serializer.ProxyTypeSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.BotConfigSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.BotAccountSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.ArtifactSerializer]
|
||||
*
|
||||
* @property enabled 是否启用机器人.
|
||||
* @property account 机器人帐号信息, 用于访问 API.
|
||||
* @property disableBuiltInAbility 是否禁用 AbilityBot 自带命令.
|
||||
@ -126,6 +133,13 @@ data class MavenRepositoryConfig(
|
||||
* ScalaBot App 配置.
|
||||
*
|
||||
* App 配置信息与 BotConfig 分开, 分别存储在各自单独的文件中.
|
||||
*
|
||||
* 使用 Gson 解析时, 请添加以下类型适配器:
|
||||
* - [net.lamgc.scalabot.config.serializer.ProxyTypeSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.MavenRepositoryConfigSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.AuthenticationSerializer]
|
||||
* - [net.lamgc.scalabot.config.serializer.UsernameAuthenticatorSerializer]
|
||||
*
|
||||
* @property proxy Telegram API 代理配置.
|
||||
* @property metrics 运行指标数据配置. 可通过时序数据库记录运行数据.
|
||||
* @property mavenRepositories Maven 远端仓库配置.
|
||||
|
@ -219,6 +219,12 @@ internal class ProxyConfigTest {
|
||||
assertEquals(8080, actualConfig.port)
|
||||
assertEquals(ProxyType.HTTP, actualConfig.type)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toString test`() {
|
||||
assertEquals("NO_PROXY", ProxyConfig(ProxyType.NO_PROXY).toString())
|
||||
assertEquals("HTTP://example.org:1008", ProxyConfig(ProxyType.HTTP, "example.org", 1008).toString())
|
||||
}
|
||||
}
|
||||
|
||||
internal class MetricsConfigTest {
|
||||
|
Reference in New Issue
Block a user