diff options
| author | Harsh Shandilya <me@msfjarvis.dev> | 2022-06-04 15:24:46 +0530 |
|---|---|---|
| committer | Harsh Shandilya <me@msfjarvis.dev> | 2022-06-05 03:58:15 +0530 |
| commit | 504c8b466c44b4a8591976ed5d8662f7c83f67fc (patch) | |
| tree | 3ff0a7d13736da9a9103617a196e4562287705ea /build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning | |
| parent | fee75104962548404ed2ffc4f6ef9c397b86683f (diff) | |
| download | HardenedPass-504c8b466c44b4a8591976ed5d8662f7c83f67fc.tar.gz HardenedPass-504c8b466c44b4a8591976ed5d8662f7c83f67fc.zip | |
android-plugins: convert to full plugins
Diffstat (limited to 'build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning')
3 files changed, 131 insertions, 0 deletions
diff --git a/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/Constants.kt b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/Constants.kt new file mode 100644 index 00000000..0d6f5c67 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/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 dev.msfjarvis.aps.gradle.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/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt new file mode 100644 index 00000000..6b604e75 --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/versioning/VersioningPlugin.kt @@ -0,0 +1,71 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.gradle.versioning + +import com.android.build.gradle.internal.plugins.AppPlugin +import com.vdurmont.semver4j.Semver +import java.util.Properties +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.kotlin.dsl.register + +/** + * A Gradle [Plugin] that takes a [Project] with the [AppPlugin] applied and dynamically sets the + * versionCode and versionName properties based on values read from a [VERSIONING_PROP_FILE] file in + * 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("Unused") +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 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" + } + val contents = providers.fileContents(propFile).asText + val versionProps = Properties().also { it.load(contents.get().byteInputStream()) } + val versionName = + requireNotNull(versionProps.getProperty(VERSIONING_PROP_VERSION_NAME)) { + "version.properties must contain a '$VERSIONING_PROP_VERSION_NAME' property" + } + val versionCode = + 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) + } + 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) + } + } + } + } +} 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 new file mode 100644 index 00000000..762cf57b --- /dev/null +++ b/build-logic/android-plugins/src/main/kotlin/dev/msfjarvis/aps/gradle/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 dev.msfjarvis.aps.gradle.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<String> + + @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()) + } +} |
