From 88b881b043af31adcf54d10e1f0d9099cd2e17a5 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Thu, 20 May 2021 17:56:33 +0430 Subject: [PATCH] get rid of guava --- server/build.gradle.kts | 1 - .../database/migration/lib/runMigrations.kt | 35 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/server/build.gradle.kts b/server/build.gradle.kts index 9423a8a..2649d3a 100644 --- a/server/build.gradle.kts +++ b/server/build.gradle.kts @@ -55,7 +55,6 @@ dependencies { implementation("com.dorkbox:Utilities:1.9") // misc - implementation("com.google.guava:guava:30.1.1-jre") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0") diff --git a/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/lib/runMigrations.kt b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/lib/runMigrations.kt index 907a18d..94bb4a7 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/lib/runMigrations.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/model/database/migration/lib/runMigrations.kt @@ -10,7 +10,7 @@ package ir.armor.tachidesk.model.database.migration.lib // originally licenced under MIT by Andreas Mausch, Changes are licenced under Mozilla Public License, v. 2.0. // adopted from: https://gitlab.com/andreas-mausch/exposed-migrations/-/tree/4bf853c18a24d0170eda896ddbb899cb01233595 -import com.google.common.reflect.ClassPath +import ir.armor.tachidesk.server.ServerConfig import mu.KotlinLogging import org.jetbrains.exposed.dao.id.EntityID import org.jetbrains.exposed.sql.Database @@ -18,8 +18,15 @@ import org.jetbrains.exposed.sql.SchemaUtils.create import org.jetbrains.exposed.sql.exists import org.jetbrains.exposed.sql.transactions.TransactionManager import org.jetbrains.exposed.sql.transactions.transaction +import java.nio.file.FileSystems +import java.nio.file.Files +import java.nio.file.Paths import java.time.Clock import java.time.Instant.now +import kotlin.io.path.ExperimentalPathApi +import kotlin.io.path.isDirectory +import kotlin.io.path.name +import kotlin.streams.toList private val logger = KotlinLogging.logger {} @@ -54,13 +61,31 @@ fun runMigrations(migrations: List, database: Database = TransactionM logger.info { "Migrations finished successfully" } } +@OptIn(ExperimentalPathApi::class) +private fun getTopLevelClasses(packageName: String): List> { + ServerConfig::class.java.getResource("/" + "ir.armor.tachidesk.model.database.migration".replace('.', '/')) + val path = "/" + packageName.replace('.', '/') + val uri = ServerConfig::class.java.getResource(path).toURI() + + return when (uri.scheme) { + "jar" -> { + val fileSystem = FileSystems.newFileSystem(uri, emptyMap()) + fileSystem.getPath(path) + } + else -> Paths.get(uri) + }.let { Files.walk(it, 1) } + .toList() + .filterNot { it.isDirectory() || it.name.contains('$') } // '$' means it's not a top level class + .filter { it.name.endsWith(".class") } + .map { Class.forName("$packageName.${it.name.substringBefore(".class")}") } +} + @Suppress("UnstableApiUsage") -fun loadMigrationsFrom(classPath: String): List { - return ClassPath.from(Thread.currentThread().contextClassLoader) - .getTopLevelClasses(classPath) +fun loadMigrationsFrom(packageName: String): List { + return getTopLevelClasses(packageName) .map { logger.debug("found Migration class ${it.name}") - val clazz = it.load().getDeclaredConstructor().newInstance() + val clazz = it.getDeclaredConstructor().newInstance() if (clazz is Migration) clazz else