AsyncDependenciesResolver: provide implementation for sync resolve
This commit is contained in:
@@ -11,5 +11,6 @@
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="util" />
|
||||
<orderEntry type="library" name="kotlin-reflect" level="project" />
|
||||
<orderEntry type="library" name="kotlinx-coroutines-core" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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 kotlinx.coroutines.experimental.runBlocking
|
||||
import kotlin.script.dependencies.DependenciesResolver
|
||||
import kotlin.script.dependencies.Environment
|
||||
import kotlin.script.dependencies.ScriptContents
|
||||
import kotlin.script.dependencies.experimental.AsyncDependenciesResolver
|
||||
|
||||
// wraps AsyncDependenciesResolver to provide implementation for synchronous DependenciesResolver::resolve
|
||||
class AsyncDependencyResolverWrapper(private val delegate: AsyncDependenciesResolver): AsyncDependenciesResolver {
|
||||
|
||||
override fun resolve(
|
||||
scriptContents: ScriptContents, environment: Environment
|
||||
): DependenciesResolver.ResolveResult
|
||||
= runBlocking { delegate.resolveAsync(scriptContents, environment) }
|
||||
|
||||
|
||||
suspend override fun resolveAsync(
|
||||
scriptContents: ScriptContents, environment: Environment
|
||||
): DependenciesResolver.ResolveResult
|
||||
= delegate.resolveAsync(scriptContents, environment)
|
||||
}
|
||||
+6
-1
@@ -29,6 +29,7 @@ import kotlin.reflect.KParameter
|
||||
import kotlin.reflect.full.memberFunctions
|
||||
import kotlin.script.dependencies.DependenciesResolver
|
||||
import kotlin.script.dependencies.ScriptDependenciesResolver
|
||||
import kotlin.script.dependencies.experimental.AsyncDependenciesResolver
|
||||
import kotlin.script.templates.AcceptedAnnotations
|
||||
|
||||
open class KotlinScriptDefinitionFromAnnotatedTemplate(
|
||||
@@ -68,7 +69,11 @@ open class KotlinScriptDefinitionFromAnnotatedTemplate(
|
||||
} ?: return null
|
||||
|
||||
val resolver = instantiateResolver(defAnn.resolver)
|
||||
return if (resolver is DependenciesResolver) resolver else resolver?.let(::ApiChangeDependencyResolverWrapper)
|
||||
return when (resolver) {
|
||||
is AsyncDependenciesResolver -> AsyncDependencyResolverWrapper(resolver)
|
||||
is DependenciesResolver -> resolver
|
||||
else -> resolver?.let(::ApiChangeDependencyResolverWrapper)
|
||||
}
|
||||
}
|
||||
|
||||
private fun <T : Any> instantiateResolver(resolverClass: KClass<T>): T? {
|
||||
|
||||
@@ -49,6 +49,7 @@ import java.net.URLClassLoader
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.script.dependencies.*
|
||||
import kotlin.script.dependencies.DependenciesResolver.ResolveResult
|
||||
import kotlin.script.dependencies.experimental.AsyncDependenciesResolver
|
||||
import kotlin.script.templates.AcceptedAnnotations
|
||||
import kotlin.script.templates.ScriptTemplateDefinition
|
||||
import kotlin.script.templates.standard.ScriptTemplateWithArgs
|
||||
@@ -249,6 +250,16 @@ class ScriptTemplateTest {
|
||||
messageCollector.assertHasMessage("debug", desiredSeverity = CompilerMessageSeverity.LOGGING)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAsyncResolver() {
|
||||
val aClass = compileScript("fib.kts", ScriptWithAsyncResolver::class, null)
|
||||
Assert.assertNotNull(aClass)
|
||||
val out = captureOut {
|
||||
aClass!!.getConstructor(Integer.TYPE).newInstance(4)
|
||||
}
|
||||
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testSmokeScriptException() {
|
||||
val aClass = compileScript("smoke_exception.kts", ScriptWithArrayParam::class)
|
||||
@@ -396,6 +407,16 @@ class ErrorReportingResolver : TestKotlinScriptDependenciesResolver() {
|
||||
}
|
||||
}
|
||||
|
||||
class TestAsyncResolver : TestKotlinScriptDependenciesResolver(), AsyncDependenciesResolver {
|
||||
override suspend fun resolveAsync(
|
||||
scriptContents: ScriptContents,
|
||||
environment: Environment
|
||||
): ResolveResult = super<TestKotlinScriptDependenciesResolver>.resolve(scriptContents, environment)
|
||||
|
||||
override fun resolve(scriptContents: ScriptContents, environment: Environment): ResolveResult =
|
||||
super<AsyncDependenciesResolver>.resolve(scriptContents, environment)
|
||||
}
|
||||
|
||||
@ScriptTemplateDefinition(
|
||||
scriptFilePattern =".*\\.kts",
|
||||
resolver = TestKotlinScriptDummyDependenciesResolver::class)
|
||||
@@ -450,6 +471,9 @@ abstract class ScriptWithArray2DParam(val param: Array<Array<in String>>)
|
||||
@ScriptTemplateDefinition(resolver = ErrorReportingResolver::class)
|
||||
abstract class ScriptReportingErrors(val num: Int)
|
||||
|
||||
@ScriptTemplateDefinition(resolver = TestAsyncResolver::class)
|
||||
abstract class ScriptWithAsyncResolver(val num: Int)
|
||||
|
||||
@Target(AnnotationTarget.FILE)
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
annotation class DependsOn(val path: String)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
val j: Int = i
|
||||
|
||||
val s: String = str
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package custom.scriptDefinition
|
||||
|
||||
import kotlin.script.dependencies.*
|
||||
import kotlin.script.dependencies.experimental.*
|
||||
import kotlin.script.templates.*
|
||||
import java.io.File
|
||||
|
||||
class TestDependenciesResolver : AsyncDependenciesResolver {
|
||||
suspend override fun resolveAsync(scriptContents: ScriptContents, environment: Environment): DependenciesResolver.ResolveResult {
|
||||
return ScriptDependencies(
|
||||
classpath = listOf(environment["template-classes"] as File)
|
||||
).asSuccess()
|
||||
}
|
||||
}
|
||||
|
||||
@ScriptTemplateDefinition(TestDependenciesResolver::class, scriptFilePattern = "script.kts")
|
||||
class Template : Base()
|
||||
|
||||
open class Base {
|
||||
val i = 3
|
||||
val str = ""
|
||||
}
|
||||
+6
@@ -42,6 +42,12 @@ public class ScriptConfigurationHighlightingTestGenerated extends AbstractScript
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/script/definition/highlighting"), Pattern.compile("^([^\\.]+)$"), TargetBackend.ANY, false);
|
||||
}
|
||||
|
||||
@TestMetadata("asyncResolver")
|
||||
public void testAsyncResolver() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/script/definition/highlighting/asyncResolver/");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("customBaseClass")
|
||||
public void testCustomBaseClass() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/script/definition/highlighting/customBaseClass/");
|
||||
|
||||
Reference in New Issue
Block a user