@@ -2,52 +2,75 @@ package org.jetbrains.kotlinx.jupyter.api.plugin
2
2
3
3
import org.gradle.api.Plugin
4
4
import org.gradle.api.Project
5
+ import org.gradle.kotlin.dsl.findByType
5
6
import org.gradle.kotlin.dsl.invoke
6
7
import org.gradle.kotlin.dsl.register
7
8
import org.gradle.kotlin.dsl.repositories
9
+ import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
8
10
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin
9
11
import org.jetbrains.kotlin.gradle.plugin.KaptExtension
12
+ import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
10
13
import org.jetbrains.kotlinx.jupyter.api.plugin.tasks.JupyterApiResourcesTask
11
14
12
15
class ApiGradlePlugin : Plugin <Project > {
13
- override fun apply (target : Project ) {
14
- target. pluginManager.run {
16
+ override fun apply (target : Project ): Unit = with (target) {
17
+ pluginManager.run {
15
18
apply (Kapt3GradleSubplugin ::class .java)
16
19
}
17
20
18
- val jupyterBuildPath = target. buildDir.resolve(FQNS_PATH )
19
- target. extensions.configure<KaptExtension >(" kapt" ) {
21
+ val jupyterBuildPath = buildDir.resolve(FQNS_PATH )
22
+ extensions.configure<KaptExtension >(" kapt" ) {
20
23
arguments {
21
24
arg(" kotlin.jupyter.fqn.path" , jupyterBuildPath)
22
25
}
23
26
}
24
27
25
- target. repositories {
28
+ repositories {
26
29
mavenCentral()
27
30
}
28
31
29
32
val pluginExtension = KotlinJupyterPluginExtension (target)
30
- target. extensions.add(" kotlinJupyter" , pluginExtension)
33
+ extensions.add(" kotlinJupyter" , pluginExtension)
31
34
pluginExtension.addDependenciesIfNeeded()
32
35
33
- target. tasks {
36
+ tasks {
34
37
val cleanJupyterTask = register(" cleanJupyterPluginFiles" ) {
35
38
doLast {
36
39
jupyterBuildPath.deleteRecursively()
37
40
}
38
41
}
39
42
40
43
val resourcesTaskName = " processJupyterApiResources"
41
- register<JupyterApiResourcesTask >(resourcesTaskName) {
42
- val kaptKotlinTask = findByName(" kaptKotlin" )
43
- if (kaptKotlinTask != null ) {
44
- dependsOn(kaptKotlinTask)
45
- kaptKotlinTask.dependsOn(cleanJupyterTask)
44
+ fun registerResourceTask () {
45
+ register<JupyterApiResourcesTask >(resourcesTaskName) {
46
+ val kaptKotlinTask = findByName(" kaptKotlin" )
47
+ if (kaptKotlinTask != null ) {
48
+ dependsOn(kaptKotlinTask)
49
+ kaptKotlinTask.dependsOn(cleanJupyterTask)
50
+ }
46
51
}
47
52
}
48
53
49
- named(" processResources" ) {
50
- dependsOn(resourcesTaskName)
54
+ // apply configuration to JVM-only project
55
+ plugins.withId(" org.jetbrains.kotlin.jvm" ) {
56
+ // Task should be registered after plugin is applied
57
+ registerResourceTask()
58
+ named(" processResources" ) {
59
+ dependsOn(resourcesTaskName)
60
+ }
61
+ }
62
+
63
+ // apply only to multiplatform plugin
64
+ plugins.withId(" org.jetbrains.kotlin.multiplatform" ) {
65
+ // Task should be registered after plugin is applied
66
+ registerResourceTask()
67
+ extensions.findByType<KotlinMultiplatformExtension >()?.apply {
68
+ val jvmTargetName = targets.filterIsInstance<KotlinJvmTarget >().firstOrNull()?.name
69
+ ? : error(" Single JVM target not found in a multiplatform project" )
70
+ named(jvmTargetName + " ProcessResources" ) {
71
+ dependsOn(resourcesTaskName)
72
+ }
73
+ }
51
74
}
52
75
}
53
76
}
0 commit comments