Updated on 2026-08-14

This commit is contained in:
Tangem 2025-11-11 19:05:35 +02:00
parent c54ca6134c
commit f88abf06b5
4 changed files with 112 additions and 7 deletions

1
.gitignore vendored
View file

@ -53,3 +53,4 @@ app/src/external/google-services.json
# Kotlin Plugin
.kotlin/
find-latest-release-branch.output

View file

@ -4,6 +4,7 @@ import com.android.build.gradle.AppExtension
import com.tangem.plugin.configuration.model.AppConfig
import com.tangem.plugin.configuration.model.BuildType
import com.tangem.plugin.configuration.utils.BuildConfigFieldFactory
import com.tangem.plugin.configuration.utils.VersionNameProvider
import org.gradle.api.Project
import com.android.build.gradle.internal.dsl.BuildType as AndroidBuildType
@ -29,11 +30,11 @@ private fun AppExtension.configureDefaultConfig(project: Project) {
AppConfig.versionCode
}
versionName = if (project.hasProperty("versionName")) {
project.property("versionName") as String
} else {
AppConfig.versionName
}
// Get version name from property, git branch, or default config
val versionNameProvider = VersionNameProvider(project)
versionName = versionNameProvider.getVersionName()
project.logger.lifecycle("Resolved versionName: $versionName")
buildFeatures.buildConfig = true

View file

@ -3,8 +3,6 @@ package com.tangem.plugin.configuration.model
internal object AppConfig {
const val packageName = "com.tangem.wallet"
const val versionCode = 1
const val versionName = "1.0.0-SNAPSHOT"
// const val versionName = "100.0.0-SNAPSHOT" //TODO: [REDACTED_JIRA]
const val minSdkVersion = 24
const val targetSdkVersion = 35
const val compileSdkVersion = 35

View file

@ -0,0 +1,105 @@
package com.tangem.plugin.configuration.utils
import org.gradle.api.Project
import org.gradle.api.provider.Provider
/**
* Provides version name based on current git branch
*/
internal class VersionNameProvider(
private val project: Project,
) {
/**
* Get version name for current branch.
* If -PversionName is provided, uses that value.
* Otherwise, derives version from git branch name.
*/
fun getVersionName(): String {
// Check if versionName is provided as gradle property
if (project.hasProperty("versionName")) {
return project.property("versionName") as String
}
// Get current branch name using Provider API (configuration cache compatible)
val currentBranch = getCurrentBranchProvider().get()
// Try to extract version from branch name (releases/X.Y)
val versionFromBranch = extractVersionFromBranch(currentBranch)
if (versionFromBranch != null) {
return versionFromBranch
}
// For other branches (develop, feature/*), find latest release branch and increment minor
val latestReleaseBranch = findLatestReleaseBranch()
if (latestReleaseBranch != null) {
val versionFromLatest = extractVersionFromBranch(latestReleaseBranch)
if (versionFromLatest != null) {
return incrementMinorVersion(versionFromLatest)
}
}
// Fallback to default if nothing works
return "1.0.0-SNAPSHOT"
}
private fun getCurrentBranchProvider(): Provider<String> {
return project.providers.exec {
commandLine("git", "rev-parse", "--abbrev-ref", "HEAD")
}.standardOutput.asText.map { it.trim() }
}
private fun findLatestReleaseBranch(): String? {
val scriptPath = project.rootProject.file("tangem-android-tools/CI/shell_scripts/find-latest-release-branch.sh")
if (!scriptPath.exists()) {
project.logger.warn("Script not found: $scriptPath")
return null
}
val currentBranch = getCurrentBranchProvider().get()
val outputFile = project.rootProject.file("find-latest-release-branch.output")
return try {
project.providers.exec {
commandLine("sh", scriptPath.absolutePath, currentBranch)
}.standardOutput.asText.get()
if (!outputFile.exists()) {
project.logger.warn("Script output file not found")
return null
}
outputFile.readText().trim().also { result ->
project.logger.lifecycle("Found latest release branch: $result")
outputFile.delete()
}
} catch (e: Exception) {
project.logger.warn("Failed to execute script: ${e.message}")
outputFile.delete()
null
}
}
private fun extractVersionFromBranch(branch: String): String? {
val regex = Regex("""^releases/(\d+)\.(\d+)(?:\.(\d+))?$""")
val matchResult = regex.find(branch) ?: return null
val major = matchResult.groupValues[1]
val minor = matchResult.groupValues[2]
val patch = matchResult.groupValues[3].ifEmpty { "0" }
return "$major.$minor.$patch"
}
private fun incrementMinorVersion(version: String): String {
val parts = version.split(".")
if (parts.size < 2) return version
val major = parts[0]
val minor = parts[1].toIntOrNull() ?: return version
val newMinor = minor + 1
return "$major.$newMinor.0"
}
}