Skip to content

Commit

Permalink
fix resultOf which wasn't working, but need to get classpath isolatio…
Browse files Browse the repository at this point in the history
…n back, had to include the parent classloader because shadowJar plugin isn't doing the right thing.
  • Loading branch information
apatrida committed Feb 3, 2017
1 parent 6ec1e5f commit 161b182
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 9 deletions.
5 changes: 3 additions & 2 deletions build.gradle
Expand Up @@ -45,6 +45,7 @@ configurations {
}

dependencies {
compile project(":jupyter-lib")
compile "uy.kohesive.keplin:keplin-core:$keplinVersion"
compile "uy.kohesive.keplin:keplin-maven-resolver:$keplinVersion"

Expand All @@ -55,8 +56,10 @@ dependencies {

shadow "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
shadow "org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion"
// shadow project(":jupyter-lib")

deploy "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
// deploy project(":jupyter-lib")
}

jar.manifest.attributes(
Expand All @@ -72,9 +75,7 @@ shadowJar {
baseName = 'kotlin-jupyter-kernel'
classifier = ''
mergeServiceFiles()
// configurations = [project.configurations.runtime]
dependencies {
// exclude(dependency("uy.kohesive.keplin:.*:.*"))
exclude(dependency("org.jetbrains.kotlin:.*:.*"))
}
}
Expand Down
5 changes: 5 additions & 0 deletions jupyter-lib/build.gradle
@@ -0,0 +1,5 @@
apply plugin: 'kotlin'

dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion"
}
14 changes: 14 additions & 0 deletions jupyter-lib/src/main/kotlin/jupyter/kotlin/results.kt
@@ -0,0 +1,14 @@
@file:Suppress("unused")

package jupyter.kotlin

abstract class ScriptTemplateWithDisplayHelpers(val args: kotlin.Array<kotlin.String>) {
fun resultOf(vararg mimeToData: Pair<String, Any>): MimeTypedResult = MimeTypedResult(mapOf(*mimeToData))
}

fun mimeResult(vararg mimeToData: Pair<String, Any>): MimeTypedResult = MimeTypedResult(mapOf(*mimeToData))
fun textResult(text: String): Map<String, Any> = MimeTypedResult(mapOf("text/plain" to text))

class MimeTypedResult(mimeData: Map<String, Any>): Map<String, Any> by mimeData


1 change: 1 addition & 0 deletions settings.gradle
@@ -0,0 +1 @@
include 'jupyter-lib'
2 changes: 2 additions & 0 deletions src/main/kotlin/org/jetbrains/kotlin/jupyter/commands.kt
@@ -1,5 +1,7 @@
package org.jetbrains.kotlin.jupyter

import jupyter.kotlin.textResult

enum class ReplCommands(val desc: String) {
help("display help"),
classpath("show current classpath")
Expand Down
15 changes: 12 additions & 3 deletions src/main/kotlin/org/jetbrains/kotlin/jupyter/protocol.kt
@@ -1,6 +1,8 @@
package org.jetbrains.kotlin.jupyter

import com.beust.klaxon.JsonObject
import jupyter.kotlin.MimeTypedResult
import jupyter.kotlin.textResult
import org.jetbrains.kotlin.config.KotlinCompilerVersion
import uy.kohesive.keplin.kotlin.script.EvalResult
import uy.kohesive.keplin.kotlin.script.ReplCompilerException
Expand Down Expand Up @@ -167,11 +169,19 @@ fun JupyterConnection.evalWithIO(body: () -> EvalResult?): ResponseWithMessage {
val stdOut = forkedOut.capturedOutput.toString("UTF-8").emptyWhenNull()
val stdErr = forkedError.capturedOutput.toString("UTF-8").emptyWhenNull()

if (exec.resultValue is Unit) {
if (exec.resultValue == Unit) {
ResponseWithMessage(ResponseState.OkSilent, textResult("OK"), stdOut, stdErr)
} else {
try {
ResponseWithMessage(ResponseState.Ok, textResult(exec.resultValue.toString()), stdOut, stdErr)
println("result is ${exec.resultValue?.javaClass?.name} and is MimeTypedResult = ${exec.resultValue is MimeTypedResult}")
if (exec.resultValue is MimeTypedResult) {
println("response type is TypedResult")
val mimeTypedResponse = (exec.resultValue as MimeTypedResult)
println("data = $mimeTypedResponse")
ResponseWithMessage(ResponseState.Ok, mimeTypedResponse, stdOut, stdErr)
} else {
ResponseWithMessage(ResponseState.Ok, textResult(exec.resultValue.toString()), stdOut, stdErr)
}
} catch (e: Exception) {
ResponseWithMessage(ResponseState.Error, textResult("Error!"), stdOut,
joinLines(stdErr, "error: Unable to convert result to a string: ${e}"))
Expand Down Expand Up @@ -219,5 +229,4 @@ fun JupyterConnection.evalWithIO(body: () -> EvalResult?): ResponseWithMessage {
fun joinLines(vararg parts: String): String = parts.filter(String::isNotBlank).joinToString("\n")
fun String.nullWhenEmpty(): String? = if (this.isBlank()) null else this
fun String?.emptyWhenNull(): String = if (this == null || this.isBlank()) "" else this
fun textResult(text: String): Map<String, Any> = mapOf("text/plain" to text)

11 changes: 7 additions & 4 deletions src/main/kotlin/org/jetbrains/kotlin/jupyter/repl.kt
@@ -1,5 +1,7 @@
package org.jetbrains.kotlin.jupyter

import jupyter.kotlin.MimeTypedResult
import jupyter.kotlin.ScriptTemplateWithDisplayHelpers
import org.jetbrains.kotlin.cli.common.repl.ReplCodeLine
import org.jetbrains.kotlin.cli.common.repl.ScriptArgsWithTypes
import org.jetbrains.kotlin.config.KotlinCompilerVersion
Expand All @@ -9,19 +11,21 @@ import uy.kohesive.keplin.kotlin.script.SimplifiedRepl
import uy.kohesive.keplin.kotlin.script.resolver.AnnotationTriggeredScriptDefinition
import uy.kohesive.keplin.kotlin.script.resolver.JarFileScriptDependenciesResolver
import uy.kohesive.keplin.kotlin.script.resolver.maven.MavenScriptDependenciesResolver
import uy.kohesive.keplin.kotlin.script.util.assertNotEmpty
import uy.kohesive.keplin.kotlin.script.util.findClassJars
import kotlin.reflect.KClass
import kotlin.script.templates.standard.ScriptTemplateWithArgs


class ReplForJupyter(val conn: JupyterConnection) {
private val EMPTY_SCRIPT_ARGS: Array<out Any?> = arrayOf(emptyArray<String>())
private val EMPTY_SCRIPT_ARGS_TYPES: Array<out KClass<out Any>> = arrayOf(Array<String>::class)
private val engine = SimplifiedRepl(scriptDefinition = AnnotationTriggeredScriptDefinition(
"varargTemplateWithMavenResolving",
ScriptTemplateWithArgs::class,
ScriptTemplateWithDisplayHelpers::class,
ScriptArgsWithTypes(EMPTY_SCRIPT_ARGS, EMPTY_SCRIPT_ARGS_TYPES),
listOf(JarFileScriptDependenciesResolver(), MavenScriptDependenciesResolver())),
additionalClasspath = conn.config.classpath
additionalClasspath = conn.config.classpath + findClassJars(MimeTypedResult::class).assertNotEmpty("Must have MimeTypedResult in classpath"),
sharedHostClassLoader = Thread.currentThread().contextClassLoader
)

fun checkComplete(executionNumber: Long, code: String): CheckResult {
Expand All @@ -47,4 +51,3 @@ class ReplForJupyter(val conn: JupyterConnection) {
}
}


0 comments on commit 161b182

Please sign in to comment.