Replace message collector with reporting lambda

to simplify usages and slightly reduce dependencies
This commit is contained in:
Ilya Chernikov
2019-04-24 12:41:10 +02:00
parent 61c1312f1a
commit a4c049d26e
6 changed files with 56 additions and 46 deletions
@@ -15,6 +15,7 @@ import org.jetbrains.kotlin.idea.core.script.ScriptDefinitionContributor
import org.jetbrains.kotlin.idea.core.script.loadDefinitionsFromTemplates import org.jetbrains.kotlin.idea.core.script.loadDefinitionsFromTemplates
import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource
import org.jetbrains.kotlin.scripting.definitions.reporter
import kotlin.script.experimental.intellij.ScriptDefinitionsProvider import kotlin.script.experimental.intellij.ScriptDefinitionsProvider
class BridgeScriptDefinitionsContributor(private val project: Project) : ScriptDefinitionContributor { class BridgeScriptDefinitionsContributor(private val project: Project) : ScriptDefinitionContributor {
@@ -34,7 +35,7 @@ class BridgeScriptDefinitionsContributor(private val project: Project) : ScriptD
else ScriptDefinitionsFromClasspathDiscoverySource( else ScriptDefinitionsFromClasspathDiscoverySource(
classPath, classPath,
emptyMap(), emptyMap(),
messageCollector messageCollector.reporter
).definitions ).definitions
explicitDefinitions + discoveredDefinitions explicitDefinitions + discoveredDefinitions
} }
@@ -25,6 +25,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.useLazyTaskConfiguration import org.jetbrains.kotlin.gradle.tasks.useLazyTaskConfiguration
import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast import org.jetbrains.kotlin.gradle.utils.isGradleVersionAtLeast
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource
import org.jetbrains.kotlin.scripting.definitions.reporter
import java.io.File import java.io.File
private const val MIN_SUPPORTED_GRADLE_MAJOR_VERSION = 5 private const val MIN_SUPPORTED_GRADLE_MAJOR_VERSION = 5
@@ -166,7 +167,7 @@ internal class DiscoverScriptExtensionsTransform : ArtifactTransform() {
val definitions = val definitions =
ScriptDefinitionsFromClasspathDiscoverySource( ScriptDefinitionsFromClasspathDiscoverySource(
listOf(input), emptyMap(), listOf(input), emptyMap(),
PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false) PrintingMessageCollector(System.out, MessageRenderer.WITHOUT_PATHS, false).reporter
).definitions ).definitions
val extensions = definitions.mapTo(arrayListOf()) { it.fileExtension } val extensions = definitions.mapTo(arrayListOf()) { it.fileExtension }
return if (extensions.isNotEmpty()) { return if (extensions.isNotEmpty()) {
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots import org.jetbrains.kotlin.cli.jvm.config.jvmClasspathRoots
import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.scripting.definitions.loadScriptTemplatesFromClasspath import org.jetbrains.kotlin.scripting.definitions.loadScriptTemplatesFromClasspath
import org.jetbrains.kotlin.scripting.definitions.reporter
const val KOTLIN_SCRIPTING_PLUGIN_ID = "kotlin.scripting" const val KOTLIN_SCRIPTING_PLUGIN_ID = "kotlin.scripting"
@@ -21,7 +22,7 @@ fun configureScriptDefinitions(
) { ) {
// TODO: consider using escaping to allow kotlin escaped names in class names // TODO: consider using escaping to allow kotlin escaped names in class names
val templatesFromClasspath = loadScriptTemplatesFromClasspath( val templatesFromClasspath = loadScriptTemplatesFromClasspath(
scriptTemplates, configuration.jvmClasspathRoots, emptyList(), baseClassloader, scriptResolverEnv, messageCollector scriptTemplates, configuration.jvmClasspathRoots, emptyList(), baseClassloader, scriptResolverEnv, messageCollector.reporter
) )
configuration.addAll(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, templatesFromClasspath.toList()) configuration.addAll(ScriptingConfigurationKeys.SCRIPT_DEFINITIONS, templatesFromClasspath.toList())
} }
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.scripting.definitions package org.jetbrains.kotlin.scripting.definitions
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import java.io.File import java.io.File
import kotlin.script.experimental.annotations.KotlinScript import kotlin.script.experimental.annotations.KotlinScript
import kotlin.script.experimental.api.KotlinType import kotlin.script.experimental.api.KotlinType
@@ -22,15 +21,15 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
private val annotationsFromAsm: ArrayList<BinAnnData>, private val annotationsFromAsm: ArrayList<BinAnnData>,
private val className: String, private val className: String,
private val classpath: List<File>, private val classpath: List<File>,
private val messageCollector: MessageCollector private val messageReporter: MessageReporter
) : KotlinScriptDefinitionAdapterFromNewAPIBase() { ) : KotlinScriptDefinitionAdapterFromNewAPIBase() {
constructor( constructor(
classBytes: ByteArray, classBytes: ByteArray,
className: String, className: String,
classpath: List<File>, classpath: List<File>,
messageCollector: MessageCollector messageReporter: MessageReporter
) : this(loadAnnotationsFromClass(classBytes), className, classpath, messageCollector) ) : this(loadAnnotationsFromClass(classBytes), className, classpath, messageReporter)
override val hostConfiguration: ScriptingHostConfiguration by lazy(LazyThreadSafetyMode.PUBLICATION) { override val hostConfiguration: ScriptingHostConfiguration by lazy(LazyThreadSafetyMode.PUBLICATION) {
ScriptingHostConfiguration(defaultJvmScriptingHostConfiguration) { ScriptingHostConfiguration(defaultJvmScriptingHostConfiguration) {
@@ -39,7 +38,7 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
} }
override val scriptCompilationConfiguration: ScriptCompilationConfiguration by lazy(LazyThreadSafetyMode.PUBLICATION) { override val scriptCompilationConfiguration: ScriptCompilationConfiguration by lazy(LazyThreadSafetyMode.PUBLICATION) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.LOGGING, CompilerMessageSeverity.LOGGING,
"Configure scripting: loading script definition class $className using classpath $classpath\n. ${Thread.currentThread().stackTrace}" "Configure scripting: loading script definition class $className using classpath $classpath\n. ${Thread.currentThread().stackTrace}"
) )
@@ -50,10 +49,10 @@ class LazyScriptDefinitionFromDiscoveredClass internal constructor(
LazyScriptDefinitionFromDiscoveredClass::class LazyScriptDefinitionFromDiscoveredClass::class
) )
} catch (ex: ClassNotFoundException) { } catch (ex: ClassNotFoundException) {
messageCollector.report(CompilerMessageSeverity.ERROR, "Cannot find script definition class $className") messageReporter(CompilerMessageSeverity.ERROR, "Cannot find script definition class $className")
InvalidScriptDefinition InvalidScriptDefinition
} catch (ex: Exception) { } catch (ex: Exception) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.ERROR, CompilerMessageSeverity.ERROR,
"Error processing script definition class $className: ${ex.message}\nclasspath:\n${classpath.joinToString("\n", " ")}" "Error processing script definition class $className: ${ex.message}\nclasspath:\n${classpath.joinToString("\n", " ")}"
) )
@@ -23,10 +23,17 @@ import kotlin.script.templates.ScriptTemplateDefinition
const val SCRIPT_DEFINITION_MARKERS_PATH = "META-INF/kotlin/script/templates/" const val SCRIPT_DEFINITION_MARKERS_PATH = "META-INF/kotlin/script/templates/"
const val SCRIPT_DEFINITION_MARKERS_EXTENSION_WITH_DOT = ".classname" const val SCRIPT_DEFINITION_MARKERS_EXTENSION_WITH_DOT = ".classname"
typealias MessageReporter = (CompilerMessageSeverity, String) -> Unit
val MessageCollector.reporter: MessageReporter
get() = { severity, message ->
this.report(severity, message)
}
class ScriptDefinitionsFromClasspathDiscoverySource( class ScriptDefinitionsFromClasspathDiscoverySource(
private val classpath: List<File>, private val classpath: List<File>,
private val scriptResolverEnv: Map<String, Any?>, private val scriptResolverEnv: Map<String, Any?>,
private val messageCollector: MessageCollector private val messageReporter: MessageReporter
) : ScriptDefinitionsSource { ) : ScriptDefinitionsSource {
override val definitions: Sequence<KotlinScriptDefinition> = run { override val definitions: Sequence<KotlinScriptDefinition> = run {
@@ -34,7 +41,7 @@ class ScriptDefinitionsFromClasspathDiscoverySource(
classpath, classpath,
this::class.java.classLoader, this::class.java.classLoader,
scriptResolverEnv, scriptResolverEnv,
messageCollector messageReporter
) )
} }
} }
@@ -44,7 +51,7 @@ private const val MANIFEST_RESOURCE_NAME = "/META-INF/MANIFEST.MF"
fun discoverScriptTemplatesInClassLoader( fun discoverScriptTemplatesInClassLoader(
classLoader: ClassLoader, classLoader: ClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): Sequence<KotlinScriptDefinition> { ): Sequence<KotlinScriptDefinition> {
val classpath = classLoader.getResources(MANIFEST_RESOURCE_NAME).asSequence().mapNotNull { val classpath = classLoader.getResources(MANIFEST_RESOURCE_NAME).asSequence().mapNotNull {
try { try {
@@ -54,25 +61,25 @@ fun discoverScriptTemplatesInClassLoader(
} }
} }
val classpathWithLoader = SimpleClasspathWithClassLoader(classpath.toList(), classLoader) val classpathWithLoader = SimpleClasspathWithClassLoader(classpath.toList(), classLoader)
return scriptTemplatesDiscoverySequence(classpathWithLoader, scriptResolverEnv, messageCollector) return scriptTemplatesDiscoverySequence(classpathWithLoader, scriptResolverEnv, messageReporter)
} }
fun discoverScriptTemplatesInClasspath( fun discoverScriptTemplatesInClasspath(
classpath: List<File>, classpath: List<File>,
baseClassLoader: ClassLoader?, baseClassLoader: ClassLoader?,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): Sequence<KotlinScriptDefinition> { ): Sequence<KotlinScriptDefinition> {
// TODO: try to find a way to reduce classpath (and classloader) to minimal one needed to load script definition and its dependencies // TODO: try to find a way to reduce classpath (and classloader) to minimal one needed to load script definition and its dependencies
val classpathWithLoader = LazyClasspathWithClassLoader(baseClassLoader) { classpath } val classpathWithLoader = LazyClasspathWithClassLoader(baseClassLoader) { classpath }
return scriptTemplatesDiscoverySequence(classpathWithLoader, scriptResolverEnv, messageCollector) return scriptTemplatesDiscoverySequence(classpathWithLoader, scriptResolverEnv, messageReporter)
} }
private fun scriptTemplatesDiscoverySequence( private fun scriptTemplatesDiscoverySequence(
classpathWithLoader: ClasspathWithClassLoader, classpathWithLoader: ClasspathWithClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): Sequence<KotlinScriptDefinition> { ): Sequence<KotlinScriptDefinition> {
return sequence { return sequence {
// for jar files the definition class is expected in the same jar as the discovery file // for jar files the definition class is expected in the same jar as the discovery file
@@ -96,10 +103,10 @@ private fun scriptTemplatesDiscoverySequence(
jar, jar,
classpathWithLoader, classpathWithLoader,
scriptResolverEnv, scriptResolverEnv,
messageCollector messageReporter
) )
if (notFoundClasses.isNotEmpty()) { if (notFoundClasses.isNotEmpty()) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"Configure scripting: unable to find script definitions [${notFoundClasses.joinToString(", ")}]" "Configure scripting: unable to find script definitions [${notFoundClasses.joinToString(", ")}]"
) )
@@ -115,7 +122,7 @@ private fun scriptTemplatesDiscoverySequence(
val discoveryMarkers = File(dep, SCRIPT_DEFINITION_MARKERS_PATH).listFiles() val discoveryMarkers = File(dep, SCRIPT_DEFINITION_MARKERS_PATH).listFiles()
if (discoveryMarkers?.isEmpty() == false) { if (discoveryMarkers?.isEmpty() == false) {
val (foundDefinitionClasses, notFoundDefinitions) = discoveryMarkers.map { it.name } val (foundDefinitionClasses, notFoundDefinitions) = discoveryMarkers.map { it.name }
.partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageCollector) .partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageReporter)
foundDefinitionClasses.forEach { foundDefinitionClasses.forEach {
yield(it) yield(it)
} }
@@ -124,11 +131,11 @@ private fun scriptTemplatesDiscoverySequence(
} }
else -> { else -> {
// assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here // assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configure scripting: Unknown classpath entry $dep") messageReporter(CompilerMessageSeverity.LOGGING, "Configure scripting: Unknown classpath entry $dep")
} }
} }
} catch (e: IOException) { } catch (e: IOException) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e" CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e"
) )
} }
@@ -138,19 +145,19 @@ private fun scriptTemplatesDiscoverySequence(
if (remainingDefinitionCandidates.isEmpty()) break if (remainingDefinitionCandidates.isEmpty()) break
try { try {
val (foundDefinitionClasses, notFoundDefinitions) = val (foundDefinitionClasses, notFoundDefinitions) =
remainingDefinitionCandidates.partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageCollector) remainingDefinitionCandidates.partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageReporter)
foundDefinitionClasses.forEach { foundDefinitionClasses.forEach {
yield(it) yield(it)
} }
remainingDefinitionCandidates = notFoundDefinitions remainingDefinitionCandidates = notFoundDefinitions
} catch (e: IOException) { } catch (e: IOException) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e" CompilerMessageSeverity.STRONG_WARNING, "Configure scripting: unable to process classpath entry $dep: $e"
) )
} }
} }
if (remainingDefinitionCandidates.isNotEmpty()) { if (remainingDefinitionCandidates.isNotEmpty()) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"The following script definitions are not found in the classpath: [${remainingDefinitionCandidates.joinToString()}]" "The following script definitions are not found in the classpath: [${remainingDefinitionCandidates.joinToString()}]"
) )
@@ -164,7 +171,7 @@ fun loadScriptTemplatesFromClasspath(
dependenciesClasspath: List<File>, dependenciesClasspath: List<File>,
baseClassLoader: ClassLoader, baseClassLoader: ClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): Sequence<KotlinScriptDefinition> = ): Sequence<KotlinScriptDefinition> =
if (scriptTemplates.isEmpty()) emptySequence() if (scriptTemplates.isEmpty()) emptySequence()
else sequence { else sequence {
@@ -174,7 +181,7 @@ fun loadScriptTemplatesFromClasspath(
baseClassLoader, baseClassLoader,
it, it,
scriptResolverEnv, scriptResolverEnv,
messageCollector messageReporter
) )
} }
initialLoadedDefinitions.forEach { initialLoadedDefinitions.forEach {
@@ -192,15 +199,15 @@ fun loadScriptTemplatesFromClasspath(
val (loadedDefinitions, notFoundTemplates) = when { val (loadedDefinitions, notFoundTemplates) = when {
dep.isFile && dep.extension == "jar" -> { // checking for extension is the compiler current behaviour, so the same logic is implemented here dep.isFile && dep.extension == "jar" -> { // checking for extension is the compiler current behaviour, so the same logic is implemented here
JarFile(dep).use { jar -> JarFile(dep).use { jar ->
remainingTemplates.partitionLoadJarDefinitions(jar, classpathWithLoader, scriptResolverEnv, messageCollector) remainingTemplates.partitionLoadJarDefinitions(jar, classpathWithLoader, scriptResolverEnv, messageReporter)
} }
} }
dep.isDirectory -> { dep.isDirectory -> {
remainingTemplates.partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageCollector) remainingTemplates.partitionLoadDirDefinitions(dep, classpathWithLoader, scriptResolverEnv, messageReporter)
} }
else -> { else -> {
// assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here // assuming that invalid classpath entries will be reported elsewhere anyway, so do not spam user with additional warnings here
messageCollector.report(CompilerMessageSeverity.LOGGING, "Configure scripting: Unknown classpath entry $dep") messageReporter(CompilerMessageSeverity.LOGGING, "Configure scripting: Unknown classpath entry $dep")
DefinitionsLoadPartitionResult( DefinitionsLoadPartitionResult(
listOf(), listOf(),
remainingTemplates remainingTemplates
@@ -214,7 +221,7 @@ fun loadScriptTemplatesFromClasspath(
remainingTemplates = notFoundTemplates remainingTemplates = notFoundTemplates
} }
} catch (e: IOException) { } catch (e: IOException) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"Configure scripting: unable to process classpath entry $dep: $e" "Configure scripting: unable to process classpath entry $dep: $e"
) )
@@ -222,7 +229,7 @@ fun loadScriptTemplatesFromClasspath(
} }
if (remainingTemplates.isNotEmpty()) { if (remainingTemplates.isNotEmpty()) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"Configure scripting: unable to find script definition classes: ${remainingTemplates.joinToString(", ")}" "Configure scripting: unable to find script definition classes: ${remainingTemplates.joinToString(", ")}"
) )
@@ -237,7 +244,7 @@ private data class DefinitionsLoadPartitionResult(
private inline fun List<String>.partitionLoadDefinitions( private inline fun List<String>.partitionLoadDefinitions(
classpathWithLoader: ClasspathWithClassLoader, classpathWithLoader: ClasspathWithClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector, noinline messageReporter: MessageReporter,
getBytes: (String) -> ByteArray? getBytes: (String) -> ByteArray?
): DefinitionsLoadPartitionResult { ): DefinitionsLoadPartitionResult {
val loaded = ArrayList<KotlinScriptDefinition>() val loaded = ArrayList<KotlinScriptDefinition>()
@@ -250,7 +257,7 @@ private inline fun List<String>.partitionLoadDefinitions(
definitionName, definitionName,
classpathWithLoader, classpathWithLoader,
scriptResolverEnv, scriptResolverEnv,
messageCollector messageReporter
) )
} }
when { when {
@@ -267,8 +274,8 @@ private fun List<String>.partitionLoadJarDefinitions(
jar: JarFile, jar: JarFile,
classpathWithLoader: ClasspathWithClassLoader, classpathWithLoader: ClasspathWithClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): DefinitionsLoadPartitionResult = partitionLoadDefinitions(classpathWithLoader, scriptResolverEnv, messageCollector) { definitionName -> ): DefinitionsLoadPartitionResult = partitionLoadDefinitions(classpathWithLoader, scriptResolverEnv, messageReporter) { definitionName ->
jar.getJarEntry("${definitionName.replace('.', '/')}.class")?.let { jar.getInputStream(it).readBytes() } jar.getJarEntry("${definitionName.replace('.', '/')}.class")?.let { jar.getInputStream(it).readBytes() }
} }
@@ -276,8 +283,8 @@ private fun List<String>.partitionLoadDirDefinitions(
dir: File, dir: File,
classpathWithLoader: ClasspathWithClassLoader, classpathWithLoader: ClasspathWithClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): DefinitionsLoadPartitionResult = partitionLoadDefinitions(classpathWithLoader, scriptResolverEnv, messageCollector) { definitionName -> ): DefinitionsLoadPartitionResult = partitionLoadDefinitions(classpathWithLoader, scriptResolverEnv, messageReporter) { definitionName ->
File(dir, "${definitionName.replace('.', '/')}.class").takeIf { it.exists() && it.isFile }?.readBytes() File(dir, "${definitionName.replace('.', '/')}.class").takeIf { it.exists() && it.isFile }?.readBytes()
} }
@@ -286,7 +293,7 @@ private fun loadScriptDefinition(
templateClassName: String, templateClassName: String,
classpathWithLoader: ClasspathWithClassLoader, classpathWithLoader: ClasspathWithClassLoader,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): KotlinScriptDefinition? { ): KotlinScriptDefinition? {
val anns = loadAnnotationsFromClass(templateClassBytes) val anns = loadAnnotationsFromClass(templateClassBytes)
for (ann in anns) { for (ann in anns) {
@@ -296,21 +303,21 @@ private fun loadScriptDefinition(
anns, anns,
templateClassName, templateClassName,
classpathWithLoader.classpath, classpathWithLoader.classpath,
messageCollector messageReporter
) )
} else if (ann.name == ScriptTemplateDefinition::class.simpleName) { } else if (ann.name == ScriptTemplateDefinition::class.simpleName) {
val templateClass = classpathWithLoader.classLoader.loadClass(templateClassName).kotlin val templateClass = classpathWithLoader.classLoader.loadClass(templateClassName).kotlin
def = KotlinScriptDefinitionFromAnnotatedTemplate(templateClass, scriptResolverEnv, classpathWithLoader.classpath) def = KotlinScriptDefinitionFromAnnotatedTemplate(templateClass, scriptResolverEnv, classpathWithLoader.classpath)
} }
if (def != null) { if (def != null) {
messageCollector.report( messageReporter(
CompilerMessageSeverity.LOGGING, CompilerMessageSeverity.LOGGING,
"Configure scripting: Added template $templateClassName from ${classpathWithLoader.classpath}" "Configure scripting: Added template $templateClassName from ${classpathWithLoader.classpath}"
) )
return def return def
} }
} }
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"Configure scripting: $templateClassName is not marked with any known kotlin script annotation" "Configure scripting: $templateClassName is not marked with any known kotlin script annotation"
) )
@@ -321,7 +328,7 @@ private fun loadScriptDefinition(
classLoader: ClassLoader, classLoader: ClassLoader,
template: String, template: String,
scriptResolverEnv: Map<String, Any?>, scriptResolverEnv: Map<String, Any?>,
messageCollector: MessageCollector messageReporter: MessageReporter
): KotlinScriptDefinition? { ): KotlinScriptDefinition? {
try { try {
val cls = classLoader.loadClass(template) val cls = classLoader.loadClass(template)
@@ -339,7 +346,7 @@ private fun loadScriptDefinition(
} else { } else {
KotlinScriptDefinitionFromAnnotatedTemplate(cls.kotlin, scriptResolverEnv) KotlinScriptDefinitionFromAnnotatedTemplate(cls.kotlin, scriptResolverEnv)
} }
messageCollector.report( messageReporter(
CompilerMessageSeverity.INFO, CompilerMessageSeverity.INFO,
"Added script definition $template to configuration: name = ${def.name}, " + "Added script definition $template to configuration: name = ${def.name}, " +
"resolver = ${def.dependencyResolver.javaClass.name}" "resolver = ${def.dependencyResolver.javaClass.name}"
@@ -349,7 +356,7 @@ private fun loadScriptDefinition(
// not found - not an error, return null // not found - not an error, return null
} catch (ex: Exception) { } catch (ex: Exception) {
// other exceptions - might be an error // other exceptions - might be an error
messageCollector.report( messageReporter(
CompilerMessageSeverity.STRONG_WARNING, CompilerMessageSeverity.STRONG_WARNING,
"Error on loading script definition $template: ${ex.message}" "Error on loading script definition $template: ${ex.message}"
) )
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.scripting.configuration.configureScriptDefinitions
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionProvider import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionProvider
import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionsFromClasspathDiscoverySource
import org.jetbrains.kotlin.scripting.definitions.StandardScriptDefinition import org.jetbrains.kotlin.scripting.definitions.StandardScriptDefinition
import org.jetbrains.kotlin.scripting.definitions.reporter
import java.io.File import java.io.File
class ScriptingCompilerConfigurationExtension(val project: MockProject) : CompilerConfigurationExtension { class ScriptingCompilerConfigurationExtension(val project: MockProject) : CompilerConfigurationExtension {
@@ -66,7 +67,7 @@ class ScriptingCompilerConfigurationExtension(val project: MockProject) : Compil
ScriptDefinitionsFromClasspathDiscoverySource( ScriptDefinitionsFromClasspathDiscoverySource(
configuration.jvmClasspathRoots, configuration.jvmClasspathRoots,
configuration.get(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION) ?: emptyMap(), configuration.get(ScriptingConfigurationKeys.LEGACY_SCRIPT_RESOLVER_ENVIRONMENT_OPTION) ?: emptyMap(),
messageCollector messageCollector.reporter
) )
) )
} }