build-logic: rework implementation details of versioning plugin

This commit is contained in:
Harsh Shandilya 2022-06-12 17:30:33 +05:30
parent fbd326fa48
commit fefe570c41
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
2 changed files with 47 additions and 26 deletions

View file

@ -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
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)
}
}
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 {
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)
}
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)
check(androidAppPluginApplied.get()) {
"Plugin 'com.android.application' must be applied to ${project.displayName} to use the Versioning Plugin"
}
}
}

View file

@ -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())