Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gradle-like API for adding dependencies #382

Merged
merged 1 commit into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we try to add library immediately, recursive execution happens - execution DURING another execution. We don't know how to handle it, because we use previous script instances for evaluation, and it's not clear what to do without them. So we just schedule the execution to the end of the current execution.


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