7
7
8
8
package net.pearx.multigradle.util.platform
9
9
10
- import com.moowork.gradle.node.NodeExtension
11
- import com.moowork.gradle.node.NodePlugin
12
- import com.moowork.gradle.node.npm.NpmTask
13
- import com.moowork.gradle.node.task.NodeTask
14
- import net.pearx.multigradle.util.alias
15
10
import net.pearx.multigradle.util.invoke
16
11
import net.pearx.multigradle.util.kotlinMpp
17
12
import org.gradle.api.Project
18
- import org.gradle.api.Task
19
- import org.gradle.api.tasks.Sync
20
- import org.gradle.api.tasks.TaskProvider
21
13
import org.gradle.kotlin.dsl.*
22
- import java.nio.file.Files
23
14
24
- class JsPlatformConfig (project : Project ) : PlatformConfig(project) {
25
- var nodeJsVersion: String by project.the<NodeExtension >().alias(NodeExtension ::getVersion, NodeExtension ::setVersion)
26
- var npmVersion: String by project.the<NodeExtension >().alias(NodeExtension ::getNpmVersion, NodeExtension ::setNpmVersion)
27
- lateinit var mochaVersion: String
28
- lateinit var mochaJunitReporterVersion: String
29
- var npmPackages = mutableMapOf<String , String >()
30
-
31
- inline fun npmPackages (init : MutableMap <String , String >.() -> Unit ) {
32
- init (npmPackages)
33
- }
34
- }
15
+ class JsPlatformConfig (project : Project ) : PlatformConfig(project)
35
16
36
17
val JsPlatform = platform(" js" , listOf (" jsTest" ), { JsPlatformConfig (it) }) { ext ->
37
- apply<NodePlugin >()
38
-
39
- configure<NodeExtension > {
40
- download = true
41
-
42
- val cacheDir = file(" $rootDir /.gradle/node" )
43
- workDir = file(" $cacheDir /nodejs" )
44
- npmWorkDir = file(" $cacheDir /npm" )
45
- yarnWorkDir = file(" $cacheDir /yarn" )
46
- // todo move to $buildDir/node_modules when https://github.com/srs/gradle-node-plugin/issues/300 will be resolved
47
- nodeModulesDir = file(" $projectDir /node_modules" )
48
- }
49
-
50
18
kotlinMpp {
51
19
js {
20
+ browser {
21
+ testTask {
22
+ useKarma {
23
+ useChromeHeadless()
24
+ usePhantomJS()
25
+ useFirefoxHeadless()
26
+ useOpera()
27
+ useSafari()
28
+ useIe()
29
+ }
30
+ }
31
+ }
32
+ nodejs {
33
+ testTask {
34
+ useMocha()
35
+ }
36
+ }
52
37
compilations.configureEach {
53
38
kotlinOptions {
54
39
moduleKind = " umd"
@@ -68,54 +53,4 @@ val JsPlatform = platform("js", listOf("jsTest"), { JsPlatformConfig(it) }) { ex
68
53
}
69
54
}
70
55
}
71
-
72
- tasks {
73
- val jsMainCompilation = kotlinMpp.js().compilations[" main" ]
74
- val jsTestCompilation = kotlinMpp.js().compilations[" test" ]
75
-
76
- val jsTestSyncNodeModules by registering(Sync ::class ) {
77
- dependsOn(jsTestCompilation.compileKotlinTask, jsMainCompilation.compileKotlinTask)
78
- from(jsTestCompilation.runtimeDependencyFiles.map { if (it.extension == " jar" ) zipTree(it) else it })
79
- include { it.path.endsWith(" .js" , true ) }
80
- into(" $buildDir /kotlinjs" )
81
- }
82
-
83
- val jsTestInstallPackages by registering(NpmTask ::class ) {
84
- doFirst {
85
- setArgs(mutableListOf (
86
- " install" ,
87
- " mocha@${ext().mochaVersion} " ,
88
- " mocha-jenkins-reporter@${ext().mochaJunitReporterVersion} " )
89
- .apply {
90
- for ((name, version) in ext().npmPackages)
91
- add(" $name @$version " )
92
- })
93
- }
94
- }
95
-
96
- val jsTestRunMocha by registering(NodeTask ::class ) {
97
- dependsOn(jsTestInstallPackages, jsTestSyncNodeModules, jsTestCompilation.compileAllTaskName)
98
- setScript(file(" ${project.the<NodeExtension >().nodeModulesDir} /mocha/bin/mocha" ))
99
- setEnvironment(mapOf (" NODE_PATH" to " $buildDir /kotlinjs" , " JUNIT_REPORT_PATH" to " $buildDir /test-results/jsTest/mocha.xml" ))
100
- setArgs(listOf (jsTestCompilation.output.classesDirs.first().toString(), " --reporter" , " mocha-jenkins-reporter" ))
101
- }
102
-
103
- named(" jsTest" ) {
104
- dependsOn(jsTestRunMocha)
105
- }
106
-
107
-
108
- listOf<TaskProvider <out Task >>(jsTestRunMocha, jsTestSyncNodeModules, jsTestInstallPackages, named(" npmSetup" ), named(" nodeSetup" )).forEach { task ->
109
- task.configure {
110
- onlyIf {
111
- try {
112
- Files .newDirectoryStream(jsTestCompilation.output.classesDirs.first().toPath()).use { f -> f.iterator().hasNext() }
113
- }
114
- catch (e: Exception ) {
115
- false
116
- }
117
- }
118
- }
119
- }
120
- }
121
56
}
0 commit comments