Skip to content

Commit 90d199c

Browse files
committed
impl: read plugin version from the extension.json
- Toolbox does not provide a plugin manager interface with metadata about current plugin. So we had to implement one for ourselves in order to have simple things like plugin version. - the implementation relies on the extension.json which is deployed in the root of the jar (i.e. the plugin jar) - the rest of the json fields are ignored for now
1 parent 8faed95 commit 90d199c

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.coder.toolbox.plugin
2+
3+
import com.squareup.moshi.Json
4+
import com.squareup.moshi.JsonClass
5+
6+
/**
7+
* Small subset representation of extension.json
8+
*/
9+
@JsonClass(generateAdapter = true)
10+
data class PluginInfo(
11+
@Json(name = "id") val id: String,
12+
@Json(name = "version") val version: String)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.coder.toolbox.plugin
2+
3+
import com.squareup.moshi.Moshi
4+
import java.io.InputStream
5+
6+
object PluginManager {
7+
8+
val pluginInfo: PluginInfo by lazy {
9+
loadPluginMetadata()
10+
}
11+
12+
private fun loadPluginMetadata(): PluginInfo {
13+
val resourcePath = "/extension.json"
14+
val inputStream: InputStream? = PluginManager.javaClass.getResourceAsStream(resourcePath)
15+
?: throw IllegalArgumentException("Resource not found: $resourcePath")
16+
17+
if (inputStream == null) {
18+
throw IllegalStateException("Can't load plugin information")
19+
}
20+
21+
inputStream.use { stream ->
22+
val jsonContent = stream.bufferedReader().readText()
23+
return Moshi.Builder().build().adapter(PluginInfo::class.java).fromJson(jsonContent)
24+
?: throw IllegalArgumentException("Failed to parse JSON")
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)