Skip to content

Commit 3bb209c

Browse files
authored
Use JupyterIntegration API (#191)
As a suggestion from Kotlin/kotlin-jupyter-libraries#358 (comment), move the imports from the library JSON descriptor to the library itself. Also add basic tests on the integration. This change makes the descriptor more flexible. Currently, with imports in JSON, if a new import is ever added to the descriptor, ```diff { "description": "A library to use the Develocity API in Kotlin scripts or projects", "properties": [ - { "name": "version", "value": "2024.1.0" }, + { "name": "version", "value": "2024.2.0" }, ... "imports": [ "com.gabrielfeo.develocity.api.*", "com.gabrielfeo.develocity.api.model.*", "com.gabrielfeo.develocity.api.extension.*", + "com.gabrielfeo.develocity.api.new.*" ] } ``` using it with a past version, which didn't yet contain the new package, would break: ``` %use develocity-api-kotlin(2024.1.0) \_ now fails with "Unresolved reference" if using the latest descriptor ```
1 parent 963df39 commit 3bb209c

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ org.gradle.caching=true
88
org.gradle.kotlin.dsl.skipMetadataVersionCheck=false
99
systemProp.scan.capture-build-logging=false
1010
systemProp.scan.capture-test-logging=false
11+
kotlin.jupyter.add.testkit=false

library/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins {
1212
`java-library`
1313
`maven-publish`
1414
signing
15+
kotlin("jupyter.api") version "0.12.0-181"
1516
}
1617

1718
val repoUrl = "https://github.com/gabrielfeo/develocity-api-kotlin"
@@ -47,6 +48,12 @@ tasks.withType<DokkaTask>().configureEach {
4748
}
4849
}
4950

51+
tasks.processJupyterApiResources {
52+
libraryProducers = listOf(
53+
"com.gabrielfeo.develocity.api.internal.jupyter.DevelocityApiJupyterIntegration",
54+
)
55+
}
56+
5057
tasks.named<Jar>("javadocJar") {
5158
from(tasks.dokkaHtml)
5259
}
@@ -80,6 +87,7 @@ dependencies {
8087
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")
8188
integrationTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.0")
8289
integrationTestImplementation("com.google.guava:guava:33.1.0-jre")
90+
integrationTestImplementation("org.jetbrains.kotlinx:kotlin-jupyter-test-kit:0.12.0-181")
8391
}
8492

8593
fun libraryPom() = Action<MavenPom> {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.gabrielfeo.develocity.api.internal.jupyter
2+
3+
import com.google.common.reflect.ClassPath
4+
import com.google.common.reflect.ClassPath.ClassInfo
5+
import org.intellij.lang.annotations.Language
6+
import org.jetbrains.kotlinx.jupyter.api.Code
7+
import org.jetbrains.kotlinx.jupyter.testkit.JupyterReplTestCase
8+
import kotlin.reflect.KVisibility
9+
import kotlin.test.Test
10+
11+
@ExperimentalStdlibApi
12+
class DevelocityApiJupyterIntegrationTest : JupyterReplTestCase() {
13+
14+
@Test
15+
fun `imports all extensions`() = assertSucceeds("""
16+
com.gabrielfeo.develocity.api.BuildsApi::getGradleAttributesFlow
17+
com.gabrielfeo.develocity.api.BuildsApi::getBuildsFlow
18+
19+
val attrs = emptyList<com.gabrielfeo.develocity.api.model.BuildAttributesValue>()
20+
"custom value name" in attrs
21+
attrs["custom value name"]
22+
""")
23+
24+
@Test
25+
fun `imports all public classes`() {
26+
val classes = allPublicClassesRecursive("com.gabrielfeo.develocity.api")
27+
val references = classes.joinToString("\n") { "${it.name}::class" }
28+
println("Running code:\n$references")
29+
assertSucceeds(references)
30+
}
31+
32+
@Suppress("SameParameterValue")
33+
private fun allPublicClassesRecursive(packageName: String): List<ClassInfo> {
34+
val cp = ClassPath.from(this::class.java.classLoader)
35+
return cp.getTopLevelClassesRecursive(packageName)
36+
.filter { "internal" !in it.packageName }
37+
.filter { !it.name.endsWith("Kt") }
38+
.filter { Class.forName(it.name).kotlin.visibility == KVisibility.PUBLIC }
39+
}
40+
41+
private fun assertSucceeds(@Language("kts") code: Code) {
42+
code.lines().forEach {
43+
execRendered(it)
44+
}
45+
}
46+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.gabrielfeo.develocity.api.internal.jupyter
2+
3+
import org.jetbrains.kotlinx.jupyter.api.libraries.JupyterIntegration
4+
5+
@Suppress("unused")
6+
class DevelocityApiJupyterIntegration : JupyterIntegration() {
7+
8+
override fun Builder.onLoaded() {
9+
import("com.gabrielfeo.develocity.api.*")
10+
import("com.gabrielfeo.develocity.api.model.*")
11+
import("com.gabrielfeo.develocity.api.extension.*")
12+
}
13+
}

0 commit comments

Comments
 (0)