[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
import org.gradle.api.file.FileCollection
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.js.dce.K2JSDce
import org.jetbrains.kotlin.compilerRunner.runToolInSeparateProcess
@@ -65,21 +73,52 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
@Input
var jvmArgs = mutableListOf<String>()
@Incremental
override fun getClasspath(): FileCollection {
return super.getClasspath()
}
private val buildDir by lazy {
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
fun performDce() {
val inputFiles = (listOf(source) + classpath
fun performDce(inputChanges: InputChanges) {
// 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) }
.map { objects.fileCollection().from(it).asFileTree })
.reduce(FileTree::plus)
.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 allArgs = argsArray + outputDirArgs + inputFiles
@@ -93,7 +132,6 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
jvmArgs
)
throwGradleExceptionIfError(exitCode)
}
private fun isDceCandidate(file: File): Boolean {
@@ -107,4 +145,9 @@ abstract class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), Kotl
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}"
}
}