-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
174 lines (149 loc) · 5.9 KB
/
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import com.github.gradle.node.npm.task.NpxTask
import java.nio.file.Files
import java.util.concurrent.Executors
plugins {
kotlin("multiplatform") version "2.0.20"
kotlin("plugin.serialization") version "2.0.20"
id("com.github.node-gradle.node") version "7.0.2"
}
group = "org.example"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
val kotlinWrappers = "1.0.0-pre.819"
kotlin {
js(IR) {
browser {
commonWebpackConfig {
cssSupport {
enabled.set(true)
}
// commonWebpackConfig {
// configDirectory = projectDir.resolve("webpack.config.d")
// }
}
runTask {
mainOutputFileName = "main.bundle.js"
devServer = devServer.copy(
open = true,
port = 3000,
)
sourceMaps = true
}
webpackTask {
mainOutputFileName = "main.bundle.js"
sourceMaps = true
}
}
binaries.executable()
}
sourceSets {
val jsMain by getting {
dependencies {
implementation(project.dependencies.platform("org.jetbrains.kotlin-wrappers:kotlin-wrappers-bom:$kotlinWrappers"))
implementation("org.jetbrains.kotlin-wrappers:kotlin-js")
implementation("org.jetbrains.kotlin-wrappers:kotlin-browser")
implementation("org.jetbrains.kotlin-wrappers:kotlin-css")
// implementation("org.jetbrains.kotlin-wrappers:kotlin-emotion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3")
implementation("org.jetbrains.kotlinx:kotlinx-html-js:0.11.0")
implementation(npm("tailwindcss", "3.4.4"))
implementation(npm("sortablejs", "1.15.1"))
}
}
}
}
val buildStyle: TaskProvider<NpxTask> = tasks.register<NpxTask>("buildStyle") {
println("Running tailwind")
command.set("${project.buildDir}/js/node_modules/.bin/tailwindcss")
args.set(listOf("-i", "src/jsMain/resources/template.css", "-o", "src/jsMain/resources/style.css", "--verbose"))
inputs.files(fileTree("src/jsMain/kotlin"))
inputs.file("src/jsMain/resources/template.css")
outputs.file("src/jsMain/resources/style.css")
}
tasks.named("jsProcessResources") {
dependsOn(buildStyle)
}
tasks.register("watch") {
dependsOn("buildStyle")
doLast {
val kotlinSourceDir = project.file("src/jsMain/kotlin")
val templateCssFile = project.file("src/jsMain/resources/template.css")
val outputCssFile = project.file("src/jsMain/resources/style.css")
val executor = Executors.newSingleThreadScheduledExecutor()
var devServerProcess: Process? = null
fun shutdown() {
println("Shutting down watch task...")
executor.shutdownNow()
devServerProcess?.destroyForcibly()
// Wait for the dev server to terminate
devServerProcess?.waitFor(5, TimeUnit.SECONDS)
// Kill any remaining Gradle daemons
"pkill -f 'GradleDaemon'".runCommand()
// Kill any remaining Node.js processes
"pkill -f 'node'".runCommand()
}
val shutdownHook = Thread {
shutdown()
}
Runtime.getRuntime().addShutdownHook(shutdownHook)
try {
executor.scheduleWithFixedDelay({
try {
val outputCssModifiedTime = if (outputCssFile.exists()) Files.getLastModifiedTime(outputCssFile.toPath()).toMillis() else 0L
val templateCssChanged = Files.getLastModifiedTime(templateCssFile.toPath()).toMillis() > outputCssModifiedTime
val kotlinSourceChanged = kotlinSourceDir.walk().filter { it.isFile }.any {
Files.getLastModifiedTime(it.toPath()).toMillis() > outputCssModifiedTime
}
if (templateCssChanged || kotlinSourceChanged) {
println("Changes detected! Rebuilding styles...")
outputCssFile.delete()
project.exec {
commandLine("./gradlew", "buildStyle")
isIgnoreExitValue = true
}
}
} catch (e: Exception) {
println("Error in watch task: ${e.message}")
}
}, 0, 1, TimeUnit.SECONDS)
// Start the Kotlin webpack dev server in a separate process
devServerProcess = ProcessBuilder("./gradlew", "jsBrowserRun", "--continuous")
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT)
.start()
// Wait for the dev server process to finish
devServerProcess.waitFor()
} finally {
// Ensure cleanup happens even if an exception is thrown
shutdown()
Runtime.getRuntime().removeShutdownHook(shutdownHook)
}
}
}
tasks {
register("deploy") {
group = "build"
description = "Performs a production build and copies output to dist directory"
dependsOn("build")
doLast {
println("Executed tasks: ${gradle.taskGraph.allTasks.map { it.name }}")
delete("$rootDir/dist")
mkdir("$rootDir/dist")
copy {
from("$buildDir/dist/js/productionExecutable")
into("$rootDir/dist")
include("**/*")
}
println("\nProduction build completed and copied to $rootDir/dist")
}
}
}
fun String.runCommand() {
try {
Runtime.getRuntime().exec(this)
} catch (e: Exception) {
println("Error running command '$this': ${e.message}")
}
}