summaryrefslogtreecommitdiffhomepage
path: root/android/buildSrc/src/main/kotlin/Utils.kt
blob: f9f9a7528c6971f5e4cdc1997f340c763f971d20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import org.gradle.api.Project

// This is a hack and will not work correctly under all scenarios.
// See DROID-1696 for how we can improve this.
fun Project.isReleaseBuild() =
    gradle.startParameter.getTaskNames().any {
        it.contains("release", ignoreCase = true) || it.contains("fdroid", ignoreCase = true)
    }

fun Project.isAlphaBuild(): Boolean {
    val versionName = generateVersionName()
    return versionName.contains("alpha")
}

fun Project.isDevBuild(): Boolean {
    val versionName = generateVersionName()
    return versionName.contains("-dev-")
}

fun Project.generateVersionCode(): Int {
    val versionCode =
        getIntPropertyOrNull("mullvad.app.config.override.versionCode")
            ?: execVersionCodeCargoCommand()
    // This is a safety net to avoid generating too big version codes, since that could potentially
    // be hard and inconvenient to recover from.
    require(versionCode <= MAX_ALLOWED_VERSION_CODE) {
        "versionCode ($versionCode) must be <= $MAX_ALLOWED_VERSION_CODE"
    }
    return versionCode
}

fun Project.generateVersionName(): String =
    getStringPropertyOrNull("mullvad.app.config.override.versionName")
        ?: execVersionNameCargoCommand()

fun Project.generateRemapArguments(): String {
    val script = "${projectDir.parent}/../building/rustc-remap-path-prefix.sh"
    return providers.exec { commandLine(script) }.standardOutput.asText.get().trim()
}

private fun Project.execVersionCodeCargoCommand() =
    providers
        .exec { commandLine("cargo", "run", "-q", "--bin", "mullvad-version", "versionCode") }
        .standardOutput
        .asText
        .get()
        .trim()
        .toInt()

private fun Project.execVersionNameCargoCommand() =
    providers
        .exec { commandLine("cargo", "run", "-q", "--bin", "mullvad-version", "versionName") }
        .standardOutput
        .asText
        .get()
        .trim()

fun Project.getStringPropertyOrNull(name: String): String? = findProperty(name)?.toString()

fun Project.getIntPropertyOrNull(name: String): Int? = findProperty(name)?.toString()?.toInt()

fun Project.getBooleanPropertyOrNull(name: String): Boolean? =
    findProperty(name)?.toString()?.toBooleanStrict()

fun Project.getStringProperty(name: String): String = properties[name].toString()

fun Project.getIntProperty(name: String): Int = properties[name].toString().toInt()

fun Project.getBooleanProperty(name: String): Boolean =
    properties[name].toString().toBooleanStrict()

// Fetch a string and that is split by `,` into a list of strings
const val STRING_LIST_SEPARATOR = ','

fun Project.getStringListProperty(name: String): List<String> =
    properties[name].toString().split(STRING_LIST_SEPARATOR)