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)
|
||||
}
|
||||
|
||||
fun makeScriptDefsFromConfigs(configs: List<KotlinScripDeftConfig>): List<KotlinScriptDefinitionFromTemplate> =
|
||||
configs.map {
|
||||
val loader = URLClassLoader(it.classpath.map { File(it).toURI().toURL() }.toTypedArray())
|
||||
val cl = loader.loadClass(it.def)
|
||||
KotlinScriptDefinitionFromTemplate(cl.kotlin, null)
|
||||
fun makeScriptDefsFromConfigs(configs: List<KotlinScripDeftConfig>,
|
||||
errorsHandler: ((KotlinScripDeftConfig, Exception) -> Unit) = fun (ep, ex) = println(ex)): List<KotlinScriptDefinitionFromTemplate> =
|
||||
configs.mapNotNull {
|
||||
try {
|
||||
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")
|
||||
|
||||
+4
-3
@@ -83,10 +83,11 @@ class KotlinScriptConfigurationManager(
|
||||
}
|
||||
|
||||
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()) {
|
||||
scriptDefinitionProvider.setScriptDefinitions(
|
||||
it.map { KotlinConfigurableScriptDefinition(it, kotlinEnvVars) } + StandardScriptDefinition)
|
||||
scriptDefinitionProvider.setScriptDefinitions(it + 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"
|
||||
interface="org.jetbrains.kotlin.extensions.StorageComponentContainerContributor"
|
||||
area="IDEA_PROJECT"/>
|
||||
<extensionPoint name="scriptTemplateProvider"
|
||||
interface="org.jetbrains.kotlin.script.ScriptTemplateProvider"/>
|
||||
</extensionPoints>
|
||||
</idea-plugin>
|
||||
|
||||
@@ -10,5 +10,7 @@
|
||||
|
||||
<!-- diagnosticSuppressor decelerated in common.xml -->
|
||||
<diagnosticSuppressor implementation="org.jetbrains.kotlin.idea.debugger.DiagnosticSuppressorForDebugger"/>
|
||||
|
||||
<scriptTemplateProvider implementation="org.jetbrains.kotlin.idea.core.script.GradleScriptTemplateProvider"/>
|
||||
</extensions>
|
||||
</idea-plugin>
|
||||
|
||||
Reference in New Issue
Block a user