From 6f61ea7f67707a96fe1c85f5ae752d1dd8d6dd52 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Wed, 19 Feb 2020 16:51:09 +0300 Subject: [PATCH] [JS DCE] Add an ability to define overwriting strategy when copying dependencies in dev-mode * CLI option "-Xdev-mode-overwriting-strategy" * System Property "kotlin.js.dce.devmode.overwriting.strategy" Possible values: "older", "all". #KT-36349 Fixed --- .../cli/common/arguments/K2JSDceArguments.kt | 30 +++++++++------- .../jetbrains/kotlin/cli/js/dce/K2JSDce.kt | 35 +++++++++---------- .../{jsExtraHelp.args => dceExtraHelp.args} | 0 .../{jsExtraHelp.out => dceExtraHelp.out} | 2 ++ .../kotlin/cli/CliTestGenerated.java | 10 +++--- 5 files changed, 40 insertions(+), 37 deletions(-) rename compiler/testData/cli/js-dce/{jsExtraHelp.args => dceExtraHelp.args} (100%) rename compiler/testData/cli/js-dce/{jsExtraHelp.out => dceExtraHelp.out} (62%) diff --git a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt index 7e747490e9a..029e1d3e6af 100644 --- a/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt +++ b/compiler/cli/cli-common/src/org/jetbrains/kotlin/cli/common/arguments/K2JSDceArguments.kt @@ -1,21 +1,13 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * 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. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.cli.common.arguments +import org.jetbrains.kotlin.cli.common.arguments.DevModeOverwritingStrategies.ALL +import org.jetbrains.kotlin.cli.common.arguments.DevModeOverwritingStrategies.OLDER + class K2JSDceArguments : CommonToolArguments() { companion object { @JvmStatic private val serialVersionUID = 0L @@ -48,4 +40,16 @@ class K2JSDceArguments : CommonToolArguments() { ) @GradleOption(DefaultValues.BooleanFalseDefault::class) var devMode: Boolean by FreezableVar(false) + + @Argument( + value = "-Xdev-mode-overwriting-strategy", + valueDescription = "{$OLDER|$ALL}", + description = "Overwriting strategy during copy dependencies in development mode" + ) + var devModeOverwritingStrategy: String? by NullableStringFreezableVar(null) +} + +object DevModeOverwritingStrategies { + const val OLDER = "older" + const val ALL = "all" } diff --git a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt index 0a44a8e85e5..109945611c3 100644 --- a/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt +++ b/compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt @@ -1,23 +1,13 @@ /* - * Copyright 2010-2017 JetBrains s.r.o. - * - * 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. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package org.jetbrains.kotlin.cli.js.dce import org.jetbrains.kotlin.cli.common.CLITool import org.jetbrains.kotlin.cli.common.ExitCode +import org.jetbrains.kotlin.cli.common.arguments.DevModeOverwritingStrategies import org.jetbrains.kotlin.cli.common.arguments.K2JSDceArguments import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.MessageCollector @@ -63,7 +53,13 @@ class K2JSDce : CLITool() { return if (!arguments.devMode) { performDce(files, arguments, messageCollector) } else { - copyFiles(files) + val devModeOverwritingStrategy = + arguments.devModeOverwritingStrategy ?: + System.getProperty("kotlin.js.dce.devmode.overwriting.strategy", DevModeOverwritingStrategies.OLDER) + + val overwriteOnlyOlderFiles = devModeOverwritingStrategy == DevModeOverwritingStrategies.OLDER + + copyFiles(files, overwriteOnlyOlderFiles) ExitCode.OK } } @@ -96,21 +92,22 @@ class K2JSDce : CLITool() { return ExitCode.OK } - private fun copyFiles(files: List) { + private fun copyFiles(files: List, overwriteOnlyOlderFiles: Boolean) { for (file in files) { - copyResource(file.resource, File(file.outputPath)) + copyResource(file.resource, File(file.outputPath), overwriteOnlyOlderFiles) file.sourceMapResource?.let { sourceMap -> val sourceMapTarget = File(file.outputPath + ".map") val inputFile = File(sourceMap.name) if (!inputFile.exists() || !mapSourcePaths(inputFile, sourceMapTarget)) { - copyResource(sourceMap, sourceMapTarget) + copyResource(sourceMap, sourceMapTarget, overwriteOnlyOlderFiles) } } } } - private fun copyResource(resource: InputResource, targetFile: File) { - if (targetFile.exists() && resource.lastModified() < targetFile.lastModified()) return + private fun copyResource(resource: InputResource, targetFile: File, overwriteOnlyOlderFiles: Boolean) { + // TODO shouldn't it be "<="? + if (overwriteOnlyOlderFiles && targetFile.exists() && resource.lastModified() < targetFile.lastModified()) return targetFile.parentFile.mkdirs() resource.reader().use { input -> diff --git a/compiler/testData/cli/js-dce/jsExtraHelp.args b/compiler/testData/cli/js-dce/dceExtraHelp.args similarity index 100% rename from compiler/testData/cli/js-dce/jsExtraHelp.args rename to compiler/testData/cli/js-dce/dceExtraHelp.args diff --git a/compiler/testData/cli/js-dce/jsExtraHelp.out b/compiler/testData/cli/js-dce/dceExtraHelp.out similarity index 62% rename from compiler/testData/cli/js-dce/jsExtraHelp.out rename to compiler/testData/cli/js-dce/dceExtraHelp.out index 09a48bbfe9c..dba48d5b8ad 100644 --- a/compiler/testData/cli/js-dce/jsExtraHelp.out +++ b/compiler/testData/cli/js-dce/dceExtraHelp.out @@ -1,5 +1,7 @@ Usage: kotlin-dce-js where advanced options include: + -Xdev-mode-overwriting-strategy={older|all} + Overwriting strategy during copy dependencies in development mode -Xprint-reachability-info Print declarations marked as reachable Advanced options are non-standard and may be changed or removed without any notice. diff --git a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java index 8130ab2bda6..22083d228d8 100644 --- a/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java @@ -976,6 +976,11 @@ public class CliTestGenerated extends AbstractCliTest { KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/cli/js-dce"), Pattern.compile("^(.+)\\.args$"), null, false); } + @TestMetadata("dceExtraHelp.args") + public void testDceExtraHelp() throws Exception { + runTest("compiler/testData/cli/js-dce/dceExtraHelp.args"); + } + @TestMetadata("dceHelp.args") public void testDceHelp() throws Exception { runTest("compiler/testData/cli/js-dce/dceHelp.args"); @@ -996,11 +1001,6 @@ public class CliTestGenerated extends AbstractCliTest { runTest("compiler/testData/cli/js-dce/invalidFilename.args"); } - @TestMetadata("jsExtraHelp.args") - public void testJsExtraHelp() throws Exception { - runTest("compiler/testData/cli/js-dce/jsExtraHelp.args"); - } - @TestMetadata("nonExistingSourcePath.args") public void testNonExistingSourcePath() throws Exception { runTest("compiler/testData/cli/js-dce/nonExistingSourcePath.args");