-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
433 lines (330 loc) · 13 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.kapt)
alias(libs.plugins.kotlin.serialization)
id("java")
id("application")
alias(libs.plugins.gmazzo.buildconfig)
alias(libs.plugins.touchportal.plugin.packager)
}
/* Versions follow Semantic Versioning (https://semver.org) for most things and a simple version number for inside Touch Portal
* <version>-<pre-release>+<metadata>
* Full = 1.1.2
* Pre-release = alpha.<build date>-<build time> or beta.<build date>
* Metadata = resources bundle (eg debug/trace)
* eg. 1.0.4-alpha.20241103-1234+debug or 1.0.4-beta.20241205+debug
*/
val versionMajor: Int = 0
val versionMinor: Int = 7
val versionPatch: Int = 1
// Chooses which resources bundle to include in IDE Testing - e.g. debug/trace and metadata extension for non-release builds
// Mainly for choosing logging options - "DEBUG" by default, change to "TRACE" needed
project.extra["resourcesBundle"] = "DEBUG"
val pluginFullName: String = "Veadotube Touch Portal Plugin"
val pluginShortName: String = "Veadotube Plugin"
val mainClassSimpleName: String = "VeadoTouchPlugin"
val mainClassPackage: String = "io.github.dissonantau.veadotubetouchportalplugin"
group = mainClassPackage
tpPlugin.mainClassSimpleName.set(mainClassSimpleName)
/* Build Dirs */
val buildsDir: Directory = rootProject.layout.projectDirectory.dir("pluginBuilds")
println("veadotube PluginBuilds Dir: $buildsDir")
val resourcesMain: Directory = layout.projectDirectory.dir("src/main/resources")
val resourcesRelease: Directory = layout.projectDirectory.dir("src/release/resources")
val resourcesDebug: Directory = layout.projectDirectory.dir("src/debug/resources")
val resourcesTrace: Directory = layout.projectDirectory.dir("src/trace/resources")
/* Gradle defined run task */
application.mainClass = "$mainClassPackage.$mainClassSimpleName"
tasks.run<JavaExec> {
dependsOn(tasks.packagePlugin)
args(listOf("start"))
workingDir =
project.layout.buildDirectory.get().dir("plugin").dir(mainClassSimpleName).asFile
}
project.extra["releaseName"] = mainClassSimpleName
println("Release project: ${project.extra["releaseName"]}")
// Version becomes 1203
val versionCode: Int = versionMajor * 1000 + versionMinor * 100 + versionPatch
project.extra["versionCode"] = versionCode
println("Version Code: $versionCode")
// Version Base Name becomes 1.2.3, doesn't change
val versionBaseName = "$versionMajor.$versionMinor.$versionPatch"
project.extra["versionBaseName"] = versionBaseName
project.version = versionBaseName
// Version Semantic Name - becomes 1.2.3-alpha etc. - will be updated as needed
project.extra["versionName"] = "$versionMajor.$versionMinor.$versionPatch-snapshot"
val versionSemanticProvider: Provider<String> = provider { "${project.extra["versionName"]}" }
// Changed by tasks if needed
project.extra["releaseBuild"] = false
val releaseBuildProvider: Provider<Boolean> = provider { project.extra["releaseBuild"] as Boolean }
// whether this is alpha/beta. Ignored during Build Release
project.extra["preReleaseVersion"] = "snapshot"
val preReleaseVersionProvider: Provider<String> = provider { "${project.extra["preReleaseVersion"]}" }
val resourcesBundleProvider: Provider<String> = provider { "${project.extra["resourcesBundle"]}" }
updateReleaseType()
generateSemanticVersion(releaseBuildProvider, preReleaseVersionProvider, resourcesBundleProvider)
setMainResources(resourcesBundleProvider)
buildConfig {
packageName.set(project.group.toString())
buildConfigField("String", "NAME", "\"$pluginFullName\"")
buildConfigField("String", "NAME_SHORT", "\"$pluginShortName\"")
buildConfigField("String", "VERSION_BASE_NAME", "\"${project.extra["versionBaseName"]}\"")
buildConfigField("String", "VERSION_NAME", provider { "\"${project.extra["versionName"]}\"" })
buildConfigField("long", "VERSION_CODE", "${project.extra["versionCode"]}")
}
repositories {
mavenCentral()
}
dependencies {
/* Main Dependencies */
// BleatKan
implementation(libs.bleatkan)
// Touch Portal
implementation(libs.touchportal.plugin.sdk)
kapt(libs.touchportal.plugin.sdk.processor)
// Kotlin BOM
runtimeOnly(libs.kotlin.bom)
implementation(platform(libs.kotlin.gradle.plugins.bom))
// Coroutines - concurrent library
//implementation(libs.kotlinx.coroutines.bom)
//implementation(libs.kotlinx.coroutines.core)
//runtimeOnly(libs.kotlinx.coroutines.slf4j)
// Log4J
implementation(platform(libs.log4j.bom))
implementation(libs.log4j.core)
implementation(libs.log4j.api)
implementation(libs.log4j.slf4j2.impl)
// Generic Logging Interface that can use Log4J
implementation(platform(libs.slf4j.bom))
implementation(libs.slf4j.api)
implementation(libs.logging.kotlin) //Kotlin Wrapper for slf4j
// Apache Commons Collections 4 - mainly for LRUMap
// https://mvnrepository.com/artifact/org.apache.commons/commons-collections4
implementation(libs.apache.commons.collections4)
/* Testing Dependencies */
testImplementation(libs.kotlin.test)
//testImplementation(libs.kotlinx.coroutines.debug)
testImplementation(libs.kotlin.test.junit5)
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)
testRuntimeOnly(libs.junit.platform.launcher)
testImplementation(libs.mockk)
}
kotlin {
//jvmToolchain(8)
compilerOptions {
javaParameters = true // Needed for TP SDK to get annotated parameter names correctly
jvmTarget.set(JvmTarget.JVM_1_8)
apiVersion.set(KotlinVersion.KOTLIN_2_0)
languageVersion.set(KotlinVersion.KOTLIN_2_0)
}
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks {
// Calculate Version/Build names, etc.
register("calculatePluginVersion") {
doFirst {
updateReleaseType()
generateSemanticVersion(releaseBuildProvider, preReleaseVersionProvider, resourcesBundleProvider)
}
doLast {
setMainResources(resourcesBundleProvider)
}
}
// Run Version Calculation during Build, including IDE Import/refresh
// targeting BuildConfigTask - might not be needed anymore?
//project.tasks.withType(BuildConfigTask::class).forEach {
// println("Add calculatePluginVersion to dependsOn $name > Class ${javaClass.name}")
// it.dependsOn(
// named("calculateVersion")
// )
//}
named<Copy>("processResources") {
duplicatesStrategy = DuplicatesStrategy.WARN
}
/* Task to copy to PluginBuilds after build */
register<Copy>("copyToPluginBuilds") {
duplicatesStrategy = DuplicatesStrategy.WARN
mustRunAfter(
named("calculatePluginVersion")
)
doFirst {
println("Copy to '${buildsDir.dir("${project.extra["releaseName"]}_${project.extra["versionBaseName"]}")}'")
}
dependsOn(
named("calculatePluginVersion"),
packagePlugin
)
from(packagePlugin)
into { buildsDir.dir("${project.extra["releaseName"]}_${project.extra["versionBaseName"]}") }
rename { filename ->
val newFilename = filename.replace(".tpp", "_${project.extra["versionName"]}.tpp")
println("Copy $filename to $newFilename")
newFilename
}
}
/* Meta Build Jobs */
register("buildCopyBetaTraceToPluginBuilds") {
group = "build"
doFirst {
println("Set to Beta Trace Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "TRACE"
project.extra["preReleaseVersion"] = "beta"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyAlphaTraceToPluginBuilds") {
group = "build"
doFirst {
println("Set to Alpha Trace Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "TRACE"
project.extra["preReleaseVersion"] = "alpha"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyBetaDebugToPluginBuilds") {
group = "build"
doFirst {
println("Set to Beta Debug Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "DEBUG"
project.extra["preReleaseVersion"] = "beta"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyAlphaDebugToPluginBuilds") {
group = "build"
doFirst {
println("Set to Alpha Debug Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "DEBUG"
project.extra["preReleaseVersion"] = "alpha"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyBetaInfoToPluginBuilds") {
group = "build"
doFirst {
println("Set to Beta Debug Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "INFO"
project.extra["preReleaseVersion"] = "beta"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyAlphaInfoToPluginBuilds") {
group = "build"
doFirst {
println("Set to Alpha Debug Build")
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "INFO"
project.extra["preReleaseVersion"] = "alpha"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
register("buildCopyReleaseToPluginBuilds") {
group = "build"
doFirst {
println("Set to Release Build")
project.extra["releaseBuild"] = true
project.extra["resourcesBundle"] = "INFO"
}
finalizedBy(
named("copyToPluginBuilds"),
)
}
test {
useJUnitPlatform()
}
}
fun updateReleaseType() {
// Get Environment Var if it exists
try {
val envBuildType: String = System.getenv("BUILD_TYPE")
// Release Type - Variable from Build Tasks > Environment Var > Debug Default
if (envBuildType.isNotBlank()) {
println("Release Type: $envBuildType")
when (envBuildType) {
"RELEASE" -> {
project.extra["releaseBuild"] = true
project.extra["resourcesBundle"] = "INFO"
}
"TRACE" -> {
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "TRACE"
}
else -> {
// DEBUG or TRACE
project.extra["releaseBuild"] = false
project.extra["resourcesBundle"] = "DEBUG"
}
}
}
} catch (_: Throwable) {
}
}
fun generateSemanticVersion(
releaseBuildProvider: Provider<Boolean>,
preReleaseVersionProvider: Provider<String>,
resourcesBundleProvider: Provider<String>
) {
val genVersionSemantic =
if (releaseBuildProvider.get()) "${project.extra["versionBaseName"]}"
else {
println("Base Version Name: ${project.extra["versionBaseName"]}")
val preReleaseVersion = preReleaseVersionProvider.get()
println("Pre-release Version: $preReleaseVersion")
val pattern = when (preReleaseVersion) {
"beta" -> "yyyyMMdd"
else -> "yyyyMMdd-HHmm"
}
val timeOfBuild = DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.now())
println("Time of Build: $timeOfBuild")
val resourcesBundle = resourcesBundleProvider.get()
val metadata = if (resourcesBundle.isNotBlank()) {
if (resourcesBundle.isNotBlank()) println("Resources Metadata: $resourcesBundle")
"+${resourcesBundle.lowercase()}"
} else ""
"$versionMajor.$versionMinor.$versionPatch-$preReleaseVersion.${timeOfBuild}$metadata"
}
project.extra["versionName"] = genVersionSemantic
project.version = genVersionSemantic
println("Version Updated Name: $genVersionSemantic")
}
fun setMainResources(release: Provider<String>) {
val releaseType = release.get()
println("Assign Source Set Resources - currently $releaseType")
sourceSets.main.get().resources.setSrcDirs(listOf(resourcesMain))
sourceSets.main {
resources {
when (releaseType) {
"INFO" -> srcDir(resourcesRelease)
"TRACE" -> srcDir(resourcesTrace)
"DEBUG" -> srcDir(resourcesDebug)
"SNAPSHOT" -> srcDir(resourcesDebug)
else -> throw IllegalArgumentException("Missing Resources Tag")
}
}
}
}