[Gradle, JS] Detect classpath changes in KotlinJsDce

Now -Xdev-mode-overwriting-strategy=older compiler argument is handled on Gradle side to detect incremental changes to increase performance
#KT-35640 Fixed
This commit is contained in:
Alexander Likhachev
2021-10-06 21:04:45 +03:00
committed by Space
parent 5db4ec15d2
commit 8c8b311d9f
@@ -16,8 +16,16 @@
package org.jetbrains.kotlin.gradle.tasks package org.jetbrains.kotlin.gradle.tasks
import org.gradle.api.file.FileCollection
import org.gradle.api.file.FileTree import org.gradle.api.file.FileTree
import org.gradle.api.tasks.* import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.TaskAction
import org.gradle.work.ChangeType
import org.gradle.work.Incremental
import org.gradle.work.InputChanges
import org.jetbrains.kotlin.cli.common.arguments.DevModeOverwritingStrategies
import org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments import org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments
import org.jetbrains.kotlin.cli.js.dce.K2JSDce import org.jetbrains.kotlin.cli.js.dce.K2JSDce
import org.jetbrains.kotlin.compilerRunner.runToolInSeparateProcess import org.jetbrains.kotlin.compilerRunner.runToolInSeparateProcess
@@ -65,21 +73,52 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
@Input @Input
var jvmArgs = mutableListOf<String>() var jvmArgs = mutableListOf<String>()
@Incremental
override fun getClasspath(): FileCollection {
return super.getClasspath()
}
private val buildDir by lazy { private val buildDir by lazy {
project.buildDir project.buildDir
} }
private val isDevMode
get() = dceOptions.devMode || "-dev-mode" in dceOptions.freeCompilerArgs
private val isExplicitDevModeAllStrategy
get() = strategyAllArg in dceOptions.freeCompilerArgs ||
strategyOlderArg !in dceOptions.freeCompilerArgs &&
System.getProperty("kotlin.js.dce.devmode.overwriting.strategy") == DevModeOverwritingStrategies.ALL
@TaskAction @TaskAction
fun performDce() { fun performDce(inputChanges: InputChanges) {
val inputFiles = (listOf(source) + classpath // in case of explicit `all` strategy do not perform incremental copy
val shouldPerformIncrementalCopy = isDevMode && !isExplicitDevModeAllStrategy
val classpathFiles = if (shouldPerformIncrementalCopy) {
inputChanges.getFileChanges(classpath)
.filter { it.changeType == ChangeType.MODIFIED || it.changeType == ChangeType.ADDED }
.map { it.file }
} else {
classpath
}
val inputFiles = (listOf(source) + classpathFiles
.filter { !kotlinFilesOnly || isDceCandidate(it) } .filter { !kotlinFilesOnly || isDceCandidate(it) }
.map { objects.fileCollection().from(it).asFileTree }) .map { objects.fileCollection().from(it).asFileTree })
.reduce(FileTree::plus) .reduce(FileTree::plus)
.files.map { it.path } .files.map { it.path }
val outputDirArgs = arrayOf("-output-dir", destinationDir.path) val outputDirArgs = arrayOf("-output-dir", destinationDirectory.get().asFile.path)
val argsArray = serializedCompilerArguments.toTypedArray() val processedSerializedArgs = if (shouldPerformIncrementalCopy) {
var shouldAddStrategyAllArgument = true
val processedArgs = serializedCompilerArguments
.map { if (it == strategyOlderArg) strategyAllArg.also { shouldAddStrategyAllArgument = false } else it }
if (shouldAddStrategyAllArgument) processedArgs + strategyAllArg else processedArgs
} else {
serializedCompilerArguments
}
val argsArray = processedSerializedArgs.toTypedArray()
val log = GradleKotlinLogger(logger) val log = GradleKotlinLogger(logger)
val allArgs = argsArray + outputDirArgs + inputFiles val allArgs = argsArray + outputDirArgs + inputFiles
@@ -93,7 +132,6 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
jvmArgs jvmArgs
) )
throwGradleExceptionIfError(exitCode) throwGradleExceptionIfError(exitCode)
} }
private fun isDceCandidate(file: File): Boolean { private fun isDceCandidate(file: File): Boolean {
@@ -107,4 +145,9 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
return File("${file.canonicalPathWithoutExtension()}.meta.js").exists() return File("${file.canonicalPathWithoutExtension()}.meta.js").exists()
} }
companion object {
const val strategyAllArg = "-Xdev-mode-overwriting-strategy=${DevModeOverwritingStrategies.ALL}"
const val strategyOlderArg = "-Xdev-mode-overwriting-strategy=${DevModeOverwritingStrategies.OLDER}"
}
} }