Implement basic support of the new scripting in the frontend and plugin
This commit is contained in:
+23
-8
@@ -28,7 +28,9 @@ import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||
import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate
|
||||
import org.jetbrains.kotlin.script.ScriptDefinitionProvider
|
||||
import org.jetbrains.kotlin.script.ScriptTemplatesProvider
|
||||
import org.jetbrains.kotlin.script.experimental.KotlinScriptDefinitionAdapterFromNewAPI
|
||||
import org.jetbrains.kotlin.utils.PathUtil
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.flattenTo
|
||||
import java.io.File
|
||||
import java.net.URLClassLoader
|
||||
@@ -37,6 +39,7 @@ import kotlin.concurrent.read
|
||||
import kotlin.concurrent.write
|
||||
import kotlin.script.dependencies.Environment
|
||||
import kotlin.script.dependencies.ScriptContents
|
||||
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
|
||||
import kotlin.script.experimental.dependencies.DependenciesResolver
|
||||
import kotlin.script.experimental.dependencies.ScriptDependencies
|
||||
import kotlin.script.experimental.dependencies.asSuccess
|
||||
@@ -139,18 +142,31 @@ fun loadDefinitionsFromTemplates(
|
||||
* i.e. gradle resolver may depend on some jars that 'built.gradle.kts' files should not depend on.
|
||||
*/
|
||||
additionalResolverClasspath: List<File> = emptyList()
|
||||
): List<KotlinScriptDefinitionFromAnnotatedTemplate> = try {
|
||||
): List<KotlinScriptDefinition> = try {
|
||||
val classpath = templateClasspath + additionalResolverClasspath
|
||||
LOG.info("[kts] loading script definitions $templateClassNames using cp: ${classpath.joinToString(File.pathSeparator)}")
|
||||
val baseLoader = ScriptDefinitionContributor::class.java.classLoader
|
||||
val loader = if (classpath.isEmpty()) baseLoader else URLClassLoader(classpath.map { it.toURI().toURL() }.toTypedArray(), baseLoader)
|
||||
|
||||
templateClassNames.map {
|
||||
KotlinScriptDefinitionFromAnnotatedTemplate(
|
||||
loader.loadClass(it).kotlin,
|
||||
environment,
|
||||
templateClasspath
|
||||
)
|
||||
templateClassNames.mapNotNull {
|
||||
val template = loader.loadClass(it).kotlin
|
||||
when {
|
||||
template.annotations.firstIsInstanceOrNull<org.jetbrains.kotlin.script.ScriptTemplateDefinition>() != null ||
|
||||
template.annotations.firstIsInstanceOrNull<kotlin.script.templates.ScriptTemplateDefinition>() != null -> {
|
||||
KotlinScriptDefinitionFromAnnotatedTemplate(
|
||||
template,
|
||||
environment,
|
||||
templateClasspath
|
||||
)
|
||||
}
|
||||
template.annotations.firstIsInstanceOrNull<kotlin.script.experimental.annotations.KotlinScript>() != null -> {
|
||||
KotlinScriptDefinitionAdapterFromNewAPI(ScriptDefinitionFromAnnotatedBaseClass(template))
|
||||
}
|
||||
else -> {
|
||||
LOG.error("[kts] cannot find a valid script definition annotation on the class $template")
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ex: Throwable) {
|
||||
@@ -208,4 +224,3 @@ class BundledKotlinScriptDependenciesResolver(private val project: Project) : De
|
||||
return jdk?.homePath
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user