Add script dependencies modification tracker, some optimizations

This commit is contained in:
Ilya Chernikov
2016-10-26 15:29:27 +02:00
parent 3382ab3d9c
commit 2e03a6f1fd
7 changed files with 95 additions and 44 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.parsing.KotlinParserDefinition
import org.jetbrains.kotlin.psi.KtScript
import java.io.File
import kotlin.comparisons.compareValues
import kotlin.reflect.KClass
import kotlin.script.templates.standard.ScriptTemplateWithArgs
@@ -42,13 +43,33 @@ open class KotlinScriptDefinition(val template: KClass<out Any>) {
open fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? = null
}
interface KotlinScriptExternalDependencies {
interface KotlinScriptExternalDependencies : Comparable<KotlinScriptExternalDependencies> {
val javaHome: String? get() = null
val classpath: Iterable<File> get() = emptyList()
val imports: Iterable<String> get() = emptyList()
val sources: Iterable<File> get() = emptyList()
val scripts: Iterable<File> get() = emptyList()
override fun compareTo(other: KotlinScriptExternalDependencies): Int =
compareValues(javaHome, other.javaHome)
.chainCompare { compareIterables(classpath, other.classpath) }
.chainCompare { compareIterables(imports, other.imports) }
.chainCompare { compareIterables(sources, other.sources) }
.chainCompare { compareIterables(scripts, other.scripts) }
}
object StandardScriptDefinition : KotlinScriptDefinition(ScriptTemplateWithArgs::class)
private fun<T: Comparable<T>> compareIterables(a: Iterable<T>, b: Iterable<T>): Int {
val ia = a.iterator()
val ib = b.iterator()
while (true) {
if (ia.hasNext() && !ib.hasNext()) return 1
if (!ia.hasNext() && !ib.hasNext()) return 0
if (!ia.hasNext()) return -1
val compRes = compareValues(ia.next(), ib.next())
if (compRes != 0) return compRes
}
}
private inline fun Int.chainCompare(compFn: () -> Int ): Int = if (this != 0) this else compFn()