From 7da26c4b23a457f48961cf1293b2ae2effc0d258 Mon Sep 17 00:00:00 2001 From: "aman.kapoor" Date: Tue, 8 Feb 2022 15:48:38 +0530 Subject: [PATCH] Gradle Extensions for uncluttering build files --- app/build.gradle.kts | 42 ++----------------- .../logvue/utils/GradleExtensions.kt | 41 ++++++++++++++++++ 2 files changed, 45 insertions(+), 38 deletions(-) create mode 100644 buildSrc/src/main/kotlin/com/voxfinite/logvue/utils/GradleExtensions.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index d0fcf8b..cd20c33 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -1,6 +1,9 @@ import org.jetbrains.compose.compose import org.jetbrains.compose.desktop.application.dsl.TargetFormat import com.voxfinite.logvue.Dependencies +import com.voxfinite.logvue.utils.findPkg +import com.voxfinite.logvue.utils.ghActionOutput +import com.voxfinite.logvue.utils.getMainAppVersion val pluginsDir: File by rootProject.extra @@ -13,7 +16,7 @@ plugins { val r8: Configuration by configurations.creating group = "com.voxfinite" -version = appVersion() +version = project.getMainAppVersion() val appName = "logvue" val appMainClass = "com.voxfinite.logvue.app.MainKt" @@ -154,40 +157,3 @@ gradle.buildFinished { nativePkg.ghActionOutput("app_pkg") jarPkg.ghActionOutput("uber_jar") } - -fun File.findPkg(format: String?) = when (format != null) { - true -> walk().firstOrNull { it.isFile && it.name.endsWith(format, ignoreCase = true) } - else -> null -} - -fun File?.ghActionOutput(prefix: String) = this?.let { - when (System.getenv("GITHUB_ACTIONS").toBoolean()) { - true -> println( - """ - ::set-output name=${prefix}_name::${it.name} - ::set-output name=${prefix}_path::${it.absolutePath} - """.trimIndent() - ) - else -> println("$prefix: $this") - } -} - -fun appVersion() : String { - val key = "APP_VERSION" - return if (project.hasProperty(key)) { - val version = project.property("APP_VERSION").toString() - println("Version = $version") - if (version.isBlank()) { - return "1.0.0" - } - if (version.matches(Regex("^[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,4}"))) { - return version - } - if (version.matches(Regex("^v[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,4}"))) { - return version.removePrefix("v") - } - "1.0.0" - } else { - "1.0.0" - } -} diff --git a/buildSrc/src/main/kotlin/com/voxfinite/logvue/utils/GradleExtensions.kt b/buildSrc/src/main/kotlin/com/voxfinite/logvue/utils/GradleExtensions.kt new file mode 100644 index 0000000..b621371 --- /dev/null +++ b/buildSrc/src/main/kotlin/com/voxfinite/logvue/utils/GradleExtensions.kt @@ -0,0 +1,41 @@ +package com.voxfinite.logvue.utils + +import org.gradle.api.Project +import java.io.File + +fun Project.getMainAppVersion() : String { + val key = "APP_VERSION" + return if (hasProperty(key)) { + val version = property("APP_VERSION").toString() + println("Version = $version") + if (version.isBlank()) { + return "1.0.0" + } + if (version.matches(Regex("^[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,4}"))) { + return version + } + if (version.matches(Regex("^v[\\d]{1,3}.[\\d]{1,3}.[\\d]{1,4}"))) { + return version.removePrefix("v") + } + "1.0.0" + } else { + "1.0.0" + } +} + +fun File.findPkg(format: String?) = when (format != null) { + true -> walk().firstOrNull { it.isFile && it.name.endsWith(format, ignoreCase = true) } + else -> null +} + +fun File?.ghActionOutput(prefix: String) = this?.let { + when (System.getenv("GITHUB_ACTIONS").toBoolean()) { + true -> println( + """ + ::set-output name=${prefix}_name::${it.name} + ::set-output name=${prefix}_path::${it.absolutePath} + """.trimIndent() + ) + else -> println("$prefix: $this") + } +} \ No newline at end of file