From ae3d574473698e3009c7615e483317faae126aad Mon Sep 17 00:00:00 2001 From: Yan Zhulanow Date: Tue, 27 Mar 2018 20:16:56 +0300 Subject: [PATCH] Pill: Support Android tests --- buildSrc/src/main/kotlin/pill/plugin.kt | 30 +++++++++++++++++++------ buildSrc/src/main/kotlin/tasks.kt | 27 ++++++++++++++++------ 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/buildSrc/src/main/kotlin/pill/plugin.kt b/buildSrc/src/main/kotlin/pill/plugin.kt index 8b3d3f7ad11..8badaff5cef 100644 --- a/buildSrc/src/main/kotlin/pill/plugin.kt +++ b/buildSrc/src/main/kotlin/pill/plugin.kt @@ -77,6 +77,8 @@ class JpsCompatiblePlugin : Plugin { if (project == project.rootProject) { project.tasks.create("pill") { doLast { pill(project) } + TaskUtils.useAndroidSdk(this) + TaskUtils.useAndroidJar(this) } project.tasks.create("unpill") { @@ -195,17 +197,31 @@ class JpsCompatiblePlugin : Plugin { junitConfiguration.apply { getOrCreateChild("option", "name" to "WORKING_DIRECTORY").setAttribute("value", "file://\$PROJECT_DIR\$") getOrCreateChild("option", "name" to "VM_PARAMETERS").also { vmParams -> - val ideaHomePathOptionKey = "-Didea.home.path=" - val ideaHomePathOption = ideaHomePathOptionKey + platformDirProjectRelative - - val existingOptions = vmParams.getAttributeValue("value", "") + var options = vmParams.getAttributeValue("value", "") .split(' ') .map { it.trim() } - .filter { it.isNotEmpty() && !it.startsWith(ideaHomePathOptionKey) } + .filter { it.isNotEmpty() } - val newOptions = existingOptions.joinToString(" ") + " " + ideaHomePathOption + fun addOrReplaceOptionValue(name: String, value: Any) { + options = options.filter { !it.startsWith("-D$name=") } + listOf("-D$name=$value") + } - vmParams.setAttribute("value", newOptions) + val robolectricClasspath = project.rootProject + .project(":plugins:android-extensions-compiler") + .configurations.getByName("robolectricClasspath") + .files.joinToString(File.pathSeparator) + + val androidJarPath = project.configurations.getByName("androidJar").singleFile + val androidSdkPath = project.configurations.getByName("androidSdk").singleFile + + addOrReplaceOptionValue("idea.home.path", platformDirProjectRelative) + addOrReplaceOptionValue("ideaSdk.androidPlugin.path", platformDirProjectRelative + "/plugins/android/lib") + addOrReplaceOptionValue("robolectric.classpath", robolectricClasspath) + + addOrReplaceOptionValue("android.sdk", "\$PROJECT_DIR\$/" + androidSdkPath.toRelativeString(projectDir)) + addOrReplaceOptionValue("android.jar", "\$PROJECT_DIR\$/" + androidJarPath.toRelativeString(projectDir)) + + vmParams.setAttribute("value", options.joinToString(" ")) } } diff --git a/buildSrc/src/main/kotlin/tasks.kt b/buildSrc/src/main/kotlin/tasks.kt index ebbe8187b04..0a66ff81df3 100644 --- a/buildSrc/src/main/kotlin/tasks.kt +++ b/buildSrc/src/main/kotlin/tasks.kt @@ -84,7 +84,17 @@ private inline fun String.isFirstChar(f: (Char) -> Boolean) = isNotEmpty() && f( inline fun Project.getOrCreateTask(taskName: String, body: T.() -> Unit): T = (tasks.findByName(taskName)?.let { it as T } ?: task(taskName)).apply { body() } -private fun Test.useAndroidConfiguration(systemPropertyName: String, configName: String) { +object TaskUtils { + fun useAndroidSdk(task: Task) { + task.useAndroidConfiguration(systemPropertyName = "android.sdk", configName = "androidSdk") + } + + fun useAndroidJar(task: Task) { + task.useAndroidConfiguration(systemPropertyName = "android.jar", configName = "androidJar") + } +} + +private fun Task.useAndroidConfiguration(systemPropertyName: String, configName: String) { val configuration = with(project) { configurations.getOrCreate(configName) .also { @@ -96,15 +106,18 @@ private fun Test.useAndroidConfiguration(systemPropertyName: String, configName: } dependsOn(configuration) - doFirst { - systemProperty(systemPropertyName, configuration.singleFile.canonicalPath) + + if (this is Test) { + doFirst { + systemProperty(systemPropertyName, configuration.singleFile.canonicalPath) + } } } -fun Test.useAndroidSdk() { - useAndroidConfiguration(systemPropertyName = "android.sdk", configName = "androidSdk") +fun Task.useAndroidSdk() { + TaskUtils.useAndroidSdk(this) } -fun Test.useAndroidJar() { - useAndroidConfiguration(systemPropertyName = "android.jar", configName = "androidJar") +fun Task.useAndroidJar() { + TaskUtils.useAndroidJar(this) } \ No newline at end of file