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 17, 2022
1 parent 88d0bfb commit 76273f4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.jetbrains.kotlinx.jupyter.api.VariableDeclarationCallback
import org.jetbrains.kotlinx.jupyter.api.VariableUpdateCallback
import org.jetbrains.kotlinx.jupyter.util.AcceptanceRule
import org.jetbrains.kotlinx.jupyter.util.NameAcceptanceRule
import java.io.File
import kotlin.reflect.KMutableProperty

/**
Expand Down Expand Up @@ -234,6 +235,45 @@ abstract class JupyterIntegration : LibraryDefinitionProducer {
interruptionCallbacks.add(action)
}

private val gradleLikeConfigurator = GradleLikeConfigurator()
fun gradle(configure: DependenciesConfigurator.() -> Unit) {
gradleLikeConfigurator.configure()
}

inner class GradleLikeConfigurator : DependenciesConfigurator {
private val repositoryConfigurator = RepositoryHandlerScopeImpl()

override fun repositories(action: RepositoryHandlerScope.() -> Unit) {
repositoryConfigurator.action()
}

inner class RepositoryHandlerScopeImpl : RepositoryHandlerScope {
override fun dir(dir: File) {
repositories.add(dir.absolutePath)
}

override fun maven(url: String) {
repositories.add(url)
}
}

private val dependenciesConfigurator = DependencyHandlerScopeImpl()

override fun dependencies(action: DependencyHandlerScope.() -> Unit) {
dependenciesConfigurator.action()
}

inner class DependencyHandlerScopeImpl : DependencyHandlerScope {
override fun implementation(coordinates: String) {
dependencies.add(coordinates)
}

override fun implementation(group: String, artifact: String, version: String) {
implementation("$group:$artifact:$version")
}
}
}

internal fun getDefinition() =
libraryDefinition {
it.init = init
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.jetbrains.kotlinx.jupyter.api.libraries

import java.io.File

interface DependenciesConfigurator {
fun dependencies(action: DependencyHandlerScope.() -> Unit)
fun repositories(action: RepositoryHandlerScope.() -> Unit)
}

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")
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,28 @@ class ReplTests : AbstractSingleReplTest() {
res.sortedMatches().contains("doyaaaaaken")
}

@Test
fun testDependencyConfigurationAnnotationCompletion() {
eval(
"""
USE {
gradle {
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 76273f4

Please sign in to comment.