Skip to content

Commit

Permalink
Fix bug that haven't allowed to execute any code (including dependenc…
Browse files Browse the repository at this point in the history
…ies code) via USE(). Add Gradle-like API for adding dependencies.

Fix #367
  • Loading branch information
ileasile committed Aug 25, 2022
1 parent b51aaf6 commit a4df334
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletionResult.Success>()
res.sortedMatches().contains("config4k")
}

@Test
fun testExternalStaticFunctions() {
val res = eval(
Expand Down

0 comments on commit a4df334

Please sign in to comment.