Skip to content

Commit 44fa6ed

Browse files
committed
Build script cleanups
1 parent b7215ca commit 44fa6ed

File tree

16 files changed

+91
-38
lines changed

16 files changed

+91
-38
lines changed

build-logic/build-logic-base/build.gradle.kts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ plugins {
44
}
55

66
dependencies {
7-
api(libs.gradle.android)
8-
api(libs.gradle.kotlin)
9-
api(libs.gradle.cocoapods)
10-
api(libs.gradle.detekt)
11-
api(libs.gradle.dokka)
12-
api(libs.gradle.kotlin.compose)
13-
api(libs.gradle.jetbrains.compose)
14-
api(libs.gradle.kotlin.jvm)
15-
api(libs.gradle.maven.publish)
7+
api(rootLibs.gradle.android)
8+
api(rootLibs.gradle.kotlin)
9+
api(rootLibs.gradle.cocoapods)
10+
api(rootLibs.gradle.detekt)
11+
api(rootLibs.gradle.dokka)
12+
api(rootLibs.gradle.kotlin.compose)
13+
api(rootLibs.gradle.jetbrains.compose)
14+
api(rootLibs.gradle.kotlin.jvm)
15+
api(rootLibs.gradle.maven.publish)
1616
}
1717

1818
val autoDetectPluginRegex = Regex("""^(?:public\s+)?class\s+(\w+)BuildLogicPlugin\s*:.*$""", RegexOption.MULTILINE)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fun KotlinMultiplatformExtension.allAndroidNative() {
146146
androidNativeX86()
147147
}
148148

149-
fun KotlinMultiplatformExtension.allAppleMobile(x64: Boolean = true, onlyComposeSupport: Boolean) {
149+
fun KotlinMultiplatformExtension.allAppleMobile(x64: Boolean = true, onlyComposeSupport: Boolean = false) {
150150
allIos(x64 = x64)
151151
allTvos()
152152
allWatchos(onlyComposeSupport = onlyComposeSupport)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ fun Project.setupBuildLogicBase(block: Project.() -> Unit) {
167167
}
168168
}
169169

170-
val libs get() = buildLogicBaseDeps.libs
170+
val rootLibs get() = buildLogicBaseDeps.libs

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

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,21 @@ import java.io.File
1010

1111
val Project.isRootProject get() = this == rootProject
1212

13-
fun shell(
14-
command: String,
13+
fun cli(
14+
vararg command: String,
1515
workingDir: File? = null,
1616
env: Map<String, String> = emptyMap(),
1717
inheritIO: Boolean = false,
1818
): String {
19-
val processBuilder = ProcessBuilder("/bin/bash", "-c", command)
19+
var cmd = command.first().replace("/", File.separator)
20+
if (OS.current == OS.Windows) {
21+
if (File("$cmd.bat").exists()) {
22+
cmd += ".bat"
23+
} else if (File("$cmd.exe").exists()) {
24+
cmd += ".exe"
25+
}
26+
}
27+
val processBuilder = ProcessBuilder(cmd, *command.drop(1).toTypedArray())
2028
workingDir?.let { processBuilder.directory(it) }
2129
processBuilder.redirectErrorStream(true)
2230
processBuilder.environment().putAll(env)
@@ -26,10 +34,18 @@ fun shell(
2634
if (inheritIO) {
2735
println(it)
2836
}
29-
check(exitCode == 0) { "Process exit code was: $exitCode\nOriginal command: $command" }
37+
check(exitCode == 0) { "Process exit code was: $exitCode\nOriginal command: ${command.toList()}\nResult:$it" }
3038
}
3139
}
3240

41+
fun shell(
42+
command: String,
43+
workingDir: File? = null,
44+
env: Map<String, String> = emptyMap(),
45+
inheritIO: Boolean = false,
46+
): String =
47+
cli("/bin/bash", "-c", command, workingDir = workingDir, env = env, inheritIO = inheritIO)
48+
3349
fun Project.withGeneratedBuildFile(category: String, path: String, sourceSet: String? = null, content: () -> String) {
3450
val generatedDir = file("${getGeneratedBuildFilesRoot()}/$category")
3551
extensions.findByType<KotlinJvmExtension>()?.apply {
@@ -61,16 +77,35 @@ internal fun Project.getGeneratedBuildFilesRoot(): File =
6177
file("$projectDir/build/generated/source/build-logic")
6278

6379
internal fun Project.detectProjectVersion(): String =
64-
System.getenv("OVERRIDE_VERSION")?.takeIf { it.isNotBlank() }
65-
?: shell("git tag --points-at HEAD").split("\n").filter {
80+
System.getenv("OVERRIDE_VERSION")?.removePrefix("v")?.removePrefix("-")?.takeIf { it.isNotBlank() }
81+
?: cli("git", "tag", "--points-at", "HEAD").split("\n").filter {
6682
versionRegex.matchEntire(it) != null
6783
}.maxByOrNull {
6884
VersionComparable(versionRegex.matchEntire(it)!!.destructured.toList())
6985
}?.removePrefix("v")?.removePrefix("-") ?: run {
70-
val branchName = shell("git rev-parse --abbrev-ref HEAD")
86+
val branchName = cli("git", "rev-parse", "--abbrev-ref", "HEAD")
7187
"999999.0.0-${sanitizeBranchName(branchName)}.1"
7288
}
7389

90+
enum class OS {
91+
Linux,
92+
macOS,
93+
Windows,
94+
;
95+
96+
companion object Companion {
97+
val current: OS by lazy {
98+
val osName = System.getProperty("os.name").lowercase()
99+
when {
100+
"mac" in osName || "darwin" in osName -> macOS
101+
"linux" in osName -> Linux
102+
"windows" in osName -> Windows
103+
else -> error("Unknown operating system: $osName")
104+
}
105+
}
106+
}
107+
}
108+
74109
private class VersionComparable(val parts: List<String>) : Comparable<VersionComparable> {
75110
override fun compareTo(other: VersionComparable): Int {
76111
for ((l, r) in parts.zip(other.parts)) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ fun Project.setupVersionCatalog() {
99
gradle.taskGraph.whenReady {
1010
configure<CatalogPluginExtension> {
1111
versionCatalog {
12-
version("kotlin", libs.findVersion("kotlin").get().toString())
12+
version("kotlin", rootLibs.findVersion("kotlin").get().toString())
1313
val versionAlias = version(rootProject.name.lowercase(), project.version.toString())
1414
for (subproject in rootProject.subprojects) {
1515
if (!subproject.plugins.hasPlugin("maven-publish") ||

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fun Project.setupBuildLogic(block: Project.() -> Unit) {
4848
setupPlatformProject()
4949
}
5050
if (extensions.findByType<BaseExtension>() != null) {
51-
setupAndroid(coreLibraryDesugaring = libs.findLibrary("desugarJdkLibs").get())
51+
setupAndroid(coreLibraryDesugaring = rootLibs.findLibrary("desugarJdkLibs").get())
5252
}
5353
if (extensions.findByType<KotlinMultiplatformExtension>() != null) {
5454
setupKmp {
@@ -61,8 +61,8 @@ fun Project.setupBuildLogic(block: Project.() -> Unit) {
6161
}
6262

6363
sourceSets["jvmCommonTest"].dependencies {
64-
implementation(libs.findLibrary("kotlin-test-junit").get())
65-
implementation(libs.findLibrary("junit").get())
64+
implementation(rootLibs.findLibrary("kotlin-test-junit").get())
65+
implementation(rootLibs.findLibrary("junit").get())
6666
}
6767
}
6868
// testDebugUnitTest throws an error if there are no tests
@@ -86,7 +86,7 @@ fun Project.setupBuildLogic(block: Project.() -> Unit) {
8686
}
8787
}
8888
if (extensions.findByType<KotlinBaseExtension>() != null) {
89-
setupKtLint(libs.findLibrary("ktlint-cli").get())
89+
setupKtLint(rootLibs.findLibrary("ktlint-cli").get())
9090
}
9191
if (extensions.findByType<KotlinJvmExtension>() != null) {
9292
setupKotlinJvm()

build-logic/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
alias(libs.plugins.kotlin) apply false
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[versions]
2+
kotlin = "2.2.0"
3+
4+
[libraries]
5+
6+
[plugins]
7+
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

build-logic/settings.gradle.kts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,30 @@ dependencyResolutionManagement {
88
}
99

1010
versionCatalogs {
11-
create("libs") {
11+
create("rootLibs") {
1212
from(files("../gradle/libs.versions.toml"))
1313
}
1414
}
1515
}
1616

1717
rootProject.name = "build-logic-root"
1818

19-
include(":build-logic-base")
20-
include(":build-logic")
19+
val ignorePaths = setOf("build", "docs", "gradle", "src")
20+
fun autoDetectModules(root: File) {
21+
for (file in root.listFiles()) {
22+
if (file.name.startsWith(".") || file.name in ignorePaths) {
23+
continue
24+
}
25+
if (file.isDirectory) {
26+
val children = file.list()
27+
if ("settings.gradle.kts" in children) continue
28+
if (children.any { it == "build.gradle.kts" }) {
29+
include(":" + file.relativeTo(rootDir).path.replace("/", ":").replace("\\", ":"))
30+
} else {
31+
autoDetectModules(file)
32+
}
33+
}
34+
}
35+
}
36+
37+
autoDetectModules(rootDir)

gradle.properties

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ kotlin.mpp.androidSourceSetLayoutVersion=2
2525

2626
# This is not yet ready for strict mode
2727
#kotlin.js.generate.externals=true
28-
# FIXME: This breaks Dokka + Gradle 8 and will be fixed with Kotlin 1.9. We currently don't need it anyway.
29-
# https://youtrack.jetbrains.com/issue/KT-57482/cleanNativeDistributionCommonization-is-not-compatible-with-configuration-cache-with-gradle-8.0
30-
# https://youtrack.jetbrains.com/issue/KT-58818
31-
#kotlin.mpp.enableCInteropCommonization=true
28+
29+
kotlin.mpp.enableCInteropCommonization=true
3230

3331
kotlin.native.binary.objcExportSuspendFunctionLaunchThreadRestriction=none
3432

0 commit comments

Comments
 (0)