From a4df334222397f5a33eff19ba195c493de50246a Mon Sep 17 00:00:00 2001 From: Ilya Muradyan Date: Wed, 17 Aug 2022 20:25:11 +0200 Subject: [PATCH] Fix bug that haven't allowed to execute any code (including dependencies code) via USE(). Add Gradle-like API for adding dependencies. Fix #367 --- .../jupyter/api/libraries/gradleLike.kt | 45 +++++++++++++++++++ .../ScriptTemplateWithDisplayHelpers.kt | 2 +- .../kotlinx/jupyter/test/repl/ReplTests.kt | 20 +++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/libraries/gradleLike.kt diff --git a/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/libraries/gradleLike.kt b/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/libraries/gradleLike.kt new file mode 100644 index 000000000..59dfd3977 --- /dev/null +++ b/jupyter-lib/api/src/main/kotlin/org/jetbrains/kotlinx/jupyter/api/libraries/gradleLike.kt @@ -0,0 +1,45 @@ +package org.jetbrains.kotlinx.jupyter.api.libraries + +import java.io.File + +interface RepositoryHandlerScope { + fun maven(url: String) + fun dir(dir: File) +} + +interface DependencyHandlerScope { + fun implementation(coordinates: String) + fun implementation(group: String, artifact: String, version: String) +} + +fun RepositoryHandlerScope.mavenCentral() = maven("https://repo.maven.apache.org/maven2/") +fun RepositoryHandlerScope.google() = maven("https://dl.google.com/dl/android/maven2/") +fun RepositoryHandlerScope.mavenLocal() = maven("*mavenLocal") + +fun JupyterIntegration.Builder.repositories(action: RepositoryHandlerScope.() -> Unit) { + RepositoryHandlerScopeImpl(this).action() +} + +fun JupyterIntegration.Builder.dependencies(action: DependencyHandlerScope.() -> Unit) { + DependencyHandlerScopeImpl(this).action() +} + +private class RepositoryHandlerScopeImpl(private val builder: JupyterIntegration.Builder) : RepositoryHandlerScope { + override fun dir(dir: File) { + builder.repositories(dir.absolutePath) + } + + override fun maven(url: String) { + builder.repositories(url) + } +} + +private class DependencyHandlerScopeImpl(private val builder: JupyterIntegration.Builder) : DependencyHandlerScope { + override fun implementation(coordinates: String) { + builder.dependencies(coordinates) + } + + override fun implementation(group: String, artifact: String, version: String) { + implementation("$group:$artifact:$version") + } +} diff --git a/jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt b/jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt index 87b1f6b80..14ce15fd3 100644 --- a/jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt +++ b/jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt @@ -20,7 +20,7 @@ abstract class ScriptTemplateWithDisplayHelpers( fun EXECUTE(code: String) = host.scheduleExecution(CodeExecution(code).toExecutionCallback()) - fun USE(library: LibraryDefinition) = host.addLibrary(library) + fun USE(library: LibraryDefinition) = host.scheduleExecution { addLibrary(library) } fun USE(builder: JupyterIntegration.Builder.() -> Unit) { val o = object : JupyterIntegration() { diff --git a/src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt b/src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt index 0035ad7ed..f70fb563d 100644 --- a/src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt +++ b/src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt @@ -140,6 +140,26 @@ class ReplTests : AbstractSingleReplTest() { res.sortedMatches().contains("doyaaaaaken") } + @Test + fun testDependencyConfigurationAnnotationCompletion() { + eval( + """ + USE { + repositories { + mavenCentral() + } + dependencies { + implementation("io.github.config4k:config4k:0.4.2") + } + } + """.trimIndent() + ) + + val res = repl.completeBlocking("import io.github.", 17) + res.shouldBeInstanceOf() + res.sortedMatches().contains("config4k") + } + @Test fun testExternalStaticFunctions() { val res = eval(