Implement automatic loading of .main.kts scripts support
also fix discovery from ScriptDefinitionsProvider
This commit is contained in:
@@ -100,13 +100,15 @@ interface KotlinPaths {
|
|||||||
CoroutinesCore(PathUtil.KOTLINX_COROUTINES_CORE_NAME),
|
CoroutinesCore(PathUtil.KOTLINX_COROUTINES_CORE_NAME),
|
||||||
KotlinDaemon(PathUtil.KOTLIN_DAEMON_NAME),
|
KotlinDaemon(PathUtil.KOTLIN_DAEMON_NAME),
|
||||||
Ktor(PathUtil.KTOR_NAME),
|
Ktor(PathUtil.KTOR_NAME),
|
||||||
|
MainKts(PathUtil.MAIN_KTS_NAME)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Maybe we need separate classpaths for compilers with and without the daemon
|
// TODO: Maybe we need separate classpaths for compilers with and without the daemon
|
||||||
enum class ClassPaths(val contents: List<Jar> = emptyList()) {
|
enum class ClassPaths(val contents: List<Jar> = emptyList()) {
|
||||||
Empty(),
|
Empty(),
|
||||||
Compiler(Jar.Compiler, Jar.StdLib, Jar.Reflect, Jar.ScriptRuntime, Jar.Trove4j, Jar.KotlinDaemon, Jar.Ktor),
|
Compiler(Jar.Compiler, Jar.StdLib, Jar.Reflect, Jar.ScriptRuntime, Jar.Trove4j, Jar.KotlinDaemon, Jar.Ktor),
|
||||||
CompilerWithScripting(Compiler, Jar.ScriptingPlugin, Jar.ScriptingImpl, Jar.ScriptingLib, Jar.ScriptingJvmLib)
|
CompilerWithScripting(Compiler, Jar.ScriptingPlugin, Jar.ScriptingImpl, Jar.ScriptingLib, Jar.ScriptingJvmLib),
|
||||||
|
MainKts(Jar.MainKts, Jar.ScriptRuntime, Jar.StdLib, Jar.Reflect)
|
||||||
;
|
;
|
||||||
|
|
||||||
constructor(vararg jars: Jar) : this(jars.asList())
|
constructor(vararg jars: Jar) : this(jars.asList())
|
||||||
|
|||||||
@@ -79,6 +79,7 @@ object PathUtil {
|
|||||||
const val KOTLIN_SCRIPTING_COMPILER_IMPL_JAR = "$KOTLIN_SCRIPTING_COMPILER_IMPL_NAME.jar"
|
const val KOTLIN_SCRIPTING_COMPILER_IMPL_JAR = "$KOTLIN_SCRIPTING_COMPILER_IMPL_NAME.jar"
|
||||||
const val JS_ENGINES_NAME = "js.engines"
|
const val JS_ENGINES_NAME = "js.engines"
|
||||||
const val JS_ENGINES_JAR = "$JS_ENGINES_NAME.jar"
|
const val JS_ENGINES_JAR = "$JS_ENGINES_NAME.jar"
|
||||||
|
const val MAIN_KTS_NAME = "kotlin-main-kts"
|
||||||
|
|
||||||
val KOTLIN_SCRIPTING_PLUGIN_CLASSPATH_JARS = arrayOf(
|
val KOTLIN_SCRIPTING_PLUGIN_CLASSPATH_JARS = arrayOf(
|
||||||
KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR, KOTLIN_SCRIPTING_COMPILER_IMPL_JAR,
|
KOTLIN_SCRIPTING_COMPILER_PLUGIN_JAR, KOTLIN_SCRIPTING_COMPILER_IMPL_JAR,
|
||||||
|
|||||||
@@ -92,6 +92,9 @@
|
|||||||
<scriptDefinitionContributor id="ConsoleScriptDefinitionContributor"
|
<scriptDefinitionContributor id="ConsoleScriptDefinitionContributor"
|
||||||
implementation="org.jetbrains.kotlin.console.ConsoleScriptDefinitionContributor"/>
|
implementation="org.jetbrains.kotlin.console.ConsoleScriptDefinitionContributor"/>
|
||||||
|
|
||||||
|
<scriptDefinitionsProvider id="MainKtsScriptDefinitionSource"
|
||||||
|
implementation="org.jetbrains.kotlin.idea.script.MainKtsScriptDefinitionSource"/>
|
||||||
|
|
||||||
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.JvmPlatformKindResolution"/>
|
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.JvmPlatformKindResolution"/>
|
||||||
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.JsPlatformKindResolution"/>
|
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.JsPlatformKindResolution"/>
|
||||||
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.CommonPlatformKindResolution"/>
|
<idePlatformKindResolution implementation="org.jetbrains.kotlin.caches.resolve.CommonPlatformKindResolution"/>
|
||||||
|
|||||||
@@ -30,12 +30,13 @@ class BridgeScriptDefinitionsContributor(private val project: Project) : ScriptD
|
|||||||
if (explicitClasses.isEmpty()) emptySequence()
|
if (explicitClasses.isEmpty()) emptySequence()
|
||||||
else loadDefinitionsFromTemplates(explicitClasses, classPath, hostConfiguration).asSequence()
|
else loadDefinitionsFromTemplates(explicitClasses, classPath, hostConfiguration).asSequence()
|
||||||
val discoveredDefinitions =
|
val discoveredDefinitions =
|
||||||
if (provider.useDiscovery()) emptySequence()
|
if (provider.useDiscovery())
|
||||||
else ScriptDefinitionsFromClasspathDiscoverySource(
|
ScriptDefinitionsFromClasspathDiscoverySource(
|
||||||
classPath,
|
classPath,
|
||||||
hostConfiguration,
|
hostConfiguration,
|
||||||
::loggingReporter
|
::loggingReporter
|
||||||
).definitions
|
).definitions
|
||||||
|
else emptySequence()
|
||||||
explicitDefinitions + discoveredDefinitions
|
explicitDefinitions + discoveredDefinitions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* 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.idea.script
|
||||||
|
|
||||||
|
import com.intellij.openapi.diagnostic.Logger
|
||||||
|
import org.jetbrains.kotlin.utils.KotlinPaths
|
||||||
|
import org.jetbrains.kotlin.utils.PathUtil
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.script.experimental.intellij.ScriptDefinitionsProvider
|
||||||
|
|
||||||
|
class MainKtsScriptDefinitionSource : ScriptDefinitionsProvider {
|
||||||
|
override val id: String = ".main.kts script"
|
||||||
|
|
||||||
|
override fun getDefinitionClasses(): Iterable<String> = emptyList()
|
||||||
|
|
||||||
|
override fun getDefinitionsClassPath(): Iterable<File> {
|
||||||
|
|
||||||
|
val paths = PathUtil.kotlinPathsForIdeaPlugin
|
||||||
|
return if (paths.jar(KotlinPaths.Jar.MainKts).exists()) {
|
||||||
|
paths.classPath(KotlinPaths.ClassPaths.MainKts)
|
||||||
|
} else {
|
||||||
|
Logger.getInstance(MainKtsScriptDefinitionSource::class.java).warn("[kts] Support for .main.kts scripts is not loaded: kotlin-main-kts.jar not found")
|
||||||
|
emptyList()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun useDiscovery(): Boolean = true
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user