11import org.gradle.api.tasks.testing.logging.TestExceptionFormat
22import java.nio.file.Files
33import java.nio.file.Paths
4+ import java.util.Locale
45import kotlin.io.path.absolutePathString
56import kotlin.io.path.createTempDirectory
67import kotlin.io.path.isExecutable
@@ -11,11 +12,23 @@ plugins {
1112 jacoco
1213}
1314
15+ enum class OS {
16+ MAC , WINDOWS , LINUX
17+ }
18+
19+ val currentOS = when {
20+ System .getProperty(" os.name" ).lowercase().contains(" mac" ) -> OS .MAC
21+ System .getProperty(" os.name" ).lowercase().contains(" windows" ) -> OS .WINDOWS
22+ else -> OS .LINUX
23+ }
24+
1425
1526val isNotarizing = System .getenv(" NOTARIZE" ) != null || gradle.startParameter.taskNames.find { s ->
1627 s.lowercase().contains(" notarize" ) || s.lowercase().contains(" jpackage" ) || s.lowercase().contains(" staple" )
1728} != null
1829
30+
31+
1932val provisionprofileDir = layout.projectDirectory
2033 .dir(" mac-resources" )
2134 .dir(" provisionprofile" )
@@ -75,7 +88,28 @@ java {
7588 }
7689}
7790
91+ tasks.register<Exec >(" compileWindowsApi" ) {
92+ onlyIf {
93+ currentOS == OS .WINDOWS
94+ }
95+ group = " build"
96+ description = " Compile C code and output the dll to the resource directory."
97+
98+ commandLine(
99+ " gcc" ,
100+ " -shared" ,
101+ " -o" ,
102+ " ./src/main/resources/native/WindowsApi.dll" ,
103+ " ./src/main/c/WindowsApi.c" ,
104+ " -lcomdlg32" ,
105+ " -lgdi32"
106+ )
107+ }
108+
78109tasks.register<Exec >(" compileSwift" ) {
110+ onlyIf {
111+ System .getProperty(" os.name" ).lowercase().contains(" mac" )
112+ }
79113 group = " build"
80114 description = " Compile Swift code and output the dylib to the resource directory."
81115
@@ -96,7 +130,11 @@ tasks.register<Exec>("compileSwift") {
96130}
97131
98132tasks.named<JavaCompile >(" compileJava" ) {
99- dependsOn(" compileSwift" )
133+ if (currentOS == OS .MAC ) {
134+ dependsOn(" compileSwift" )
135+ } else if (currentOS == OS .WINDOWS ) {
136+ dependsOn(" compileWindowsApi" )
137+ }
100138 options.compilerArgs.addAll(listOf (
101139 " --add-exports" ,
102140 " java.base/sun.security.x509=ALL-UNNAMED" ,
@@ -148,24 +186,28 @@ tasks.named<Test>("test") {
148186var mainClassName = " tanin.javaelectron.Main"
149187application {
150188 mainClass.set(mainClassName)
151- applicationDefaultJvmArgs = listOf (
152- " -XstartOnFirstThread" ,
153- " --add-exports" ,
154- " java.base/sun.security.x509=ALL-UNNAMED" ,
155- " --add-exports" ,
156- " java.base/sun.security.tools.keytool=ALL-UNNAMED" ,
157- )
189+ applicationDefaultJvmArgs = buildList {
190+ if (currentOS == OS .MAC ) {
191+ add(" -XstartOnFirstThread" )
192+ }
193+ add(" --add-exports" )
194+ add(" java.base/sun.security.x509=ALL-UNNAMED" )
195+ add(" --add-exports" )
196+ add(" java.base/sun.security.tools.keytool=ALL-UNNAMED" )
197+ }
158198}
159199
160200tasks.jar {
161201 manifest.attributes[" Main-Class" ] = mainClassName
162202}
163203
204+ val executableExt = if (currentOS == OS .WINDOWS ) " .cmd" else " "
205+
164206tasks.register<Exec >(" compileTailwind" ) {
165207 environment(" NODE_ENV" , " production" )
208+ executable = " ./node_modules/.bin/postcss${executableExt} "
166209
167- commandLine(
168- " ./node_modules/.bin/postcss" ,
210+ args = listOf (
169211 " ./frontend/stylesheets/tailwindbase.css" ,
170212 " --config" ,
171213 " ." ,
@@ -177,9 +219,9 @@ tasks.register<Exec>("compileTailwind") {
177219tasks.register<Exec >(" compileSvelte" ) {
178220 environment(" NODE_ENV" , " production" )
179221 environment(" ENABLE_SVELTE_CHECK" , " true" )
222+ executable = " ./node_modules/.bin/webpack${executableExt} "
180223
181- commandLine(
182- " ./node_modules/webpack/bin/webpack.js" ,
224+ args = listOf (
183225 " --config" ,
184226 " ./webpack.config.js" ,
185227 " --output-path" ,
@@ -398,6 +440,13 @@ tasks.register("bareJpackage") {
398440 outputs.file(outputFile)
399441 outputDir.get().asFile.deleteRecursively()
400442
443+ // -XstartOnFirstThread is required for MacOS
444+ val maybeStartOnFirstThread = if (currentOS == OS .MAC ) {
445+ " -XstartOnFirstThread"
446+ } else {
447+ " "
448+ }
449+
401450 doLast {
402451 runCmd(
403452 jpackageBin.absolutePathString(),
@@ -418,9 +467,8 @@ tasks.register("bareJpackage") {
418467 " --app-content" , provisionprofileDir.file(" embedded.provisionprofile" ).asFile.absolutePath,
419468 " --app-content" , layout.buildDirectory.file(" resources-native" ).get().asFile.resolve(" app" ).absolutePath,
420469 " --java-options" ,
421- // -XstartOnFirstThread is required for MacOs
422470 // -Djava.library.path=$APPDIR/resources is needed because we put all dylibs there.
423- " -XstartOnFirstThread -Djava.library.path=\$ APPDIR/resources --add-exports java.base/sun.security.x509=ALL-UNNAMED --add-exports java.base/sun.security.tools.keytool=ALL-UNNAMED"
471+ " $maybeStartOnFirstThread -Djava.library.path=\$ APPDIR/resources --add-exports java.base/sun.security.x509=ALL-UNNAMED --add-exports java.base/sun.security.tools.keytool=ALL-UNNAMED"
424472 )
425473 }
426474}
0 commit comments