Refactor script dependencies management
This commit is contained in:
committed by
Pavel V. Talanov
parent
c244414f3c
commit
082290f8e3
@@ -26,11 +26,9 @@ import org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler
|
||||
import org.jetbrains.kotlin.cli.jvm.config.addJvmClasspathRoot
|
||||
import org.jetbrains.kotlin.codegen.CompilationException
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.addKotlinSourceRoot
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.script.KotlinScriptDefinition
|
||||
import org.jetbrains.kotlin.script.KotlinScriptExtraImport
|
||||
import org.jetbrains.kotlin.script.ScriptParameter
|
||||
import org.jetbrains.kotlin.script.StandardScriptDefinition
|
||||
import org.jetbrains.kotlin.test.ConfigurationKind
|
||||
@@ -45,14 +43,14 @@ import java.lang.reflect.InvocationTargetException
|
||||
class ScriptTest {
|
||||
@Test
|
||||
fun testScriptWithParam() {
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()), null)
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testStandardScriptWithParams() {
|
||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition, null)
|
||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
||||
Assert.assertNotNull(aClass)
|
||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4", "comment"))
|
||||
Assert.assertNotNull(anObj)
|
||||
@@ -60,7 +58,7 @@ class ScriptTest {
|
||||
|
||||
@Test
|
||||
fun testStandardScriptWithoutParams() {
|
||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition, null)
|
||||
val aClass = compileScript("fib_std.kts", StandardScriptDefinition)
|
||||
Assert.assertNotNull(aClass)
|
||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, emptyList())
|
||||
Assert.assertNotNull(anObj)
|
||||
@@ -68,7 +66,7 @@ class ScriptTest {
|
||||
|
||||
@Test
|
||||
fun testScriptWithParamConversion() {
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()), null)
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
|
||||
Assert.assertNotNull(aClass)
|
||||
val anObj = KotlinToJVMBytecodeCompiler.tryConstructClassPub(aClass!!, listOf("4"))
|
||||
Assert.assertNotNull(anObj)
|
||||
@@ -76,67 +74,67 @@ class ScriptTest {
|
||||
|
||||
@Test
|
||||
fun testScriptWithPackage() {
|
||||
val aClass = compileScript("fib.pkg.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()), null)
|
||||
val aClass = compileScript("fib.pkg.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithScriptDefinition() {
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()), null)
|
||||
val aClass = compileScript("fib.kts", SimpleParamsTestScriptDefinition(".kts", numIntParam()))
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithClassParameter() {
|
||||
val aClass = compileScript("fib_cp.kts", ReflectedParamClassTestScriptDefinition(".kts", "param", TestParamClass::class), null, runIsolated = false)
|
||||
val aClass = compileScript("fib_cp.kts", ReflectedParamClassTestScriptDefinition(".kts", "param", TestParamClass::class), runIsolated = false)
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(TestParamClass::class.java).newInstance(TestParamClass(4))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithBaseClass() {
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLClass::class), null, runIsolated = false)
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLClass::class), runIsolated = false)
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithBaseClassWithParam() {
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassWithParamsTestScriptDefinition(".kts", numIntParam() + numIntParam("passthrough"), TestDSLClassWithParam::class, numIntParam("passthrough")), null, runIsolated = false)
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassWithParamsTestScriptDefinition(".kts", numIntParam() + numIntParam("passthrough"), TestDSLClassWithParam::class, numIntParam("passthrough")), runIsolated = false)
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE, Integer.TYPE).newInstance(4, 1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithInterface() {
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLInterface::class), null, runIsolated = false)
|
||||
val aClass = compileScript("fib_dsl.kts", ReflectedSuperclassTestScriptDefinition(".kts", numIntParam(), TestDSLInterface::class), runIsolated = false)
|
||||
Assert.assertNotNull(aClass)
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithClasspath() {
|
||||
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), null, runIsolated = true, suppressOutput = true)
|
||||
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), runIsolated = true, suppressOutput = true)
|
||||
Assert.assertNull(aClass1)
|
||||
|
||||
val cp = classpathFromClassloader(ScriptTest::class.java.classLoader).filter { it.contains("kotlin-runtime") || it.contains("junit") }
|
||||
Assert.assertFalse(cp.isEmpty())
|
||||
|
||||
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = cp), null, runIsolated = true)
|
||||
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = cp), runIsolated = true)
|
||||
Assert.assertNotNull(aClass2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testScriptWithExtraImports() {
|
||||
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), extraImport = null, runIsolated = true, suppressOutput = true)
|
||||
val aClass1 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), runIsolated = true, suppressOutput = true)
|
||||
Assert.assertNull(aClass1)
|
||||
|
||||
val cp = classpathFromClassloader(ScriptTest::class.java.classLoader).filter { it.contains("kotlin-runtime") || it.contains("junit") }
|
||||
Assert.assertFalse(cp.isEmpty())
|
||||
|
||||
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = emptyList()), extraImport = SimpleScriptExtraImport(cp), runIsolated = true)
|
||||
val aClass2 = compileScript("fib_ext.kts", SimpleParamsWithClasspathTestScriptDefinition(".kts", numIntParam(), classpath = cp, extraDependencies = SimpleScriptExtraDependencies(cp)), runIsolated = true)
|
||||
Assert.assertNotNull(aClass2)
|
||||
}
|
||||
|
||||
@@ -163,22 +161,20 @@ class ScriptTest {
|
||||
private fun compileScript(
|
||||
scriptPath: String,
|
||||
scriptDefinition: KotlinScriptDefinition,
|
||||
extraImport: KotlinScriptExtraImport?,
|
||||
runIsolated: Boolean = true,
|
||||
suppressOutput: Boolean = false): Class<*>? =
|
||||
compileScriptImpl("compiler/testData/script/" + scriptPath, scriptDefinition, extraImport, runIsolated, suppressOutput)
|
||||
compileScriptImpl("compiler/testData/script/" + scriptPath, scriptDefinition, runIsolated, suppressOutput)
|
||||
|
||||
private fun compileSmokeTestScript(
|
||||
scriptPath: String,
|
||||
scriptDefinition: KotlinScriptDefinition,
|
||||
runIsolated: Boolean = true,
|
||||
suppressOutput: Boolean = false): Class<*>? =
|
||||
compileScriptImpl("compiler/testData/integration/smoke/" + scriptPath, scriptDefinition, null, runIsolated, suppressOutput)
|
||||
compileScriptImpl("compiler/testData/integration/smoke/" + scriptPath, scriptDefinition, runIsolated, suppressOutput)
|
||||
|
||||
private fun compileScriptImpl(
|
||||
scriptPath: String,
|
||||
scriptDefinition: KotlinScriptDefinition,
|
||||
extraImport: KotlinScriptExtraImport?,
|
||||
runIsolated: Boolean,
|
||||
suppressOutput: Boolean): Class<*>?
|
||||
{
|
||||
@@ -193,10 +189,6 @@ class ScriptTest {
|
||||
configuration.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
||||
configuration.addKotlinSourceRoot(scriptPath)
|
||||
configuration.add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, scriptDefinition)
|
||||
extraImport?.let {
|
||||
configuration.put(JVMConfigurationKeys.SCRIPTS_EXTRA_IMPORTS, scriptPath, it)
|
||||
}
|
||||
scriptDefinition.getScriptDependenciesClasspath().forEach { configuration.addJvmClasspathRoot(File(it)) }
|
||||
|
||||
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
|
||||
private val kotlinPaths by lazy { PathUtil.getKotlinPathsForCompiler() }
|
||||
|
||||
override fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): ScriptDependencies? {
|
||||
override fun invoke(annotations: Iterable<KtAnnotationEntry>, context: Any?): KotlinScriptExternalDependencies? {
|
||||
if (annotations.none()) return null
|
||||
val anns = annotations.map { parseAnnotation(it) }.filter { it.name == depends::class.simpleName }
|
||||
val cp = anns.flatMap {
|
||||
@@ -129,9 +129,8 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
}
|
||||
}
|
||||
}
|
||||
return object : ScriptDependencies {
|
||||
return object : KotlinScriptExternalDependencies {
|
||||
override val classpath = cp
|
||||
override val implicitImports = emptyList<String>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,30 +140,23 @@ class GetTestKotlinScriptDependencies : GetScriptDependencies {
|
||||
?.filter { it.contains("out/test") }
|
||||
?: emptyList()
|
||||
|
||||
override fun invoke(context: Any?): ScriptDependencies? {
|
||||
return object : ScriptDependencies {
|
||||
override fun invoke(context: Any?): KotlinScriptExternalDependencies? {
|
||||
return object : KotlinScriptExternalDependencies {
|
||||
override val classpath = classpathFromClassloader()
|
||||
override val implicitImports = emptyList<String>()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun parseAnnotation(ann: KtAnnotationEntry): SimpleUntypedAst.Node.list<Any> {
|
||||
val wann = SimpleUntypedAst.KtAnnotationWrapper(ann)
|
||||
val vals = wann.valueArguments
|
||||
return SimpleUntypedAst.Node.list(wann.name, vals)
|
||||
}
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyExtractor(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
abstract class ScriptWithIntParam(num: Int)
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyExtractor(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
abstract class ScriptWithClassParam(param: TestParamClass)
|
||||
|
||||
@ScriptFilePattern(".*\\.kts")
|
||||
@ScriptDependencyExtractor(GetTestKotlinScriptDependencies::class)
|
||||
@ScriptDependencyResolver(GetTestKotlinScriptDependencies::class)
|
||||
abstract class ScriptWithBaseClass(num: Int, passthrough: Int) : TestDSLClassWithParam(passthrough)
|
||||
|
||||
@Target(AnnotationTarget.FILE)
|
||||
|
||||
Reference in New Issue
Block a user