Skip to content

Commit

Permalink
refactor: simplify dependency version resource generation (#2851)
Browse files Browse the repository at this point in the history
Co-authored-by: Vitor Hugo Schwaab <[email protected]>
Co-authored-by: Mohamad Jaara <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2024
1 parent b6381f1 commit 319be19
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 107 deletions.
100 changes: 0 additions & 100 deletions buildSrc/src/main/kotlin/DependenciesVersionTask.kt

This file was deleted.

70 changes: 70 additions & 0 deletions buildSrc/src/main/kotlin/WriteKeyValuesToFileTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import org.gradle.api.DefaultTask
import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputFile
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/.
*/
abstract class WriteKeyValuesToFileTask : DefaultTask() {

/**
* The JSON file where the [keyValues] will be written
*/
@get:OutputFile
abstract val outputJsonFile: Property<File>

/**
* Map of key-value pairs that will be written to the [outputJsonFile].
*/
@get:Input
abstract val keyValues: MapProperty<String, String?>

init {
group = "build"
description = "Write a set of key-value pairs to a desired file"
}

@TaskAction
fun processGitBuildIdentifier() {
val outFile = outputJsonFile.get()
require(!outFile.isDirectory) {
"The specified output must be a regular file, not a directory: ${outFile.absolutePath}"
}
runCatching {
logger.debug("\uD83D\uDD27 Writing key-values to ${outFile.absolutePath}.")
keyValues.get().toJsonString().also { writeToFile(it) }
}.onFailure {
logger.error("\uD83D\uDD27 Failed to write key-values to file: ${it.stackTraceToString()}")
writeToFile("{}")
}
}

/**
* Write the given [text] to the [outputJsonFile].
*/
private fun writeToFile(text: String) {
FileOutputStream(outputJsonFile.get()).use {
it.write(text.toByteArray())
}
logger.debug("\u2705 Successfully wrote '$text' to $outputJsonFile.")
}
}
26 changes: 19 additions & 7 deletions buildSrc/src/main/kotlin/scripts/compilation.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,37 @@

package scripts

import DependenciesVersionTask
import WriteKeyValuesToFileTask
import IncludeGitBuildTask

plugins {
id("com.android.application") apply false
}

// TODO: Extract to a convention plugin
project.tasks.register("includeGitBuildIdentifier", IncludeGitBuildTask::class) {
val gitIdTask = project.tasks.register("includeGitBuildIdentifier", IncludeGitBuildTask::class) {
println("> Registering Task :includeGitBuildIdentifier")
}

project.tasks.register("dependenciesVersionTask", DependenciesVersionTask::class) {
println("> Registering Task :dependenciesVersionTask")
val dependenciesVersionTask = project.tasks.register("dependenciesVersionTask", WriteKeyValuesToFileTask::class) {
outputJsonFile.set(project.file("src/main/assets/dependencies_version.json"))
val catalogs = project.extensions.getByType(VersionCatalogsExtension::class.java)
val catalog = catalogs.named("klibs")
val pairs = mapOf(
"avs" to catalog.findVersion("avs").get().requiredVersion,
"core-crypto" to catalog.findVersion("core-crypto-multiplatform").get().requiredVersion
)
keyValues.set(pairs)
}

project.afterEvaluate {
project.tasks.matching { it.name.startsWith("bundle") || it.name.startsWith("assemble") }.configureEach {
dependsOn("includeGitBuildIdentifier")
dependsOn("dependenciesVersionTask")
project.tasks.matching {
it.name.startsWith("merge") &&
it.name.endsWith("Assets") ||
it.name.startsWith("lintVitalAnalyze")
}
.configureEach {
dependsOn(gitIdTask)
dependsOn(dependenciesVersionTask)
}
}
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ rootDir
include(it)
}

dependencyResolutionManagement {
versionCatalogs {
create("klibs") {
from(files("kalium/gradle/libs.versions.toml"))
}
}
}

// A work-around where we define the included builds in a different file
// so Reloaded's Dependabot doesn't try to look into Kalium's build.gradle.kts, which is inaccessible as it is a git submodule.
// See: https://github.com/dependabot/dependabot-core/issues/7201#issuecomment-1571319655
Expand Down

0 comments on commit 319be19

Please sign in to comment.