Skip to content

Commit d98017f

Browse files
committed
Bumped build-logic-base
1 parent fbb1507 commit d98017f

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

build-logic/build-logic-base/src/main/kotlin/com/ensody/buildlogic/BuildLogicBasePlugin.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ fun Project.setupBuildLogicBase(block: Project.() -> Unit) {
3535
group = (listOf(rootProject.group) + project.path.trimStart(':').split(".").dropLast(1))
3636
.joinToString(".")
3737
block()
38+
afterEvaluate {
39+
val generatedRoot = getGeneratedBuildFilesRoot()
40+
val generatedRelativePaths = generatedFiles[this.path].orEmpty().flatMap { it.relativeTo(generatedRoot).withParents().map { it.toString() } }.toSet()
41+
for (file in generatedRoot.walkBottomUp()) {
42+
if (file == generatedRoot) continue
43+
if (file.relativeTo(generatedRoot).toString() !in generatedRelativePaths) {
44+
file.deleteRecursively()
45+
}
46+
}
47+
}
3848
}
3949

4050
val libs get() = buildLogicBaseDeps.libs

build-logic/build-logic-base/src/main/kotlin/com/ensody/buildlogic/Utils.kt

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
11
package com.ensody.buildlogic
22

3+
import com.android.build.gradle.internal.cxx.io.writeTextIfDifferent
34
import org.gradle.api.Project
5+
import org.gradle.kotlin.dsl.findByType
6+
import org.gradle.kotlin.dsl.get
7+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmExtension
8+
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
9+
import java.io.File
410

511
val Project.isRootProject get() = this == rootProject
612

7-
fun shell(command: String): String {
8-
val process = ProcessBuilder("/bin/bash", "-c", command).start()
13+
fun shell(command: String, workingDir: File? = null, inheritIO: Boolean = false): String {
14+
val processBuilder = ProcessBuilder("/bin/bash", "-c", command)
15+
workingDir?.let { processBuilder.directory(it) }
16+
processBuilder.redirectErrorStream(true)
17+
val process = processBuilder.start()
918
return process.inputStream.bufferedReader().readText().trim().also {
10-
process.waitFor()
19+
val exitCode = process.waitFor()
20+
if (inheritIO) {
21+
println(it)
22+
}
23+
check(exitCode == 0) { "Process exit code was: $exitCode\nOriginal command: $command"}
1124
}
1225
}
1326

27+
fun Project.withGeneratedBuildFile(category: String, path: String, sourceSet: String? = null, content: () -> String) {
28+
val generatedDir = file("${getGeneratedBuildFilesRoot()}/$category")
29+
extensions.findByType<KotlinJvmExtension>()?.apply {
30+
sourceSets[sourceSet ?: "main"].kotlin.srcDir(generatedDir)
31+
} ?: extensions.findByType<KotlinMultiplatformExtension>()?.apply {
32+
sourceSets[sourceSet ?: "commonMain"].kotlin.srcDir(generatedDir)
33+
} ?: error("Don't know how to add generated build file because project ${this.path} has unknown type")
34+
val outputPath = file("$generatedDir/$path")
35+
outputPath.writeTextIfDifferent(content().trimIndent().trimStart() + "\n")
36+
generatedFiles.getOrPut(this.path) { mutableSetOf() }.add(outputPath.normalize().absoluteFile)
37+
}
38+
39+
internal val generatedFiles = mutableMapOf<String, MutableSet<File>>()
40+
41+
internal fun File.withParents(): List<File> =
42+
buildList {
43+
add(this@withParents)
44+
while (true) {
45+
add(last().parentFile ?: break)
46+
}
47+
}
48+
49+
internal fun Project.getGeneratedBuildFilesRoot(): File =
50+
file("$projectDir/build/generated/source/build-logic")
51+
1452
internal fun Project.detectProjectVersion(): String =
1553
System.getenv("OVERRIDE_VERSION")?.takeIf { it.isNotBlank() }
1654
?: shell("git tag --points-at HEAD").split("\n").filter {

0 commit comments

Comments
 (0)