Skip to content

Commit

Permalink
Merge branch 'release/2.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
G00fY2 committed Jun 8, 2020
2 parents 850a273 + 6d91adc commit b417ff5
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 74 deletions.
16 changes: 16 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'eu.nanogiants:android-versioning:2.1.0'
classpath 'eu.nanogiants:android-versioning:2.1.1'
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "eu.nanogiants"
version = "2.1.0"
version = "2.1.1"

repositories {
google()
Expand Down
17 changes: 9 additions & 8 deletions src/main/kotlin/eu/nanogiants/gradle/Versioning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

package eu.nanogiants.gradle

import eu.nanogiants.gradle.ext.getOutput
import eu.nanogiants.gradle.ext.runCommand
import org.gradle.api.GradleException

object Versioning {

Expand All @@ -15,18 +16,18 @@ object Versioning {
return (if (checkBranch) getBranchNameOrTag() else getTag()).apply { println("VersionName $this") }
}

private fun getCommitCount(): Int = "git rev-list --count HEAD".getOutput().toInt()
private fun getCommitCount() = "git rev-list --count HEAD".runCommand().toIntOrNull()
?: throw GradleException("Error reading current commit count.")

private fun getTag(): String {
val revList = "git rev-list --tags --max-count=1".getOutput()
return "git describe --tags $revList".getOutput()
val revList = "git rev-list --tags --max-count=1".runCommand()
val result = "git describe --tags $revList".runCommand()
return if (result.isNotEmpty()) result else throw GradleException("Error reading current tag")
}

private fun getBranch(): String = "git rev-parse --abbrev-ref HEAD".getOutput()

private fun getBranchNameOrTag(): String {
val branch = getBranch()
return if (branch.startsWith("release/") || branch.startsWith("hotfix/")) {
val branch = "git rev-parse --abbrev-ref HEAD".runCommand()
return if (branch.matches("^release/\\d+.*".toRegex()) || branch.matches("^hotfix/\\d+.*".toRegex())) {
println("Generate versionName from branch $branch")
branch.split("/")[1]
} else {
Expand Down
53 changes: 9 additions & 44 deletions src/main/kotlin/eu/nanogiants/gradle/VersioningPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
package eu.nanogiants.gradle

import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import com.android.build.gradle.internal.api.BaseVariantOutputImpl
import com.android.build.gradle.internal.dsl.DefaultConfig
import eu.nanogiants.gradle.ext.addRenameArtifactAction
import eu.nanogiants.gradle.ext.addRenameMappingAction
import eu.nanogiants.gradle.ext.generateOutputName
import eu.nanogiants.gradle.ext.listContains
import org.gradle.api.GradleException
import org.gradle.api.Plugin
Expand Down Expand Up @@ -54,13 +53,13 @@ class VersioningPlugin : Plugin<Project> {
appExtension.applicationVariants.configureEach { variant ->
if (variant.name == variantName && !extension.excludeBuildTypes.listContains(variant.buildType.name)) {
val bundleName = "$baseName-${variant.baseName}.aab"
val newBundleName = getOutputName(baseName, variant, appExtension.defaultConfig, "aab")
val newBundleName = variant.generateOutputName(baseName, "aab")
val bundleOutputPath = "${project.buildDir.absolutePath}/outputs/bundle/$variantName/"

task.addRenameArtifactAction(project, bundleName, newBundleName, bundleOutputPath)
task.addRenameArtifactAction(bundleName, newBundleName, bundleOutputPath)

val newMappingName = getOutputName(baseName, variant, appExtension.defaultConfig, "txt", "aab")
task.addRenameMappingAction(project, variant, newMappingName)
val newMappingName = variant.generateOutputName(baseName, "txt", "aab")
task.addRenameMappingAction(variant, newMappingName)
}
}
} else if (task.name.matches(assembleRegex)) {
Expand All @@ -72,51 +71,17 @@ class VersioningPlugin : Plugin<Project> {

variant.outputs.configureEach {
val apkName = (it as BaseVariantOutputImpl).outputFileName
val newApkName = getOutputName(baseName, variant, appExtension.defaultConfig, "apk")
val newApkName = variant.generateOutputName(baseName, "apk")

task.addRenameArtifactAction(project, apkName, newApkName, apkOutputPath)
task.addRenameArtifactAction(apkName, newApkName, apkOutputPath)
}

val newMappingName = getOutputName(baseName, variant, appExtension.defaultConfig, "txt", "apk")
task.addRenameMappingAction(project, variant, newMappingName)
val newMappingName = variant.generateOutputName(baseName, "txt", "apk")
task.addRenameMappingAction(variant, newMappingName)
}
}
}
}
}
}

private fun getOutputName(
artifactBaseName: String,
variant: ApplicationVariant,
defaultConfig: DefaultConfig,
extension: String,
suffix: String = ""
): String {
return StringBuilder().apply {
append(artifactBaseName)
variant.productFlavors.forEach {
append("-")
append(it.name)
}
append("-")
append(defaultConfig.versionName)
append("-")
append(defaultConfig.versionCode.toString())
append("-")
append(variant.buildType.name)
if (suffix.isNotEmpty()) {
append("-")
append(suffix)
}
if (extension == "apk" && !variant.isSigningReady) {
append("-unsigned.apk")
} else if (extension == "txt") {
append("-mapping.txt")
} else {
append(".")
append(extension)
}
}.toString()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ open class VersioningPluginExtension {
fun getVersionName(checkBranch: Boolean = false) = Versioning.getVersionName(checkBranch)

var excludeBuildTypes: String? = null
}
}
35 changes: 35 additions & 0 deletions src/main/kotlin/eu/nanogiants/gradle/ext/ApplicationVariantExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Created by NanoGiants GmbH on 06-07-2020.
* Copyright © 2020 NanoGiants GmbH. All rights reserved.
*/

package eu.nanogiants.gradle.ext

import com.android.build.gradle.api.ApplicationVariant

internal fun ApplicationVariant.generateOutputName(baseName: String, extension: String, suffix: String = ""): String {
return StringBuilder().apply {
append(baseName)
productFlavors.forEach {
append("-")
append(it.name)
}
append("-")
append(versionName)
append("-")
append(versionCode.toString())
append("-")
append(buildType.name)
if (suffix.isNotEmpty()) {
append("-")
append(suffix)
}
if (extension == "apk" && !isSigningReady) {
append("-unsigned")
} else if (extension == "txt") {
append("-mapping")
}
append(".")
append(extension)
}.toString()
}
24 changes: 11 additions & 13 deletions src/main/kotlin/eu/nanogiants/gradle/ext/StringExt.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
/*
* Created by NanoGiants GmbH on 06-06-2020.
* Created by NanoGiants GmbH on 06-07-2020.
* Copyright © 2020 NanoGiants GmbH. All rights reserved.
*/

package eu.nanogiants.gradle.ext

import java.io.BufferedReader
import java.io.IOException
import java.util.*
import org.gradle.api.GradleException
import java.util.Locale
import java.util.concurrent.TimeUnit

internal fun String.getOutput() = this.runCommand()?.use { return it.readText().trim() } ?: ""

internal fun String.runCommand(): BufferedReader? = try {
ProcessBuilder(this.split("\\s".toRegex())).start().run {
waitFor(10, TimeUnit.SECONDS)
inputStream.bufferedReader()
internal fun String.runCommand(): String {
return try {
ProcessBuilder(this.split("\\s".toRegex())).start().run {
waitFor(10, TimeUnit.SECONDS)
inputStream.bufferedReader().use { it.readText().trim() }
}
} catch (e: Exception) {
throw GradleException(e.message ?: "Error executing command :$this")
}
} catch (e: IOException) {
e.printStackTrace()
null
}

internal fun String?.listContains(element: String) =
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/eu/nanogiants/gradle/ext/TaskExt.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/*
* Created by NanoGiants GmbH on 06-06-2020.
* Created by NanoGiants GmbH on 06-07-2020.
* Copyright © 2020 NanoGiants GmbH. All rights reserved.
*/

package eu.nanogiants.gradle.ext

import com.android.build.gradle.api.ApplicationVariant
import org.gradle.api.Project
import org.gradle.api.Task
import java.io.File

internal fun Task.addRenameArtifactAction(project: Project, oldOutput: String, newOutput: String, outputPath: String) {
internal fun Task.addRenameArtifactAction(oldOutput: String, newOutput: String, outputPath: String) {
println(newOutput)

doLast {
Expand All @@ -19,12 +18,12 @@ internal fun Task.addRenameArtifactAction(project: Project, oldOutput: String, n
if (success) {
println("Created file://${newFile.absolutePath}")
} else {
project.logger.error("Renaming $oldOutput to $newOutput failed.")
logger.error("Renaming $oldOutput to $newOutput failed.")
}
}
}

internal fun Task.addRenameMappingAction(project: Project, variant: ApplicationVariant, newOutput: String) {
internal fun Task.addRenameMappingAction(variant: ApplicationVariant, newOutput: String) {
if (variant.buildType.isMinifyEnabled) {
variant.mappingFileProvider.orNull?.firstOrNull()?.let { mappingFile ->
println(newOutput)
Expand All @@ -35,7 +34,7 @@ internal fun Task.addRenameMappingAction(project: Project, variant: ApplicationV
if (success) {
println("Created file://${newFile.absolutePath}")
} else {
project.logger.error("Renaming mapping.txt to $newOutput failed.")
logger.error("Renaming mapping.txt to $newOutput failed.")
}
}
}
Expand Down

0 comments on commit b417ff5

Please sign in to comment.