From 664e1fbba06792c82ad2ce71d16ca687077e9c9a Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sat, 5 Feb 2022 01:57:41 +0530 Subject: Begin rework for configuration cache compatibility (#1709) --- .../src/main/kotlin/versioning/Constants.kt | 14 +++++ .../src/main/kotlin/versioning/VersioningPlugin.kt | 71 ++++++---------------- .../src/main/kotlin/versioning/VersioningTask.kt | 46 ++++++++++++++ 3 files changed, 77 insertions(+), 54 deletions(-) create mode 100644 build-logic/android-plugins/src/main/kotlin/versioning/Constants.kt create mode 100644 build-logic/android-plugins/src/main/kotlin/versioning/VersioningTask.kt (limited to 'build-logic/android-plugins/src/main') diff --git a/build-logic/android-plugins/src/main/kotlin/versioning/Constants.kt b/build-logic/android-plugins/src/main/kotlin/versioning/Constants.kt new file mode 100644 index 00000000..000f5438 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/versioning/Constants.kt @@ -0,0 +1,14 @@ +/* + * Copyright © 2014-2022 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package versioning + +const val VERSIONING_PROP_FILE = "version.properties" +const val VERSIONING_PROP_VERSION_NAME = "versioning-plugin.versionName" +const val VERSIONING_PROP_VERSION_CODE = "versioning-plugin.versionCode" +const val VERSIONING_PROP_COMMENT = + """# +# This file was automatically generated by 'versioning-plugin'. DO NOT EDIT MANUALLY. +#""" diff --git a/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt index 3083921f..5d838b63 100644 --- a/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt +++ b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningPlugin.kt @@ -7,18 +7,10 @@ package versioning import com.android.build.gradle.internal.plugins.AppPlugin import com.vdurmont.semver4j.Semver -import java.io.OutputStream import java.util.Properties import org.gradle.api.Plugin import org.gradle.api.Project - -private const val VERSIONING_PROP_FILE = "version.properties" -private const val VERSIONING_PROP_VERSION_NAME = "versioning-plugin.versionName" -private const val VERSIONING_PROP_VERSION_CODE = "versioning-plugin.versionCode" -private const val VERSIONING_PROP_COMMENT = - """ -This file was automatically generated by 'versioning-plugin'. DO NOT EDIT MANUALLY. -""" +import org.gradle.kotlin.dsl.register /** * A Gradle [Plugin] that takes a [Project] with the [AppPlugin] applied and dynamically sets the @@ -26,23 +18,9 @@ This file was automatically generated by 'versioning-plugin'. DO NOT EDIT MANUAL * the [Project.getBuildDir] directory. It also adds Gradle tasks to bump the major, minor, and * patch versions along with one to prepare the next snapshot. */ -@Suppress("UnstableApiUsage", "NAME_SHADOWING") +@Suppress("UnstableApiUsage", "NAME_SHADOWING", "Unused") class VersioningPlugin : Plugin { - /** Generate the Android 'versionCode' property */ - private fun Semver.androidCode(): Int { - return major * 1_00_00 + minor * 1_00 + patch - } - - /** Write an Android-specific variant of [this] to [stream] */ - private fun Semver.writeForAndroid(stream: OutputStream) { - val newVersionCode = androidCode() - val props = Properties() - props.setProperty(VERSIONING_PROP_VERSION_CODE, "$newVersionCode") - props.setProperty(VERSIONING_PROP_VERSION_NAME, toString()) - props.store(stream, VERSIONING_PROP_COMMENT) - } - override fun apply(project: Project) { with(project) { val appPlugin = @@ -67,40 +45,25 @@ class VersioningPlugin : Plugin { appPlugin.extension.defaultConfig.versionCode = versionCode afterEvaluate { val version = Semver(versionName) - tasks.register("clearPreRelease") { - doLast { version.withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) } + tasks.register("clearPreRelease") { + semverString.set(version.withClearedSuffix().toString()) + propertyFile.set(propFile) } - tasks.register("bumpMajor") { - doLast { - version - .withIncMajor() - .withClearedSuffix() - .writeForAndroid(propFile.asFile.outputStream()) - } + tasks.register("bumpMajor") { + semverString.set(version.withIncMajor().withClearedSuffix().toString()) + propertyFile.set(propFile) } - tasks.register("bumpMinor") { - doLast { - version - .withIncMinor() - .withClearedSuffix() - .writeForAndroid(propFile.asFile.outputStream()) - } + tasks.register("bumpMinor") { + semverString.set(version.withIncMinor().withClearedSuffix().toString()) + propertyFile.set(propFile) } - tasks.register("bumpPatch") { - doLast { - version - .withIncPatch() - .withClearedSuffix() - .writeForAndroid(propFile.asFile.outputStream()) - } + tasks.register("bumpPatch") { + semverString.set(version.withIncPatch().withClearedSuffix().toString()) + propertyFile.set(propFile) } - tasks.register("bumpSnapshot") { - doLast { - version - .withIncMinor() - .withSuffix("SNAPSHOT") - .writeForAndroid(propFile.asFile.outputStream()) - } + tasks.register("bumpSnapshot") { + semverString.set(version.withIncMinor().withSuffix("SNAPSHOT").toString()) + propertyFile.set(propFile) } } } diff --git a/build-logic/android-plugins/src/main/kotlin/versioning/VersioningTask.kt b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningTask.kt new file mode 100644 index 00000000..fe437c04 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/versioning/VersioningTask.kt @@ -0,0 +1,46 @@ +/* + * Copyright © 2014-2022 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package versioning + +import com.vdurmont.semver4j.Semver +import org.gradle.api.DefaultTask +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.CacheableTask +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.OutputFile +import org.gradle.api.tasks.TaskAction + +@CacheableTask +abstract class VersioningTask : DefaultTask() { + @get:Input abstract val semverString: Property + + @get:OutputFile abstract val propertyFile: RegularFileProperty + + /** Generate the Android 'versionCode' property */ + private fun Semver.androidCode(): Int { + return major * 1_00_00 + minor * 1_00 + patch + } + + private fun Semver.toPropFileText(): String { + val newVersionCode = androidCode() + val newVersionName = toString() + return buildString { + appendLine(VERSIONING_PROP_COMMENT) + append(VERSIONING_PROP_VERSION_CODE) + append('=') + appendLine(newVersionCode) + append(VERSIONING_PROP_VERSION_NAME) + append('=') + appendLine(newVersionName) + } + } + + @TaskAction + fun execute() { + propertyFile.get().asFile.writeText(Semver(semverString.get()).toPropFileText()) + } +} -- cgit v1.2.3