diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-06-12 17:30:33 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2022-06-12 17:56:26 +0530 |
commit | fefe570c41567aeb0b4f5cef6ab787001ad0af43 (patch) | |
tree | 5f0369b04391038ffacc4defc9fcb5199cdbe96d | |
parent | fbd326fa485d02ece651f95881dd821679f15de1 (diff) |
build-logic: rework implementation details of versioning plugin
2 files changed, 47 insertions, 26 deletions
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<Project> { 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<Project> { 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<VersioningTask>("clearPreRelease") { - semverString.set(version.withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register<VersioningTask>("bumpMajor") { - semverString.set(version.withIncMajor().withClearedSuffix().toString()) - propertyFile.set(propFile) + project.plugins.withType<AppPlugin> { + androidAppPluginApplied.set(true) + extensions.getByType<ApplicationAndroidComponentsExtension>().onVariants { variant -> + val mainOutput = + variant.outputs.single { it.outputType == VariantOutputConfiguration.OutputType.SINGLE } + mainOutput.versionName.set(versionName) + mainOutput.versionCode.set(versionCode) } - tasks.register<VersioningTask>("bumpMinor") { - semverString.set(version.withIncMinor().withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register<VersioningTask>("bumpPatch") { - semverString.set(version.withIncPatch().withClearedSuffix().toString()) - propertyFile.set(propFile) - } - tasks.register<VersioningTask>("bumpSnapshot") { - semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString()) - propertyFile.set(propFile) + } + val version = Semver(versionName) + tasks.register<VersioningTask>("clearPreRelease") { + description = "Remove the pre-release suffix from the version" + semverString.set(version.withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register<VersioningTask>("bumpMajor") { + description = "Increment the major version" + semverString.set(version.withIncMajor().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register<VersioningTask>("bumpMinor") { + description = "Increment the minor version" + semverString.set(version.withIncMinor().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register<VersioningTask>("bumpPatch") { + description = "Increment the patch version" + semverString.set(version.withIncPatch().withClearedSuffix().toString()) + propertyFile.set(propFile) + } + tasks.register<VersioningTask>("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()) |