| author | Da Risk <da_risk@geekorum.com> |
| Mon, 09 Mar 2026 16:20:21 -0400 | |
| changeset 100 | 9ec927c219b0 |
| parent 93 | 0f0e69edeafc |
| permissions | -rw-r--r-- |
| 10 | 1 |
/* |
2 |
* Geekdroid is a utility library for development on the Android |
|
3 |
* Platform. |
|
4 |
* |
|
|
75
534a19e25217
build: update license headers
Da Risk <da_risk@geekorum.com>
parents:
61
diff
changeset
|
5 |
* Copyright (C) 2017-2025 by Frederic-Charles Barthelery. |
| 10 | 6 |
* |
7 |
* This file is part of Geekdroid. |
|
8 |
* |
|
9 |
* Geekdroid is free software: you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation, either version 3 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* Geekdroid is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with Geekdroid. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
| 1 | 22 |
package com.geekorum.build |
23 |
||
| 19 | 24 |
import com.android.build.api.variant.ApplicationAndroidComponentsExtension |
25 |
import com.android.build.api.variant.BuildConfigField |
|
26 |
import com.android.build.api.variant.VariantOutputConfiguration.OutputType |
|
27 |
import org.gradle.api.DefaultTask |
|
| 1 | 28 |
import org.gradle.api.Project |
| 19 | 29 |
import org.gradle.api.file.RegularFileProperty |
30 |
import org.gradle.api.provider.Property |
|
31 |
import org.gradle.api.tasks.Input |
|
32 |
import org.gradle.api.tasks.OutputFile |
|
33 |
import org.gradle.api.tasks.TaskAction |
|
34 |
import org.gradle.configurationcache.extensions.capitalized |
|
35 |
import org.gradle.kotlin.dsl.register |
|
36 |
import org.gradle.process.ExecOperations |
|
37 |
import java.io.ByteArrayOutputStream |
|
| 1 | 38 |
import java.io.File |
| 19 | 39 |
import javax.inject.Inject |
| 1 | 40 |
|
| 19 | 41 |
internal fun ExecOperations.getGitSha1(projectDir: File): String? = runCommand("git rev-parse HEAD", workingDir = projectDir)?.trim()
|
42 |
||
43 |
internal fun ExecOperations.getHgSha1(projectDir: File): String? = runCommand("hg id --debug -i -r .", workingDir = projectDir)?.trim()
|
|
44 |
||
| 100 | 45 |
internal fun ExecOperations.getHgLocalRevisionNumber(projectDir: File): String? {
|
46 |
val hg = File(projectDir, ".hg") |
|
47 |
return if (hg.exists()) {
|
|
48 |
runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
|
|
49 |
} else null |
|
50 |
} |
|
| 1 | 51 |
|
| 19 | 52 |
private fun ExecOperations.getChangeSet(projectDir: File): String {
|
53 |
val git = File(projectDir, ".git") |
|
54 |
val hg = File(projectDir, ".hg") |
|
| 1 | 55 |
return when {
|
| 19 | 56 |
git.exists() -> "git:${getGitSha1(projectDir)}"
|
57 |
hg.exists() -> "hg:${getHgSha1(projectDir)}"
|
|
| 1 | 58 |
else -> "unknown" |
59 |
} |
|
60 |
} |
|
61 |
||
62 |
/** |
|
63 |
* Compute a version code following this format : MmmPBBB |
|
64 |
* M is major, mm is minor, P is patch |
|
65 |
* BBB is build version number from hg |
|
66 |
*/ |
|
| 19 | 67 |
private fun ExecOperations.computeChangesetVersionCode(projectDir: File, major: Int = 0, minor: Int = 0, patch: Int = 0): Int {
|
| 1 | 68 |
val base = (major * 1000000) + (minor * 10000) + (patch * 1000) |
| 19 | 69 |
return base + (getHgLocalRevisionNumber(projectDir)?.trim()?.toIntOrNull() ?: 0) |
70 |
} |
|
71 |
||
72 |
private fun ExecOperations.runCommand( |
|
73 |
command: String, |
|
74 |
workingDir: File = File(".")
|
|
75 |
): String? {
|
|
76 |
val output = ByteArrayOutputStream() |
|
77 |
val result = exec {
|
|
78 |
commandLine(command.split("\\s".toRegex()))
|
|
79 |
setWorkingDir(workingDir) |
|
80 |
setStandardOutput(output) |
|
81 |
setErrorOutput(output) |
|
82 |
} |
|
83 |
result.rethrowFailure() |
|
84 |
return output.toString(Charsets.UTF_8) |
|
| 1 | 85 |
} |
86 |
||
| 19 | 87 |
abstract class VersionCodeTask : DefaultTask() {
|
88 |
||
89 |
@get:OutputFile |
|
90 |
abstract val versionCodeOutputFile: RegularFileProperty |
|
91 |
||
92 |
@get:OutputFile |
|
93 |
abstract val changesetOutputFile: RegularFileProperty |
|
94 |
||
95 |
@get:Input |
|
96 |
abstract val repositoryDirectory: Property<String> |
|
97 |
||
98 |
@get:Input |
|
99 |
abstract val major: Property<Int> |
|
100 |
||
101 |
@get:Input |
|
102 |
abstract val minor: Property<Int> |
|
103 |
||
104 |
@get:Input |
|
105 |
abstract val patch: Property<Int> |
|
106 |
||
107 |
@get:Inject |
|
108 |
abstract val exec: ExecOperations |
|
109 |
||
110 |
@TaskAction |
|
111 |
fun computeVersionCode() {
|
|
112 |
val projectDir = File(repositoryDirectory.get()) |
|
113 |
val versionCode = exec.computeChangesetVersionCode(projectDir, major.getOrElse(0), minor.getOrElse(0), patch.getOrElse(0)) |
|
114 |
versionCodeOutputFile.get().asFile.writeText("$versionCode")
|
|
115 |
} |
|
116 |
||
117 |
@TaskAction |
|
118 |
fun computeChangeset() {
|
|
119 |
val projectDir = File(repositoryDirectory.get()) |
|
120 |
val changeset = exec.getChangeSet(projectDir) |
|
121 |
changesetOutputFile.get().asFile.writeText(changeset) |
|
| 1 | 122 |
} |
123 |
} |
|
| 19 | 124 |
|
125 |
/** |
|
126 |
* @param versionNameSuffix extra string to add to version name |
|
127 |
*/ |
|
128 |
fun ApplicationAndroidComponentsExtension.configureVersionChangeset(project: Project, major: Int, minor: Int, patch: Int, versionNameSuffix: String = "") {
|
|
129 |
// Note: Everything in there is incubating. |
|
130 |
||
131 |
// onVariantProperties registers an action that configures variant properties during |
|
132 |
// variant computation (which happens during afterEvaluate) |
|
133 |
onVariants {
|
|
134 |
// Because app module can have multiple output when using mutli-APK, versionCode/Name |
|
135 |
// are only available on the variant output. |
|
136 |
// Here gather the output when we are in single mode (ie no multi-apk) |
|
137 |
val mainOutput = it.outputs.single { it.outputType == OutputType.SINGLE }
|
|
138 |
||
139 |
// create version Code generating task |
|
| 100 | 140 |
val versionCodeTask = project.tasks.register<VersionCodeTask>("computeVersionCodeFor${it.name.capitalized()}") {
|
| 19 | 141 |
this.major.set(major) |
142 |
this.minor.set(minor) |
|
143 |
this.patch.set(patch) |
|
144 |
repositoryDirectory.set(project.rootDir.absolutePath) |
|
145 |
versionCodeOutputFile.set(project.layout.buildDirectory.file("intermediates/versionCode.txt"))
|
|
146 |
changesetOutputFile.set(project.layout.buildDirectory.file("intermediates/changeset.txt"))
|
|
147 |
} |
|
148 |
||
149 |
// wire version code from the task output |
|
150 |
// map will create a lazy Provider that |
|
151 |
// 1. runs just before the consumer(s), ensuring that the producer (VersionCodeTask) has run |
|
152 |
// and therefore the file is created. |
|
153 |
// 2. contains task dependency information so that the consumer(s) run after the producer. |
|
154 |
mainOutput.versionCode.set(versionCodeTask.map { it.versionCodeOutputFile.get().asFile.readText().toInt() })
|
|
155 |
mainOutput.versionName.set("$major.$minor.$patch$versionNameSuffix")
|
|
156 |
||
| 91 | 157 |
it.buildConfigFields?.put("REPOSITORY_CHANGESET", versionCodeTask.map {
|
| 19 | 158 |
BuildConfigField("String", "\"${it.changesetOutputFile.get().asFile.readText()}\"", "Repository changeset")
|
159 |
}) |
|
160 |
} |
|
161 |
} |
|
| 100 | 162 |
|
163 |
private fun String.capitalized() = replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
|