From 041dc0c49bc5d373eaab4646f89b0d5b0d96d228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9a=20Benz?= Date: Fri, 6 Sep 2024 11:51:42 +0200 Subject: [PATCH] manipulateManifest --- .../linth/InjectMetaIntoManifestTask.kt | 64 +++++++++++++++++++ .../java/ch/ubique/linth/common/GitUtils.kt | 26 ++++++++ 2 files changed, 90 insertions(+) create mode 100644 plugin-build/plugin/src/main/java/ch/ubique/linth/common/GitUtils.kt diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/InjectMetaIntoManifestTask.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/InjectMetaIntoManifestTask.kt index 33b1e39..0b74bc7 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/InjectMetaIntoManifestTask.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/InjectMetaIntoManifestTask.kt @@ -1,14 +1,34 @@ package ch.ubique.linth +import ch.ubique.linth.common.GitUtils +import ch.ubique.linth.common.capitalize import org.gradle.api.DefaultTask import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction +import java.io.File abstract class InjectMetaIntoManifestTask : DefaultTask() { + private val METADATA_KEY_BUILDID = "ub_buildid" + private val METADATA_KEY_BUILDNUMBER = "ub_buildnumber" + private val METADATA_KEY_BRANCH = "ub_branch" + private val METADATA_KEY_FLAVOR = "ub_flavor" + + + private var buildId: String = "0" + private var buildNumber: String = "0" + private var buildBranch: String = "master" + private var buildFlavor: String = "default" + + init { description = "Inject Metadata into Manifest" group = "linth" + buildId = project.findProperty("buildid")?.toString() ?: project.findProperty("ubappid")?.toString() ?: "localbuild" + buildNumber = project.findProperty("buildnumber")?.toString() ?: "0" + buildBranch = project.findProperty("branch")?.toString() ?: GitUtils.obtainBranch() + } @get:Input @@ -19,7 +39,51 @@ abstract class InjectMetaIntoManifestTask : DefaultTask() { fun injectMetadataIntoManifest() { flavorAndBuildType.forEach { (flavor, buildType) -> println("Injecting metadata into manifest for flavor: $flavor and buildType: $buildType") + val variantName = flavor + buildType.capitalize() + val manifestFile = File( + project.layout.buildDirectory.asFile.get(), + "intermediates/merged_manifests/${variantName}/process${variantName}Manifest/AndroidManifest.xml" + ) + if (manifestFile.exists()) { + manipulateManifestFile(manifestFile) + } else { + println("Manifest file not found for flavor: $flavor and buildType: $buildType") + } + } } + + /** + * Add custom meta data to manifest. + * @param manifestFile + */ + + private fun manipulateManifestFile(manifestFile: File) { + // read manifest file + var manifestContent = manifestFile.readText(Charsets.UTF_8) + + // inject meta-data tags into the manifest + manifestContent = addMetaData(manifestContent, METADATA_KEY_BUILDID, buildId) + manifestContent = addMetaData(manifestContent, METADATA_KEY_BUILDNUMBER, buildNumber) + manifestContent = addMetaData(manifestContent, METADATA_KEY_BRANCH, buildBranch) + manifestContent = addMetaData(manifestContent, METADATA_KEY_FLAVOR, buildFlavor) + + // store modified manifest + manifestFile.writeText(manifestContent, Charsets.UTF_8) + } + + /** + * Inject a into the manifest XML. + * @param manifest content + * @param metaName meta-data key + * @param metaValue meta-data value + * @return + */ + private fun addMetaData(manifest: String, metaName: String, metaValue: String): String { + val xmlAppClosingTag = "" + val metaTag = "" + return manifest.replace("${xmlAppClosingTag}", " $metaTag\n $xmlAppClosingTag") + } + } \ No newline at end of file diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/GitUtils.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/GitUtils.kt new file mode 100644 index 0000000..41627b8 --- /dev/null +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/GitUtils.kt @@ -0,0 +1,26 @@ +package ch.ubique.linth.common + +import java.io.BufferedReader + +object GitUtils { + + /** + * Get the branch name from version control. + * @return + */ + + fun obtainBranch(): String { + val cmdGitBranch = "git rev-parse --abbrev-ref HEAD" + val process = ProcessBuilder(*cmdGitBranch.split(" ").toTypedArray()) + .redirectErrorStream(true) + .start() + + var branchName: String = process.inputStream.bufferedReader().use(BufferedReader::readText).trim() + if (branchName.isEmpty()) { + branchName = "develop" + } + println("Branch name: $branchName") + return branchName + } + +} \ No newline at end of file