-
Notifications
You must be signed in to change notification settings - Fork 160
/
ZiplinePlugin.kt
102 lines (88 loc) · 4.08 KB
/
ZiplinePlugin.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
/*
* Copyright (C) 2021 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.zipline.gradle
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Delete
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerPluginSupportPlugin
import org.jetbrains.kotlin.gradle.plugin.SubpluginArtifact
import org.jetbrains.kotlin.gradle.plugin.SubpluginOption
import org.jetbrains.kotlin.gradle.targets.js.ir.JsIrBinary
import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget
import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack
class ZiplinePlugin : KotlinCompilerPluginSupportPlugin {
override fun isApplicable(kotlinCompilation: KotlinCompilation<*>): Boolean = true
override fun getCompilerPluginId(): String = BuildConfig.KOTLIN_PLUGIN_ID
override fun getPluginArtifact(): SubpluginArtifact = SubpluginArtifact(
groupId = BuildConfig.KOTLIN_PLUGIN_GROUP,
artifactId = BuildConfig.KOTLIN_PLUGIN_NAME,
version = BuildConfig.KOTLIN_PLUGIN_VERSION,
)
override fun getPluginArtifactForNative(): SubpluginArtifact = SubpluginArtifact(
groupId = BuildConfig.KOTLIN_PLUGIN_GROUP,
artifactId = BuildConfig.KOTLIN_PLUGIN_HOSTED_NAME,
version = BuildConfig.KOTLIN_PLUGIN_VERSION,
)
override fun apply(target: Project) {
super.apply(target)
val extension = target.extensions.findByType(KotlinMultiplatformExtension::class.java)
?: return
extension.targets.withType(KotlinJsIrTarget::class.java).all { kotlinTarget ->
kotlinTarget.binaries.withType(JsIrBinary::class.java).all { kotlinBinary ->
registerCompileZiplineTask(target, kotlinBinary)
}
}
target.tasks.named("clean", Delete::class.java).configure { clean ->
clean.delete.add(target.projectDir.resolve(ZiplineCompileTask.configFilePath))
}
}
private fun registerCompileZiplineTask(project: Project, kotlinBinary: JsIrBinary) {
// Like 'compileDevelopmentExecutableKotlinJsZipline'.
val linkTaskName = kotlinBinary.linkTaskName
val compileZiplineTaskName = "${linkTaskName}Zipline"
// For every JS executable, create a task that compiles its .js to .zipline.
// input: build/compileSync/main/productionExecutable/kotlin
// output: build/compileSync/main/productionExecutable/kotlinZipline
project.tasks.register(compileZiplineTaskName, ZiplineCompileTask::class.java) { createdTask ->
createdTask.description = "Compile .js to .zipline"
createdTask.dependsOn(kotlinBinary.linkTaskName)
val linkTask = kotlinBinary.linkTask.get()
val linkOutputDir = project.file(linkTask.kotlinOptions.outputFile!!).parentFile
createdTask.inputDir.set(linkOutputDir)
createdTask.outputDir.set(linkOutputDir.parentFile.resolve("${linkOutputDir.name}Zipline"))
}
project.tasks.withType(KotlinWebpack::class.java).configureEach { kotlinWebpack ->
kotlinWebpack.dependsOn(compileZiplineTaskName)
}
}
override fun applyToCompilation(
kotlinCompilation: KotlinCompilation<*>
): Provider<List<SubpluginOption>> {
val project = kotlinCompilation.target.project
// Configure Kotlin JS to generate modular JS files
project.tasks.withType(KotlinJsCompile::class.java).configureEach { task ->
task.kotlinOptions {
freeCompilerArgs += listOf("-Xir-per-module")
}
}
return project.provider {
listOf() // No options.
}
}
}