Rename-refactor interfaces and methods of the script dependencies resolving
This commit is contained in:
committed by
Pavel V. Talanov
parent
5fd013ef42
commit
8a06eafaf1
@@ -19,7 +19,7 @@ package org.jetbrains.kotlin.script
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
|
||||
object SimpleUntypedAst {
|
||||
object SimpleAnnotationAst {
|
||||
|
||||
sealed class Node<out T>(val name: String) {
|
||||
class empty(name: String) : Node<Unit>(name)
|
||||
@@ -47,11 +47,11 @@ object SimpleUntypedAst {
|
||||
internal fun convert(expression: KtExpression): Node<Any> = when (expression) {
|
||||
is KtStringTemplateExpression -> {
|
||||
if (expression.entries.isEmpty())
|
||||
SimpleUntypedAst.Node.str(name, "")
|
||||
SimpleAnnotationAst.Node.str(name, "")
|
||||
else if (expression.entries.size == 1)
|
||||
convert(expression.entries[0])
|
||||
else
|
||||
SimpleUntypedAst.Node.str(name, "")
|
||||
SimpleAnnotationAst.Node.str(name, "")
|
||||
// TODO: parse expressions, etc. e.g.:
|
||||
// convertStringTemplateExpression(expression, parent, expression.entries.size - 1)
|
||||
}
|
||||
@@ -73,9 +73,9 @@ object SimpleUntypedAst {
|
||||
}
|
||||
}
|
||||
|
||||
fun parseAnnotation(ann: KtAnnotationEntry): SimpleUntypedAst.Node.list<Any> {
|
||||
val wann = SimpleUntypedAst.KtAnnotationWrapper(ann)
|
||||
fun parseAnnotation(ann: KtAnnotationEntry): SimpleAnnotationAst.Node.list<Any> {
|
||||
val wann = SimpleAnnotationAst.KtAnnotationWrapper(ann)
|
||||
val vals = wann.valueArguments
|
||||
return SimpleUntypedAst.Node.list(wann.name, vals)
|
||||
return SimpleAnnotationAst.Node.list(wann.name, vals)
|
||||
}
|
||||
|
||||
|
||||
@@ -35,13 +35,13 @@ import kotlin.reflect.KClass
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class ScriptFilePattern(val pattern: String)
|
||||
|
||||
interface GetScriptDependencies {
|
||||
operator fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? = null
|
||||
interface ScriptDependenciesResolver {
|
||||
fun resolve(projectRoot: File?, scriptFile: File?, annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? = null
|
||||
}
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class ScriptDependencyResolver(val extractor: KClass<out GetScriptDependencies>)
|
||||
annotation class ScriptDependencyResolver(val resolver: KClass<out ScriptDependenciesResolver>)
|
||||
|
||||
data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>, val context: Any?) : KotlinScriptDefinition {
|
||||
override val name = template.simpleName!!
|
||||
@@ -62,12 +62,12 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>, val
|
||||
override fun getScriptName(script: KtScript): Name = ScriptNameUtil.fileNameWithExtensionStripped(script, KotlinParserDefinition.STD_SCRIPT_EXT)
|
||||
|
||||
private val dependenciesResolvers by lazy {
|
||||
template.annotations.mapNotNull { it as? ScriptDependencyResolver }.map { it.extractor.constructors.first().call() }
|
||||
template.annotations.mapNotNull { it as? ScriptDependencyResolver }.map { it.resolver.constructors.first().call() }
|
||||
}
|
||||
|
||||
override fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? {
|
||||
val fileAnnotations = getAnnotationEntries(file, project)
|
||||
val fileDeps = dependenciesResolvers.mapNotNull { it(fileAnnotations, context) }
|
||||
val fileDeps = dependenciesResolvers.mapNotNull { it.resolve(project.basePath?.let { File(it) }, File(getFilePath(file)), fileAnnotations, context) }
|
||||
return KotlinScriptExternalDependenciesUnion(fileDeps)
|
||||
}
|
||||
|
||||
|
||||
@@ -116,16 +116,16 @@ class ScriptTest2 {
|
||||
}
|
||||
}
|
||||
|
||||
class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
class TestKotlinScriptDependenciesResolver : ScriptDependenciesResolver {
|
||||
|
||||
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
||||
|
||||
override fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? {
|
||||
override fun resolve(projectRoot: File?, scriptFile: File?, annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? {
|
||||
val anns = annotations.map { parseAnnotation(it) }.filter { it.name == depends::class.simpleName }
|
||||
val cp = anns.flatMap {
|
||||
it.value.mapNotNull {
|
||||
when (it) {
|
||||
is SimpleUntypedAst.Node.str -> if (it.value == "@{runtime}") kotlinPaths.runtimePath else File(it.value)
|
||||
is SimpleAnnotationAst.Node.str -> if (it.value == "@{runtime}") kotlinPaths.runtimePath else File(it.value)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
@@ -136,22 +136,22 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
}
|
||||
|
||||
private fun classpathFromClassloader(): List<File> =
|
||||
(GetTestKotlinScriptDependencies::class.java.classLoader as? URLClassLoader)?.urLs
|
||||
(TestKotlinScriptDependenciesResolver::class.java.classLoader as? URLClassLoader)?.urLs
|
||||
?.mapNotNull { it.toFile() }
|
||||
?.filter { it.path.contains("out") && it.path.contains("test") }
|
||||
?: emptyList()
|
||||
}
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(TestKotlinScriptDependenciesResolver::class)
|
||||
abstract class ScriptWithIntParam(num: Int)
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(TestKotlinScriptDependenciesResolver::class)
|
||||
abstract class ScriptWithClassParam(param: TestParamClass)
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(TestKotlinScriptDependenciesResolver::class)
|
||||
abstract class ScriptWithBaseClass(num: Int, passthrough: Int) : TestDSLClassWithParam(passthrough)
|
||||
|
||||
@Target(AnnotationTarget.FILE)
|
||||
|
||||
Reference in New Issue
Block a user