Skip to content

Commit

Permalink
Fix #255
Browse files Browse the repository at this point in the history
  • Loading branch information
ileasile committed Jul 7, 2021
1 parent 4e0d30a commit d220303
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 84 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ shadowJarVersion=7.0.0
kotlinxSerializationVersion=1.1.0
ktlintGradleVersion=10.0.0
ktlintVersion=0.40.0
publishPluginVersion=0.0.27-dev
publishPluginVersion=0.0.29-dev
junitVersion=5.7.1
slf4jVersion=1.7.30
logbackVersion=1.2.3
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
27 changes: 20 additions & 7 deletions jupyter-lib/kotlin-jupyter-api-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,21 @@ val saveVersion by tasks.registering {
}
}

tasks.processResources {
dependsOn(saveVersion)
}
tasks {
processResources {
dependsOn(saveVersion)
}

test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
}
}

tasks.test {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
register<Jar>("sourceJar") {
archiveClassifier.set("sources")
from(sourceSets.named("main").get().allSource)
}
}

Expand Down Expand Up @@ -90,6 +97,12 @@ pluginBundle {
}

publishing {
publications {
withType<MavenPublication> {
artifact(tasks["sourceJar"])
}
}

repositories {
(rootProject.findProperty("localPublicationsRepo") as? java.nio.file.Path)?.let {
maven {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package org.jetbrains.kotlinx.jupyter.api.plugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.Copy
import org.gradle.kotlin.dsl.findByType
import org.gradle.kotlin.dsl.invoke
import org.gradle.kotlin.dsl.named
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
import org.jetbrains.kotlinx.jupyter.api.plugin.tasks.whenAdded

class ApiGradlePlugin : Plugin<Project> {
override fun apply(target: Project): Unit = with(target) {
Expand Down Expand Up @@ -41,37 +44,50 @@ class ApiGradlePlugin : Plugin<Project> {
}

val resourcesTaskName = "processJupyterApiResources"
fun registerResourceTask() {
register<JupyterApiResourcesTask>(resourcesTaskName) {
val kaptKotlinTask = findByName("kaptKotlin")
if (kaptKotlinTask != null) {
dependsOn(kaptKotlinTask)
kaptKotlinTask.dependsOn(cleanJupyterTask)
kaptKotlinTask.outputs.dir(jupyterBuildPath)
}
fun registerResourceTask(): JupyterApiResourcesTask {
findByName(resourcesTaskName) ?: register<JupyterApiResourcesTask>(resourcesTaskName)
return named<JupyterApiResourcesTask>(resourcesTaskName).get()
}

fun dependOnProcessingTask(processTaskName: String) {
val jupyterTask = registerResourceTask()
tasks.named<Copy>(processTaskName) {
dependsOn(resourcesTaskName)
from(jupyterTask.outputDir)
}
}

fun dependOnKapt(kaptTaskName: String) {
registerResourceTask()
tasks.whenAdded(
{ it.name == kaptTaskName },
{
tasks.named(resourcesTaskName) {
dependsOn(it)
it.dependsOn(cleanJupyterTask)
it.outputs.dir(jupyterBuildPath)
}
}
)
}

// 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)
}
dependOnProcessingTask("processResources")
dependOnKapt("kaptKotlin")
}

// 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)
}
targets.whenAdded(
{ (it is KotlinJvmTarget) },
{
dependOnProcessingTask(it.name + "ProcessResources")
}
)
}
dependOnKapt("kaptKotlinJvm")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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
import org.jetbrains.kotlinx.jupyter.api.plugin.tasks.whenAdded
import java.util.Locale

private fun Project.configureDependency(scope: String, dependencyNotation: Any) {
Expand All @@ -19,13 +20,17 @@ private fun Project.configureDependency(scope: String, dependencyNotation: Any)
// 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(Locale.ROOT))
?: error("$scope configuration is not resolved for a multiplatform project")
dependencies {
configuration.invoke(dependencyNotation)
}
targets.whenAdded(
{ it is KotlinJvmTarget },
{
val jvmTargetName = it.name
val configuration = project.configurations.findByName(jvmTargetName + scope.capitalize(Locale.ROOT))
?: error("$scope configuration is not resolved for a multiplatform project")
dependencies {
configuration.invoke(dependencyNotation)
}
}
)
}
}
}
Expand All @@ -39,13 +44,14 @@ class KotlinJupyterPluginExtension(
}

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"
dependencies {
kaptConf(mavenCoordinates)
configurations.whenAdded({ it.name == "kapt" }) { kaptConf ->
val apiVersion = version ?: apiVersion()
val mavenCoordinates = "$GROUP_ID:kotlin-jupyter-api-annotations:$apiVersion"
dependencies {
kaptConf(mavenCoordinates)
}
configureDependency("implementation", mavenCoordinates)
}
configureDependency("implementation", mavenCoordinates)
}

internal fun addDependenciesIfNeeded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ 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,30 +26,7 @@ open class JupyterApiResourcesTask : DefaultTask() {
var libraryDefinitions: List<String> = emptyList()

@OutputDirectory
val outputDir: File

init {
val sourceSets = project.extensions.getByName("sourceSets") as SourceSetContainer
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")
}
}
}
val outputDir: File = project.buildDir.resolve("jupyterProcessedResources")

@TaskAction
fun createDescriptions() {
Expand All @@ -64,7 +36,9 @@ open class JupyterApiResourcesTask : DefaultTask() {
) + getScanResultFromAnnotations()
val json = Gson().toJson(resultObject)

val libFile = outputDir.resolve("libraries.json")
val jupyterDir = outputDir.resolve("META-INF/kotlin-jupyter-libraries")
val libFile = jupyterDir.resolve("libraries.json")
libFile.parentFile.mkdirs()
libFile.writeText(json)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jetbrains.kotlinx.jupyter.api.plugin.tasks

import org.gradle.api.NamedDomainObjectCollection

fun <T> NamedDomainObjectCollection<T>.whenAdded(condition: (T) -> Boolean, action: (T) -> Unit) {
val element = find(condition)
if (element != null) {
action(element)
return
}
whenObjectAdded {
val addedElement = this
if (condition(addedElement)) action(addedElement)
}
}
Loading

0 comments on commit d220303

Please sign in to comment.