JS: change the way how DCE resolves input paths

This commit is contained in:
Alexey Andreev
2017-11-17 18:05:08 +03:00
parent 8514a7706f
commit ffdebfab45
13 changed files with 156 additions and 99 deletions
@@ -18,11 +18,8 @@ package org.jetbrains.kotlin.gradle.plugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.internal.file.UnionFileCollection
import org.gradle.api.internal.file.UnionFileTree
import org.gradle.api.plugins.JavaPluginConvention
import org.gradle.api.tasks.Copy
import org.gradle.api.tasks.SourceSet
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinJsDce
@@ -33,7 +30,7 @@ class KotlinJsDcePlugin : Plugin<Project> {
project.pluginManager.apply(Kotlin2JsPluginWrapper::class.java)
val javaPluginConvention = project.convention.getPlugin(JavaPluginConvention::class.java)
javaPluginConvention.sourceSets?.forEach { processSourceSet(project, it) }
javaPluginConvention.sourceSets.forEach { processSourceSet(project, it) }
}
private fun processSourceSet(project: Project, sourceSet: SourceSet) {
@@ -42,57 +39,26 @@ class KotlinJsDcePlugin : Plugin<Project> {
val dceTaskName = sourceSet.getTaskName(DCE_TASK_PREFIX, TASK_SUFFIX)
val dceTask = project.tasks.create(dceTaskName, KotlinJsDce::class.java).also {
it.dependsOn(kotlinTask)
project.tasks.findByName("build")?.dependsOn(it)
project.tasks.findByName("build").dependsOn(it)
}
project.afterEvaluate {
val outputDir = File(kotlinTask.outputFile).parentFile
val dependenciesDir = File(outputDir, "dependencies")
val dependenciesTemporaryDir = File(outputDir, "dependencies-tmp")
copyDependencies(project, sourceSet, dependenciesDir, dependenciesTemporaryDir, dceTask)
val dceInputTrees = listOf(project.fileTree(kotlinTask.outputFile), project.fileTree(dependenciesDir))
val configuration = project.configurations.findByName(sourceSet.compileConfigurationName)
val dceInputTrees = listOf(project.fileTree(kotlinTask.outputFile)) + configuration.map { project.fileTree(it) }
val dceInputFiles = UnionFileTree("dce-input", dceInputTrees)
with (dceTask) {
classpath = sourceSet.compileClasspath
destinationDir = File(outputDir, "min")
source(dceInputFiles.filter { it.path.endsWith(".js") })
source(dceInputFiles)
}
}
}
private fun copyDependencies(project: Project, sourceSet: SourceSet, outputDir: File, tmpDir: File, dceTask: Task) {
val configuration = project.configurations.findByName(sourceSet.compileConfigurationName) ?: return
val zippedFiles = UnionFileCollection(configuration.map { project.zipTree(it) })
val files = project.fileTree(tmpDir)
.filter { file -> SUFFIXES.any { file.path.endsWith(it) } }
.filter { file -> SUFFIXES.any { File(file.path.removeSuffix(it) + ".meta.js").exists() } }
// This intermediate task is needed due to bug in Gradle that causes infinite loops in continuous build mode
val unpackName = sourceSet.getTaskName(UNPACK_DEPENDENCIES_TASK_PREFIX, TASK_SUFFIX)
val unpackTask = project.tasks.create(unpackName, Copy::class.java).apply {
from(zippedFiles)
into(tmpDir)
}
val name = sourceSet.getTaskName(DEPENDENCIES_TASK_PREFIX, TASK_SUFFIX)
with(project.tasks.create(name, Copy::class.java)) {
from(files)
into(outputDir)
includeEmptyDirs = true
dceTask.dependsOn(this)
dependsOn(unpackTask)
}
}
companion object {
private const val TASK_SUFFIX = "kotlinJs"
private const val UNPACK_DEPENDENCIES_TASK_PREFIX = "unpackDependencies"
private const val DEPENDENCIES_TASK_PREFIX = "copyDependencies"
private const val DCE_TASK_PREFIX = "runDce"
private val SUFFIXES = listOf(".js", ".js.map")
}
}