diff --git a/build.gradle.kts b/build.gradle.kts index 2b6f31483..08c6c6769 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 -) { - 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, platform: String, include: Boolean): Array { + return modules.flatMap { + when (include) { + true -> listOf(":$it:${platform}Test") + false -> listOf("-x", ":$it:build") + } + }.toTypedArray() +} + fun buildExcludeOptions(modules: List): Array { - 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)