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

Feature/plugin mpp #159

Merged
merged 1 commit into from
Mar 9, 2021
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
4 changes: 2 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Kotlin kernel for IPython/Jupyter

[Kotlin](https://kotlinlang.org/) (1.4.30) kernel for [Jupyter](https://jupyter.org).
[Kotlin](https://kotlinlang.org/) (1.4.31) kernel for [Jupyter](https://jupyter.org).

Beta version. Tested with Jupyter Notebook 6.0.3, Jupyter Lab 1.2.6 and Jupyter Console 6.1.0
on Windows, Ubuntu Linux and macOS. Using with Jupyter Console frontend is problematic now because of
Expand Down Expand Up @@ -189,7 +189,7 @@ List of supported libraries:
- [kaliningraph](https://github.com/breandan/kaliningraph) - Graph library with a DSL for constructing graphs and visualizing the behavior of graph algorithms
- [khttp](https://github.com/jkcclemens/khttp) - HTTP networking library
- [klaxon](https://github.com/cbeust/klaxon) - JSON parser for Kotlin
- [kmath](https://github.com/mipt-npm/kmath) - Experimental Kotlin mathematical library operating on generic algebras
- [kmath](https://github.com/mipt-npm/kmath) - Experimental Kotlin algebra-based mathematical library
- [koma](https://koma.kyonifer.com/index.html) - Scientific computing library
- [kotlin-dl](https://github.com/JetBrains/KotlinDL) - KotlinDL library which provides Keras-like API for deep learning
- [kotlin-statistics](https://github.com/thomasnield/kotlin-statistics) - Idiomatic statistical operators for Kotlin
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# kotlinVersion=1.4.255-SNAPSHOT
kotlinVersion=1.4.30
kotlinVersion=1.4.31
kotlinLanguageLevel=1.4
jvmTarget=1.8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project.version = rootProject.version
project.group = "org.jetbrains.kotlin"

val junitVersion: String by rootProject
val kotlinVersion: String by rootProject

repositories {
jcenter()
Expand All @@ -21,7 +22,7 @@ dependencies {
// Temporary solution until Kotlin 1.4 will be supported in
// .kts buildscripts and it will be possible to use
// kotlinx.serialization in plugin code
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.20")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
implementation("com.google.code.gson:gson:2.8.6")

testImplementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,75 @@ package org.jetbrains.kotlinx.jupyter.api.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.invoke
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.repositories
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin
import org.jetbrains.kotlin.gradle.plugin.KaptExtension
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlinx.jupyter.api.plugin.tasks.JupyterApiResourcesTask

class ApiGradlePlugin : Plugin<Project> {
override fun apply(target: Project) {
target.pluginManager.run {
override fun apply(target: Project): Unit = with(target) {
pluginManager.run {
apply(Kapt3GradleSubplugin::class.java)
}

val jupyterBuildPath = target.buildDir.resolve(FQNS_PATH)
target.extensions.configure<KaptExtension>("kapt") {
val jupyterBuildPath = buildDir.resolve(FQNS_PATH)
extensions.configure<KaptExtension>("kapt") {
arguments {
arg("kotlin.jupyter.fqn.path", jupyterBuildPath)
}
}

target.repositories {
repositories {
mavenCentral()
}

val pluginExtension = KotlinJupyterPluginExtension(target)
target.extensions.add("kotlinJupyter", pluginExtension)
extensions.add("kotlinJupyter", pluginExtension)
pluginExtension.addDependenciesIfNeeded()

target.tasks {
tasks {
val cleanJupyterTask = register("cleanJupyterPluginFiles") {
doLast {
jupyterBuildPath.deleteRecursively()
}
}

val resourcesTaskName = "processJupyterApiResources"
register<JupyterApiResourcesTask>(resourcesTaskName) {
val kaptKotlinTask = findByName("kaptKotlin")
if (kaptKotlinTask != null) {
dependsOn(kaptKotlinTask)
kaptKotlinTask.dependsOn(cleanJupyterTask)
fun registerResourceTask() {
register<JupyterApiResourcesTask>(resourcesTaskName) {
val kaptKotlinTask = findByName("kaptKotlin")
if (kaptKotlinTask != null) {
dependsOn(kaptKotlinTask)
kaptKotlinTask.dependsOn(cleanJupyterTask)
}
}
}

named("processResources") {
dependsOn(resourcesTaskName)
// apply configuration to JVM-only project
plugins.withId("org.jetbrains.kotlin.jvm") {
// Task should be registered after plugin is applied
registerResourceTask()
named("processResources") {
dependsOn(resourcesTaskName)
}
}

// apply only to multiplatform plugin
plugins.withId("org.jetbrains.kotlin.multiplatform") {
// Task should be registered after plugin is applied
registerResourceTask()
extensions.findByType<KotlinMultiplatformExtension>()?.apply {
val jvmTargetName = targets.filterIsInstance<KotlinJvmTarget>().firstOrNull()?.name
?: error("Single JVM target not found in a multiplatform project")
named(jvmTargetName + "ProcessResources") {
dependsOn(resourcesTaskName)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,49 @@ package org.jetbrains.kotlinx.jupyter.api.plugin

import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.findByType
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget

private fun Project.configureDependency(scope: String, dependencyNotation: Any) {
// apply configuration to JVM-only project
plugins.withId("org.jetbrains.kotlin.jvm") {
val configuration = project.configurations.findByName(scope)
?: error("$scope configuration is not resolved for a Kotlin-JVM project")
dependencies {
configuration.invoke(dependencyNotation)
}
}
// apply only to multiplatform plugin
plugins.withId("org.jetbrains.kotlin.multiplatform") {
extensions.findByType<KotlinMultiplatformExtension>()?.apply {
val jvmTargetName = targets.filterIsInstance<KotlinJvmTarget>().firstOrNull()?.name
?: error("Single JVM target not found in a multiplatform project")
val configuration = project.configurations.findByName(jvmTargetName + scope.capitalize())
?: error("$scope configuration is not resolved for a multiplatform project")
dependencies {
configuration.invoke(dependencyNotation)
}
}
}
}

class KotlinJupyterPluginExtension(
private val project: Project
) {
fun addApiDependency(version: String? = null) {
fun addApiDependency(version: String? = null) = with(project) {
val apiVersion = version ?: apiVersion()
val compileOnlyConf = project.configurations.findByName("compileOnly") ?: return
project.dependencies {
compileOnlyConf("$GROUP_ID:kotlin-jupyter-api:$apiVersion")
}
configureDependency("compileOnly", "$GROUP_ID:kotlin-jupyter-api:$apiVersion")
}

fun addScannerDependency(version: String? = null) {
val implementationConf = project.configurations.findByName("implementation") ?: return
val kaptConf = project.configurations.findByName("kapt") ?: return
fun addScannerDependency(version: String? = null) = with(project) {
val kaptConf = configurations.findByName("kapt") ?: return
val apiVersion = version ?: apiVersion()
val mavenCoordinates = "$GROUP_ID:kotlin-jupyter-api-annotations:$apiVersion"
project.dependencies {
implementationConf(mavenCoordinates)
dependencies {
kaptConf(mavenCoordinates)
}
configureDependency("implementation", mavenCoordinates)
}

internal fun addDependenciesIfNeeded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import com.google.gson.Gson
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.findByType
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget
import org.jetbrains.kotlinx.jupyter.api.plugin.ApiGradlePlugin
import java.io.File

Expand All @@ -31,9 +35,25 @@ open class JupyterApiResourcesTask : DefaultTask() {

init {
val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
val mainSourceSet = sourceSets.named("main").get()
outputDir = mainSourceSet.output.resourcesDir?.resolve("META-INF/kotlin-jupyter-libraries")
?: throw IllegalStateException("No resources dir for main source set")
when {
project.plugins.findPlugin("org.jetbrains.kotlin.jvm") != null -> {
val mainSourceSet: SourceSet = sourceSets.named("main").get()
outputDir = mainSourceSet.output.resourcesDir?.resolve("META-INF/kotlin-jupyter-libraries")
?: throw IllegalStateException("No resources dir for main source set")
}
project.plugins.findPlugin("org.jetbrains.kotlin.multiplatform") != null -> {
val mppExtension = project.extensions.findByType<KotlinMultiplatformExtension>()
?: error("Kotlin MPP extension not found")
val jvmTargetName = mppExtension.targets.filterIsInstance<KotlinJvmTarget>().firstOrNull()?.name
?: error("Single JVM target not found in a multiplatform project")
// TODO properly resolve resource directory
outputDir = project.buildDir.resolve("processedResources/$jvmTargetName/main")
.resolve("META-INF/kotlin-jupyter-libraries")
}
else -> {
error("Kotlin plugin not found in the project")
}
}
}

@TaskAction
Expand Down
17 changes: 10 additions & 7 deletions libraries/kmath.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
{
"description": "Experimental Kotlin mathematical library operating on generic algebras",
"description": "Experimental Kotlin algebra-based mathematical library",
"properties": {
"v": "0.1.3"
"v": "0.2.0"
},
"link": "https://github.com/mipt-npm/kmath",
"repositories": [
"https://dl.bintray.com/mipt-npm/scientifik"
"https://dl.bintray.com/mipt-npm/kscience",
"https://dl.bintray.com/mipt-npm/dev"
],
"dependencies": [
"scientifik:kmath-core-jvm:$v"
"kscience.kmath:kmath-commons:$v",
"kscience.kmath:kmath-for-real:$v"
],
"imports": [
"scientifik.kmath.linear.*",
"scientifik.kmath.operations.*",
"scientifik.kmath.structures.*"
"kscience.kmath.linear.*",
"kscience.kmath.operations.*",
"kscience.kmath.structures.*",
"kscience.kmath.real.*"
]
}
2 changes: 1 addition & 1 deletion libraries/plotly-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"https://dl.bintray.com/mipt-npm/dev"
],
"properties": {
"v": "0.2.0",
"v": "0.3.1",
"port": "8882"
},
"link": "https://github.com/mipt-npm/plotly.kt",
Expand Down
3 changes: 2 additions & 1 deletion libraries/plotly.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
"kotlinx.html.*"
],
"repositories": [
"https://repo.kotlin.link",
"https://dl.bintray.com/mipt-npm/dataforge",
"https://dl.bintray.com/mipt-npm/kscience",
"https://dl.bintray.com/mipt-npm/dev"
],
"properties": {
"v": "0.2.0"
"v": "0.3.1"
},
"link": "https://github.com/mipt-npm/plotly.kt",
"description": "An experimental plotly.kt integration module. Supports static plots and HTML dashboards.",
Expand Down