From d10fd0fa118404d31432c691159f662dfa500f98 Mon Sep 17 00:00:00 2001 From: Akash Yadav Date: Thu, 13 Apr 2023 18:25:22 +0530 Subject: [PATCH] fix(gradle-plugin): invalid build version name in AndroidIDEBuildInfo --- .../ide/src/main/java/ProjectConfig.kt | 100 ++++++++++++++---- build.gradle.kts | 38 ------- gradle-plugin/build.gradle.kts | 2 +- 3 files changed, 79 insertions(+), 61 deletions(-) diff --git a/build-logic/ide/src/main/java/ProjectConfig.kt b/build-logic/ide/src/main/java/ProjectConfig.kt index 9f8ea5cbb..c7687b78c 100644 --- a/build-logic/ide/src/main/java/ProjectConfig.kt +++ b/build-logic/ide/src/main/java/ProjectConfig.kt @@ -15,38 +15,94 @@ * along with AndroidIDE. If not, see . */ -/** - * @author Akash Yadav - */ +import org.gradle.api.Project + +/** @author Akash Yadav */ object ProjectConfig { - /** - * The GitHub organization for this project. - */ + /** The GitHub organization for this project. */ const val GITHUB_ORGANIZATION = "AndroidIDEOfficial" - /** - * The GitHub repsitory for this project. - */ + /** The GitHub repsitory for this project. */ const val GITHUB_REPOSITORY = "AndroidIDE" - /** - * The GitHub repository URL for this project. - */ + /** The GitHub repository URL for this project. */ const val GITHUB_URL = "https://github.com/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}" - /** - * The Project website. - */ + /** The Project website. */ const val PROJECT_SITE = "https://androidide.com" - /** - * The GIT SCM URL. - */ + /** The GIT SCM URL. */ const val SCM_GIT = "scm:git:git://github.com/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}.git" - /** - * The SSH SCM URL. - */ + /** The SSH SCM URL. */ const val SCM_SSH = "scm:git:ssh://git@github.com/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}.git" -} \ No newline at end of file +} + +private var simpleVersion: String? = null + +val Project.simpleVersionName: String + get() { + if (simpleVersion != null) { + return simpleVersion!! + } + + val version = rootProject.version.toString() + val regex = Regex("^v\\d+\\.?\\d+\\.?\\d+-\\w+") + + simpleVersion = + regex.find(version)?.value?.substring(1)?.also { + logger.warn("Simple version name is '$it' (from version $version)") + } + + if (simpleVersion == null) { + if (CI.isTestEnv) { + return "1.0.0-beta" + } + + throw IllegalStateException( + "Cannot extract simple version name. Invalid version string '$version'. Version names must be SEMVER with 'v' prefix" + ) + } + + return simpleVersion!! + } + +private var versionCode: Int? = null +val Project.projectVersionCode: Int + get() { + if (versionCode != null) { + return versionCode!! + } + + val version = simpleVersionName + val regex = Regex("^\\d+\\.?\\d+\\.?\\d+") + + versionCode = + regex.find(version)?.value?.replace(".", "")?.toInt()?.also { + logger.warn("Version code is '$it' (from version ${version}).") + } + + if (versionCode == null) { + throw IllegalStateException( + "Cannot extract version code. Invalid version string '$version'. Version names must be SEMVER with 'v' prefix" + ) + } + + return versionCode!! + } + +private var publishing: String? = null +val Project.publishingVersion: String + get() { + if (publishing != null) { + return publishing!! + } + + publishing = simpleVersionName + if (CI.isCiBuild && CI.branchName != "main") { + publishing += "-${CI.commitHash}-SNAPSHOT" + } + + return publishing!! + } diff --git a/build.gradle.kts b/build.gradle.kts index 3e2b0f881..0742fc5a0 100755 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -44,44 +44,6 @@ buildscript { } } -val Project.simpleVersionName: String by lazy { - val version = rootProject.version.toString() - val regex = Regex("^v\\d+\\.?\\d+\\.?\\d+-\\w+") - - return@lazy regex.find(version)?.value?.substring(1)?.also { - logger.warn("Simple version name is '$it' (from version $version)") - } - ?: run { - if (CI.isTestEnv) { - return@run "1.0.0-beta" - } - - throw IllegalStateException( - "Cannot extract simple version name. Invalid version string '$version'. Version names must be SEMVER with 'v' prefix" - ) - } -} - -val Project.projectVersionCode: Int by lazy { - val version = simpleVersionName - val regex = Regex("^\\d+\\.?\\d+\\.?\\d+") - - return@lazy regex.find(version)?.value?.replace(".", "")?.toInt()?.also { - logger.warn("Version code is '$it' (from version ${version}).") - } - ?: throw IllegalStateException( - "Cannot extract version code. Invalid version string '$version'. Version names must be SEMVER with 'v' prefix" - ) -} - -val Project.publishingVersion by lazy { - var version = simpleVersionName - if (CI.isCiBuild && CI.branchName != "main") { - version += "-${CI.commitHash}-SNAPSHOT" - } - return@lazy version -} - fun Project.configureBaseExtension() { extensions.findByType(BaseExtension::class)?.run { compileSdkVersion(BuildConfig.compileSdk) diff --git a/gradle-plugin/build.gradle.kts b/gradle-plugin/build.gradle.kts index c68729975..a69aab2f2 100644 --- a/gradle-plugin/build.gradle.kts +++ b/gradle-plugin/build.gradle.kts @@ -32,7 +32,7 @@ tasks.create("generateBuildInfo") { .file(buildInfoFileIn) .replaceContents( dest = project.file(buildInfoFile), - candidates = arrayOf("@@BUILD_VERSION@@" to project.version.toString()) + candidates = arrayOf("@@BUILD_VERSION@@" to project.publishingVersion) ) }