Refactor script.util templates

This commit is contained in:
Ilya Chernikov
2016-12-02 14:33:34 +01:00
parent eb3d8a1a4a
commit cfcd3b8186
6 changed files with 47 additions and 42 deletions
@@ -22,7 +22,7 @@
</dependencies>
<properties>
<kotlin.compiler.scriptTemplates>org.jetbrains.kotlin.script.util.StandardScript</kotlin.compiler.scriptTemplates>
<kotlin.compiler.scriptTemplates>org.jetbrains.kotlin.script.util.templates.StandardScriptTemplate</kotlin.compiler.scriptTemplates>
<kotlin.compiler.scriptClasspath>${org.jetbrains.kotlin:kotlin-script-util:jar}</kotlin.compiler.scriptClasspath>
</properties>
@@ -3,7 +3,7 @@ import java.lang.Exception
if (!args.isEmpty())
println("some args passed")
if (this !is org.jetbrains.kotlin.script.util.StandardScript)
if (this !is org.jetbrains.kotlin.script.util.templates.StandardScriptTemplate)
throw Exception("Unexpected script base class")
println("Hello from Kotlin script file!")
@@ -102,8 +102,8 @@ val defaultScriptBaseClasspath: List<File> by lazy {
?: emptyList()
}
class DefaultKotlinResolver() :
class ContextBasedResolver() :
KotlinAnnotatedScriptDependenciesResolver(defaultScriptBaseClasspath, arrayListOf())
class DefaultKotlinAnnotatedScriptDependenciesResolver :
class ContextAndAnnotationsBasedResolver :
KotlinAnnotatedScriptDependenciesResolver(defaultScriptBaseClasspath, arrayListOf(DirectResolver(), MavenResolver()))
@@ -1,33 +0,0 @@
/*
* 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.
*/
@file:Suppress("unused") // an API
package org.jetbrains.kotlin.script.util
import org.jetbrains.kotlin.script.ScriptTemplateDefinition
@ScriptTemplateDefinition(resolver = DefaultKotlinResolver::class, scriptFilePattern = ".*\\.kts")
abstract class StandardScript(val args: Array<String>)
@ScriptTemplateDefinition(resolver = DefaultKotlinAnnotatedScriptDependenciesResolver::class, scriptFilePattern = ".*\\.kts")
abstract class StandardScriptWithAnnotatedResolving(val args: Array<String>)
@ScriptTemplateDefinition(resolver = DefaultKotlinResolver::class, scriptFilePattern = ".*\\.kts")
abstract class ScriptWithBindings(val bindings: Map<String, Any?>)
@ScriptTemplateDefinition(resolver = DefaultKotlinAnnotatedScriptDependenciesResolver::class, scriptFilePattern = ".*\\.kts")
abstract class ScriptWithBindingsAndAnnotatedResolving(val bindings: Map<String, Any?>)
@@ -0,0 +1,35 @@
/*
* 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.
*/
@file:Suppress("unused") // an API
package org.jetbrains.kotlin.script.util.templates
import org.jetbrains.kotlin.script.ScriptTemplateDefinition
import org.jetbrains.kotlin.script.util.ContextAndAnnotationsBasedResolver
import org.jetbrains.kotlin.script.util.ContextBasedResolver
@ScriptTemplateDefinition(resolver = ContextBasedResolver::class, scriptFilePattern = ".*\\.kts")
abstract class StandardScriptTemplate(val args: Array<String>)
@ScriptTemplateDefinition(resolver = ContextAndAnnotationsBasedResolver::class, scriptFilePattern = ".*\\.kts")
abstract class StandardScriptTemplateWithAnnotatedResolving(val args: Array<String>)
@ScriptTemplateDefinition(resolver = ContextBasedResolver::class, scriptFilePattern = ".*\\.kts")
abstract class ScriptTemplateWithBindings(val bindings: Map<String, Any?>)
@ScriptTemplateDefinition(resolver = ContextAndAnnotationsBasedResolver::class, scriptFilePattern = ".*\\.kts")
abstract class ScriptTemplateWithBindingsAndAnnotatedResolving(val bindings: Map<String, Any?>)
@@ -31,6 +31,9 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.addKotlinSourceRoot
import org.jetbrains.kotlin.script.KotlinScriptDefinition
import org.jetbrains.kotlin.script.KotlinScriptDefinitionFromAnnotatedTemplate
import org.jetbrains.kotlin.script.util.templates.ScriptTemplateWithBindings
import org.jetbrains.kotlin.script.util.templates.StandardScriptTemplate
import org.jetbrains.kotlin.script.util.templates.StandardScriptTemplateWithAnnotatedResolving
import org.jetbrains.kotlin.utils.PathUtil
import org.jetbrains.kotlin.utils.PathUtil.getResourcePathForClass
import org.junit.Assert
@@ -60,7 +63,7 @@ done
@Test
fun testArgsHelloWorld() {
val scriptClass = compileScript("args-hello-world.kts", StandardScript::class)
val scriptClass = compileScript("args-hello-world.kts", StandardScriptTemplate::class)
Assert.assertNotNull(scriptClass)
val ctor = scriptClass?.getConstructor(Array<String>::class.java)
Assert.assertNotNull(ctor)
@@ -73,7 +76,7 @@ done
@Test
fun testBndHelloWorld() {
val scriptClass = compileScript("bindings-hello-world.kts", ScriptWithBindings::class)
val scriptClass = compileScript("bindings-hello-world.kts", ScriptTemplateWithBindings::class)
Assert.assertNotNull(scriptClass)
val ctor = scriptClass?.getConstructor(Map::class.java)
Assert.assertNotNull(ctor)
@@ -87,7 +90,7 @@ done
@Test
fun testResolveStdHelloWorld() {
try {
compileScript("args-junit-hello-world.kts", StandardScript::class)
compileScript("args-junit-hello-world.kts", StandardScriptTemplate::class)
Assert.fail("Should throw exception")
}
catch (e: Exception) {
@@ -95,9 +98,9 @@ done
Assert.assertTrue("Expecting message \"$expectedMsg...\"", e.message?.startsWith(expectedMsg) ?: false)
}
val scriptClass = compileScript("args-junit-hello-world.kts", StandardScriptWithAnnotatedResolving::class)
val scriptClass = compileScript("args-junit-hello-world.kts", StandardScriptTemplateWithAnnotatedResolving::class)
if (scriptClass == null) {
val resolver = DefaultKotlinAnnotatedScriptDependenciesResolver()
val resolver = ContextAndAnnotationsBasedResolver()
System.err.println(resolver.baseClassPath)
}
Assert.assertNotNull(scriptClass)