Skip to content

Commit

Permalink
manipulateManifest
Browse files Browse the repository at this point in the history
  • Loading branch information
benz-ubique committed Sep 6, 2024
1 parent 51b038b commit 041dc0c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 <meta-data> 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 = "</application>"
val metaTag = "<meta-data android:name=\"$metaName\" android:value=\"$metaValue\" />"
return manifest.replace("${xmlAppClosingTag}", " $metaTag\n $xmlAppClosingTag")
}

}
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit 041dc0c

Please sign in to comment.