55
66package dev.msfjarvis.aps.gradle.versioning
77
8+ import com.android.build.api.variant.ApplicationAndroidComponentsExtension
9+ import com.android.build.api.variant.VariantOutputConfiguration
810import com.android.build.gradle.internal.plugins.AppPlugin
911import com.vdurmont.semver4j.Semver
1012import java.util.Properties
13+ import java.util.concurrent.atomic.AtomicBoolean
1114import org.gradle.api.Plugin
1215import org.gradle.api.Project
16+ import org.gradle.kotlin.dsl.getByType
1317import 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 }
0 commit comments