|
| 1 | +package org.jetbrains.kotlinx.dataframe.jupyter |
| 2 | + |
| 3 | +import org.jetbrains.kotlinx.jupyter.api.EmbeddedKernelRunMode |
| 4 | +import org.jetbrains.kotlinx.jupyter.config.DefaultKernelLoggerFactory |
| 5 | +import org.jetbrains.kotlinx.jupyter.config.defaultRepositoriesCoordinates |
| 6 | +import org.jetbrains.kotlinx.jupyter.libraries.LibraryResolver |
| 7 | +import org.jetbrains.kotlinx.jupyter.libraries.createLibraryHttpUtil |
| 8 | +import org.jetbrains.kotlinx.jupyter.repl.ReplForJupyter |
| 9 | +import org.jetbrains.kotlinx.jupyter.repl.creating.createRepl |
| 10 | +import org.jetbrains.kotlinx.jupyter.repl.embedded.NoOpInMemoryReplResultsHolder |
| 11 | +import org.jetbrains.kotlinx.jupyter.testkit.ClasspathLibraryResolver |
| 12 | +import org.jetbrains.kotlinx.jupyter.testkit.ReplProvider |
| 13 | +import org.jetbrains.kotlinx.jupyter.testkit.ToEmptyLibraryResolver |
| 14 | + |
| 15 | +/** Mirrors [ReplProvider.forLibrariesTesting] but `extraCompilerArguments` to set opt-in's. */ |
| 16 | +@Suppress("unused") |
| 17 | +fun ReplProvider.Companion.forLibrariesTesting( |
| 18 | + libraries: Collection<String>, |
| 19 | + extraCompilerArguments: List<String> = emptyList(), |
| 20 | +): ReplProvider = |
| 21 | + withDefaultClasspathResolution( |
| 22 | + shouldResolveToEmpty = { it in libraries }, |
| 23 | + extraCompilerArguments = extraCompilerArguments, |
| 24 | + ) |
| 25 | + |
| 26 | +private val httpUtil = createLibraryHttpUtil(DefaultKernelLoggerFactory) |
| 27 | + |
| 28 | +fun withDefaultClasspathResolution( |
| 29 | + shouldResolve: (String?) -> Boolean = { true }, |
| 30 | + shouldResolveToEmpty: (String?) -> Boolean = { false }, |
| 31 | + extraCompilerArguments: List<String> = emptyList(), |
| 32 | +) = ReplProvider { classpath -> |
| 33 | + val resolver = |
| 34 | + run { |
| 35 | + var res: LibraryResolver = ClasspathLibraryResolver(httpUtil.libraryDescriptorsManager, null, shouldResolve) |
| 36 | + res = ToEmptyLibraryResolver(res, shouldResolveToEmpty) |
| 37 | + res |
| 38 | + } |
| 39 | + |
| 40 | + createRepl( |
| 41 | + httpUtil = httpUtil, |
| 42 | + scriptClasspath = classpath, |
| 43 | + kernelRunMode = EmbeddedKernelRunMode, |
| 44 | + mavenRepositories = defaultRepositoriesCoordinates, |
| 45 | + libraryResolver = resolver, |
| 46 | + inMemoryReplResultsHolder = NoOpInMemoryReplResultsHolder, |
| 47 | + extraCompilerArguments = extraCompilerArguments, |
| 48 | + ).apply { |
| 49 | + initializeWithCurrentClasspath() |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +private fun ReplForJupyter.initializeWithCurrentClasspath() { |
| 54 | + eval { librariesScanner.addLibrariesFromClassLoader(currentClassLoader, this, notebook) } |
| 55 | +} |
0 commit comments