| author | Da Risk <da_risk@geekorum.com> |
| Mon, 17 Feb 2025 18:52:41 -0400 | |
| changeset 75 | 534a19e25217 |
| parent 61 | 7a8bbdc7b290 |
| child 91 | c02a6ae4f610 |
| 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 |
||
45 |
internal fun ExecOperations.getHgLocalRevisionNumber(projectDir: File): String? = runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
|
|
| 1 | 46 |
|
| 19 | 47 |
private fun ExecOperations.getChangeSet(projectDir: File): String {
|
48 |
val git = File(projectDir, ".git") |
|
49 |
val hg = File(projectDir, ".hg") |
|
| 1 | 50 |
return when {
|
| 19 | 51 |
git.exists() -> "git:${getGitSha1(projectDir)}"
|
52 |
hg.exists() -> "hg:${getHgSha1(projectDir)}"
|
|
| 1 | 53 |
else -> "unknown" |
54 |
} |
|
55 |
} |
|
56 |
||
57 |
/** |
|
58 |
* Compute a version code following this format : MmmPBBB |
|
59 |
* M is major, mm is minor, P is patch |
|
60 |
* BBB is build version number from hg |
|
61 |
*/ |
|
| 19 | 62 |
private fun ExecOperations.computeChangesetVersionCode(projectDir: File, major: Int = 0, minor: Int = 0, patch: Int = 0): Int {
|
| 1 | 63 |
val base = (major * 1000000) + (minor * 10000) + (patch * 1000) |
| 19 | 64 |
return base + (getHgLocalRevisionNumber(projectDir)?.trim()?.toIntOrNull() ?: 0) |
65 |
} |
|
66 |
||
67 |
private fun ExecOperations.runCommand( |
|
68 |
command: String, |
|
69 |
workingDir: File = File(".")
|
|
70 |
): String? {
|
|
71 |
val output = ByteArrayOutputStream() |
|
72 |
val result = exec {
|
|
73 |
commandLine(command.split("\\s".toRegex()))
|
|
74 |
setWorkingDir(workingDir) |
|
75 |
setStandardOutput(output) |
|
76 |
setErrorOutput(output) |
|
77 |
} |
|
78 |
result.rethrowFailure() |
|
79 |
return output.toString(Charsets.UTF_8) |
|
| 1 | 80 |
} |
81 |
||
| 19 | 82 |
abstract class VersionCodeTask : DefaultTask() {
|
83 |
||
84 |
@get:OutputFile |
|
85 |
abstract val versionCodeOutputFile: RegularFileProperty |
|
86 |
||
87 |
@get:OutputFile |
|
88 |
abstract val changesetOutputFile: RegularFileProperty |
|
89 |
||
90 |
@get:Input |
|
91 |
abstract val repositoryDirectory: Property<String> |
|
92 |
||
93 |
@get:Input |
|
94 |
abstract val major: Property<Int> |
|
95 |
||
96 |
@get:Input |
|
97 |
abstract val minor: Property<Int> |
|
98 |
||
99 |
@get:Input |
|
100 |
abstract val patch: Property<Int> |
|
101 |
||
102 |
@get:Inject |
|
103 |
abstract val exec: ExecOperations |
|
104 |
||
105 |
@TaskAction |
|
106 |
fun computeVersionCode() {
|
|
107 |
val projectDir = File(repositoryDirectory.get()) |
|
108 |
val versionCode = exec.computeChangesetVersionCode(projectDir, major.getOrElse(0), minor.getOrElse(0), patch.getOrElse(0)) |
|
109 |
versionCodeOutputFile.get().asFile.writeText("$versionCode")
|
|
110 |
} |
|
111 |
||
112 |
@TaskAction |
|
113 |
fun computeChangeset() {
|
|
114 |
val projectDir = File(repositoryDirectory.get()) |
|
115 |
val changeset = exec.getChangeSet(projectDir) |
|
116 |
changesetOutputFile.get().asFile.writeText(changeset) |
|
| 1 | 117 |
} |
118 |
} |
|
| 19 | 119 |
|
120 |
/** |
|
121 |
* @param versionNameSuffix extra string to add to version name |
|
122 |
*/ |
|
123 |
fun ApplicationAndroidComponentsExtension.configureVersionChangeset(project: Project, major: Int, minor: Int, patch: Int, versionNameSuffix: String = "") {
|
|
124 |
// Note: Everything in there is incubating. |
|
125 |
||
126 |
// onVariantProperties registers an action that configures variant properties during |
|
127 |
// variant computation (which happens during afterEvaluate) |
|
128 |
onVariants {
|
|
129 |
// Because app module can have multiple output when using mutli-APK, versionCode/Name |
|
130 |
// are only available on the variant output. |
|
131 |
// Here gather the output when we are in single mode (ie no multi-apk) |
|
132 |
val mainOutput = it.outputs.single { it.outputType == OutputType.SINGLE }
|
|
133 |
||
134 |
// create version Code generating task |
|
135 |
val versionCodeTask = project.tasks.register<VersionCodeTask>("computeVersionCodeFor${it.name.capitalized()}") {
|
|
136 |
this.major.set(major) |
|
137 |
this.minor.set(minor) |
|
138 |
this.patch.set(patch) |
|
139 |
repositoryDirectory.set(project.rootDir.absolutePath) |
|
140 |
versionCodeOutputFile.set(project.layout.buildDirectory.file("intermediates/versionCode.txt"))
|
|
141 |
changesetOutputFile.set(project.layout.buildDirectory.file("intermediates/changeset.txt"))
|
|
142 |
} |
|
143 |
||
144 |
// wire version code from the task output |
|
145 |
// map will create a lazy Provider that |
|
146 |
// 1. runs just before the consumer(s), ensuring that the producer (VersionCodeTask) has run |
|
147 |
// and therefore the file is created. |
|
148 |
// 2. contains task dependency information so that the consumer(s) run after the producer. |
|
149 |
mainOutput.versionCode.set(versionCodeTask.map { it.versionCodeOutputFile.get().asFile.readText().toInt() })
|
|
150 |
mainOutput.versionName.set("$major.$minor.$patch$versionNameSuffix")
|
|
151 |
||
152 |
it.buildConfigFields.put("REPOSITORY_CHANGESET", versionCodeTask.map {
|
|
153 |
BuildConfigField("String", "\"${it.changesetOutputFile.get().asFile.readText()}\"", "Repository changeset")
|
|
154 |
}) |
|
155 |
} |
|
156 |
} |