Fix after review, add logging for scripting-specific behavior
This commit is contained in:
+17
-6
@@ -17,6 +17,7 @@
|
||||
package org.jetbrains.kotlin.script
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.project.Project
|
||||
import java.io.File
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
||||
@@ -42,6 +43,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
|
||||
else scriptDefinitionProvider.findScriptDefinition(file)
|
||||
?.let { it.getDependenciesFor(file, project, null) }
|
||||
.apply {
|
||||
log.info("[kts] new cached deps for $path: ${this?.classpath?.joinToString(File.pathSeparator)}")
|
||||
cacheLock.write {
|
||||
if (this == null) {
|
||||
cacheOfNulls.add(path)
|
||||
@@ -62,6 +64,7 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
|
||||
val scriptDef = scriptDefinitionProvider.findScriptDefinition(file)
|
||||
if (scriptDef != null) {
|
||||
val deps = scriptDef.getDependenciesFor(file, project, null)
|
||||
log.info("[kts] cached deps for $path: ${deps?.classpath?.joinToString(File.pathSeparator)}")
|
||||
if (deps != null) {
|
||||
cache.put(path, deps)
|
||||
}
|
||||
@@ -88,16 +91,21 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
|
||||
deps != null && (oldDeps == null ||
|
||||
!deps.classpath.isSamePathListAs(oldDeps.classpath) || !deps.sources.isSamePathListAs(oldDeps.sources)) -> {
|
||||
// changed or new
|
||||
log.info("[kts] updated/new cached deps for $path: ${deps.classpath.joinToString(File.pathSeparator)}")
|
||||
cache.put(path, deps)
|
||||
cacheOfNulls.remove(path)
|
||||
file
|
||||
}
|
||||
deps != null -> {
|
||||
// same as before
|
||||
log.info("[kts] unchanged deps for $path")
|
||||
null
|
||||
}
|
||||
else -> {
|
||||
if (cache.remove(path) != null || cacheOfNulls.remove(path)) file // cleared
|
||||
if (cache.remove(path) != null || cacheOfNulls.remove(path)) {
|
||||
log.info("[kts] removed deps for $path")
|
||||
file
|
||||
} // cleared
|
||||
else null // same as before
|
||||
}
|
||||
}
|
||||
@@ -142,12 +150,15 @@ class KotlinScriptExternalImportsProvider(val project: Project, private val scri
|
||||
@JvmStatic
|
||||
fun getInstance(project: Project): KotlinScriptExternalImportsProvider? =
|
||||
ServiceManager.getService(project, KotlinScriptExternalImportsProvider::class.java)
|
||||
internal val log = Logger.getInstance(KotlinScriptExternalImportsProvider::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun Iterable<File>.isSamePathListAs(other: Iterable<File>): Boolean {
|
||||
val c1 = asSequence().map { it.canonicalPath }
|
||||
val c2 = other.asSequence().map { it.canonicalPath }
|
||||
return c1.zip(c2).all { it.first == it.second }
|
||||
}
|
||||
internal fun Iterable<File>.isSamePathListAs(other: Iterable<File>): Boolean =
|
||||
with (Pair(iterator(), other.iterator())) {
|
||||
while (first.hasNext() && second.hasNext()) {
|
||||
if (first.next().canonicalPath != second.next().canonicalPath) return false
|
||||
}
|
||||
!(first.hasNext() || second.hasNext())
|
||||
}
|
||||
|
||||
|
||||
@@ -71,7 +71,13 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>,
|
||||
when {
|
||||
resolver != null -> ScriptTemplateDefinitionData(resolver.javaClass.kotlin, { resolver }, filePattern)
|
||||
// TODO: logScriptDefMessage missing or invalid constructor
|
||||
defAnn != null -> ScriptTemplateDefinitionData(defAnn.resolver, { defAnn.resolver.primaryConstructor?.call() }, filePattern)
|
||||
defAnn != null -> ScriptTemplateDefinitionData(defAnn.resolver,
|
||||
{
|
||||
defAnn.resolver.primaryConstructor?.call() ?: null.apply {
|
||||
log.error("[kts] No default constructor found for ${defAnn.resolver.qualifiedName}")
|
||||
}
|
||||
},
|
||||
filePattern)
|
||||
else -> ScriptTemplateDefinitionData(BasicScriptDependenciesResolver::class, ::BasicScriptDependenciesResolver, filePattern)
|
||||
}
|
||||
}
|
||||
@@ -93,7 +99,6 @@ data class KotlinScriptDefinitionFromTemplate(val template: KClass<out Any>,
|
||||
// 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)
|
||||
|
||||
|
||||
override fun <TF> getDependenciesFor(file: TF, project: Project, previousDependencies: KotlinScriptExternalDependencies?): KotlinScriptExternalDependencies? {
|
||||
|
||||
val script = BasicScriptContents(file, getAnnotations = {
|
||||
|
||||
+5
-1
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.script
|
||||
|
||||
import com.intellij.openapi.diagnostic.Logger
|
||||
import com.intellij.openapi.extensions.ExtensionPointName
|
||||
import com.intellij.openapi.extensions.Extensions
|
||||
import com.intellij.openapi.project.Project
|
||||
@@ -46,7 +47,8 @@ interface ScriptTemplateProvider {
|
||||
}
|
||||
|
||||
fun makeScriptDefsFromTemplateProviderExtensions(project: Project,
|
||||
errorsHandler: ((ScriptTemplateProvider, Exception) -> Unit) = { ep, ex -> throw ex }): List<KotlinScriptDefinitionFromTemplate> =
|
||||
errorsHandler: ((ScriptTemplateProvider, Exception) -> Unit) = { ep, ex -> throw ex }
|
||||
): List<KotlinScriptDefinitionFromTemplate> =
|
||||
makeScriptDefsFromTemplateProviders(Extensions.getArea(project).getExtensionPoint(ScriptTemplateProvider.EP_NAME).extensions.asIterable(),
|
||||
errorsHandler)
|
||||
|
||||
@@ -57,6 +59,8 @@ fun makeScriptDefsFromTemplateProviders(providers: Iterable<ScriptTemplateProvid
|
||||
return providers.filter { it.isValid }.sortedByDescending { it.version }.mapNotNull { provider ->
|
||||
try {
|
||||
idToVersion.get(provider.id)?.let { ver -> errorsHandler(provider, RuntimeException("Conflicting scriptTemplateProvider ${provider.id}, using one with version $ver")) }
|
||||
Logger.getInstance("makeScriptDefsFromTemplateProviders")
|
||||
.info("[kts] loading script definition ${provider.templateClassName} using cp: ${provider.dependenciesClasspath.joinToString(File.pathSeparator)}")
|
||||
val loader = URLClassLoader(provider.dependenciesClasspath.map { File(it).toURI().toURL() }.toTypedArray(), ScriptTemplateProvider::class.java.classLoader)
|
||||
val cl = loader.loadClass(provider.templateClassName)
|
||||
idToVersion.put(provider.id, provider.version)
|
||||
|
||||
Reference in New Issue
Block a user