Skip to content

Commit

Permalink
Merge pull request #1 from Chase22/migrate-to-kotlin-js
Browse files Browse the repository at this point in the history
migrate-to-kotlin-js
  • Loading branch information
Chase22 authored Jan 11, 2025
2 parents fb86e4f + 71e831c commit 40c61f3
Show file tree
Hide file tree
Showing 48 changed files with 3,751 additions and 9,167 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ jobs:

- name: Build
run: |
./gradlew yarn_build
./gradlew build
- name: Check
run: |
./gradlew yarn_test-ci
./gradlew check
- name: Setup Pages
uses: actions/configure-pages@v5
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: 'calculator/dist'
path: 'calculator/build/dist/mpf-calculator/productionExecutable'
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ node_modules
.gradle
.idea

build
build

.kotlin
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package de.chasenet.foxhole

import org.gradle.api.DefaultTask
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.OutputFile
Expand All @@ -11,24 +9,28 @@ import java.time.OffsetDateTime

abstract class DownloadJsonDataTask : DefaultTask() {
@OutputFile
val outputFile: RegularFileProperty = project.objects.fileProperty().convention(
project.layout.buildDirectory.file("foxhole.json")
)
val outputFile: RegularFileProperty =
project.objects.fileProperty().convention(
project.layout.buildDirectory.file("foxhole.json"),
)

init {
outputs.upToDateWhen {
// Check if output is not older than 1 day
Duration.between(
Instant.ofEpochSecond(outputFile.asFile.get().lastModified()),
OffsetDateTime.now()
).toDays() <= 1
Duration
.between(
Instant.ofEpochSecond(outputFile.asFile.get().lastModified()),
OffsetDateTime.now(),
).toDays() <= 1
}
}

@TaskAction
fun downloadFile() {
URI.create("https://foxholelogi.com/assets/foxhole.json").toURL().openStream()
URI
.create("https://foxholelogi.com/assets/foxhole.json")
.toURL()
.openStream()
.copyTo(outputFile.asFile.get().outputStream())
}

}
}
3 changes: 0 additions & 3 deletions calculator/.babelrc

This file was deleted.

6 changes: 0 additions & 6 deletions calculator/.gitignore

This file was deleted.

145 changes: 93 additions & 52 deletions calculator/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,76 +1,117 @@
import de.chasenet.foxhole.DownloadJsonDataTask
import de.chasenet.foxhole.GenerateIndexFileTask
import groovy.json.JsonSlurper
import org.jetbrains.kotlin.gradle.dsl.JsModuleKind
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget

plugins {
base
id("html-builder")
kotlin("multiplatform") version "2.1.0"
kotlin("plugin.serialization") version "2.1.0"
}

abstract class YarnExec : AbstractExecTask<YarnExec>(YarnExec::class.java) {
@get:Input
abstract val script: Property<String>
val modules = arrayOf("mpf-calculator")

init {
group = "yarn"
dependencies {
commonMainImplementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.3")
commonTestImplementation(kotlin("test"))
}

val htmlDir =
provider {
tasks.getByName<ProcessResources>("htmlGeneratorProcessResources").destinationDir
}

override fun exec() {
commandLine("sh")
workingDir(this.project.projectDir)
args("-c", "yarn ${script.get()}")
logging.captureStandardOutput(LogLevel.INFO)
logging.captureStandardError(LogLevel.ERROR)
super.exec()
kotlin {
modules.forEach {
js(it, IR) {
browser {
binaries.executable()

commonWebpackConfig {
cssSupport {
enabled = true
}
}

webpackTask {
sourceMaps = true
mainOutputFileName = "$it.js"
}

runTask {
sourceMaps = true
mainOutputFileName = "$it.js"
}
}
}
}

}
jvm("htmlGenerator")
sourceSets {
this.getByName("htmlGeneratorMain") {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:0.11.0")
}
}

JsonSlurper().parse(file("package.json"))
.uncheckedCast<Map<String, Any>>()
.get("scripts")
.uncheckedCast<Map<String, Any>>()
.keys.forEach {
task<YarnExec>("yarn_$it") {
dependsOn("yarn_setup")
script = it
modules.forEach { module ->
named("${module}Main") {
resources.srcDirs(
htmlDir,
provider {
downloadJsonData.outputFile
.get()
.asFile.parentFile
},
)

dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-js:1.10.1")
}
}
}
}

task<YarnExec>("yarn_setup") {
script = ""
}
tasks.withType<KotlinJsCompile>().configureEach {
compilerOptions {
target = "es2015"
this.moduleKind = JsModuleKind.MODULE_ES
}
}

val downloadJsonData = tasks.register<DownloadJsonDataTask>("downloadJsonData")

val generateIndexFile = tasks.register<GenerateIndexFileTask>("generateIndexFile") {
dependsOn(downloadJsonData)
val downloadJsonData =
task<DownloadJsonDataTask>("downloadJsonData") {
outputFile = layout.buildDirectory.dir("json").map { it.file("foxhole.json") }
}

foxholeJsonDataFile.set(downloadJsonData.flatMap { it.outputFile })
val htmlGeneratorJar by tasks.existing
val htmlGeneratorRuntimeClasspath by configurations.existing

outputFile = layout.projectDirectory.dir("src/generated").file("index.html")
val buildHtml =
task<JavaExec>("buildHtml") {
dependsOn(downloadJsonData)

}
outputs.dir(htmlDir)

tasks.named("yarn_build") {
dependsOn(generateIndexFile)
}
group = "build"

tasks.named("yarn_start") {
dependsOn(generateIndexFile)
}
classpath(htmlGeneratorJar, htmlGeneratorRuntimeClasspath)
mainClass = "de.chasenet.foxhole.MainKt"

tasks.build {
dependsOn("yarn_build")
}
args(
htmlDir.get().absolutePath,
downloadJsonData.outputFile
.get()
.asFile.absolutePath,
)
}

tasks.check {
dependsOn("yarn_test-ci")
kotlin.targets.withType<KotlinJsIrTarget>().configureEach {
runTask.configure {
dependsOn(buildHtml)
}
}

tasks.clean {
dependsOn("yarn_clean")
tasks.withType<ProcessResources> {
if (name == "htmlGeneratorProcessResources") return@withType
dependsOn(buildHtml, downloadJsonData)
}

@Suppress("UNCHECKED_CAST")
fun <T> Any?.uncheckedCast(): T = this as T
32 changes: 0 additions & 32 deletions calculator/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions calculator/src/ArrayUtils.ts

This file was deleted.

25 changes: 0 additions & 25 deletions calculator/src/Cost.test.ts

This file was deleted.

52 changes: 0 additions & 52 deletions calculator/src/Cost.ts

This file was deleted.

3 changes: 0 additions & 3 deletions calculator/src/HtmlUtils.ts

This file was deleted.

Loading

0 comments on commit 40c61f3

Please sign in to comment.