Skip to content

Commit

Permalink
[JS DCE] Add an ability to define overwriting strategy when copying d…
Browse files Browse the repository at this point in the history
…ependencies in dev-mode

* CLI option "-Xdev-mode-overwriting-strategy"
* System Property "kotlin.js.dce.devmode.overwriting.strategy"

Possible values: "older", "all".

#KT-36349 Fixed

(cherry picked from commit 6f61ea7)
  • Loading branch information
bashor committed Feb 21, 2020
1 parent 1f9b772 commit ffc3d7f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"
}
35 changes: 16 additions & 19 deletions compiler/cli/cli-js/src/org/jetbrains/kotlin/cli/js/dce/K2JSDce.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -63,7 +53,13 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
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
}
}
Expand Down Expand Up @@ -96,21 +92,22 @@ class K2JSDce : CLITool<K2JSDceArguments>() {
return ExitCode.OK
}

private fun copyFiles(files: List<InputFile>) {
private fun copyFiles(files: List<InputFile>, 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 ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Usage: kotlin-dce-js <options> <source files>
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.
Expand Down
10 changes: 5 additions & 5 deletions compiler/tests/org/jetbrains/kotlin/cli/CliTestGenerated.java

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ffc3d7f

Please sign in to comment.