|
10
|
1 |
/*
|
|
|
2 |
* Geekdroid is a utility library for development on the Android
|
|
|
3 |
* Platform.
|
|
|
4 |
*
|
|
|
5 |
* Copyright (C) 2017-2020 by Frederic-Charles Barthelery.
|
|
|
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 |
|
|
|
24 |
import org.gradle.api.Project
|
|
|
25 |
import java.io.File
|
|
|
26 |
import java.io.IOException
|
|
|
27 |
import java.util.concurrent.TimeUnit
|
|
|
28 |
|
|
|
29 |
internal fun Project.getGitSha1(): String? = runCommand("git rev-parse HEAD", workingDir = projectDir)?.trim()
|
|
|
30 |
|
|
|
31 |
internal fun Project.getHgSha1(): String? = runCommand("hg id --debug -i -r .", workingDir = projectDir)?.trim()
|
|
|
32 |
|
|
|
33 |
internal fun Project.getHgLocalRevisionNumber(): String? = runCommand("hg id -n -r .", workingDir = projectDir)?.trim()
|
|
|
34 |
|
|
|
35 |
fun Project.getChangeSet(): String {
|
|
|
36 |
val git = rootProject.file(".git")
|
|
|
37 |
val hg = rootProject.file(".hg")
|
|
|
38 |
return when {
|
|
|
39 |
git.exists() -> "git:${getGitSha1()}"
|
|
|
40 |
hg.exists() -> "hg:${getHgSha1()}"
|
|
|
41 |
else -> "unknown"
|
|
|
42 |
}
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Compute a version code following this format : MmmPBBB
|
|
|
47 |
* M is major, mm is minor, P is patch
|
|
|
48 |
* BBB is build version number from hg
|
|
|
49 |
*/
|
|
|
50 |
fun Project.computeChangesetVersionCode(major: Int = 0, minor: Int = 0, patch: Int = 0): Int {
|
|
|
51 |
val base = (major * 1000000) + (minor * 10000) + (patch * 1000)
|
|
|
52 |
return base + (getHgLocalRevisionNumber()?.trim()?.toIntOrNull() ?: 0)
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private fun Project.runCommand(
|
|
|
56 |
command: String,
|
|
|
57 |
workingDir: File = File("."),
|
|
|
58 |
timeoutAmount: Long = 60,
|
|
|
59 |
timeoutUnit: TimeUnit = TimeUnit.MINUTES
|
|
|
60 |
): String? {
|
|
|
61 |
return try {
|
|
|
62 |
ProcessBuilder(*command.split("\\s".toRegex()).toTypedArray())
|
|
|
63 |
.directory(workingDir)
|
|
|
64 |
.redirectOutput(ProcessBuilder.Redirect.PIPE)
|
|
|
65 |
.redirectError(ProcessBuilder.Redirect.PIPE)
|
|
|
66 |
.start().apply {
|
|
|
67 |
waitFor(timeoutAmount, timeoutUnit)
|
|
|
68 |
}.inputStream.bufferedReader().readText()
|
|
|
69 |
} catch (e: IOException) {
|
|
|
70 |
logger.info("Unable to run command", e)
|
|
|
71 |
null
|
|
|
72 |
}
|
|
|
73 |
}
|