From fefe570c41567aeb0b4f5cef6ab787001ad0af43 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 12 Jun 2022 17:30:33 +0530 Subject: build-logic: rework implementation details of versioning plugin --- .../aps/gradle/versioning/VersioningPlugin.kt | 69 ++++++++++++++-------- .../aps/gradle/versioning/VersioningTask.kt | 4 ++ 2 files changed, 47 insertions(+), 26 deletions(-) (limited to 'build-logic/android-plugins/src/main') diff --git a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt index 6b604e75..5272dd8a 100644 --- a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt @@ -5,12 +5,17 @@ package dev.msfjarvis.aps.gradle.versioning +import com.android.build.api.variant.ApplicationAndroidComponentsExtension +import com.android.build.api.variant.VariantOutputConfiguration import com.android.build.gradle.internal.plugins.AppPlugin import com.vdurmont.semver4j.Semver import java.util.Properties +import java.util.concurrent.atomic.AtomicBoolean import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.kotlin.dsl.getByType import org.gradle.kotlin.dsl.register +import org.gradle.kotlin.dsl.withType /** * A Gradle [Plugin] that takes a [Project] with the [AppPlugin] applied and dynamically sets the @@ -23,10 +28,7 @@ class VersioningPlugin : Plugin { override fun apply(project: Project) { with(project) { - val appPlugin = - requireNotNull(plugins.findPlugin(AppPlugin::class.java)) { - "Plugin 'com.android.application' must be applied to use this plugin" - } + val androidAppPluginApplied = AtomicBoolean(false) val propFile = layout.projectDirectory.file(VERSIONING_PROP_FILE) require(propFile.asFile.exists()) { "A 'version.properties' file must exist in the project subdirectory to use this plugin" @@ -41,29 +43,44 @@ class VersioningPlugin : Plugin { requireNotNull(versionProps.getProperty(VERSIONING_PROP_VERSION_CODE).toInt()) { "version.properties must contain a '$VERSIONING_PROP_VERSION_CODE' property" } - appPlugin.extension.defaultConfig.versionName = versionName - appPlugin.extension.defaultConfig.versionCode = versionCode - afterEvaluate { - val version = Semver(versionName) - tasks.register("clearPreRelease") { - semverString.set(version.withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register("bumpMajor") { - semverString.set(version.withIncMajor().withClearedSuffix().toString()) - propertyFile.set(propFile) + project.plugins.withType { + androidAppPluginApplied.set(true) + extensions.getByType().onVariants { variant -> + val mainOutput = + variant.outputs.single { it.outputType == VariantOutputConfiguration.OutputType.SINGLE } + mainOutput.versionName.set(versionName) + mainOutput.versionCode.set(versionCode) } - tasks.register("bumpMinor") { - semverString.set(version.withIncMinor().withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register("bumpPatch") { - semverString.set(version.withIncPatch().withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register("bumpSnapshot") { - semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString()) - propertyFile.set(propFile) + } + val version = Semver(versionName) + tasks.register("clearPreRelease") { + description = "Remove the pre-release suffix from the version" + semverString.set(version.withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register("bumpMajor") { + description = "Increment the major version" + semverString.set(version.withIncMajor().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register("bumpMinor") { + description = "Increment the minor version" + semverString.set(version.withIncMinor().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register("bumpPatch") { + description = "Increment the patch version" + semverString.set(version.withIncPatch().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register("bumpSnapshot") { + description = "Increment the minor version and add the `SNAPSHOT` suffix" + semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString()) + propertyFile.set(propFile) + } + afterEvaluate { + check(androidAppPluginApplied.get()) { + "Plugin 'com.android.application' must be applied to ${project.displayName} to use the Versioning Plugin" } } } diff --git a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningTask.kt b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningTask.kt index 762cf57b..b39fdaa4 100644 --- a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningTask.kt +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningTask.kt @@ -39,6 +39,10 @@ abstract class VersioningTask : DefaultTask() { } } + override fun getGroup(): String { + return "versioning" + } + @TaskAction fun execute() { propertyFile.get().asFile.writeText(Semver(semverString.get()).toPropFileText()) -- cgit v1.2.3