buildSrc: introduce versioning plugin

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-01-18 18:43:05 +05:30
parent dc61aa07ac
commit e9a55cc1db
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
5 changed files with 129 additions and 2 deletions

View file

@ -4,6 +4,7 @@ plugins {
kotlin("kapt")
kotlin("plugin.serialization") version "1.4.21"
id("dagger.hilt.android.plugin")
`versioning-plugin`
`lobsters-plugin`
`core-library-desugaring`
}
@ -11,8 +12,6 @@ plugins {
android {
defaultConfig {
applicationId = "dev.msfjarvis.lobsters"
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions.annotationProcessorOptions {
argument("room.schemaLocation", "${projectDir}/schemas")

6
app/version.properties Normal file
View file

@ -0,0 +1,6 @@
#
#This file was automatically generated by 'versioning-plugin'. DO NOT EDIT MANUALLY.
#
#Mon Jan 18 18:42:38 IST 2021
versioning-plugin.versionCode=10000
versioning-plugin.versionName=1.0.0

View file

@ -24,6 +24,10 @@ gradlePlugin {
id = "core-library-desugaring"
implementationClass = "CoreLibraryDesugaringPlugin"
}
register("versioning") {
id = "versioning-plugin"
implementationClass = "VersioningPlugin"
}
}
}
@ -34,4 +38,5 @@ dependencies {
implementation(build.getValue("androidGradlePlugin_builder"))
implementation(build.getValue("androidGradlePlugin_builderModel"))
implementation(build.getValue("androidGradlePlugin_lintModel"))
implementation(build.getValue("jsemver"))
}

View file

@ -3,6 +3,7 @@ rootProject.ext.versions = [
daggerHilt: '2.30.1-alpha',
kotlin: '1.4.21',
lint: '27.3.0-alpha04',
semver: '0.9.0',
]
rootProject.ext.build = [
@ -12,4 +13,5 @@ rootProject.ext.build = [
androidGradlePlugin_lintModel: "com.android.tools.lint:lint-model:${versions.lint}",
daggerGradlePlugin: "com.google.dagger:hilt-android-gradle-plugin:${versions.daggerHilt}",
kotlinGradlePlugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}",
jsemver: "com.github.zafarkhaja:java-semver:${versions.semver}",
]

View file

@ -0,0 +1,115 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.android.build.gradle.internal.plugins.AppPlugin
import com.github.zafarkhaja.semver.Version
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.
"""
/**
* 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(
"UnstableApiUsage",
"NAME_SHADOWING"
)
class VersioningPlugin : Plugin<Project> {
/**
* Generate the Android 'versionCode' property
*/
private fun Version.androidCode(): Int {
return majorVersion * 1_00_00 +
minorVersion * 1_00 +
patchVersion
}
/**
* Write an Android-specific variant of [this] to [stream]
*/
private fun Version.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)
}
/**
* Returns the same [Version], but with build metadata stripped.
*/
private fun Version.clearPreRelease(): Version {
return Version.forIntegers(majorVersion, minorVersion, patchVersion)
}
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.forUseAtConfigurationTime()
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 = Version.valueOf(versionName)
tasks.register("clearPreRelease") {
doLast {
version.clearPreRelease()
.writeForAndroid(propFile.asFile.outputStream())
}
}
tasks.register("bumpMajor") {
doLast {
version.incrementMajorVersion()
.writeForAndroid(propFile.asFile.outputStream())
}
}
tasks.register("bumpMinor") {
doLast {
version.incrementMinorVersion()
.writeForAndroid(propFile.asFile.outputStream())
}
}
tasks.register("bumpPatch") {
doLast {
version.incrementPatchVersion()
.writeForAndroid(propFile.asFile.outputStream())
}
}
tasks.register("bumpSnapshot") {
doLast {
version.incrementMinorVersion()
.setPreReleaseVersion("SNAPSHOT")
.writeForAndroid(propFile.asFile.outputStream())
}
}
}
}
}
}