This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
KarmaConfigTask.kt
108 lines (96 loc) · 4.14 KB
/
KarmaConfigTask.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package org.jetbrains.kotlin.gradle.frontend.karma
import groovy.json.*
import org.gradle.api.*
import org.gradle.api.tasks.*
import org.jetbrains.kotlin.gradle.frontend.util.*
import org.jetbrains.kotlin.gradle.frontend.webpack.*
import java.io.*
open class KarmaConfigTask : DefaultTask() {
@get:Input
val sourceMaps: Boolean
get() = project.frontendExtension.sourceMaps
@get:Nested
val extension: KarmaExtension by lazy { project.extensions.getByType(KarmaExtension::class.java) }
@OutputFile
var karmaConfigFile: File = project.buildDir.resolve("karma.conf.js")
@TaskAction
fun main() {
val prepares = mutableListOf<String>()
val plugins = mutableSetOf("karma-phantomjs-launcher").apply { this += extension.plugins }
val preprocessors = extension.preprocessors.toMutableSet()
val clientConfig = mutableMapOf<String, Any>()
if (extension.customConfigFile.isNotBlank()) {
val file = project.projectDir.resolve(extension.customConfigFile)
file.copyTo(project.buildDir.resolve("karma.conf.js"), true)
} else {
val config = linkedMapOf(
"basePath" to project.buildDir.absolutePath,
"frameworks" to extension.frameworks.toMutableList(),
"reporters" to extension.reporters.toMutableList(),
"files" to listOf<String>(
kotlinTestOutput(project).absolutePath
),
"exclude" to listOf("*~", "*.swp", "*.swo"),
"port" to extension.port,
"runnerPort" to extension.runnerPort,
"colors" to false,
"autoWatch" to true,
"browsers" to listOf("PhantomJS"),
"captureTimeout" to 5000,
"singleRun" to false,
"preprocessors" to mapOf(
kotlinTestOutput(project).absolutePath to preprocessors
),
"plugins" to plugins,
"client" to clientConfig
)
if ("junit" in extension.reporters) {
config["junitReporter"] = mapOf(
"outputFile" to project.buildDir.resolve("reports/karma.xml").absolutePath,
"suite" to "karma"
)
plugins.add("karma-junit-reporter")
}
if ("qunit" in extension.frameworks) {
if ("karma-qunit" !in plugins) {
plugins += "karma-qunit"
}
clientConfig["clearContext"] = false
clientConfig["qunit"] = hashMapOf(
"showUI" to true,
"testTimeout" to 5000
)
}
if (sourceMaps) {
preprocessors += "sourcemap"
plugins += "karma-sourcemap-loader"
}
if (extension.enableWebPack) {
project.tasks.withType(GenerateWebPackConfigTask::class.java).single().let { webpackTask ->
webpackTask.webPackConfigFile.ifCanRead { file ->
prepares += "var webpackConfig = require(${JsonOutput.toJson(file.absolutePath)})"
prepares += "webpackConfig.resolve.modules.push(" + JsonOutput.toJson(kotlinTestOutput(project).absolutePath) + ")"
plugins += "karma-webpack"
preprocessors += "webpack"
config["webpack"] = "\$webpackConfig"
}
}
}
val configText = """
#PREPARES
module.exports = function (config) {
config.set(#CONFIG)
};
""".trimIndent()
.replace("#CONFIG", JsonBuilder(config).toPrettyString())
.replace("#PREPARES", prepares.joinToString(";\n", postfix = ";\n"))
.replace("\"\\\$([^\"]+)\"".toRegex()) { m -> m.groupValues[1] }
karmaConfigFile.writeText(configText)
}
}
private inline fun File.ifCanRead(block: (File) -> Unit) {
if (exists() && canRead()) {
block(this)
}
}
}