build.gradle.kts 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
  2. import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
  3. import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpackConfig
  4. import org.springframework.boot.gradle.tasks.bundling.BootJar
  5. import org.springframework.boot.gradle.tasks.run.BootRun
  6. plugins {
  7. val kotlinVersion: String by System.getProperties()
  8. kotlin("plugin.serialization") version kotlinVersion
  9. kotlin("multiplatform") version kotlinVersion
  10. id("io.spring.dependency-management") version System.getProperty("dependencyManagementPluginVersion")
  11. id("org.springframework.boot") version System.getProperty("springBootVersion")
  12. kotlin("plugin.spring") version kotlinVersion
  13. val kvisionVersion: String by System.getProperties()
  14. id("kvision") version kvisionVersion
  15. }
  16. version = "1.0"
  17. group = "me.mementomorri"
  18. repositories {
  19. mavenCentral()
  20. jcenter()
  21. mavenLocal()
  22. }
  23. // Versions
  24. val kotlinVersion: String by System.getProperties()
  25. val kvisionVersion: String by System.getProperties()
  26. val coroutinesVersion: String by project
  27. val springAutoconfigureR2dbcVersion: String by project
  28. val springDataR2dbcVersion: String by project
  29. val r2dbcPostgresqlVersion: String by project
  30. val r2dbcH2Version: String by project
  31. val kweryVersion: String by project
  32. val webDir = file("src/frontendMain/web")
  33. val mainClassName = "me.mementomorri.MainKt"
  34. kotlin {
  35. jvm("backend") {
  36. withJava()
  37. compilations.all {
  38. kotlinOptions {
  39. jvmTarget = "1.8"
  40. freeCompilerArgs = listOf("-Xjsr305=strict")
  41. }
  42. }
  43. }
  44. js("frontend") {
  45. browser {
  46. runTask {
  47. outputFileName = "main.bundle.js"
  48. sourceMaps = false
  49. devServer = KotlinWebpackConfig.DevServer(
  50. open = false,
  51. port = 3000,
  52. proxy = mutableMapOf(
  53. "/kv/*" to "http://localhost:8080",
  54. "/kvws/*" to mapOf("target" to "ws://localhost:8080", "ws" to true)
  55. ),
  56. static = mutableListOf("$buildDir/processedResources/frontend/main")
  57. )
  58. }
  59. webpackTask {
  60. outputFileName = "main.bundle.js"
  61. }
  62. testTask {
  63. useKarma {
  64. useChromeHeadless()
  65. }
  66. }
  67. }
  68. binaries.executable()
  69. }
  70. sourceSets {
  71. val commonMain by getting {
  72. dependencies {
  73. api("io.kvision:kvision-server-spring-boot:$kvisionVersion")
  74. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
  75. }
  76. kotlin.srcDir("build/generated-src/common")
  77. }
  78. val commonTest by getting {
  79. dependencies {
  80. implementation(kotlin("test-common"))
  81. implementation(kotlin("test-annotations-common"))
  82. }
  83. }
  84. val backendMain by getting {
  85. dependencies {
  86. implementation(kotlin("stdlib-jdk8"))
  87. implementation(kotlin("reflect"))
  88. implementation("org.springframework.boot:spring-boot-starter")
  89. implementation("org.springframework.boot:spring-boot-devtools")
  90. implementation("org.springframework.boot:spring-boot-starter-webflux")
  91. }
  92. }
  93. val backendTest by getting {
  94. dependencies {
  95. implementation(kotlin("test"))
  96. implementation(kotlin("test-junit"))
  97. implementation("org.springframework.boot:spring-boot-starter-test")
  98. }
  99. }
  100. val frontendMain by getting {
  101. resources.srcDir(webDir)
  102. dependencies {
  103. implementation("io.kvision:kvision:$kvisionVersion")
  104. implementation("io.kvision:kvision-bootstrap:$kvisionVersion")
  105. implementation("io.kvision:kvision-bootstrap-css:$kvisionVersion")
  106. implementation("io.kvision:kvision-datacontainer:$kvisionVersion")
  107. }
  108. kotlin.srcDir("build/generated-src/frontend")
  109. }
  110. val frontendTest by getting {
  111. dependencies {
  112. implementation(kotlin("test-js"))
  113. implementation("io.kvision:kvision-testutils:$kvisionVersion")
  114. }
  115. }
  116. }
  117. }
  118. fun getNodeJsBinaryExecutable(): String {
  119. val nodeDir = NodeJsRootPlugin.apply(rootProject).nodeJsSetupTaskProvider.get().destination
  120. val isWindows = System.getProperty("os.name").toLowerCase().contains("windows")
  121. val nodeBinDir = if (isWindows) nodeDir else nodeDir.resolve("bin")
  122. val command = NodeJsRootPlugin.apply(rootProject).nodeCommand
  123. val finalCommand = if (isWindows && command == "node") "node.exe" else command
  124. return nodeBinDir.resolve(finalCommand).absolutePath
  125. }
  126. tasks {
  127. create("generatePotFile", Exec::class) {
  128. dependsOn("compileKotlinFrontend")
  129. executable = getNodeJsBinaryExecutable()
  130. args("${rootProject.buildDir}/js/node_modules/gettext-extract/bin/gettext-extract")
  131. inputs.files(kotlin.sourceSets["frontendMain"].kotlin.files)
  132. outputs.file("$projectDir/src/frontendMain/resources/i18n/messages.pot")
  133. }
  134. }
  135. afterEvaluate {
  136. tasks {
  137. getByName("frontendProcessResources", Copy::class) {
  138. dependsOn("compileKotlinFrontend")
  139. exclude("**/*.pot")
  140. doLast("Convert PO to JSON") {
  141. destinationDir.walkTopDown().filter {
  142. it.isFile && it.extension == "po"
  143. }.forEach {
  144. exec {
  145. executable = getNodeJsBinaryExecutable()
  146. args(
  147. "${rootProject.buildDir}/js/node_modules/gettext.js/bin/po2json",
  148. it.absolutePath,
  149. "${it.parent}/${it.nameWithoutExtension}.json"
  150. )
  151. println("Converted ${it.name} to ${it.nameWithoutExtension}.json")
  152. }
  153. it.delete()
  154. }
  155. }
  156. }
  157. create("frontendArchive", Jar::class).apply {
  158. dependsOn("frontendBrowserProductionWebpack")
  159. group = "package"
  160. archiveAppendix.set("frontend")
  161. val distribution =
  162. project.tasks.getByName("frontendBrowserProductionWebpack", KotlinWebpack::class).destinationDirectory!!
  163. from(distribution) {
  164. include("*.*")
  165. }
  166. from(webDir)
  167. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  168. into("/public")
  169. inputs.files(distribution, webDir)
  170. outputs.file(archiveFile)
  171. manifest {
  172. attributes(
  173. mapOf(
  174. "Implementation-Title" to rootProject.name,
  175. "Implementation-Group" to rootProject.group,
  176. "Implementation-Version" to rootProject.version,
  177. "Timestamp" to System.currentTimeMillis()
  178. )
  179. )
  180. }
  181. }
  182. getByName("backendProcessResources", Copy::class) {
  183. duplicatesStrategy = DuplicatesStrategy.EXCLUDE
  184. }
  185. getByName("bootJar", BootJar::class) {
  186. dependsOn("frontendArchive", "backendMainClasses")
  187. classpath = files(
  188. kotlin.targets["backend"].compilations["main"].output.allOutputs +
  189. project.configurations["backendRuntimeClasspath"] +
  190. (project.tasks["frontendArchive"] as Jar).archiveFile
  191. )
  192. }
  193. getByName("jar", Jar::class).apply {
  194. dependsOn("bootJar")
  195. }
  196. getByName("bootRun", BootRun::class) {
  197. dependsOn("backendMainClasses")
  198. classpath = files(
  199. kotlin.targets["backend"].compilations["main"].output.allOutputs +
  200. project.configurations["backendRuntimeClasspath"]
  201. )
  202. }
  203. create("backendRun") {
  204. dependsOn("bootRun")
  205. group = "run"
  206. }
  207. getByName("compileKotlinBackend") {
  208. dependsOn("compileKotlinMetadata")
  209. }
  210. getByName("compileKotlinFrontend") {
  211. dependsOn("compileKotlinMetadata")
  212. }
  213. }
  214. }
  215. //tasks.create("stage") {
  216. // dependsOn("jar")
  217. //}