-
Notifications
You must be signed in to change notification settings - Fork 113
Descriptors in libraries.json #488
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
Changes from 2 commits
e71460b
a3a7fed
b8c9f41
2d40bc2
42ed64d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,11 +15,13 @@ class KotlinJupyterPluginExtension( | |
|
||
private val libraryProducers: MutableSet<FQNAware> = mutableSetOf() | ||
private val libraryDefinitions: MutableSet<FQNAware> = mutableSetOf() | ||
private val libraryDescriptors: MutableSet<String> = mutableSetOf() | ||
|
||
internal val libraryFqns get() = | ||
LibrariesScanResult( | ||
definitions = libraryDefinitions, | ||
producers = libraryProducers, | ||
descriptors = libraryDescriptors, | ||
) | ||
|
||
internal fun addDependenciesIfNeeded() { | ||
|
@@ -42,6 +44,7 @@ class KotlinJupyterPluginExtension( | |
/** | ||
* Add adding library integrations by specifying their fully qualified names | ||
*/ | ||
@Suppress("unused") | ||
fun integrations(action: IntegrationsSpec.() -> Unit) { | ||
IntegrationsSpec().apply(action) | ||
} | ||
|
@@ -56,6 +59,11 @@ class KotlinJupyterPluginExtension( | |
fun definition(className: String) { | ||
libraryDefinitions.add(FQNAware(className)) | ||
} | ||
|
||
@Suppress("unused") | ||
fun descriptor(descriptor: String) { | ||
|
||
libraryDescriptors.add(descriptor) | ||
} | ||
} | ||
|
||
companion object { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,13 @@ open class JupyterApiResourcesTask : DefaultTask() { | |
@Input | ||
var libraryDefinitions: List<String> = emptyList() | ||
|
||
/** | ||
* List of JSON library descriptors in the | ||
* [library descriptor format](https://github.com/Kotlin/kotlin-jupyter/blob/master/docs/libraries.md#creating-a-library-descriptor). | ||
*/ | ||
@Input | ||
var libraryDescriptors: List<String> = emptyList() | ||
|
||
@OutputDirectory | ||
val outputDir: File = project.getBuildDirectory().resolve("jupyterProcessedResources") | ||
|
||
|
@@ -42,6 +49,7 @@ open class JupyterApiResourcesTask : DefaultTask() { | |
LibrariesScanResult( | ||
definitions = libraryDefinitions.map { FQNAware(it) }.toSet(), | ||
producers = libraryProducers.map { FQNAware(it) }.toSet(), | ||
descriptors = libraryDescriptors.toSet(), | ||
) | ||
|
||
val resultObject = | ||
|
@@ -70,14 +78,16 @@ open class JupyterApiResourcesTask : DefaultTask() { | |
} | ||
|
||
return LibrariesScanResult( | ||
fqns("definitions"), | ||
fqns("producers"), | ||
definitions = fqns("definitions"), | ||
|
||
producers = fqns("producers"), | ||
descriptors = emptySet(), | ||
) | ||
} | ||
|
||
operator fun LibrariesScanResult.plus(other: LibrariesScanResult): LibrariesScanResult = | ||
LibrariesScanResult( | ||
definitions + other.definitions, | ||
producers + other.producers, | ||
definitions = definitions + other.definitions, | ||
producers = producers + other.producers, | ||
descriptors = descriptors + other.descriptors, | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.jetbrains.kotlinx.jupyter.libraries | ||
|
||
import kotlinx.serialization.json.Json | ||
import kotlinx.serialization.json.JsonObject | ||
import org.jetbrains.kotlinx.jupyter.api.KotlinKernelHost | ||
import org.jetbrains.kotlinx.jupyter.api.LibraryLoader | ||
import org.jetbrains.kotlinx.jupyter.api.Notebook | ||
|
@@ -27,6 +28,7 @@ class LibrariesScanner( | |
|
||
private val processedFQNs = mutableSetOf<TypeName>() | ||
private val discardedFQNs = mutableSetOf<TypeName>() | ||
private val processedDescriptors = mutableSetOf<JsonObject>() | ||
|
||
private fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNamesToLoad( | ||
host: KotlinKernelHost, | ||
|
@@ -46,10 +48,14 @@ class LibrariesScanner( | |
discardedFQNs.add(typeName) | ||
false | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant newline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ktlint adds it because of the |
||
null -> typeName !in discardedFQNs && processedFQNs.add(typeName) | ||
} | ||
} | ||
|
||
private fun Iterable<JsonObject>.filterDescriptorsToLoad() = | ||
|
||
filter { processedDescriptors.add(it) } | ||
|
||
fun addLibrariesFromClassLoader( | ||
classLoader: ClassLoader, | ||
host: KotlinKernelHost, | ||
|
@@ -77,30 +83,36 @@ class LibrariesScanner( | |
} | ||
} | ||
|
||
private val jsonParser = Json { ignoreUnknownKeys = true } | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
private fun scanForLibraries( | ||
classLoader: ClassLoader, | ||
host: KotlinKernelHost, | ||
integrationTypeNameRules: List<AcceptanceRule<TypeName>> = listOf(), | ||
): LibrariesScanResult { | ||
val results = | ||
classLoader.getResources("$KOTLIN_JUPYTER_RESOURCES_PATH/$KOTLIN_JUPYTER_LIBRARIES_FILE_NAME").toList().map { url -> | ||
val contents = url.readText() | ||
Json.decodeFromString<LibrariesScanResult>(contents) | ||
} | ||
classLoader.getResources("$KOTLIN_JUPYTER_RESOURCES_PATH/$KOTLIN_JUPYTER_LIBRARIES_FILE_NAME").toList() | ||
.map { url -> | ||
val contents = url.readText() | ||
jsonParser.decodeFromString<LibrariesScanResult>(contents) | ||
} | ||
|
||
val definitions = mutableListOf<LibrariesDefinitionDeclaration>() | ||
val producers = mutableListOf<LibrariesProducerDeclaration>() | ||
val descriptors = mutableListOf<JsonObject>() | ||
|
||
for (result in results) { | ||
definitions.addAll(result.definitions) | ||
producers.addAll(result.producers) | ||
descriptors.addAll(result.descriptors) | ||
} | ||
|
||
fun <I : LibrariesInstantiable<*>> Iterable<I>.filterNames() = filterNamesToLoad(host, integrationTypeNameRules) | ||
|
||
return LibrariesScanResult( | ||
definitions.filterNames(), | ||
producers.filterNames(), | ||
descriptors.filterDescriptorsToLoad(), | ||
) | ||
} | ||
|
||
|
@@ -140,6 +152,13 @@ class LibrariesScanner( | |
} | ||
} | ||
} | ||
|
||
scanResult.descriptors.forEach { | ||
val descriptor = parseLibraryDescriptor(it) | ||
val definition = descriptor.convertToDefinition(arguments = emptyList()) | ||
Jolanrensen marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
definitions.add(definition) | ||
} | ||
|
||
return definitions | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.