|
| 1 | +package net.thunderbird.gradle.plugin.app.versioning |
| 2 | + |
| 3 | +import com.android.build.api.artifact.SingleArtifact |
| 4 | +import com.android.build.gradle.internal.dsl.BaseAppModuleExtension |
| 5 | +import com.android.build.api.variant.ApplicationAndroidComponentsExtension |
| 6 | +import com.android.build.api.variant.ApplicationVariant |
| 7 | +import com.android.build.api.variant.Variant |
| 8 | +import java.io.File |
| 9 | +import javax.xml.parsers.DocumentBuilderFactory |
| 10 | +import javax.xml.xpath.XPathConstants |
| 11 | +import javax.xml.xpath.XPathFactory |
| 12 | +import org.gradle.api.Plugin |
| 13 | +import org.gradle.api.Project |
| 14 | +import org.gradle.api.provider.Provider |
| 15 | +import org.gradle.kotlin.dsl.assign |
| 16 | +import org.gradle.kotlin.dsl.configure |
| 17 | +import org.gradle.kotlin.dsl.register |
| 18 | + |
| 19 | +class VersioningPlugin : Plugin<Project> { |
| 20 | + override fun apply(target: Project) { |
| 21 | + with(target) { |
| 22 | + with(pluginManager) { |
| 23 | + apply("com.android.application") |
| 24 | + } |
| 25 | + |
| 26 | + configureVersioning() |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private fun Project.configureVersioning() { |
| 31 | + extensions.configure<ApplicationAndroidComponentsExtension> { |
| 32 | + onVariants { variant -> |
| 33 | + val variantName = variant.name.capitalized() |
| 34 | + val printVersionInfoTaskName = "printVersionInfo$variantName" |
| 35 | + |
| 36 | + tasks.register<PrintVersionInfoTask>(printVersionInfoTaskName) { |
| 37 | + val versionInfo = getVersionInfo(variant).get() |
| 38 | + |
| 39 | + applicationId = variant.applicationId |
| 40 | + applicationLabel = getApplicationLabel(variant) |
| 41 | + versionCode = versionInfo.versionCode |
| 42 | + versionName = versionInfo.versionName |
| 43 | + versionNameSuffix = versionInfo.versionNameSuffix |
| 44 | + |
| 45 | + // Set outputFile only if provided via -PoutputFile=... |
| 46 | + project.findProperty("outputFile")?.toString()?.let { path -> |
| 47 | + outputFile.set(File(path)) |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Get version information for the given variant. |
| 56 | + */ |
| 57 | + private fun Project.getVersionInfo(variant: ApplicationVariant): Provider<VersionInfo> { |
| 58 | + return provider { |
| 59 | + val flavorNames = variant.productFlavors.map { it.second } |
| 60 | + val androidExtension = extensions.findByType(BaseAppModuleExtension::class.java) |
| 61 | + val flavor = androidExtension?.productFlavors?.find { it.name in flavorNames } |
| 62 | + val builtType = androidExtension?.buildTypes?.find { it.name == variant.buildType } |
| 63 | + |
| 64 | + val versionCode = flavor?.versionCode ?: androidExtension?.defaultConfig?.versionCode ?: 0 |
| 65 | + val versionName = flavor?.versionName ?: androidExtension?.defaultConfig?.versionName ?: "unknown" |
| 66 | + val versionNameSuffix = builtType?.versionNameSuffix.orEmpty() |
| 67 | + |
| 68 | + VersionInfo( |
| 69 | + versionCode = versionCode, |
| 70 | + versionName = versionName, |
| 71 | + versionNameSuffix = versionNameSuffix, |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private fun Project.getApplicationLabel(variant: Variant): Provider<String> { |
| 77 | + val mergedManifest = variant.artifacts.get(SingleArtifact.MERGED_MANIFEST) |
| 78 | + |
| 79 | + return providers.zip(mergedManifest, provider { variant }) { mergedManifest, _ -> |
| 80 | + val labelRaw = readManifestApplicationLabel(mergedManifest.asFile) ?: return@zip "Unknown" |
| 81 | + |
| 82 | + // Return raw label if not a resource string |
| 83 | + val match = STRING_RESOURCE_REGEX.matchEntire(labelRaw.trim()) ?: return@zip labelRaw |
| 84 | + val resourceName = match.groupValues[1] |
| 85 | + |
| 86 | + val resourceDirs = variant.sources.res?.all?.get()?.filter { it.isNotEmpty() }?.flatten() ?: emptyList() |
| 87 | + |
| 88 | + val resolvedApplicationLabel = resourceDirs |
| 89 | + .map { it.asFile } |
| 90 | + .mapNotNull { dir -> File(dir, "values/strings.xml").takeIf { it.exists() } } |
| 91 | + .firstNotNullOfOrNull { stringResourceFile -> readStringResource(stringResourceFile, resourceName) } |
| 92 | + |
| 93 | + resolvedApplicationLabel ?: "Unknown" |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private fun readManifestApplicationLabel(manifest: File): String? { |
| 98 | + val document = DocumentBuilderFactory.newInstance() |
| 99 | + .apply { isNamespaceAware = true } |
| 100 | + .newDocumentBuilder() |
| 101 | + .parse(manifest) |
| 102 | + |
| 103 | + val apps = document.getElementsByTagName("application") |
| 104 | + if (apps.length == 0) return null |
| 105 | + |
| 106 | + val appElement = apps.item(0) |
| 107 | + return appElement.attributes?.getNamedItemNS("http://schemas.android.com/apk/res/android", "label")?.nodeValue |
| 108 | + ?: appElement.attributes?.getNamedItem("android:label")?.nodeValue |
| 109 | + ?: appElement.attributes?.getNamedItem("label")?.nodeValue |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Parses stringResourceFile to extract `<string name="resourceName">...</string>` |
| 114 | + */ |
| 115 | + private fun readStringResource(stringResourceFile: File, resourceName: String): String? { |
| 116 | + val xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stringResourceFile) |
| 117 | + val xPath = XPathFactory.newInstance().newXPath() |
| 118 | + val expression = "/resources/string[@name='$resourceName']/text()" |
| 119 | + val value = xPath.evaluate(expression, xmlDocument, XPathConstants.STRING) as String |
| 120 | + return value.trim().takeIf { it.isNotEmpty() } |
| 121 | + } |
| 122 | + |
| 123 | + private fun String.capitalized() = replaceFirstChar { |
| 124 | + if (it.isLowerCase()) it.titlecase() else it.toString() |
| 125 | + } |
| 126 | + |
| 127 | + private companion object { |
| 128 | + val STRING_RESOURCE_REGEX = "^@string/([A-Za-z0-9_]+)$".toRegex() |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | + |
0 commit comments