Simplify GetScriptDependencies interface
This commit is contained in:
committed by
Pavel V. Talanov
parent
a60e1ab807
commit
5e3ba36cc1
@@ -37,7 +37,6 @@ annotation class ScriptFilePattern(val pattern: String)
|
||||
|
||||
interface GetScriptDependencies {
|
||||
operator fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? = null
|
||||
operator fun invoke(context: Any?): KotlinScriptExternalDependencies? = null
|
||||
}
|
||||
|
||||
@Target(AnnotationTarget.CLASS)
|
||||
@@ -62,18 +61,14 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>, val
|
||||
// TODO: implement other strategy - e.g. try to extract something from match with ScriptFilePattern
|
||||
override fun getScriptName(script: KtScript): Name = ScriptNameUtil.fileNameWithExtensionStripped(script, KotlinParserDefinition.STD_SCRIPT_EXT)
|
||||
|
||||
private val dependenciesExtractors by lazy {
|
||||
private val dependenciesResolvers by lazy {
|
||||
template.annotations.mapNotNull { it as? ScriptDependencyResolver }.map { it.extractor.constructors.first().call() }
|
||||
}
|
||||
|
||||
private val dependencies: List<KotlinScriptExternalDependencies> by lazy {
|
||||
dependenciesExtractors.mapNotNull { it(context) }
|
||||
}
|
||||
|
||||
override fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? {
|
||||
val fileAnnotations = getAnnotationEntries(file, project)
|
||||
val fileDeps = dependenciesExtractors.mapNotNull { it(fileAnnotations, context) }
|
||||
return KotlinScriptExternalDependenciesUnion(dependencies + fileDeps)
|
||||
val fileDeps = dependenciesResolvers.mapNotNull { it(fileAnnotations, context) }
|
||||
return KotlinScriptExternalDependenciesUnion(fileDeps)
|
||||
}
|
||||
|
||||
private fun <TF> getAnnotationEntries(file: TF, project: Project): Iterable<KtAnnotationEntry> = when (file) {
|
||||
|
||||
@@ -119,7 +119,6 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
||||
|
||||
override fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? {
|
||||
if (annotations.none()) return null
|
||||
val anns = annotations.map { parseAnnotation(it) }.filter { it.name == depends::class.simpleName }
|
||||
val cp = anns.flatMap {
|
||||
it.value.mapNotNull {
|
||||
@@ -130,7 +129,7 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
}
|
||||
}
|
||||
return object : KotlinScriptExternalDependencies {
|
||||
override val classpath = cp
|
||||
override val classpath = classpathFromClassloader() + cp
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,12 +138,6 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
?.mapNotNull { it.toFile()?.canonicalPath }
|
||||
?.filter { it.contains("out/test") }
|
||||
?: emptyList()
|
||||
|
||||
override fun invoke(context: Any?): KotlinScriptExternalDependencies? {
|
||||
return object : KotlinScriptExternalDependencies {
|
||||
override val classpath = classpathFromClassloader()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
|
||||
Reference in New Issue
Block a user