Skip to content

Commit 655d884

Browse files
committed
Added possibility of using properties from Gradle in source code, fixed implementation version
1 parent 65f8618 commit 655d884

File tree

5 files changed

+56
-7
lines changed

5 files changed

+56
-7
lines changed

build.gradle

+26-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import com.beust.klaxon.JsonObject
22

33
import java.nio.file.Paths
44
import java.util.regex.Pattern
5+
import java.util.stream.Collectors
56

67
buildscript {
78
ext.shadowJarVersion = "5.2.0"
89
ext.kotlinVersion = '1.3.70-eap-3'
9-
ext.baseVersion = '0.7.40'
10+
ext.baseVersion = '0.7.41'
1011
repositories {
1112
jcenter()
1213
mavenLocal()
@@ -81,6 +82,10 @@ allprojects {
8182
debugPort = 1044
8283
debuggerConfig = "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$debugPort".toString()
8384

85+
mainSourceSetDir = "main"
86+
resourcesDir = "resources"
87+
runtimePropertiesFile = "runtime.properties"
88+
8489
jarsPath = "jars"
8590
librariesPath = "libraries"
8691
kernelFile = "kernel.json"
@@ -112,6 +117,7 @@ allprojects {
112117
distribGroup = "distrib"
113118
condaGroup = "conda"
114119
pyPiGroup = "pip"
120+
buildGroup = "build"
115121

116122
condaUserStable = rootProject.findProperty('condaUserStable') ?: ''
117123
condaPasswordStable = rootProject.findProperty('condaPasswordStable') ?: ''
@@ -161,6 +167,25 @@ jar.manifest.attributes(
161167
'Implementation-Version': version
162168
)
163169

170+
task buildProperties(group: buildGroup) {
171+
def outputDir = file(getSubDir(buildDir.toPath(), resourcesDir, mainSourceSetDir))
172+
173+
inputs.property "version", version
174+
outputs.dir outputDir
175+
176+
doLast {
177+
outputDir.mkdirs()
178+
def propertiesFile = file(getSubDir(outputDir.toPath(), runtimePropertiesFile))
179+
propertiesFile.text = inputs.properties.entrySet().stream().map {
180+
"${it.key}=${it.value}\n"
181+
}.collect(Collectors.joining())
182+
}
183+
}
184+
185+
processResources {
186+
dependsOn buildProperties
187+
}
188+
164189
shadowJar {
165190
archiveBaseName.set(packageName)
166191
archiveClassifier.set('')

src/main/kotlin/org/jetbrains/kotlin/jupyter/config.kt

+12-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,25 @@ enum class JupyterSockets {
4040
iopub
4141
}
4242

43+
data class RuntimeKernelProperties(
44+
var version: String = "unspecified"
45+
) {
46+
constructor(map: Map<String, String>?) : this() {
47+
if (map == null)
48+
return
49+
version = map["version"] ?: version
50+
}
51+
}
52+
4353
data class KernelConfig(
4454
val ports: Array<Int>,
4555
val transport: String,
4656
val signatureScheme: String,
4757
val signatureKey: String,
4858
val pollingIntervalMillis: Long = 100,
4959
val scriptClasspath: List<File> = emptyList(),
50-
val resolverConfig: ResolverConfig?
60+
val resolverConfig: ResolverConfig?,
61+
val runtimeProperties: RuntimeKernelProperties = RuntimeKernelProperties()
5162
)
5263

5364
val protocolVersion = "5.3"

src/main/kotlin/org/jetbrains/kotlin/jupyter/ikotlin.kt

+11-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ fun printClassPath() {
4545
log.info("Current classpath: " + cp.joinToString())
4646
}
4747

48+
fun loadRuntimeProperties(): RuntimeKernelProperties {
49+
val map = object{}.javaClass.classLoader
50+
.getResource("runtime.properties")
51+
?.readText()?.parseIniConfig()
52+
53+
return RuntimeKernelProperties(map)
54+
}
55+
4856
fun main(vararg args: String) {
4957
try {
5058
log.info("Kernel args: "+ args.joinToString { it })
@@ -62,7 +70,8 @@ fun main(vararg args: String) {
6270
signatureScheme = sigScheme ?: "hmac1-sha256",
6371
signatureKey = if (sigScheme == null || key == null) "" else key,
6472
scriptClasspath = scriptClasspath,
65-
resolverConfig = loadResolverConfig(rootPath)
73+
resolverConfig = loadResolverConfig(rootPath),
74+
runtimeProperties = loadRuntimeProperties()
6675
))
6776
} catch (e: Exception) {
6877
log.error("exception running kernel with args: \"${args.joinToString()}\"", e)
@@ -80,7 +89,7 @@ fun kernelServer(config: KernelConfig) {
8089

8190
val executionCount = AtomicLong(1)
8291

83-
val repl = ReplForJupyter(config.scriptClasspath, config.resolverConfig)
92+
val repl = ReplForJupyter(config.scriptClasspath, config.resolverConfig, config.runtimeProperties)
8493

8594
val mainThread = Thread.currentThread()
8695

src/main/kotlin/org/jetbrains/kotlin/jupyter/protocol.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ fun JupyterConnection.Socket.shellMessagesHandler(msg: Message, repl: ReplForJup
3232
"language_info" to jsonObject(
3333
"name" to "kotlin",
3434
"codemirror_mode" to "text/x-kotlin",
35-
"file_extension" to ".kt"
35+
"file_extension" to ".kt",
36+
"mimetype" to "text/x-kotlin",
37+
"pygments_lexer" to "kotlin",
38+
"version" to KotlinCompilerVersion.VERSION
3639
),
3740

3841
// Jupyter lab Console support
3942
"banner" to "Kotlin language, version ${KotlinCompilerVersion.VERSION}",
4043
"implementation" to "Kotlin",
41-
"implementation_version" to KotlinCompilerVersion.VERSION,
44+
"implementation_version" to repl!!.properties.version,
4245
"status" to "ok"
4346
)))
4447
"history_request" ->

src/main/kotlin/org/jetbrains/kotlin/jupyter/repl.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class ReplCompilerException(val errorResult: ReplCompileResult.Error) : ReplExce
3838
}
3939

4040
class ReplForJupyter(val scriptClasspath: List<File> = emptyList(),
41-
val config: ResolverConfig? = null) {
41+
val config: ResolverConfig? = null,
42+
val properties: RuntimeKernelProperties = RuntimeKernelProperties()) {
4243

4344
private val resolver = JupyterScriptDependenciesResolver(config)
4445

0 commit comments

Comments
 (0)