alexandre mommers
05/05/2024, 10:52 AMturansky
05/05/2024, 5:13 PMalexandre mommers
05/05/2024, 6:33 PMplugins {
alias(libs.plugins.kotlinMultiplatform)
}
val commonResourcesFile = getCommonProject()
.projectDir
.resolve("src")
.resolve("commonMain")
.resolve("resources")
assert(commonResourcesFile.isDirectory) { "$commonResourcesFile is not a directory" }
assert(commonResourcesFile.isNotEmpty) { "$commonResourcesFile is empty" }
kotlin {
js {
binaries.executable()
browser()
nodejs()
generateTypeScriptDefinitions()
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(projects.examples.common)
}
}
val jsMain by getting {
resources.setSrcDirs(
resources.srcDirs + setOf(
commonResourcesFile
)
)
}
}
}
fun getCommonProject() = projects.examples.common.identityPath.path
?.let(::project) ?: error("Could not find project path")
val File.isNotEmpty: Boolean
get() = this.listFiles()?.isNotEmpty() ?: false
Hildebrandt Tobias
05/06/2024, 1:27 PMbuildSrc
directory with the file
buildSrc/src/main/kotlin/Dependencies.kt
that looks like this:
import org.jetbrains.kotlin.gradle.plugin.KotlinDependencyHandler
object Version {
// Common
val kotlinxCoroutines = "1.7.3"
val kotlinxSerializationCore = "1.6.2"
val kotlinxSerializationJson = "1.6.2"
val koltinxDateTime = "0.5.0"
// Frontend
val kotlinWrappers = "1.0.0-pre.690"
// Backend
val okHttp3 = "4.12.0"
val exposed = "0.47.0"
}
object Libs {
// Common
val kotlinxSerializationCore = "org.jetbrains.kotlinx:kotlinx-serialization-core:${Version.kotlinxSerializationCore}"
val kotlinxSerializationJson = "org.jetbrains.kotlinx:kotlinx-serialization-json:${Version.kotlinxSerializationJson}"
val kotlinxDatetime = "org.jetbrains.kotlinx:kotlinx-datetime:${Version.koltinxDateTime}"
val stringFormat = "com.github.Simplx-dev:kotlin-format:${Version.stringFormat}"
// Frontend
val kotlinWrappers = BomConfig("kotlin-wrappers", "org.jetbrains.kotlin-wrappers", Version.kotlinWrappers, listOf(
"kotlin-react",
"kotlin-react-dom",
"kotlin-react-router-dom",
"kotlin-mui-base",
"kotlin-mui-material",
"kotlin-mui-icons-material",
"kotlin-mui-lab",
"kotlin-mui-system",
"kotlin-emotion",
"kotlin-css",
"kotlin-csstype",
"kotlin-tanstack-react-query",
"kotlin-tanstack-react-query-devtools",
"kotlin-tanstack-react-table",
"kotlin-tanstack-table-core",
"kotlin-react-beautiful-dnd",
))
// OkHttp BOM
val okHttp3 = BomConfig("okhttp","com.squareup.okhttp3", Version.okHttp3, listOf("okhttp","logging-interceptor","mockwebserver"))
// Exposed BOM
val exposed = BomConfig("exposed","org.jetbrains.exposed", Version.exposed, listOf("exposed-core","exposed-jdbc","exposed-dao","exposed-java-time"))
}
data class BomConfig(
private val name: String,
private val path: String,
private val version: String,
private val targets: List<String> = emptyList(),
val platform: String = "$path:$name-bom:$version",
val fullTargets: Map<String, String> = targets.associateWith { "$path:$it" }
) {
/**
* For use with BOM that have multiple paths like Jackson.
* Requires the full BOM path as well as the full path for each target.
*/
constructor(bom: String, fullTargets: List<String>, bomSplit: List<String> = bom.split(":")) : this(
name = bomSplit[1].removeSuffix("-bom"),
path = bomSplit[0],
version = bomSplit[2],
platform = bom,
fullTargets = fullTargets.associateBy { it.split(":").last() }
)
}
fun KotlinDependencyHandler.bomImplementation(config: BomConfig, limitTo: List<String> = emptyList()) {
implementation(enforcedPlatform(config.platform))
config.fullTargets
.filter { if (limitTo.isEmpty()) true else limitTo.contains(it.key) }
.forEach {
implementation(it.value)
}
}
I shorted the Libs
and Version
objects and the bom
stuff might not interest you.
But this way I have all dependencies, aside from npm, in one place.
Edit: ahhh I just saw you mean resources and not dependencies. Sorry I misread