Fix all small and medium-sized issues after review
This commit is contained in:
@@ -103,7 +103,7 @@
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<jvm>${env.JDK_17}/bin/java</jvm>
|
||||
<KOTLIN_JAVA_RUNTIME_JAR>${org.jetbrains.kotlin:kotlin-stdlib:jar}</KOTLIN_JAVA_RUNTIME_JAR>
|
||||
<kotlin.java.runtime.jar>${org.jetbrains.kotlin:kotlin-stdlib:jar}</kotlin.java.runtime.jar>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
<executions>
|
||||
|
||||
+3
-4
@@ -37,7 +37,7 @@ import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngineFactory
|
||||
import javax.script.ScriptException
|
||||
|
||||
class KotlinJsr232JvmLocalScriptEngine(
|
||||
class KotlinJsr223JvmLocalScriptEngine(
|
||||
disposable: Disposable,
|
||||
factory: ScriptEngineFactory,
|
||||
val templateClasspath: List<File>,
|
||||
@@ -68,12 +68,11 @@ class KotlinJsr232JvmLocalScriptEngine(
|
||||
fun resetAndThrowOnErrors() {
|
||||
try {
|
||||
if (hasErrors) {
|
||||
val msg = reports.joinToString("\n") { messageRenderer.render(it.severity, it.message, it.location) }
|
||||
val firstErr = reports.firstOrNull { it.severity.isError }
|
||||
if (firstErr != null)
|
||||
throw ScriptException(msg, firstErr.location.path, firstErr.location.line, firstErr.location.column)
|
||||
throw ScriptException(messageRenderer.render(firstErr.severity, firstErr.message, firstErr.location), firstErr.location.path, firstErr.location.line, firstErr.location.column)
|
||||
else
|
||||
throw ScriptException(msg)
|
||||
throw ScriptException(reports.joinToString("\n") { messageRenderer.render(it.severity, it.message, it.location) })
|
||||
}
|
||||
}
|
||||
finally {
|
||||
+6
-6
@@ -27,10 +27,10 @@ import javax.script.ScriptContext
|
||||
import javax.script.ScriptEngine
|
||||
import kotlin.script.StandardScriptTemplate
|
||||
|
||||
class KotlinJsr232JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
class KotlinJsr223JvmLocalScriptEngineFactory : KotlinJsr223JvmScriptEngineFactoryBase() {
|
||||
|
||||
override fun getScriptEngine(): ScriptEngine =
|
||||
KotlinJsr232JvmLocalScriptEngine(
|
||||
KotlinJsr223JvmLocalScriptEngine(
|
||||
Disposer.newDisposable(),
|
||||
this,
|
||||
listOf(kotlinRuntimeJar),
|
||||
@@ -88,11 +88,11 @@ private fun makeSerializableArgumentsForTemplateWithArgsAndBindings(ctx: ScriptC
|
||||
private fun File.existsOrNull(): File? = existsAndCheckOrNull { true }
|
||||
private inline fun File.existsAndCheckOrNull(check: (File.() -> Boolean)): File? = if (exists() && check()) this else null
|
||||
|
||||
private val kotlinCompilerJar = System.getProperty("KOTLIN_COMPILER_JAR")?.let(::File)?.existsOrNull()
|
||||
private val kotlinCompilerJar = System.getProperty("kotlin.compiler.jar")?.let(::File)?.existsOrNull()
|
||||
?: getPathUtilJar().existsAndCheckOrNull { name == KOTLIN_COMPILER_JAR }
|
||||
?: throw FileNotFoundException("Cannot find kotlin compiler jar, set KOTLIN_COMPILER_JAR property to proper location")
|
||||
?: throw FileNotFoundException("Cannot find kotlin compiler jar, set kotlin.compiler.jar property to proper location")
|
||||
|
||||
private val kotlinRuntimeJar = System.getProperty("KOTLIN_JAVA_RUNTIME_JAR")?.let(::File)?.existsOrNull()
|
||||
private val kotlinRuntimeJar = System.getProperty("kotlin.java.runtime.jar")?.let(::File)?.existsOrNull()
|
||||
?: kotlinCompilerJar.let { File(it.parentFile, KOTLIN_JAVA_RUNTIME_JAR) }.existsOrNull()
|
||||
?: getResourcePathForClass(StandardScriptTemplate::class.java).existsOrNull()
|
||||
?: throw FileNotFoundException("Cannot find kotlin runtime jar, set KOTLIN_JAVA_RUNTIME_JAR property to proper location")
|
||||
?: throw FileNotFoundException("Cannot find kotlin runtime jar, set kotlin.java.runtime.jar property to proper location")
|
||||
+4
@@ -16,10 +16,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.script.util
|
||||
|
||||
// in case of flat or direct resolvers the value should be a direct path or file name of a jar respectively
|
||||
// in case of maven resolver the maven coordinates string is accepted (resolved with com.jcabi.aether library)
|
||||
@Target(AnnotationTarget.FILE, AnnotationTarget.EXPRESSION, AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class DependsOn(val value: String)
|
||||
|
||||
// only flat directory repositories are supported now, so value should be a path to a directory with jars
|
||||
// TODO: support other types of repos
|
||||
@Target(AnnotationTarget.FILE, AnnotationTarget.EXPRESSION, AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class Repository(val value: String)
|
||||
|
||||
+15
-15
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.script.util.resolvers.DirectResolver
|
||||
import org.jetbrains.kotlin.script.util.resolvers.FlatLibDirectoryResolver
|
||||
import org.jetbrains.kotlin.script.util.resolvers.MavenResolver
|
||||
import org.jetbrains.kotlin.script.util.resolvers.Resolver
|
||||
import org.jetbrains.kotlin.utils.addToStdlib.check
|
||||
import java.io.File
|
||||
import java.lang.Exception
|
||||
import java.lang.IllegalArgumentException
|
||||
@@ -34,29 +35,28 @@ open class KotlinAnnotatedScriptDependenciesResolver(val baseClassPath: List<Fil
|
||||
{
|
||||
private val resolvers: MutableList<Resolver> = resolvers.toMutableList()
|
||||
|
||||
inner class ResolvedDependencies(previousDependencies: KotlinScriptExternalDependencies?, depsFromAnnotations: List<File> ) : KotlinScriptExternalDependencies {
|
||||
override val classpath = if (resolvers.isEmpty()) baseClassPath else baseClassPath + depsFromAnnotations
|
||||
override val imports = if (previousDependencies != null) emptyList() else listOf(DependsOn::class.java.`package`.name + ".*")
|
||||
}
|
||||
|
||||
@AcceptedAnnotations(DependsOn::class, Repository::class)
|
||||
override fun resolve(script: ScriptContents,
|
||||
environment: Map<String, Any?>?,
|
||||
report: (ScriptDependenciesResolver.ReportSeverity, String, ScriptContents.Position?) -> Unit,
|
||||
previousDependencies: KotlinScriptExternalDependencies?
|
||||
): Future<KotlinScriptExternalDependencies?>
|
||||
= (if (previousDependencies != null && resolveFromAnnotations(script).isEmpty()) previousDependencies
|
||||
else
|
||||
object : KotlinScriptExternalDependencies {
|
||||
override val classpath: Iterable<File> = if (resolvers.isEmpty()) baseClassPath else baseClassPath + resolveFromAnnotations(script)
|
||||
override val imports: Iterable<String> =
|
||||
previousDependencies?.let { emptyList<String>() } ?: listOf(DependsOn::class.java.`package`.name + ".*")
|
||||
}
|
||||
).asFuture()
|
||||
): Future<KotlinScriptExternalDependencies?> {
|
||||
val depsFromAnnotations: List<File> = resolveFromAnnotations(script)
|
||||
return (if (previousDependencies != null && depsFromAnnotations.isEmpty()) previousDependencies
|
||||
else ResolvedDependencies(previousDependencies, depsFromAnnotations)
|
||||
).asFuture()
|
||||
}
|
||||
|
||||
private fun resolveFromAnnotations(script: ScriptContents): List<File> {
|
||||
script.annotations.forEach {
|
||||
when (it) {
|
||||
is Repository ->
|
||||
when {
|
||||
File(it.value).run { exists() && isDirectory } -> resolvers.add(FlatLibDirectoryResolver(File(it.value)))
|
||||
else -> throw IllegalArgumentException("Illegal argument for Repository annotation: ${it.value}")
|
||||
}
|
||||
is Repository -> File(it.value).check { it.exists() && it.isDirectory }?.let { resolvers.add(FlatLibDirectoryResolver(it)) }
|
||||
?: throw IllegalArgumentException("Illegal argument for Repository annotation: ${it.value}")
|
||||
is DependsOn -> {}
|
||||
is InvalidScriptResolverAnnotation -> throw Exception("Invalid annotation ${it.name}", it.error)
|
||||
else -> throw Exception("Unknown annotation ${it.javaClass}")
|
||||
@@ -83,7 +83,7 @@ private fun classpathFromClassloader(classLoader: ClassLoader): List<File>? =
|
||||
|
||||
private fun classpathFromClasspathProperty(): List<File>? =
|
||||
System.getProperty("java.class.path")?.let {
|
||||
it.split(String.format("\\%s", File.pathSeparatorChar).toRegex()).dropLastWhile(String::isEmpty).toTypedArray()
|
||||
it.split(String.format("\\%s", File.pathSeparatorChar).toRegex()).dropLastWhile(String::isEmpty)
|
||||
.map(::File)
|
||||
}
|
||||
|
||||
|
||||
+10
-10
@@ -104,14 +104,12 @@ done
|
||||
scriptFileName: String,
|
||||
scriptTemplate: KClass<out Any>,
|
||||
environment: Map<String, Any?>? = null,
|
||||
runIsolated: Boolean = true,
|
||||
suppressOutput: Boolean = false): Class<*>? =
|
||||
compileScriptImpl("src/test/resources/scripts/" + scriptFileName, KotlinScriptDefinitionFromAnnotatedTemplate(scriptTemplate, null, null, environment), runIsolated, suppressOutput)
|
||||
compileScriptImpl("src/test/resources/scripts/" + scriptFileName, KotlinScriptDefinitionFromAnnotatedTemplate(scriptTemplate, null, null, environment), suppressOutput)
|
||||
|
||||
private fun compileScriptImpl(
|
||||
scriptPath: String,
|
||||
scriptDefinition: KotlinScriptDefinition,
|
||||
runIsolated: Boolean,
|
||||
suppressOutput: Boolean): Class<*>?
|
||||
{
|
||||
val paths = PathUtil.getKotlinPathsForDistDirectory()
|
||||
@@ -123,7 +121,7 @@ done
|
||||
try {
|
||||
val configuration = CompilerConfiguration().apply {
|
||||
addJvmClasspathRoots(PathUtil.getJdkClassesRoots())
|
||||
val rtJar = System.getProperty("KOTLIN_JAVA_RUNTIME_JAR")
|
||||
val rtJar = System.getProperty("kotlin.java.runtime.jar")
|
||||
Assert.assertNotNull(rtJar)
|
||||
addJvmClasspathRoot(File(rtJar))
|
||||
put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, messageCollector)
|
||||
@@ -141,14 +139,12 @@ done
|
||||
put(CommonConfigurationKeys.MODULE_NAME, "kotlin-script-util-test")
|
||||
add(JVMConfigurationKeys.SCRIPT_DEFINITIONS, scriptDefinition)
|
||||
put(JVMConfigurationKeys.RETAIN_OUTPUT_IN_MEMORY, true)
|
||||
put(JVMConfigurationKeys.INCLUDE_RUNTIME, true)
|
||||
}
|
||||
|
||||
val environment = KotlinCoreEnvironment.createForProduction(rootDisposable, configuration, EnvironmentConfigFiles.JVM_CONFIG_FILES)
|
||||
|
||||
try {
|
||||
return if (runIsolated) KotlinToJVMBytecodeCompiler.compileScript(environment, paths)
|
||||
else KotlinToJVMBytecodeCompiler.compileScript(environment, this.javaClass.classLoader)
|
||||
return KotlinToJVMBytecodeCompiler.compileScript(environment, paths)
|
||||
}
|
||||
catch (e: CompilationException) {
|
||||
messageCollector.report(CompilerMessageSeverity.EXCEPTION, OutputMessageUtil.renderException(e),
|
||||
@@ -183,9 +179,13 @@ done
|
||||
val outStream = ByteArrayOutputStream()
|
||||
val prevOut = System.out
|
||||
System.setOut(PrintStream(outStream))
|
||||
body()
|
||||
System.out.flush()
|
||||
System.setOut(prevOut)
|
||||
try {
|
||||
body()
|
||||
}
|
||||
finally {
|
||||
System.out.flush()
|
||||
System.setOut(prevOut)
|
||||
}
|
||||
return outStream.toString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
println("Hello, world!")
|
||||
|
||||
if (bindings.isNotEmpty()) {
|
||||
println(bindings.joinToString { "${it.key} = ${it.value}" })
|
||||
}
|
||||
|
||||
println("done")
|
||||
@@ -1,8 +0,0 @@
|
||||
|
||||
println("Hello, world!")
|
||||
|
||||
if (args.isNotEmpty()) {
|
||||
println(args.joinToString())
|
||||
}
|
||||
|
||||
println("done")
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
|
||||
println("Hello, world!")
|
||||
|
||||
if (args.isNotEmpty()) {
|
||||
println(args.joinToString())
|
||||
}
|
||||
|
||||
println("done")
|
||||
Reference in New Issue
Block a user