From 1f88d6f4644c8df88af8a85bba42eabe6a59c728 Mon Sep 17 00:00:00 2001 From: Adam <897017+asemy@users.noreply.github.com> Date: Wed, 8 Dec 2021 17:43:08 +0100 Subject: [PATCH] move schema to kotlin-multiplatform --- .../adamko/kafkatorio/gradle/extensions.kt | 17 +-- .../adamko/kafkatorio/lang/node.gradle.kts | 2 +- gradle/libs.versions.toml | 4 +- modules/events-mod/build.gradle.kts | 31 +++--- modules/events-schema/build.gradle.kts | 100 ++++++++++++++---- .../kafkatorio/events/schema/builtinTypes.kt | 1 + .../events/schema/factorioEvents.kt | 8 +- .../adamko/kafkatorio/events/schema/json.kt | 0 .../adamko/kafkatorio/events/schema/kt2ts.kt | 0 .../events/schema/FactorioEventTest.kt | 0 modules/web-map/build.gradle.kts | 1 + 11 files changed, 118 insertions(+), 46 deletions(-) rename modules/events-schema/src/{main => commonMain}/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt (99%) rename modules/events-schema/src/{main => commonMain}/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt (89%) rename modules/events-schema/src/{main => commonMain}/kotlin/dev/adamko/kafkatorio/events/schema/json.kt (100%) rename modules/events-schema/src/{main => jvmMain}/kotlin/dev/adamko/kafkatorio/events/schema/kt2ts.kt (100%) rename modules/events-schema/src/{test => jvmTest}/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt (100%) diff --git a/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/gradle/extensions.kt b/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/gradle/extensions.kt index 2dbf448..793ecef 100644 --- a/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/gradle/extensions.kt +++ b/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/gradle/extensions.kt @@ -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 + properties: MapProperty ): Spec = Spec { 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 } } diff --git a/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/lang/node.gradle.kts b/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/lang/node.gradle.kts index 53475e4..4210854 100644 --- a/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/lang/node.gradle.kts +++ b/buildSrc/src/main/kotlin/dev/adamko/kafkatorio/lang/node.gradle.kts @@ -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() diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2107b2b..59d9e3f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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" diff --git a/modules/events-mod/build.gradle.kts b/modules/events-mod/build.gradle.kts index ff7582b..bdb990c 100644 --- a/modules/events-mod/build.gradle.kts +++ b/modules/events-mod/build.gradle.kts @@ -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("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 { diff --git a/modules/events-schema/build.gradle.kts b/modules/events-schema/build.gradle.kts index 3a4c925..971aa8d 100644 --- a/modules/events-schema/build.gradle.kts +++ b/modules/events-schema/build.gradle.kts @@ -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("${schemaTs.name}DistZip") + outgoing.artifact(schemaTsZipTask.flatMap { it.archiveFile }) } diff --git a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt similarity index 99% rename from modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt rename to modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt index a4d3b7a..20d85e5 100644 --- a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/builtinTypes.kt @@ -3,6 +3,7 @@ package dev.adamko.kafkatorio.events.schema +import kotlin.jvm.JvmInline import kotlinx.serialization.Serializable diff --git a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt similarity index 89% rename from modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt rename to modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt index 6e32078..b4888b2 100644 --- a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEvents.kt @@ -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 diff --git a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/json.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/json.kt similarity index 100% rename from modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/json.kt rename to modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/json.kt diff --git a/modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/kt2ts.kt b/modules/events-schema/src/jvmMain/kotlin/dev/adamko/kafkatorio/events/schema/kt2ts.kt similarity index 100% rename from modules/events-schema/src/main/kotlin/dev/adamko/kafkatorio/events/schema/kt2ts.kt rename to modules/events-schema/src/jvmMain/kotlin/dev/adamko/kafkatorio/events/schema/kt2ts.kt diff --git a/modules/events-schema/src/test/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt b/modules/events-schema/src/jvmTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt similarity index 100% rename from modules/events-schema/src/test/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt rename to modules/events-schema/src/jvmTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt diff --git a/modules/web-map/build.gradle.kts b/modules/web-map/build.gradle.kts index 56e7897..855e89d 100644 --- a/modules/web-map/build.gradle.kts +++ b/modules/web-map/build.gradle.kts @@ -9,6 +9,7 @@ plugins { kotlin { js(IR) { +// nodejs() browser { runTask { outputFileName = "main.bundle.js"