Add previous imports to resolving interfaces to enable effective caching implementations

This commit is contained in:
Ilya Chernikov
2016-06-17 17:04:14 +02:00
committed by Pavel V. Talanov
parent f30b402640
commit 4f05f839a3
7 changed files with 20 additions and 11 deletions
@@ -46,7 +46,7 @@ data class KotlinConfigurableScriptDefinition(val config: KotlinScriptConfig, va
private val evaluatedClasspath by lazy { config.classpath.evalWithVars(environmentVars).map { File(it) }.distinctBy { it.canonicalPath } }
override fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? =
override fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? =
if (!isScript(file)) null
else {
val extDeps = getScriptDependenciesFromConfig(file)
@@ -54,7 +54,7 @@ interface KotlinScriptDefinition {
fun getScriptName(script: KtScript): Name =
ScriptNameUtil.fileNameWithExtensionStripped(script, KotlinParserDefinition.STD_SCRIPT_EXT)
fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? = null
fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? = null
}
interface KotlinScriptExternalDependencies {
@@ -36,7 +36,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
cache[path]
?: if (cacheOfNulls.contains(path)) null
else scriptDefinitionProvider.findScriptDefinition(file)
?.let { it.getDependenciesFor(file, project) }
?.let { it.getDependenciesFor(file, project, null) }
.apply { cacheLock.write {
if (this == null) {
cacheOfNulls.add(path)
@@ -57,7 +57,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
if (!cache.containsKey(path) && !cacheOfNulls.contains(path) && !uncached.contains(path)) {
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file)
if (scriptDef != null) {
val deps = scriptDef.getDependenciesFor(file, project)
val deps = scriptDef.getDependenciesFor(file, project, null)
if (deps != null) {
cache.put(path, deps)
}
@@ -78,8 +78,8 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
val path = getFilePath(file)
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file)
if (scriptDef != null) {
val deps = scriptDef.getDependenciesFor(file, project)
val oldDeps = cache[path]
val deps = scriptDef.getDependenciesFor(file, project, oldDeps)
when {
deps != null && (oldDeps == null ||
!deps.classpath.isSameClasspathAs(oldDeps.classpath) || !deps.sources.isSameClasspathAs(oldDeps.sources)) -> {
@@ -41,7 +41,11 @@ import kotlin.reflect.memberProperties
annotation class ScriptFilePattern(val pattern: String)
interface ScriptDependenciesResolver {
fun resolve(scriptFile: File?, annotations: Iterable<Annotation>, environment: Map<String, Any?>?): KotlinScriptExternalDependencies? = null
fun resolve(scriptFile: File?,
annotations: Iterable<Annotation>,
environment: Map<String, Any?>?,
previousDependencies: KotlinScriptExternalDependencies? = null
): KotlinScriptExternalDependencies? = null
}
@Target(AnnotationTarget.CLASS)
@@ -88,7 +92,7 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>, val
return res
}
override fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? {
override fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? {
val fileAnnotations = getAnnotationEntries(file, project)
.map { KtAnnotationWrapper(it) }
.mapNotNull { wrappedAnn ->
@@ -111,7 +115,7 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>, val
val annFQN = ann.first.qualifiedName
if (resolver.first.supportedAnnotationClasses.asIterable2().any { it.qualifiedName == annFQN }) ann.second else null
}
resolver.second.resolve(getFile(file), supportedAnnotations, environment)
resolver.second.resolve(getFile(file), supportedAnnotations, environment, previousDependencies)
}
return KotlinScriptExternalDependenciesUnion(fileDeps)
}
@@ -31,7 +31,7 @@ abstract class BaseScriptDefinition (val extension: String, val cp: List<File>?
override val name = "Test Kotlin Script"
override fun <TF> isScript(file: TF): Boolean = getFileName(file).endsWith(extension)
override fun getScriptName(script: KtScript): Name = ScriptNameUtil.fileNameWithExtensionStripped(script, extension)
override fun <TF> getDependenciesFor(file: TF, project: Project): KotlinScriptExternalDependencies? =
override fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? =
object : KotlinScriptExternalDependencies {
override val classpath: Iterable<File> = cp ?: (classpathFromProperty() + classpathFromClassloader(BaseScriptDefinition::class.java.classLoader)).distinct()
}
@@ -120,7 +120,12 @@ class TestKotlinScriptDependenciesResolver : ScriptDependenciesResolver {
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
override fun resolve(scriptFile: File?, annotations: Iterable<Annotation>, environment: Map<String, Any?>?): KotlinScriptExternalDependencies? {
override fun resolve(scriptFile: File?,
annotations: Iterable<Annotation>,
environment: Map<String, Any?>?,
previousDependencies: KotlinScriptExternalDependencies?
): KotlinScriptExternalDependencies?
{
val cp = annotations.flatMap {
when (it) {
is depends -> listOf(if (it.path == "@{runtime}") kotlinPaths.runtimePath else File(it.path))
@@ -309,7 +309,7 @@ internal data class ScriptModuleInfo(val project: Project, val module: Module?,
private fun dependenciesRoots(): List<VirtualFile> {
// TODO: find out whether it should be cashed (some changes listener should be implemented for the cached roots)
val jarfs = StandardFileSystems.jar()
return scriptDefinition.getDependenciesFor(scriptFile, project)?.classpath
return scriptDefinition.getDependenciesFor(scriptFile, project, null)?.classpath
?.map { it.canonicalFile }
?.distinct()
?.mapNotNull {