Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] - Gradle problem in buildAndTestSinglep and buildAndTestMultip #543

Merged
merged 5 commits into from
Nov 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 32 additions & 40 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,59 +15,51 @@ 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))
}
ModuleType.MULTIPLATFORM -> {
multiPlatformModules.forEach { module ->
commandLine(gradleCommand, ":$module:${platform}Test")
}
}
when (moduleType) {
ModulePlatformType.SINGLE -> {
val excludedModules = includeOrNotModulesToCommand(multiPlatformModules, platform, false)
project.exec { commandLine(gradleCommand, "build", *excludedModules) }
}
ModulePlatformType.MULTI -> {
val includedModules = includeOrNotModulesToCommand(multiPlatformModules, platform, true)
project.exec { commandLine(gradleCommand, *includedModules) }
}
}
}
}
}

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