Implement extension point for providing script templates
This commit is contained in:
committed by
Pavel V. Talanov
parent
082290f8e3
commit
834372a3b3
+64
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.script
|
||||||
|
|
||||||
|
import com.intellij.openapi.extensions.ExtensionPointName
|
||||||
|
import com.intellij.openapi.extensions.Extensions
|
||||||
|
import java.io.File
|
||||||
|
import java.net.URLClassLoader
|
||||||
|
|
||||||
|
interface ScriptTemplateProvider {
|
||||||
|
|
||||||
|
// for resolving ambiguities
|
||||||
|
val id: String
|
||||||
|
val version: Int
|
||||||
|
|
||||||
|
val isValid: Boolean
|
||||||
|
|
||||||
|
val templateClass: String
|
||||||
|
|
||||||
|
val dependenciesClasspath: Iterable<String>
|
||||||
|
|
||||||
|
val context: Any?
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val EP_NAME = ExtensionPointName.create<ScriptTemplateProvider>("org.jetbrains.kotlin.scriptTemplateProvider")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun makeScriptDefsFromTemplateProviderExtesions(errorsHandler: ((ScriptTemplateProvider, Exception) -> Unit) = fun (ep, ex) = println(ex)): List<KotlinScriptDefinitionFromTemplate> =
|
||||||
|
makeScriptDefsFromTemplateProviders(Extensions.getExtensions(ScriptTemplateProvider.EP_NAME).asIterable(),
|
||||||
|
errorsHandler)
|
||||||
|
|
||||||
|
fun makeScriptDefsFromTemplateProviders(providers: Iterable<ScriptTemplateProvider>,
|
||||||
|
errorsHandler: ((ScriptTemplateProvider, Exception) -> Unit) = fun (ep, ex) = println(ex)
|
||||||
|
): List<KotlinScriptDefinitionFromTemplate> {
|
||||||
|
val idToVersion = hashMapOf<String, Int>()
|
||||||
|
return providers.filter { it.isValid }.sortedByDescending { it.version }.mapNotNull {
|
||||||
|
try {
|
||||||
|
idToVersion.get(it.id)?.let { ver -> errorsHandler(it, RuntimeException("Conflicting scriptTemplateProvider ${it.id}, using one with version $ver")) }
|
||||||
|
val loader = URLClassLoader(it.dependenciesClasspath.map { File(it).toURI().toURL() }.toTypedArray())
|
||||||
|
val cl = loader.loadClass(it.templateClass)
|
||||||
|
idToVersion.put(it.id, it.version)
|
||||||
|
KotlinScriptDefinitionFromTemplate(cl.kotlin, it.context)
|
||||||
|
}
|
||||||
|
catch (ex: Exception) {
|
||||||
|
errorsHandler(it, ex)
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -39,11 +39,18 @@ fun loadScriptDefConfigs(configFile: File): List<KotlinScripDeftConfig> =
|
|||||||
XmlSerializer.deserialize(it, KotlinScripDeftConfig::class.java)
|
XmlSerializer.deserialize(it, KotlinScripDeftConfig::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun makeScriptDefsFromConfigs(configs: List<KotlinScripDeftConfig>): List<KotlinScriptDefinitionFromTemplate> =
|
fun makeScriptDefsFromConfigs(configs: List<KotlinScripDeftConfig>,
|
||||||
configs.map {
|
errorsHandler: ((KotlinScripDeftConfig, Exception) -> Unit) = fun (ep, ex) = println(ex)): List<KotlinScriptDefinitionFromTemplate> =
|
||||||
val loader = URLClassLoader(it.classpath.map { File(it).toURI().toURL() }.toTypedArray())
|
configs.mapNotNull {
|
||||||
val cl = loader.loadClass(it.def)
|
try {
|
||||||
KotlinScriptDefinitionFromTemplate(cl.kotlin, null)
|
val loader = URLClassLoader(it.classpath.map { File(it).toURI().toURL() }.toTypedArray())
|
||||||
|
val cl = loader.loadClass(it.def)
|
||||||
|
KotlinScriptDefinitionFromTemplate(cl.kotlin, null)
|
||||||
|
}
|
||||||
|
catch (ex: Exception) {
|
||||||
|
errorsHandler(it, ex)
|
||||||
|
null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Tag("scriptDef")
|
@Tag("scriptDef")
|
||||||
|
|||||||
+4
-3
@@ -83,10 +83,11 @@ class KotlinScriptConfigurationManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun reloadScriptDefinitions() {
|
private fun reloadScriptDefinitions() {
|
||||||
loadScriptConfigsFromProjectRoot(File(project.basePath ?: "")).let {
|
(makeScriptDefsFromTemplateProviderExtesions(/* TODO: add logging here */) +
|
||||||
|
loadScriptConfigsFromProjectRoot(File(project.basePath ?: "")).map { KotlinConfigurableScriptDefinition(it, kotlinEnvVars) } +
|
||||||
|
makeScriptDefsFromConfigs(loadScriptDefConfigsFromProjectRoot(File(project.basePath ?: "")))).let {
|
||||||
if (it.isNotEmpty()) {
|
if (it.isNotEmpty()) {
|
||||||
scriptDefinitionProvider.setScriptDefinitions(
|
scriptDefinitionProvider.setScriptDefinitions(it + StandardScriptDefinition)
|
||||||
it.map { KotlinConfigurableScriptDefinition(it, kotlinEnvVars) } + StandardScriptDefinition)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2016 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.idea.core.script
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.script.ScriptTemplateProvider
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class GradleScriptTemplateProvider: ScriptTemplateProvider {
|
||||||
|
|
||||||
|
private val gradleHome = System.getProperty("idea.gradle.home")?.let { File(it) }?.let { if (it.exists()) it else null }
|
||||||
|
|
||||||
|
override val id: String = "Gradle"
|
||||||
|
override val version: Int = 1
|
||||||
|
override val isValid: Boolean = gradleHome != null
|
||||||
|
|
||||||
|
override val templateClass: String = "org.gradle.script.lang.kotlin.KotlinBuildScript"
|
||||||
|
override val dependenciesClasspath: Iterable<String> = listOf(File(gradleHome, "gradle_all.jar").absolutePath)
|
||||||
|
override val context: Any? = gradleHome
|
||||||
|
}
|
||||||
@@ -34,5 +34,7 @@
|
|||||||
<extensionPoint name="storageComponentContainerContributor"
|
<extensionPoint name="storageComponentContainerContributor"
|
||||||
interface="org.jetbrains.kotlin.extensions.StorageComponentContainerContributor"
|
interface="org.jetbrains.kotlin.extensions.StorageComponentContainerContributor"
|
||||||
area="IDEA_PROJECT"/>
|
area="IDEA_PROJECT"/>
|
||||||
|
<extensionPoint name="scriptTemplateProvider"
|
||||||
|
interface="org.jetbrains.kotlin.script.ScriptTemplateProvider"/>
|
||||||
</extensionPoints>
|
</extensionPoints>
|
||||||
</idea-plugin>
|
</idea-plugin>
|
||||||
|
|||||||
@@ -10,5 +10,7 @@
|
|||||||
|
|
||||||
<!-- diagnosticSuppressor decelerated in common.xml -->
|
<!-- diagnosticSuppressor decelerated in common.xml -->
|
||||||
<diagnosticSuppressor implementation="org.jetbrains.kotlin.idea.debugger.DiagnosticSuppressorForDebugger"/>
|
<diagnosticSuppressor implementation="org.jetbrains.kotlin.idea.debugger.DiagnosticSuppressorForDebugger"/>
|
||||||
|
|
||||||
|
<scriptTemplateProvider implementation="org.jetbrains.kotlin.idea.core.script.GradleScriptTemplateProvider"/>
|
||||||
</extensions>
|
</extensions>
|
||||||
</idea-plugin>
|
</idea-plugin>
|
||||||
|
|||||||
Reference in New Issue
Block a user