Skip to content

Commit a4df334

Browse files
committed
Fix bug that haven't allowed to execute any code (including dependencies code) via USE(). Add Gradle-like API for adding dependencies.
Fix #367
1 parent b51aaf6 commit a4df334

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.jetbrains.kotlinx.jupyter.api.libraries
2+
3+
import java.io.File
4+
5+
interface RepositoryHandlerScope {
6+
fun maven(url: String)
7+
fun dir(dir: File)
8+
}
9+
10+
interface DependencyHandlerScope {
11+
fun implementation(coordinates: String)
12+
fun implementation(group: String, artifact: String, version: String)
13+
}
14+
15+
fun RepositoryHandlerScope.mavenCentral() = maven("https://repo.maven.apache.org/maven2/")
16+
fun RepositoryHandlerScope.google() = maven("https://dl.google.com/dl/android/maven2/")
17+
fun RepositoryHandlerScope.mavenLocal() = maven("*mavenLocal")
18+
19+
fun JupyterIntegration.Builder.repositories(action: RepositoryHandlerScope.() -> Unit) {
20+
RepositoryHandlerScopeImpl(this).action()
21+
}
22+
23+
fun JupyterIntegration.Builder.dependencies(action: DependencyHandlerScope.() -> Unit) {
24+
DependencyHandlerScopeImpl(this).action()
25+
}
26+
27+
private class RepositoryHandlerScopeImpl(private val builder: JupyterIntegration.Builder) : RepositoryHandlerScope {
28+
override fun dir(dir: File) {
29+
builder.repositories(dir.absolutePath)
30+
}
31+
32+
override fun maven(url: String) {
33+
builder.repositories(url)
34+
}
35+
}
36+
37+
private class DependencyHandlerScopeImpl(private val builder: JupyterIntegration.Builder) : DependencyHandlerScope {
38+
override fun implementation(coordinates: String) {
39+
builder.dependencies(coordinates)
40+
}
41+
42+
override fun implementation(group: String, artifact: String, version: String) {
43+
implementation("$group:$artifact:$version")
44+
}
45+
}

jupyter-lib/lib/src/main/kotlin/jupyter/kotlin/ScriptTemplateWithDisplayHelpers.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ abstract class ScriptTemplateWithDisplayHelpers(
2020

2121
fun EXECUTE(code: String) = host.scheduleExecution(CodeExecution(code).toExecutionCallback())
2222

23-
fun USE(library: LibraryDefinition) = host.addLibrary(library)
23+
fun USE(library: LibraryDefinition) = host.scheduleExecution { addLibrary(library) }
2424

2525
fun USE(builder: JupyterIntegration.Builder.() -> Unit) {
2626
val o = object : JupyterIntegration() {

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/repl/ReplTests.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,26 @@ class ReplTests : AbstractSingleReplTest() {
140140
res.sortedMatches().contains("doyaaaaaken")
141141
}
142142

143+
@Test
144+
fun testDependencyConfigurationAnnotationCompletion() {
145+
eval(
146+
"""
147+
USE {
148+
repositories {
149+
mavenCentral()
150+
}
151+
dependencies {
152+
implementation("io.github.config4k:config4k:0.4.2")
153+
}
154+
}
155+
""".trimIndent()
156+
)
157+
158+
val res = repl.completeBlocking("import io.github.", 17)
159+
res.shouldBeInstanceOf<CompletionResult.Success>()
160+
res.sortedMatches().contains("config4k")
161+
}
162+
143163
@Test
144164
fun testExternalStaticFunctions() {
145165
val res = eval(

0 commit comments

Comments
 (0)