From 9970c34961b53ac2b0e59628f903bb255a9de357 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Tue, 4 Aug 2015 20:00:50 +0300 Subject: [PATCH] Preprocessor: improved logging. --- .../Preprocessor.kt | 30 +++++++----- .../org.jetbrains.kotlin.preprocessor/cli.kt | 6 +-- .../logging.kt | 49 +++++++++++++++++++ 3 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/logging.kt diff --git a/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/Preprocessor.kt b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/Preprocessor.kt index 92f69b924eb..11521d7b460 100644 --- a/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/Preprocessor.kt +++ b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/Preprocessor.kt @@ -30,13 +30,21 @@ import java.io.IOException -data class Profile(val name: String, val evaluator: Evaluator, val targetRoot: File) +public data class Profile(val name: String, val evaluator: Evaluator, val targetRoot: File) -fun createJvmProfile(targetRoot: File, version: Int) = Profile("JVM$version", JvmPlatformEvaluator(version), File(targetRoot, "jvm$version")) -fun createJsProfile(targetRoot: File) = Profile("JS", JsPlatformEvaluator(), File(targetRoot, "js")) +public fun createJvmProfile(targetRoot: File, version: Int): Profile = Profile("JVM$version", JvmPlatformEvaluator(version), File(targetRoot, "jvm$version")) +public fun createJsProfile(targetRoot: File): Profile = Profile("JS", JsPlatformEvaluator(), File(targetRoot, "js")) + +public val profileEvaluators: Map Evaluator> = + listOf(6, 7, 8).toMap({ version -> "JVM$version" }, { version -> { JvmPlatformEvaluator(version) } }) + ("JS" to { JsPlatformEvaluator() }) + +public fun createProfile(name: String, targetRoot: File): Profile { + val (profileName, evaluator) = profileEvaluators.entrySet().firstOrNull { it.key.equals(name, ignoreCase = true) } ?: throw IllegalArgumentException("Profile with name '$name' is not supported") + return Profile(profileName, evaluator(), targetRoot) +} -public class Preprocessor() { +public class Preprocessor(val logger: Logger = SystemOutLogger) { val fileType = JetFileType.INSTANCE val jetPsiFactory: JetPsiFactory @@ -55,7 +63,11 @@ public class Preprocessor() { class Modify(val sourceText: String, val modifications: List) : FileProcessingResult() { fun getModifiedText(): String = modifications.applyTo(sourceText) + + override fun toString(): String = "Modify(${modifications.size()})" } + + override fun toString() = this.javaClass.simpleName } public fun processSources(sourceRoot: File, profile: Profile) { @@ -68,8 +80,6 @@ public class Preprocessor() { val sourceText = sourceFile.readText().convertLineSeparators() val psiFile = jetPsiFactory.createFile(sourceFile.name, sourceText) - //println("$psiFile") - val fileAnnotations = psiFile.parseConditionalAnnotations() if (!evaluator(fileAnnotations)) @@ -93,8 +103,8 @@ public class Preprocessor() { for (sourceFile in sourceFiles) { val result = processFileSingleEvaluator(sourceFile, evaluator) + logger.debug("$result: $sourceFile") if (result is FileProcessingResult.Skip) { - println("$sourceFile is excluded") continue } @@ -111,7 +121,7 @@ public class Preprocessor() { val resultText = result.getModifiedText() if (targetFile.exists() && targetFile.isTextEqualTo(resultText)) continue - println("Rewriting modified $targetFile") + logger.info("Rewriting modified $targetFile") targetFile.writeText(resultText) } } @@ -128,7 +138,7 @@ public class Preprocessor() { for (targetFile in targetRoot.listFiles()) { if (!processedFiles.remove(processedFiles.find { FileUtil.filesEqual(it, targetFile) })) { - println("Removing skipped $targetFile") + logger.info("Deleting skipped $targetFile") targetFile.deleteRecursively() } } @@ -173,8 +183,6 @@ public class Preprocessor() { val processedFiles = profiles.toMap({ it }, { hashSetOf()}) } - - } fun String.convertLineSeparators(): String = StringUtil.convertLineSeparators(this) diff --git a/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/cli.kt b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/cli.kt index 238b25e4ad3..98ebf685056 100644 --- a/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/cli.kt +++ b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/cli.kt @@ -28,12 +28,8 @@ fun main(args: Array) { val sourcePath = File(args[0]) val targetPath = File(args[1]) - val profileName = args[2] - val profiles = listOf(6, 7, 8).map { version -> "JVM$version" to { JvmPlatformEvaluator(version) } } + ("JS" to { JsPlatformEvaluator() }) - - val (name, evaluatorBuilder) = profiles.single { it.first.equals(profileName, ignoreCase = true) } - val profile = Profile(name, evaluatorBuilder(), targetPath) + val profile = createProfile(args[2], targetPath) println("Preprocessing sources in $sourcePath to $targetPath with profile ${profile.name}") Preprocessor().processSources(sourcePath, profile) diff --git a/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/logging.kt b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/logging.kt new file mode 100644 index 00000000000..b50c744f6be --- /dev/null +++ b/compiler/conditional-preprocessor/src/org.jetbrains.kotlin.preprocessor/logging.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2015 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. + */ + +package org.jetbrains.kotlin.preprocessor + +public interface Logger { + fun debug(msg: CharSequence) + fun info(msg: CharSequence) + fun warn(msg: CharSequence) + fun error(msg: CharSequence) +} + +public object SystemOutLogger : Logger { + private fun out(level: String, msg: CharSequence) = println("[$level] $msg") + + public var isDebugEnabled: Boolean = false + override fun debug(msg: CharSequence) = if (isDebugEnabled) out("DEBUG", msg) + override fun info(msg: CharSequence) = out("INFO", msg) + override fun warn(msg: CharSequence) = out("WARN", msg) + override fun error(msg: CharSequence) = out("ERROR", msg) +} + +public fun Logger.withPrefix(prefix: String): Logger = PrefixedLogger(prefix, this) + +public class PrefixedLogger(val prefix: String, val logger: Logger) : Logger { + private fun prefix(msg: CharSequence): CharSequence = StringBuilder { + append(prefix) + append(": ") + append(msg) + } + + override fun debug(msg: CharSequence) = logger.debug(prefix(msg)) + override fun info(msg: CharSequence) = logger.info(prefix(msg)) + override fun warn(msg: CharSequence) = logger.warn(prefix(msg)) + override fun error(msg: CharSequence) = logger.error(prefix(msg)) +} \ No newline at end of file