Skip to content

Commit 9dec2e5

Browse files
committedJul 28, 2020
Refactor libraries handling, add handling of descriptors
from different sources. Fixes #70 Also, get rid of several singletons in config and replaced them with factory and free functions.
1 parent 78d6078 commit 9dec2e5

29 files changed

+981
-394
lines changed
 

‎README.md

+20
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ The following line magics are supported:
100100
- `%use <lib1>, <lib2> ...` - injects code for supported libraries: artifact resolution, default imports, initialization code, type renderers
101101
- `%trackClasspath` - logs any changes of current classpath. Useful for debugging artifact resolution failures
102102
- `%trackExecution` - logs pieces of code that are going to be executed. Useful for debugging of libraries support
103+
- `%useLatestDescriptors` - use latest versions of library descriptors available. By default, bundled descriptors are used
103104
- `%output [options]` - output capturing settings.
104105

105106
See detailed info about line magics [here](doc/magics.md).
@@ -128,6 +129,25 @@ Several libraries can be included in single `%use` statement, separated by `,`:
128129
```
129130
%use lets-plot, krangl, mysql(8.0.15)
130131
```
132+
You can also specify the source of library descriptor. By default, it's downloaded from the latest commit on the
133+
branch which kernel was built from. If you want to try descriptor from another revision, use the following syntax:
134+
```
135+
// Specify tag
136+
%use lets-plot@0.8.2.5
137+
// Specify commit sha, with more verbose syntax
138+
%use lets-plot@ref[24a040fe22335648885b106e2f4ddd63b4d49469]
139+
// Specify git ref along with library arguments
140+
%use krangl@dev(0.10)
141+
```
142+
Other options are resolving library descriptor from a local file or from remote URL:
143+
```
144+
// Load library from file
145+
%use mylib@file[/home/user/lib.json]
146+
// Load library descriptor from a remote URL
147+
%use herlib@url[https://site.com/lib.json]
148+
// You may omit library name for file and URL resolution:
149+
%use @file[lib.json]
150+
```
131151

132152
List of supported libraries:
133153
- [klaxon](https://github.com/cbeust/klaxon) - JSON parser for Kotlin

‎src/main/kotlin/org/jetbrains/kotlin/jupyter/annotationsProcessor.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jetbrains.kotlin.jupyter
22

33
import jupyter.kotlin.KotlinFunctionInfo
4-
import org.jetbrains.kotlin.jupyter.repl.reflect.ContextUpdater
4+
import org.jetbrains.kotlin.jupyter.repl.ContextUpdater
55

66
interface AnnotationsProcessor {
77

@@ -57,4 +57,4 @@ class AnnotationsProcessorImpl(private val contextUpdater: ContextUpdater) : Ann
5757
}
5858
return codeToExecute
5959
}
60-
}
60+
}

‎src/main/kotlin/org/jetbrains/kotlin/jupyter/commands.kt

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package org.jetbrains.kotlin.jupyter
22

33
import jupyter.kotlin.textResult
4-
import org.jetbrains.kotlin.jupyter.repl.completion.CompletionResult
5-
import org.jetbrains.kotlin.jupyter.repl.completion.KotlinCompleter
6-
import org.jetbrains.kotlin.jupyter.repl.completion.ListErrorsResult
7-
import org.jetbrains.kotlin.jupyter.repl.completion.SourceCodeImpl
4+
import org.jetbrains.kotlin.jupyter.libraries.parseLibraryDescriptor
5+
import org.jetbrains.kotlin.jupyter.repl.CompletionResult
6+
import org.jetbrains.kotlin.jupyter.repl.KotlinCompleter
7+
import org.jetbrains.kotlin.jupyter.repl.ListErrorsResult
8+
import org.jetbrains.kotlin.jupyter.repl.SourceCodeImpl
89
import kotlin.script.experimental.api.ScriptDiagnostic
910
import kotlin.script.experimental.api.SourceCode
1011
import kotlin.script.experimental.api.SourceCodeCompletionVariant
@@ -51,12 +52,12 @@ fun doCommandCompletion(code: String, cursor: Int): CompletionResult {
5152
return KotlinCompleter.getResult(code, cursor, completions)
5253
}
5354

54-
fun runCommand(code: String, repl: ReplForJupyter?): Response {
55+
fun runCommand(code: String, repl: ReplForJupyter): Response {
5556
val args = code.trim().substring(1).split(" ")
5657
val cmd = getCommand(args[0]) ?: return AbortResponseWithMessage(textResult("Failed!"), "unknown command: $code\nto see available commands, enter :help")
5758
return when (cmd) {
5859
ReplCommands.classpath -> {
59-
val cp = repl!!.currentClasspath
60+
val cp = repl.currentClasspath
6061
OkResponseWithMessage(textResult("Current classpath (${cp.count()} paths):\n${cp.joinToString("\n")}"))
6162
}
6263
ReplCommands.help -> {
@@ -66,9 +67,16 @@ fun runCommand(code: String, repl: ReplForJupyter?): Response {
6667
if (it.argumentsUsage != null) s += "\n Usage: %${it.name} ${it.argumentsUsage}"
6768
s
6869
}
69-
val libraries = repl?.resolverConfig?.libraries?.awaitBlocking()?.toList()?.joinToStringIndented {
70-
"${it.first} ${it.second.link ?: ""}"
71-
}
70+
val libraryFiles =
71+
repl.homeDir?.resolve(LibrariesDir)?.listFiles { file -> file.isFile && file.name.endsWith(".$LibraryDescriptorExt") } ?: emptyArray()
72+
val libraries = libraryFiles.toList().mapNotNull { file ->
73+
val libraryName = file.nameWithoutExtension
74+
log.info("Parsing descriptor for library '$libraryName'")
75+
val descriptor = log.catchAll("Parsing descriptor for library '$libraryName' failed") {
76+
parseLibraryDescriptor(file.readText())
77+
}
78+
if (descriptor != null) "$libraryName ${descriptor.link ?: ""}" else null
79+
}.joinToStringIndented()
7280
OkResponseWithMessage(textResult("Commands:\n$commands\n\nMagics\n$magics\n\nSupported libraries:\n$libraries"))
7381
}
7482
}

0 commit comments

Comments
 (0)