move schema to kotlin-multiplatform
This commit is contained in:
parent
6d60428ab9
commit
1f88d6f464
11 changed files with 118 additions and 46 deletions
|
|
@ -7,7 +7,7 @@ import kotlinx.serialization.json.jsonPrimitive
|
|||
import org.gradle.api.Project
|
||||
import org.gradle.api.Task
|
||||
import org.gradle.api.file.RegularFileProperty
|
||||
import org.gradle.api.logging.LogLevel
|
||||
import org.gradle.api.provider.MapProperty
|
||||
import org.gradle.api.specs.Spec
|
||||
import org.gradle.kotlin.dsl.invoke
|
||||
|
||||
|
|
@ -27,18 +27,23 @@ fun Project.isProcessRunning(process: String, ignoreCase: Boolean = true): Spec<
|
|||
|
||||
fun Project.areJsonPropertiesUpToDate(
|
||||
packageJsonFile: RegularFileProperty,
|
||||
properties: () -> Map<String, String>
|
||||
properties: MapProperty<String, String>
|
||||
): Spec<Task> = Spec<Task> {
|
||||
val packageJsonContent = packageJsonFile.get().asFile.readText()
|
||||
val packageJson = jsonMapper.parseToJsonElement(packageJsonContent).jsonObject
|
||||
|
||||
properties().all { (key, expectedVal) ->
|
||||
properties.get().all { (key, expectedVal) ->
|
||||
val actualVal = packageJson[key]?.jsonPrimitive?.content
|
||||
(expectedVal == actualVal).also {
|
||||
logger.log(
|
||||
if (it) LogLevel.LIFECYCLE else LogLevel.INFO,
|
||||
val isUpToDate = expectedVal == actualVal
|
||||
|
||||
if (isUpToDate) {
|
||||
logger.info("package.json property is up to date, '$key = $actualVal'")
|
||||
} else {
|
||||
logger.lifecycle(
|
||||
"package.json has outdated property '$key' (expected: $expectedVal, actual: $actualVal)"
|
||||
)
|
||||
}
|
||||
|
||||
isUpToDate
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ val updatePackageJson by tasks.registering(UpdatePackageJson::class) {
|
|||
propertiesToCheck.put("version", "${project.version}")
|
||||
|
||||
// check to see if the properties are already up-to-date
|
||||
onlyIf(!areJsonPropertiesUpToDate(packageJsonFile) { propertiesToCheck.get() })
|
||||
onlyIf(!areJsonPropertiesUpToDate(packageJsonFile, propertiesToCheck))
|
||||
|
||||
doLast {
|
||||
val packageJsonContent = packageJsonFile.get().asFile.readText()
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@ kotlinx-serialization = "1.3.1"
|
|||
kvision = "5.6.1"
|
||||
|
||||
## test ##
|
||||
junit = "5.8.1"
|
||||
kotest = "4.6.3"
|
||||
junit = "5.8.2"
|
||||
kotest = "5.0.1"
|
||||
|
||||
## infra ##
|
||||
factorio = "1.1.48"
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ val typescriptToLua by tasks.registering(NpmTask::class) {
|
|||
description = "Convert Typescript To Lua"
|
||||
group = project.name
|
||||
|
||||
dependsOn(tasks.npmInstall, installEventsSchema)
|
||||
dependsOn(tasks.npmInstall, installEventsTsSchema)
|
||||
|
||||
execOverrides { standardOutput = System.out }
|
||||
|
||||
|
|
@ -84,23 +84,27 @@ val typescriptToLua by tasks.registering(NpmTask::class) {
|
|||
into(outputDir)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val installEventsSchema by tasks.registering(Sync::class) {
|
||||
val installEventsTsSchema by tasks.registering(Sync::class) {
|
||||
description = "Fetch the latest shared data-model"
|
||||
group = project.name
|
||||
|
||||
dependsOn(typescriptEventsSchema)
|
||||
|
||||
val outputDir = layout.projectDirectory.dir("src/main/typescript/schema")
|
||||
outputs.dir(outputDir)
|
||||
|
||||
from(
|
||||
typescriptEventsSchema.incoming
|
||||
.artifactView { lenient(true) }
|
||||
.artifacts
|
||||
.artifactFiles
|
||||
.filter { file -> file.exists() }
|
||||
.files
|
||||
.map { zipTree(it) }
|
||||
provider { typescriptEventsSchema }
|
||||
.map { eventsSchema ->
|
||||
eventsSchema.incoming
|
||||
.artifactView { lenient(true) }
|
||||
.artifacts
|
||||
.artifactFiles
|
||||
.filter { file -> file.exists() }
|
||||
.map { zipTree(it) }
|
||||
}
|
||||
) {
|
||||
// drop the first directory inside the zip
|
||||
eachFile {
|
||||
|
|
@ -108,8 +112,7 @@ val installEventsSchema by tasks.registering(Sync::class) {
|
|||
}
|
||||
includeEmptyDirs = false
|
||||
}
|
||||
|
||||
into(layout.projectDirectory.dir("src/main/typescript/schema"))
|
||||
into(outputDir)
|
||||
}
|
||||
|
||||
tasks.distZip {
|
||||
|
|
@ -130,7 +133,7 @@ distributions {
|
|||
filter<ReplaceTokens>("tokens" to projectTokens)
|
||||
includeEmptyDirs = false
|
||||
exclude {
|
||||
// exclude empty files
|
||||
// exclude empty files
|
||||
it.file.run {
|
||||
isFile && useLines { lines -> lines.all { line -> line.isBlank() } }
|
||||
}
|
||||
|
|
@ -210,7 +213,7 @@ tasks.updatePackageJson {
|
|||
packageJsonFile.set(layout.projectDirectory.file("src/main/typescript/package.json"))
|
||||
}
|
||||
|
||||
tasks.assemble { dependsOn(installEventsSchema, tasks.updatePackageJson) }
|
||||
tasks.assemble { dependsOn(installEventsTsSchema, tasks.updatePackageJson) }
|
||||
|
||||
//
|
||||
//idea {
|
||||
|
|
|
|||
|
|
@ -2,29 +2,83 @@ import dev.adamko.kafkatorio.gradle.asProvider
|
|||
import dev.adamko.kafkatorio.gradle.typescriptAttributes
|
||||
|
||||
plugins {
|
||||
id("dev.adamko.kafkatorio.lang.kotlin-jvm")
|
||||
kotlin("multiplatform")
|
||||
kotlin("plugin.serialization")
|
||||
distribution
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(platform(libs.kotlinx.serialization.bom))
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
kotlin {
|
||||
js(IR) {
|
||||
binaries.executable()
|
||||
nodejs()
|
||||
}
|
||||
jvm()
|
||||
|
||||
implementation("com.github.ntrrgc:ts-generator:1.1.2")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||
@Suppress("UnstableApiUsage") // enforcedPlatform is incubating
|
||||
sourceSets {
|
||||
|
||||
testImplementation(libs.kotest.datatest)
|
||||
all {
|
||||
languageSettings.apply {
|
||||
optIn("kotlin.RequiresOptIn")
|
||||
optIn("kotlin.ExperimentalStdlibApi")
|
||||
optIn("kotlin.time.ExperimentalTime")
|
||||
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
|
||||
optIn("kotlinx.serialization.ExperimentalSerializationApi")
|
||||
}
|
||||
}
|
||||
|
||||
val commonMain by getting {
|
||||
dependencies {
|
||||
implementation(project.dependencies.enforcedPlatform(libs.kotlin.bom))
|
||||
|
||||
implementation(project.dependencies.enforcedPlatform(libs.kotlinx.serialization.bom))
|
||||
implementation(libs.kotlinx.serialization.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
val commonTest by getting {
|
||||
dependencies {
|
||||
|
||||
implementation(kotlin("test"))
|
||||
|
||||
implementation(project.dependencies.enforcedPlatform(libs.kotest.bom))
|
||||
implementation("io.kotest:kotest-assertions-core")
|
||||
implementation("io.kotest:kotest-property")
|
||||
implementation("io.kotest:kotest-assertions-json")
|
||||
}
|
||||
}
|
||||
|
||||
val jvmMain by getting {
|
||||
dependencies {
|
||||
implementation("com.github.ntrrgc:ts-generator:1.1.2")
|
||||
implementation("org.jetbrains.kotlin:kotlin-reflect")
|
||||
}
|
||||
}
|
||||
|
||||
val jvmTest by getting {
|
||||
dependencies {
|
||||
implementation("io.kotest:kotest-runner-junit5")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val generateTypescript by tasks.registering(JavaExec::class) {
|
||||
group = project.name
|
||||
|
||||
val generatedFile = file("$temporaryDir/${project.name}.ts")
|
||||
val jvmJar by tasks.getting(Jar::class)
|
||||
val main by kotlin.jvm().compilations.getting
|
||||
classpath(
|
||||
jvmJar,
|
||||
main.runtimeDependencyFiles
|
||||
)
|
||||
|
||||
classpath(sourceSets.main.map { it.runtimeClasspath })
|
||||
mainClass.set("dev.adamko.kafkatorio.events.schema.Kt2tsKt")
|
||||
|
||||
val generatedFile = file("$temporaryDir/${project.name}.ts")
|
||||
args(generatedFile)
|
||||
|
||||
val buildOutput = layout.buildDirectory.dir("generated/typescript")
|
||||
|
|
@ -36,21 +90,25 @@ val generateTypescript by tasks.registering(JavaExec::class) {
|
|||
}
|
||||
|
||||
doLast {
|
||||
sync { from(temporaryDir); into(buildOutput) }
|
||||
}
|
||||
}
|
||||
|
||||
distributions {
|
||||
main {
|
||||
distributionBaseName.set("${rootProject.name}-${project.name}")
|
||||
contents {
|
||||
from(generateTypescript.map { it.outputs.files.asFileTree })
|
||||
sync {
|
||||
from(temporaryDir)
|
||||
into(buildOutput)
|
||||
include("**/*.ts")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val typescriptModelGenerated by configurations.registering {
|
||||
val schemaTs by distributions.registering {
|
||||
distributionBaseName.set("${rootProject.name}-${project.name}")
|
||||
contents {
|
||||
from(generateTypescript.map { it.outputs.files.asFileTree })
|
||||
}
|
||||
}
|
||||
|
||||
val typescriptModelGenerated : Configuration by configurations.creating {
|
||||
asProvider()
|
||||
typescriptAttributes(objects)
|
||||
outgoing.artifact(tasks.distZip.flatMap { it.archiveFile })
|
||||
|
||||
val schemaTsZipTask = tasks.named<Zip>("${schemaTs.name}DistZip")
|
||||
outgoing.artifact(schemaTsZipTask.flatMap { it.archiveFile })
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
package dev.adamko.kafkatorio.events.schema
|
||||
|
||||
|
||||
import kotlin.jvm.JvmInline
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
package dev.adamko.kafkatorio.events.schema
|
||||
|
||||
|
||||
import kotlin.reflect.full.starProjectedType
|
||||
//import kotlin.reflect.full.starProjectedType
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonClassDiscriminator
|
||||
|
|
@ -35,8 +35,12 @@ const val FactorioObjectDataDiscriminatorKey: String = "object_name"
|
|||
sealed class FactorioObjectData {
|
||||
|
||||
// workaround - https://github.com/Kotlin/kotlinx.serialization/issues/1664
|
||||
// val objectName: String by lazy {
|
||||
// kotlinx.serialization.serializer(this::class.starProjectedType).descriptor.serialName
|
||||
// }
|
||||
val objectName: String by lazy {
|
||||
kotlinx.serialization.serializer(this::class.starProjectedType).descriptor.serialName
|
||||
serializer().descriptor.serialName
|
||||
// kotlinx.serialization.serializer(this::class.starProjectedType).descriptor.serialName
|
||||
}
|
||||
|
||||
// alternative: find the @SerialName annotation
|
||||
|
|
@ -9,6 +9,7 @@ plugins {
|
|||
|
||||
kotlin {
|
||||
js(IR) {
|
||||
// nodejs()
|
||||
browser {
|
||||
runTask {
|
||||
outputFileName = "main.bundle.js"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue