From c4d8ef427cf1f5cff12b435c7bb693527733db5e Mon Sep 17 00:00:00 2001
From: Janne Mareike Koschinski <mail@justjanne.de>
Date: Fri, 22 Nov 2024 21:42:25 +0100
Subject: [PATCH] chore: update gradle conventions

---
 .../kotlin/AndroidApplicationConvention.kt    | 51 ++++++++++++++----
 .../main/kotlin/AndroidLibraryConvention.kt   | 28 +++++++++-
 .../main/kotlin/KotlinAndroidConvention.kt    | 28 ++++++++--
 .../src/main/kotlin/KotlinConvention.kt       | 28 ++++++++--
 .../src/main/kotlin/SigningConvention.kt      | 21 +++++++-
 .../kotlin/util/BaseExtensionExtensions.kt    |  8 ---
 .../src/main/kotlin/util/ProjectExtensions.kt | 52 +++++++++++--------
 7 files changed, 167 insertions(+), 49 deletions(-)
 delete mode 100644 gradle/convention/src/main/kotlin/util/BaseExtensionExtensions.kt

diff --git a/gradle/convention/src/main/kotlin/AndroidApplicationConvention.kt b/gradle/convention/src/main/kotlin/AndroidApplicationConvention.kt
index 0e5092c4b..108982e11 100644
--- a/gradle/convention/src/main/kotlin/AndroidApplicationConvention.kt
+++ b/gradle/convention/src/main/kotlin/AndroidApplicationConvention.kt
@@ -1,7 +1,27 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 import com.android.build.api.dsl.ApplicationExtension
 import org.gradle.api.JavaVersion
 import org.gradle.api.Plugin
 import org.gradle.api.Project
+import org.gradle.api.plugins.BasePluginExtension
 import org.gradle.kotlin.dsl.configure
 import util.git
 import java.util.*
@@ -15,6 +35,18 @@ class AndroidApplicationConvention : Plugin<Project> {
         apply("justjanne.signing")
       }
 
+      val commit = git("rev-parse", "HEAD").orNull
+      val code = git("rev-list", "--count", "HEAD", "--tags").orNull
+      val name = git("describe", "--always", "--tags", "HEAD").orNull
+
+      extensions.configure<BasePluginExtension> {
+        if (name == null) {
+          archivesName.set("${rootProject.name}")
+        } else {
+          archivesName.set("${rootProject.name}-$name")
+        }
+      }
+
       extensions.configure<ApplicationExtension> {
           compileSdk = 35
 
@@ -24,23 +56,18 @@ class AndroidApplicationConvention : Plugin<Project> {
 
           applicationId = "${rootProject.group}.${rootProject.name.lowercase(Locale.ROOT)}"
 
-          val commit = git("rev-parse", "HEAD")
-          val name = git("describe", "--always", "--tags", "HEAD")
-
-          versionCode = git("rev-list", "--count", "HEAD", "--tags")?.toIntOrNull()
-          versionName = git("describe", "--always", "--tags", "HEAD")
+          versionCode = code?.toIntOrNull()
+          versionName = name
 
           val fancyVersionName = if (commit == null || name == null) name
           else "<a href=\\\"https://git.kuschku.de/justJanne/QuasselDroid-ng/commit/$commit\\\">$name</a>"
 
-          buildConfigField("String", "GIT_HEAD", "\"${git("rev-parse", "HEAD") ?: ""}\"")
+          buildConfigField("String", "GIT_HEAD", "\"${git("rev-parse", "HEAD").getOrElse("")}\"")
           buildConfigField("String", "FANCY_VERSION_NAME", "\"${fancyVersionName ?: ""}\"")
-          buildConfigField("long", "GIT_COMMIT_DATE", "${git("show", "-s", "--format=%ct") ?: 0}L")
+          buildConfigField("long", "GIT_COMMIT_DATE", "${git("show", "-s", "--format=%ct").getOrElse("0")}L")
 
           signingConfig = signingConfigs.findByName("default")
 
-          setProperty("archivesBaseName", "${rootProject.name}-$versionName")
-
           // Disable test runner analytics
           testInstrumentationRunnerArguments["disableAnalytics"] = "true"
         }
@@ -58,6 +85,12 @@ class AndroidApplicationConvention : Plugin<Project> {
           unitTests.isIncludeAndroidResources = true
         }
 
+        packaging {
+          resources {
+            excludes.add("/META-INF/*.md")
+          }
+        }
+
         lint {
           warningsAsErrors = true
           lintConfig = file("../lint.xml")
diff --git a/gradle/convention/src/main/kotlin/AndroidLibraryConvention.kt b/gradle/convention/src/main/kotlin/AndroidLibraryConvention.kt
index d5ffa5464..d647c8ed3 100644
--- a/gradle/convention/src/main/kotlin/AndroidLibraryConvention.kt
+++ b/gradle/convention/src/main/kotlin/AndroidLibraryConvention.kt
@@ -1,9 +1,27 @@
-import org.gradle.api.JavaVersion
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 import com.android.build.api.dsl.LibraryExtension
+import org.gradle.api.JavaVersion
 import org.gradle.api.Plugin
 import org.gradle.api.Project
 import org.gradle.kotlin.dsl.configure
-import java.util.*
 
 class AndroidLibraryConvention : Plugin<Project> {
   override fun apply(target: Project) {
@@ -30,6 +48,12 @@ class AndroidLibraryConvention : Plugin<Project> {
           targetCompatibility = JavaVersion.VERSION_11
         }
 
+        packaging {
+          resources {
+            excludes.add("/META-INF/*.md")
+          }
+        }
+
         lint {
           warningsAsErrors = true
           lintConfig = file("../lint.xml")
diff --git a/gradle/convention/src/main/kotlin/KotlinAndroidConvention.kt b/gradle/convention/src/main/kotlin/KotlinAndroidConvention.kt
index 26ea31548..20caf9cf4 100644
--- a/gradle/convention/src/main/kotlin/KotlinAndroidConvention.kt
+++ b/gradle/convention/src/main/kotlin/KotlinAndroidConvention.kt
@@ -1,3 +1,22 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 import org.gradle.api.JavaVersion
 import org.gradle.api.Plugin
 import org.gradle.api.Project
@@ -7,6 +26,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
 import org.gradle.kotlin.dsl.configure
 import org.gradle.kotlin.dsl.provideDelegate
 import org.gradle.kotlin.dsl.withType
+import org.jetbrains.kotlin.gradle.dsl.JvmTarget
 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 
 class KotlinAndroidConvention : Plugin<Project> {
@@ -20,14 +40,14 @@ class KotlinAndroidConvention : Plugin<Project> {
 
       // Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
       tasks.withType<KotlinCompile>().configureEach {
-        kotlinOptions {
+        compilerOptions {
           // Set JVM target to 11
-          jvmTarget = JavaVersion.VERSION_11.toString()
+          jvmTarget.set(JvmTarget.JVM_11)
           // Treat all Kotlin warnings as errors (disabled by default)
           // Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
           val warningsAsErrors: String? by target
-          allWarningsAsErrors = warningsAsErrors.toBoolean()
-          freeCompilerArgs = freeCompilerArgs + listOf(
+          allWarningsAsErrors.set(warningsAsErrors.toBoolean())
+          freeCompilerArgs.add(
             "-opt-in=kotlin.ExperimentalUnsignedTypes"
           )
         }
diff --git a/gradle/convention/src/main/kotlin/KotlinConvention.kt b/gradle/convention/src/main/kotlin/KotlinConvention.kt
index 378f59d7f..0c8b834d4 100644
--- a/gradle/convention/src/main/kotlin/KotlinConvention.kt
+++ b/gradle/convention/src/main/kotlin/KotlinConvention.kt
@@ -1,3 +1,22 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 import org.gradle.api.JavaVersion
 import org.gradle.api.Plugin
 import org.gradle.api.Project
@@ -7,6 +26,7 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
 import org.gradle.kotlin.dsl.configure
 import org.gradle.kotlin.dsl.provideDelegate
 import org.gradle.kotlin.dsl.withType
+import org.jetbrains.kotlin.gradle.dsl.JvmTarget
 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
 
 class KotlinConvention : Plugin<Project> {
@@ -20,14 +40,14 @@ class KotlinConvention : Plugin<Project> {
 
       // Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
       tasks.withType<KotlinCompile>().configureEach {
-        kotlinOptions {
+        compilerOptions {
           // Set JVM target to 11
-          jvmTarget = JavaVersion.VERSION_11.toString()
+          jvmTarget.set(JvmTarget.JVM_11)
           // Treat all Kotlin warnings as errors (disabled by default)
           // Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
           val warningsAsErrors: String? by target
-          allWarningsAsErrors = warningsAsErrors.toBoolean()
-          freeCompilerArgs = freeCompilerArgs + listOf(
+          allWarningsAsErrors.set(warningsAsErrors.toBoolean())
+          freeCompilerArgs.add(
             "-opt-in=kotlin.ExperimentalUnsignedTypes"
           )
         }
diff --git a/gradle/convention/src/main/kotlin/SigningConvention.kt b/gradle/convention/src/main/kotlin/SigningConvention.kt
index d2813f220..c0c4a3e15 100644
--- a/gradle/convention/src/main/kotlin/SigningConvention.kt
+++ b/gradle/convention/src/main/kotlin/SigningConvention.kt
@@ -1,3 +1,22 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 import com.android.build.api.dsl.ApplicationExtension
 import org.gradle.api.Plugin
 import org.gradle.api.Project
@@ -14,7 +33,7 @@ class SigningConvention : Plugin<Project> {
 
       extensions.configure<ApplicationExtension> {
         signingConfigs {
-          SigningData.of(rootProject.properties("signing.properties"))?.let {
+          SigningData.of(rootProject.properties("signing.properties").orNull)?.let {
             create("default") {
               storeFile = file(it.storeFile)
               storePassword = it.storePassword
diff --git a/gradle/convention/src/main/kotlin/util/BaseExtensionExtensions.kt b/gradle/convention/src/main/kotlin/util/BaseExtensionExtensions.kt
deleted file mode 100644
index 667f8dac0..000000000
--- a/gradle/convention/src/main/kotlin/util/BaseExtensionExtensions.kt
+++ /dev/null
@@ -1,8 +0,0 @@
-package util
-
-import com.android.build.gradle.BaseExtension
-import org.gradle.api.plugins.ExtensionAware
-import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions
-
-fun BaseExtension.kotlinOptions(configure: KotlinJvmOptions.() -> Unit): Unit =
-  (this as ExtensionAware).extensions.configure("kotlinOptions", configure)
diff --git a/gradle/convention/src/main/kotlin/util/ProjectExtensions.kt b/gradle/convention/src/main/kotlin/util/ProjectExtensions.kt
index 6352bbfe7..6110edeac 100644
--- a/gradle/convention/src/main/kotlin/util/ProjectExtensions.kt
+++ b/gradle/convention/src/main/kotlin/util/ProjectExtensions.kt
@@ -1,26 +1,36 @@
+/*
+ * Quasseldroid - Quassel client for Android
+ *
+ * Copyright (c) 2024 Janne Mareike Koschinski
+ * Copyright (c) 2024 The Quassel Project
+ *
+ * This program is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 3 as published
+ * by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
 package util
 
 import org.gradle.api.Project
-import java.io.ByteArrayOutputStream
-import java.util.Properties
+import org.gradle.api.provider.Provider
+import java.util.*
+
 
-fun Project.git(vararg command: String) = try {
-  val stdOut = ByteArrayOutputStream()
-  exec {
-    commandLine("git", *command)
-    standardOutput = stdOut
-  }
-  stdOut.toString(Charsets.UTF_8.name()).trim()
-} catch (e: Throwable) {
-  e.printStackTrace()
-  null
-}
+fun Project.git(vararg command: String): Provider<String> =
+  providers.exec { commandLine("git", *command) }
+    .standardOutput
+    .asText
+    .map { it.trim() }
 
-fun Project.properties(fileName: String): Properties? {
-  val file = file(fileName)
-  if (!file.exists())
-    return null
-  val props = Properties()
-  props.load(file.inputStream())
-  return props
-}
+fun Project.properties(fileName: String): Provider<Properties> =
+  providers.fileContents(rootProject.layout.projectDirectory.file(fileName))
+    .asBytes
+    .map { Properties().apply { load(it.inputStream()) } }
-- 
GitLab