Skip to content

Commit

Permalink
feat: add avs and cc version to debug screen (#2830)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamadJaara authored Apr 2, 2024
1 parent 887b2e2 commit bd360e1
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ lint/tmp/

# Autogenerated file with git hash information.
app/src/main/assets/version.txt
app/src/main/assets/dependencies_version.json
/intellij.gdsl

# Editor temporary files
*~
\#*#
\#*#
60 changes: 53 additions & 7 deletions app/src/main/kotlin/com/wire/android/ui/debug/DebugDataOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -52,6 +53,7 @@ import com.wire.android.ui.home.settings.SettingsItem
import com.wire.android.ui.theme.wireColorScheme
import com.wire.android.ui.theme.wireDimensions
import com.wire.android.ui.theme.wireTypography
import com.wire.android.util.getDependenciesVersion
import com.wire.android.util.getDeviceIdString
import com.wire.android.util.getGitBuildId
import com.wire.android.util.ui.PreviewMultipleThemes
Expand All @@ -69,6 +71,9 @@ import com.wire.kalium.logic.sync.periodic.UpdateApiVersionsScheduler
import com.wire.kalium.logic.sync.slow.RestartSlowSyncProcessForRecoveryUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.collections.immutable.ImmutableMap
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -85,7 +90,8 @@ data class DebugDataOptionsState(
val commitish: String = "null",
val certificate: String = "null",
val showCertificate: Boolean = false,
val startGettingE2EICertificate: Boolean = false
val startGettingE2EICertificate: Boolean = false,
val dependencies: ImmutableMap<String, String?> = persistentMapOf()
)

@Suppress("LongParameterList")
Expand All @@ -110,10 +116,19 @@ class DebugDataOptionsViewModel
observeEncryptedProteusStorageState()
observeMlsMetadata()
checkIfCanTriggerManualMigration()
state = state.copy(
debugId = context.getDeviceIdString() ?: "null",
commitish = context.getGitBuildId()
)
checkDependenciesVersion()
setGitHashAndDeviceId()
}

private fun setGitHashAndDeviceId() {
viewModelScope.launch {
val deviceId = context.getDeviceIdString() ?: "null"
val gitBuildId = context.getGitBuildId()
state = state.copy(
debugId = deviceId,
commitish = gitBuildId
)
}
}

fun checkCrlRevocationList() {
Expand All @@ -124,6 +139,15 @@ class DebugDataOptionsViewModel
}
}

fun checkDependenciesVersion() {
viewModelScope.launch {
val dependencies = context.getDependenciesVersion().toImmutableMap()
state = state.copy(
dependencies = dependencies
)
}
}

fun enableEncryptedProteusStorage(enabled: Boolean) {
if (enabled) {
viewModelScope.launch {
Expand Down Expand Up @@ -375,7 +399,8 @@ fun DebugDataOptionsContent(
onDisableEventProcessingChange = onDisableEventProcessingChange,
onRestartSlowSyncForRecovery = onRestartSlowSyncForRecovery,
onForceUpdateApiVersions = onForceUpdateApiVersions,
checkCrlRevocationList = checkCrlRevocationList
checkCrlRevocationList = checkCrlRevocationList,
dependenciesMap = state.dependencies
)
}

Expand Down Expand Up @@ -544,7 +569,8 @@ private fun DebugToolsOptions(
onDisableEventProcessingChange: (Boolean) -> Unit,
onRestartSlowSyncForRecovery: () -> Unit,
onForceUpdateApiVersions: () -> Unit,
checkCrlRevocationList: () -> Unit
checkCrlRevocationList: () -> Unit,
dependenciesMap: ImmutableMap<String, String?>
) {
FolderHeader(stringResource(R.string.label_debug_tools_title))
Column {
Expand Down Expand Up @@ -615,6 +641,17 @@ private fun DebugToolsOptions(
)
}
)
RowItemTemplate(
modifier = Modifier.wrapContentWidth(),
title = {
Text(
style = MaterialTheme.wireTypography.body01,
color = MaterialTheme.wireColorScheme.onBackground,
text = prettyPrintMap(dependenciesMap),
modifier = Modifier.padding(start = dimensions().spacing8x)
)
}
)
}
}

Expand Down Expand Up @@ -646,6 +683,15 @@ private fun DisableEventProcessingSwitch(
}
)
}

@Stable
private fun prettyPrintMap(map: ImmutableMap<String, String?>): String = StringBuilder().apply {
append("Dependencies:\n")
map.forEach { (key, value) ->
append("$key: $value\n")
}
}.toString()

//endregion

@PreviewMultipleThemes
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/kotlin/com/wire/android/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import com.wire.kalium.logic.util.buildFileName
import com.wire.kalium.logic.util.splitFileExtensionAndCopyCounter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import okio.Path
import java.io.File
import java.io.FileNotFoundException
Expand Down Expand Up @@ -421,6 +422,14 @@ fun Context.getGitBuildId(): String = runCatching {
}
}.getOrDefault("")

suspend fun Context.getDependenciesVersion(): Map<String, String?> = withContext(Dispatchers.IO) {
assets.open("dependencies_version.json").use { inputStream ->
inputStream.bufferedReader().use { it.readText() }
}.let {
Json.decodeFromString(it)
}
}

fun Context.getProviderAuthority() = "$packageName.provider"

@VisibleForTesting
Expand Down
100 changes: 100 additions & 0 deletions buildSrc/src/main/kotlin/DependenciesVersionTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.FileOutputStream

/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
open class DependenciesVersionTask : DefaultTask() {

private val VERSION_FILE = "app/src/main/assets/dependencies_version.json"

// map of toml file and list of dependencies to extract version
private val dependencies = mapOf(
"kalium/gradle/libs.versions.toml" to listOf(
"avs",
"core-crypto"
)
)

init {
group = "release"
description = "Get dependencies version and write it to the application, if possible."
}

@TaskAction
fun processGitBuildIdentifier() {
runCatching {
println("\uD83D\uDD27 Running dependencies version parser to build.")
mutableMapOf<String, String?>().apply {
for ((tomlFile, dependencies) in dependencies) {
val toml = File(tomlFile).readText()
val tables = parseToml(toml)
for (dependency in dependencies) {
val version = tables["versions"]?.get(dependency)
println("\uD83D\uDD27 $dependency version: $version")
put(dependency, version)
}
}
}.toJsonString()
.also { writeToFile(it) }
}.onFailure {
println("\uD83D\uDD27 Failed to extract dependencies version: ${it.stackTraceToString()}")
writeToFile("{}")
}
}

fun parseToml(tomlContent: String): Map<String, Map<String, String>> {
val table = mutableMapOf<String, MutableMap<String, String>>()
var currentTable = ""

// Regular expression to match table headers and key-value pairs
val regex = Regex("""\[(.*?)\]|\s*([^\s=]+)\s*=\s*(".*?"|[^\r\n#]+)""")

// Iterate over lines of the TOML content
tomlContent.lines().forEach { line ->
val matchResult = regex.find(line)

// If it's a table header
if (line.startsWith("[")) {
currentTable = matchResult?.groups?.get(1)?.value ?: ""
table[currentTable] = mutableMapOf()
}
// If it's a key-value pair
else if (matchResult != null) {
val key = matchResult.groups[2]?.value?.trim('"')
val value = matchResult.groups[3]?.value?.trim('"')

if (!key.isNullOrBlank() && !value.isNullOrBlank()) {
table[currentTable]?.put(key, value)
}
}
}
return table
}

/**
* Write the given [text] to the [VERSION_FILE].
*/
private fun writeToFile(text: String) {
FileOutputStream(File(project.rootDir, VERSION_FILE)).use {
it.write(text.toByteArray())
}
println("\u2705 Successfully wrote $text to file.")
}
}
31 changes: 31 additions & 0 deletions buildSrc/src/main/kotlin/MapTojson.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Wire
* Copyright (C) 2024 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

fun Map<String, String?>.toJsonString (): String {
if (this.isEmpty()) return "{}"

return StringBuilder().apply {
append("{")
this@toJsonString.forEach { (key, value) ->
append("\"$key\":\"$value\",")
}
deleteCharAt(length - 1)
append("}")

}.toString()
}
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/scripts/compilation.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package scripts

import DependenciesVersionTask
import IncludeGitBuildTask

plugins {
Expand All @@ -29,8 +30,13 @@ project.tasks.register("includeGitBuildIdentifier", IncludeGitBuildTask::class)
println("> Registering Task :includeGitBuildIdentifier")
}

project.tasks.register("dependenciesVersionTask", DependenciesVersionTask::class) {
println("> Registering Task :dependenciesVersionTask")
}

project.afterEvaluate {
project.tasks.matching { it.name.startsWith("bundle") || it.name.startsWith("assemble") }.configureEach {
dependsOn("includeGitBuildIdentifier")
dependsOn("dependenciesVersionTask")
}
}

0 comments on commit bd360e1

Please sign in to comment.