Skip to content

Commit

Permalink
Move CI tasks to build.gradle.kts
Browse files Browse the repository at this point in the history
  • Loading branch information
siarhei-luskanau committed Dec 30, 2024
1 parent 18bcba6 commit f50a983
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 166 deletions.
165 changes: 161 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
@file:Suppress("PropertyName")

import java.io.InputStream
import java.util.Properties
import org.apache.tools.ant.taskdefs.condition.Os

plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
Expand All @@ -6,8 +12,6 @@ plugins {
alias(libs.plugins.detekt)
}

apply(from = "$rootDir/ci.gradle.kts")

allprojects {
apply(from = "$rootDir/ktlint.gradle")
apply(plugin = "io.gitlab.arturbosch.detekt")
Expand All @@ -31,6 +35,159 @@ subprojects {
}
}

tasks.register("clean").configure {
delete("build")
val CI_GRADLE = "CI_GRADLE"

tasks.register("ciBuildAndTest") {
group = CI_GRADLE
doLast {
gradlew(
"clean",
"ktlintCheck",
"detekt",
"lint",
"assembleDebug"
)
gradlew("test")
gradlew(
"koverXmlReport",
"koverHtmlReport",
"koverVerify"
)
}
}

tasks.register("ciSdkManagerLicenses") {
group = CI_GRADLE
doLast {
val sdkDirPath = getAndroidSdkPath(rootDir = rootDir)
getSdkManagerFile(rootDir = rootDir)?.let { sdkManagerFile ->
val yesInputStream =
object : InputStream() {
private val yesString = "y\n"
private var counter = 0

override fun read(): Int = yesString[counter % 2].also { counter++ }.code
}
providers.exec {
executable = sdkManagerFile.absolutePath
args = listOf("--list", "--sdk_root=$sdkDirPath")
println("exec: ${this.commandLine.joinToString(separator = " ")}")
}.apply { println("ExecResult: ${this.result.get()}") }
@Suppress("DEPRECATION")
exec {
executable = sdkManagerFile.absolutePath
args = listOf("--licenses", "--sdk_root=$sdkDirPath")
standardInput = yesInputStream
println("exec: ${this.commandLine.joinToString(separator = " ")}")
}.apply { println("ExecResult: $this") }
}
}
}

tasks.register("devAll") {
group = CI_GRADLE
doLast {
gradlew(
"clean",
"ktlintFormat",
"ciBuildAndTest"
)
}
}

tasks.register("devEmulatorAll") {
group = CI_GRADLE
doLast {
gradlew("ciSdkManagerLicenses")
gradlew("cleanManagedDevices", "--unused-only")
(27..35).forEach { apiLevelIt ->
val name =
listOf(
"managedVirtualDevice",
apiLevelIt.toString()
).joinToString(separator = "")
gradlew(
"${name}DebugAndroidTest",
"-Pandroid.testInstrumentationRunnerArguments.class=" +
"siarhei.luskanau.managed.virtual.device.ExampleInstrumentedTest",
"-Pandroid.testoptions.manageddevices.emulator.gpu=swiftshader_indirect",
"--enable-display"
)
}
gradlew("cleanManagedDevices")
}
}

fun gradlew(vararg tasks: String, addToSystemProperties: Map<String, String>? = null) {
providers.exec {
executable =
File(
project.rootDir,
if (Os.isFamily(Os.FAMILY_WINDOWS)) "gradlew.bat" else "gradlew"
)
.also { it.setExecutable(true) }
.absolutePath
args =
mutableListOf<String>().also { mutableArgs ->
mutableArgs.addAll(tasks)
addToSystemProperties?.toList()?.map { "-D${it.first}=${it.second}" }?.let {
mutableArgs.addAll(it)
}
mutableArgs.add("--stacktrace")
}
val sdkDirPath =
Properties().apply {
val propertiesFile = File(rootDir, "local.properties")
if (propertiesFile.exists()) {
load(propertiesFile.inputStream())
}
}.getProperty("sdk.dir")
if (sdkDirPath != null) {
val platformToolsDir = "$sdkDirPath${java.io.File.separator}platform-tools"
val pahtEnvironment = System.getenv("PATH").orEmpty()
if (!pahtEnvironment.contains(platformToolsDir)) {
environment =
environment.toMutableMap().apply {
put("PATH", "$platformToolsDir:$pahtEnvironment")
}
}
}
if (System.getenv("JAVA_HOME") == null) {
System.getProperty("java.home")?.let { javaHome ->
environment =
environment.toMutableMap().apply {
put("JAVA_HOME", javaHome)
}
}
}
if (System.getenv("ANDROID_HOME") == null) {
environment =
environment.toMutableMap().apply {
put("ANDROID_HOME", "$sdkDirPath")
}
}
println("commandLine: ${this.commandLine.joinToString(separator = " ")}")
}.apply { println("ExecResult: ${this.result.get()}") }
}

fun getAndroidSdkPath(rootDir: File): String? = Properties().apply {
val propertiesFile = File(rootDir, "local.properties")
if (propertiesFile.exists()) {
load(propertiesFile.inputStream())
}
}.getProperty("sdk.dir").let { propertiesSdkDirPath ->
(propertiesSdkDirPath ?: System.getenv("ANDROID_HOME"))
}

fun getSdkManagerFile(rootDir: File): File? =
getAndroidSdkPath(rootDir = rootDir)?.let { sdkDirPath ->
println("sdkDirPath: $sdkDirPath")
val files =
File(sdkDirPath).walk().filter { file ->
file.path.contains("cmdline-tools") && file.path.endsWith("sdkmanager")
}
files.forEach { println("walk: ${it.absolutePath}") }
val sdkmanagerFile = files.firstOrNull()
println("sdkmanagerFile: $sdkmanagerFile")
sdkmanagerFile
}
162 changes: 0 additions & 162 deletions ci.gradle.kts

This file was deleted.

0 comments on commit f50a983

Please sign in to comment.