From d6e516e12e47c7eadea2a0fdaeeaaeab580ef4ba Mon Sep 17 00:00:00 2001 From: Konstantin Tabere Date: Fri, 6 Sep 2024 18:06:41 +0200 Subject: [PATCH] Read manifest for app name and some git infos for upload --- appexample/build.gradle.kts | 2 +- appexample/src/dev/res/values/strings.xml | 3 ++ .../src/main/java/ch/ubique/linth/IconTask.kt | 25 ++-------- .../linth/InjectMetaIntoManifestTask.kt | 1 + .../main/java/ch/ubique/linth/LinthPlugin.kt | 6 ++- .../ch/ubique/linth/UploadToLinthBackend.kt | 34 +++++++++++++- .../java/ch/ubique/linth/common/GitUtils.kt | 32 ++++++++++--- .../java/ch/ubique/linth/common/IconUtils.kt | 2 +- .../ubique/linth/common/ProjectExtensions.kt | 26 +++++++++++ .../ch/ubique/linth/common/StringUtils.kt | 46 +++++++++++++++++++ .../java/ch/ubique/linth/common/XmlParser.kt | 20 ++++++-- 11 files changed, 161 insertions(+), 36 deletions(-) create mode 100644 appexample/src/dev/res/values/strings.xml create mode 100644 plugin-build/plugin/src/main/java/ch/ubique/linth/common/StringUtils.kt diff --git a/appexample/build.gradle.kts b/appexample/build.gradle.kts index ebe6e63..bf4365c 100644 --- a/appexample/build.gradle.kts +++ b/appexample/build.gradle.kts @@ -56,5 +56,5 @@ dependencies { linthPlugin { uploadKey = "f1c8846e-0c3a-44ac-b56a-53feb91d6383" -// proxy = "192.168.8.167:8888" + //proxy = "192.168.102.147:8888" } \ No newline at end of file diff --git a/appexample/src/dev/res/values/strings.xml b/appexample/src/dev/res/values/strings.xml new file mode 100644 index 0000000..c402754 --- /dev/null +++ b/appexample/src/dev/res/values/strings.xml @@ -0,0 +1,3 @@ + + Dev App Example + \ No newline at end of file diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/IconTask.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/IconTask.kt index 934e7ae..dfe3431 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/IconTask.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/IconTask.kt @@ -2,9 +2,8 @@ package ch.ubique.linth import ch.ubique.linth.common.IconUtils import ch.ubique.linth.common.getMergedManifestFile -import com.android.build.gradle.BaseExtension +import ch.ubique.linth.common.getResDirs import org.gradle.api.DefaultTask -import org.gradle.api.artifacts.ProjectDependency import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Optional @@ -41,7 +40,7 @@ abstract class IconTask : DefaultTask() { ) val generatedResDir = File("$buildDir/generated/res/launcher-icon/") - + // get banner label val defaultLabelEnabled = false//android.defaultConfig.launcherIconLabelEnabled val flavorLabelEnabled = true//flavor.launcherIconLabelEnabled @@ -53,25 +52,7 @@ abstract class IconTask : DefaultTask() { } val manifestFile = project.getMergedManifestFile(flavor, buildType) - - val androidModules: List = project.configurations - .asSequence() - .flatMap { it.dependencies } - .filterIsInstance() - .map { it.dependencyProject } - .distinct() - .mapNotNull { it.extensions.findByType(BaseExtension::class.java) } - .toList() - - val resDirs: List = androidModules - .flatMap { - listOfNotNull( - it.sourceSets.findByName(flavor), - it.sourceSets.findByName("main") - ) - } - .flatMap { it.res.srcDirs } - .filter { !it.path.contains("generated") } + val resDirs = project.getResDirs(flavor) val allIcons = IconUtils.findIcons(resDirs, manifestFile) 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 861b7bb..d633a24 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 @@ -22,6 +22,7 @@ abstract class InjectMetaIntoManifestTask : DefaultTask() { 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() diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/LinthPlugin.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/LinthPlugin.kt index b2da770..4b1db08 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/LinthPlugin.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/LinthPlugin.kt @@ -93,13 +93,13 @@ abstract class LinthPlugin : Plugin { UploadRequest( apk = it.outputFile, appIcon = it.outputFile, - appName = "some fancy name", + appName = "", //will be set from manifest packageName = packageName, flavor = flavor, branch = "someFancyBranch", minSdk = minSdk, targetSdk = targetSdk, - usesFeature = emptyList(), + usesFeature = emptyList(), //will be set from manifest buildNumber = 0L, buildTime = 0L, buildBatch = "buildBatch", @@ -110,6 +110,8 @@ abstract class LinthPlugin : Plugin { } project.tasks.register("uploadToLinth$flavorBuild", UploadToLinthBackend::class.java) { uploadTask -> + uploadTask.flavor = flavor + uploadTask.buildType = buildType uploadTask.uploadKey = extension.uploadKey.get() uploadTask.proxy = extension.proxy.orNull uploadTask.uploadRequest = uploadRequest diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/UploadToLinthBackend.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/UploadToLinthBackend.kt index 780e5bb..3cc656a 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/UploadToLinthBackend.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/UploadToLinthBackend.kt @@ -1,5 +1,9 @@ package ch.ubique.linth +import ch.ubique.linth.common.GitUtils +import ch.ubique.linth.common.StringUtils +import ch.ubique.linth.common.getMergedManifestFile +import ch.ubique.linth.common.getResDirs import ch.ubique.linth.model.UploadRequest import ch.ubique.linth.network.BackendRepository import ch.ubique.linth.network.OkHttpInstance @@ -12,9 +16,15 @@ import org.gradle.api.tasks.options.Option abstract class UploadToLinthBackend : DefaultTask() { + private var buildBranch: String = "master" + private var commitHistory: String = "no commit history" + init { description = "Uploads Apk to Linth Backend" group = "linth" + + buildBranch = project.findProperty("branch")?.toString() ?: GitUtils.obtainBranch() + commitHistory = GitUtils.obtainLastCommits() } @get:Input @@ -29,6 +39,12 @@ abstract class UploadToLinthBackend : DefaultTask() { @get:Input abstract var uploadRequest: UploadRequest + @get:Input + abstract var flavor: String + + @get:Input + abstract var buildType: String + @TaskAction fun uploadAction() { @@ -39,12 +55,28 @@ abstract class UploadToLinthBackend : DefaultTask() { OkHttpInstance.setProxy(null) } - val backendRepository = BackendRepository() + val uploadRequest = updateUploadRequestWithManifestInformation().copy( + //add git infos + branch = buildBranch, + changelog = commitHistory, + ) runBlocking { + val backendRepository = BackendRepository() backendRepository.appsUpload(uploadRequest = uploadRequest, uploadKey = uploadKey) logger.lifecycle("Upload to UbDiag successful.") } } + private fun updateUploadRequestWithManifestInformation(): UploadRequest { + val manifestFile = project.getMergedManifestFile(flavor, buildType) + val resDirs = project.getResDirs(flavor) + + val appName = StringUtils.findAppName(resDirs, manifestFile) ?: error("Did not find appName in Strings.") + + return uploadRequest.copy( + appName = appName + ) + } + } 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 index fed3d7e..be4ee36 100644 --- 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 @@ -4,22 +4,42 @@ 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() + var branchName = process.inputStream.bufferedReader().use(BufferedReader::readText).trim() if (branchName.isEmpty()) { branchName = "develop" } return branchName } + fun obtainLastCommits(numOfCommits: Int = 10): String { + /* eventuell das if-else nutzen wenn es sonst auf dem GitHub Runner nicht geht + + val process = if (Os.isFamily(Os.FAMILY_WINDOWS)) { + val lastCommits = "git -C ./ log -$numOfCommits --pretty=format:\"• %s\" --no-merges" + ProcessBuilder(*lastCommits.split(" ").toTypedArray()) + .redirectErrorStream(true) + .start() + } else { + val lastCommits = arrayOf("git", "-C", "./", "log", "-$numOfCommits", "--pretty=format:• %s", "--no-merges") + ProcessBuilder(*lastCommits) + .redirectErrorStream(true) + .start() + } + */ + + val lastCommits = "git -C ./ log -$numOfCommits --pretty=format:\"• %s\" --no-merges" + val process = ProcessBuilder(*lastCommits.split(" ").toTypedArray()) + .redirectErrorStream(true) + .start() + + val last10Commits = process.inputStream.bufferedReader().use(BufferedReader::readText).trim() + return last10Commits + } + } \ No newline at end of file diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/IconUtils.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/IconUtils.kt index 1c03438..4fb0a36 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/IconUtils.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/IconUtils.kt @@ -19,7 +19,7 @@ object IconUtils { } val xmlParser = XmlParser(manifestFile) - val fileName = xmlParser.findProperty("application", "android:icon") + val fileName = xmlParser.findAttribute("application", "android:icon") return fileName?.split("/")?.get(1) } diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/ProjectExtensions.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/ProjectExtensions.kt index 11e97ea..e094ac8 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/ProjectExtensions.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/ProjectExtensions.kt @@ -1,7 +1,9 @@ package ch.ubique.linth.common +import com.android.build.gradle.BaseExtension import org.gradle.api.Project +import org.gradle.api.artifacts.ProjectDependency import java.io.File /** @@ -14,3 +16,27 @@ fun Project.getMergedManifestFile(flavor: String, buildType: String): File { "intermediates/merged_manifests/${variantName}/process${variantName}Manifest/AndroidManifest.xml" ) } + + +fun Project.getResDirs(flavor: String): List { + val androidModules: List = configurations + .asSequence() + .flatMap { it.dependencies } + .filterIsInstance() + .map { it.dependencyProject } + .distinct() + .mapNotNull { it.extensions.findByType(BaseExtension::class.java) } + .toList() + + val resDirs: List = androidModules + .flatMap { + listOfNotNull( + it.sourceSets.findByName(flavor.lowercase()), + it.sourceSets.findByName("main") + ) + } + .flatMap { it.res.srcDirs } + .filter { it.path.contains("generated").not() } + + return resDirs +} \ No newline at end of file diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/StringUtils.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/StringUtils.kt new file mode 100644 index 0000000..a752e27 --- /dev/null +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/StringUtils.kt @@ -0,0 +1,46 @@ +package ch.ubique.linth.common + +import java.io.File + +object StringUtils { + + private fun getLabelName(manifestFile: File): String? { + if (manifestFile.isDirectory || manifestFile.exists().not()) { + return null + } + + val xmlParser = XmlParser(manifestFile) + val fileName = xmlParser.findAttribute("application", "android:label") + + return fileName?.split("/")?.get(1) + } + + /** + * Finds all icon files matching the icon specified in the given manifest. + */ + fun findAppName(resDirs: List, manifest: File): String? { + val labelName = getLabelName(manifest) ?: return null + + val stringFiles = mutableListOf() + for (resDir in resDirs) { + if (resDir.exists()) { + resDir.walkTopDown().maxDepth(1) + .filter { it.isDirectory && (it.name.startsWith("values")) } + .forEach { dir -> + dir.walkTopDown().filter { it.isFile && it.name.matches(Regex(".*strings.*.xml")) } + .forEach { stringFiles.add(it) } + } + } + } + if (stringFiles.isEmpty()) return null + + stringFiles.forEach { + val xmlParser = XmlParser(it) + val appName = xmlParser.findAttribute("string", "name", labelName, findTextValue = true) + if (appName != null) return appName + } + + return null + } + +} diff --git a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/XmlParser.kt b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/XmlParser.kt index dc5a7f0..142ede9 100644 --- a/plugin-build/plugin/src/main/java/ch/ubique/linth/common/XmlParser.kt +++ b/plugin-build/plugin/src/main/java/ch/ubique/linth/common/XmlParser.kt @@ -17,7 +17,7 @@ class XmlParser( root = document.documentElement } - fun findProperty(tag: String, property: String): String? { + fun findAttribute(tag: String, attrib: String, matchingAttribValue: String? = null, findTextValue: Boolean = false): String? { val nodeList = root.childNodes for (i in 0 until nodeList.length) { val node = nodeList.item(i) @@ -29,8 +29,22 @@ class XmlParser( val attributes = element.attributes for (j in 0 until attributes.length) { val attribute = attributes.item(j) - if (attribute.nodeName == property) { - return attribute.nodeValue + if (attribute.nodeName == attrib) { + if (matchingAttribValue != null) { + if (attribute.nodeValue == matchingAttribValue) { + return if (findTextValue) { + element.firstChild.textContent + } else { + attribute.nodeValue + } + } + } else { + return if (findTextValue) { + element.firstChild.textContent + } else { + attribute.nodeValue + } + } } } return null