From 1c412b406867f87c1a7fba34ee5ea82525a76a7f Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Thu, 13 Jul 2017 18:04:19 +0300 Subject: [PATCH] AsyncDependenciesResolver: provide implementation for sync resolve --- compiler/frontend.script/frontend.script.iml | 1 + .../script/AsyncDependencyResolverWrapper.kt | 38 +++++++++++++++++++ ...inScriptDefinitionFromAnnotatedTemplate.kt | 7 +++- .../kotlin/scripts/ScriptTemplateTest.kt | 24 ++++++++++++ .../highlighting/asyncResolver/script.kts | 3 ++ .../asyncResolver/template/template.kt | 22 +++++++++++ ...onfigurationHighlightingTestGenerated.java | 6 +++ 7 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 compiler/frontend.script/src/org/jetbrains/kotlin/script/AsyncDependencyResolverWrapper.kt create mode 100644 idea/testData/script/definition/highlighting/asyncResolver/script.kts create mode 100644 idea/testData/script/definition/highlighting/asyncResolver/template/template.kt diff --git a/compiler/frontend.script/frontend.script.iml b/compiler/frontend.script/frontend.script.iml index cced36e0e4e..ed154038a0b 100644 --- a/compiler/frontend.script/frontend.script.iml +++ b/compiler/frontend.script/frontend.script.iml @@ -11,5 +11,6 @@ + \ No newline at end of file diff --git a/compiler/frontend.script/src/org/jetbrains/kotlin/script/AsyncDependencyResolverWrapper.kt b/compiler/frontend.script/src/org/jetbrains/kotlin/script/AsyncDependencyResolverWrapper.kt new file mode 100644 index 00000000000..9cec19a0999 --- /dev/null +++ b/compiler/frontend.script/src/org/jetbrains/kotlin/script/AsyncDependencyResolverWrapper.kt @@ -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) +} \ No newline at end of file diff --git a/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt b/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt index 0c952f469ce..4fb94e17d0a 100644 --- a/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt +++ b/compiler/frontend.script/src/org/jetbrains/kotlin/script/KotlinScriptDefinitionFromAnnotatedTemplate.kt @@ -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 instantiateResolver(resolverClass: KClass): T? { diff --git a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt index cb84ff034f2..27bf01506c6 100644 --- a/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/scripts/ScriptTemplateTest.kt @@ -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.resolve(scriptContents, environment) + + override fun resolve(scriptContents: ScriptContents, environment: Environment): ResolveResult = + super.resolve(scriptContents, environment) +} + @ScriptTemplateDefinition( scriptFilePattern =".*\\.kts", resolver = TestKotlinScriptDummyDependenciesResolver::class) @@ -450,6 +471,9 @@ abstract class ScriptWithArray2DParam(val param: Array>) @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) diff --git a/idea/testData/script/definition/highlighting/asyncResolver/script.kts b/idea/testData/script/definition/highlighting/asyncResolver/script.kts new file mode 100644 index 00000000000..328e15eb5f1 --- /dev/null +++ b/idea/testData/script/definition/highlighting/asyncResolver/script.kts @@ -0,0 +1,3 @@ +val j: Int = i + +val s: String = str \ No newline at end of file diff --git a/idea/testData/script/definition/highlighting/asyncResolver/template/template.kt b/idea/testData/script/definition/highlighting/asyncResolver/template/template.kt new file mode 100644 index 00000000000..5b550240234 --- /dev/null +++ b/idea/testData/script/definition/highlighting/asyncResolver/template/template.kt @@ -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 = "" +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/script/ScriptConfigurationHighlightingTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/script/ScriptConfigurationHighlightingTestGenerated.java index baa5040b6f3..6d1e38bd5c0 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/script/ScriptConfigurationHighlightingTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/script/ScriptConfigurationHighlightingTestGenerated.java @@ -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/");