Skip to content

Commit

Permalink
Fix problem with exec and command executions, because we override and…
Browse files Browse the repository at this point in the history
… only execute the last command
  • Loading branch information
tomascayuelas committed Nov 15, 2023
1 parent 812b744 commit cd6915e
Showing 1 changed file with 37 additions and 38 deletions.
75 changes: 37 additions & 38 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,58 @@ allprojects {
group = property("project.group").toString()
}

fun isMultiplatformModule(project: Project): Boolean {
val kotlinPluginId = "libs.plugins.kotlin.multiplatform"
return project.buildFile.readText().contains(kotlinPluginId)
}
fun Project.configureBuildAndTestTask(taskName: String, moduleType: ModulePlatformType) {
val platform: String by extra
val multiPlatformModules = project.subprojects.filter { isMultiPlatformModule(it) }.map { it.name }

val multiPlatformModules = project.subprojects.filter { isMultiplatformModule(it) }.map { it.name }
tasks.register(taskName) {
val gradleCommand = getGradleCommand(platform)

enum class ModuleType { SINGLEPLATFORM, MULTIPLATFORM }
doFirst {
project.exec {commandLine(gradleCommand, "spotlessCheck") }
}

fun Project.configureBuildAndTestTask(
taskName: String,
moduleType: ModuleType,
multiPlatformModules: List<String>
) {
val platform: String by extra
tasks.register(taskName) {
doLast {
val gradleCommand = getGradleCommand(platform)
project.exec {
commandLine(gradleCommand, "spotlessCheck")
}
project.exec {
when (moduleType) {
ModuleType.SINGLEPLATFORM -> {
commandLine(gradleCommand, "build", *buildExcludeOptions(multiPlatformModules))
when (moduleType) {
ModulePlatformType.SINGLE -> {
val excludedModules = includeOrNotModulesToCommand(multiPlatformModules, platform, false)
println("modulesToExclude: ${excludedModules.toList()}")
project.exec { commandLine(gradleCommand, "build", *excludedModules) }
}
ModuleType.MULTIPLATFORM -> {
multiPlatformModules.forEach { module ->
commandLine(gradleCommand, ":$module:${platform}Test")
}

ModulePlatformType.MULTI -> {
val includedModules = includeOrNotModulesToCommand(multiPlatformModules, platform, true)
println("modulesToInclude: ${includedModules.toList()}")
project.exec { commandLine(gradleCommand, *includedModules) }
}
}
}
}
}
}

enum class ModulePlatformType { SINGLE, MULTI }

fun isMultiPlatformModule(project: Project): Boolean {
val kotlinPluginId = "libs.plugins.kotlin.multiplatform"
return project.buildFile.readText().contains(kotlinPluginId)
}

fun includeOrNotModulesToCommand(modules: List<String>, platform: String, include: Boolean): Array<String> {
return modules.flatMap {
when (include) {
true -> listOf(":$it:${platform}Test")
false -> listOf("-x", ":$it:build")
}
}.toTypedArray()
}

fun buildExcludeOptions(modules: List<String>): Array<String> {
return modules.flatMap { listOf("-x", ":$it:build") }.toTypedArray()
return modules.flatMap { module -> listOf("-x", ":$module:build") }.toTypedArray()
}

fun getGradleCommand(platform: String): String {
return if (platform == "mingwX64") "gradlew.bat" else "./gradlew"
}

configureBuildAndTestTask(
"buildAndTestMultip",
ModuleType.MULTIPLATFORM,
multiPlatformModules
)

configureBuildAndTestTask(
"buildAndTestSinglep",
ModuleType.SINGLEPLATFORM,
multiPlatformModules
)
configureBuildAndTestTask("buildAndTestMultip", ModulePlatformType.MULTI)
configureBuildAndTestTask("buildAndTestSinglep", ModulePlatformType.SINGLE)

0 comments on commit cd6915e

Please sign in to comment.