Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit fefe570

Browse files
committed
build-logic: rework implementation details of versioning plugin
1 parent fbd326f commit fefe570

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55

66
package dev.msfjarvis.aps.gradle.versioning
77

8+
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
9+
import com.android.build.api.variant.VariantOutputConfiguration
810
import com.android.build.gradle.internal.plugins.AppPlugin
911
import com.vdurmont.semver4j.Semver
1012
import java.util.Properties
13+
import java.util.concurrent.atomic.AtomicBoolean
1114
import org.gradle.api.Plugin
1215
import org.gradle.api.Project
16+
import org.gradle.kotlin.dsl.getByType
1317
import org.gradle.kotlin.dsl.register
18+
import org.gradle.kotlin.dsl.withType
1419

1520
/**
1621
* A Gradle [Plugin] that takes a [Project] with the [AppPlugin] applied and dynamically sets the
@@ -23,10 +28,7 @@ class VersioningPlugin : Plugin<Project> {
2328

2429
override fun apply(project: Project) {
2530
with(project) {
26-
val appPlugin =
27-
requireNotNull(plugins.findPlugin(AppPlugin::class.java)) {
28-
"Plugin 'com.android.application' must be applied to use this plugin"
29-
}
31+
val androidAppPluginApplied = AtomicBoolean(false)
3032
val propFile = layout.projectDirectory.file(VERSIONING_PROP_FILE)
3133
require(propFile.asFile.exists()) {
3234
"A 'version.properties' file must exist in the project subdirectory to use this plugin"
@@ -41,29 +43,44 @@ class VersioningPlugin : Plugin<Project> {
4143
requireNotNull(versionProps.getProperty(VERSIONING_PROP_VERSION_CODE).toInt()) {
4244
"version.properties must contain a '$VERSIONING_PROP_VERSION_CODE' property"
4345
}
44-
appPlugin.extension.defaultConfig.versionName = versionName
45-
appPlugin.extension.defaultConfig.versionCode = versionCode
46-
afterEvaluate {
47-
val version = Semver(versionName)
48-
tasks.register<VersioningTask>("clearPreRelease") {
49-
semverString.set(version.withClearedSuffix().toString())
50-
propertyFile.set(propFile)
51-
}
52-
tasks.register<VersioningTask>("bumpMajor") {
53-
semverString.set(version.withIncMajor().withClearedSuffix().toString())
54-
propertyFile.set(propFile)
46+
project.plugins.withType<AppPlugin> {
47+
androidAppPluginApplied.set(true)
48+
extensions.getByType<ApplicationAndroidComponentsExtension>().onVariants { variant ->
49+
val mainOutput =
50+
variant.outputs.single { it.outputType == VariantOutputConfiguration.OutputType.SINGLE }
51+
mainOutput.versionName.set(versionName)
52+
mainOutput.versionCode.set(versionCode)
5553
}
56-
tasks.register<VersioningTask>("bumpMinor") {
57-
semverString.set(version.withIncMinor().withClearedSuffix().toString())
58-
propertyFile.set(propFile)
59-
}
60-
tasks.register<VersioningTask>("bumpPatch") {
61-
semverString.set(version.withIncPatch().withClearedSuffix().toString())
62-
propertyFile.set(propFile)
63-
}
64-
tasks.register<VersioningTask>("bumpSnapshot") {
65-
semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString())
66-
propertyFile.set(propFile)
54+
}
55+
val version = Semver(versionName)
56+
tasks.register<VersioningTask>("clearPreRelease") {
57+
description = "Remove the pre-release suffix from the version"
58+
semverString.set(version.withClearedSuffix().toString())
59+
propertyFile.set(propFile)
60+
}
61+
tasks.register<VersioningTask>("bumpMajor") {
62+
description = "Increment the major version"
63+
semverString.set(version.withIncMajor().withClearedSuffix().toString())
64+
propertyFile.set(propFile)
65+
}
66+
tasks.register<VersioningTask>("bumpMinor") {
67+
description = "Increment the minor version"
68+
semverString.set(version.withIncMinor().withClearedSuffix().toString())
69+
propertyFile.set(propFile)
70+
}
71+
tasks.register<VersioningTask>("bumpPatch") {
72+
description = "Increment the patch version"
73+
semverString.set(version.withIncPatch().withClearedSuffix().toString())
74+
propertyFile.set(propFile)
75+
}
76+
tasks.register<VersioningTask>("bumpSnapshot") {
77+
description = "Increment the minor version and add the `SNAPSHOT` suffix"
78+
semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString())
79+
propertyFile.set(propFile)
80+
}
81+
afterEvaluate {
82+
check(androidAppPluginApplied.get()) {
83+
"Plugin 'com.android.application' must be applied to ${project.displayName} to use the Versioning Plugin"
6784
}
6885
}
6986
}

build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningTask.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ abstract class VersioningTask : DefaultTask() {
3939
}
4040
}
4141

42+
override fun getGroup(): String {
43+
return "versioning"
44+
}
45+
4246
@TaskAction
4347
fun execute() {
4448
propertyFile.get().asFile.writeText(Semver(semverString.get()).toPropFileText())

0 commit comments

Comments
 (0)