Skip to content

Commit ae7fbf3

Browse files
committed
refactor: add the JSON schema reference when exporting default tokens to a file
1 parent 10d561b commit ae7fbf3

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/main/kotlin/com/github/lppedd/cc/configuration/component/DefaultTokensFileExportPanel.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import com.github.lppedd.cc.CC
44
import com.github.lppedd.cc.CCBundle
55
import com.github.lppedd.cc.getResourceAsStream
66
import com.github.lppedd.cc.scaled
7+
import com.intellij.ide.plugins.PluginManagerCore
78
import com.intellij.openapi.application.runWriteAction
9+
import com.intellij.openapi.extensions.PluginId
810
import com.intellij.openapi.fileChooser.FileChooserFactory
911
import com.intellij.openapi.fileChooser.FileSaverDescriptor
1012
import com.intellij.openapi.vfs.VirtualFile
@@ -17,6 +19,10 @@ import com.intellij.uiDesigner.core.GridConstraints
1719
import com.intellij.uiDesigner.core.GridLayoutManager
1820
import com.intellij.util.ui.JBUI
1921
import com.intellij.util.ui.UIUtil
22+
import java.io.BufferedReader
23+
import java.io.InputStreamReader
24+
import java.io.Reader
25+
import java.nio.charset.StandardCharsets.UTF_8
2026
import javax.swing.JPanel
2127

2228
/**
@@ -67,13 +73,27 @@ internal class DefaultTokensFileExportPanel
6773
return
6874
}
6975

70-
getResourceAsStream("/defaults/${CC.Tokens.File}").use {
71-
runWriteAction {
72-
virtualFile.setBinaryContent(it.readBytes())
73-
}
76+
// When exporting to a file, we also need to add the JSON schema reference
77+
val inputStream = getResourceAsStream("/defaults/${CC.Tokens.File}")
78+
val reader = BufferedReader(InputStreamReader(inputStream, UTF_8))
79+
val jsonStr = reader.use(Reader::readText)
7480

75-
exportInfo.foreground = UIUtil.getLabelDisabledForeground()
76-
exportInfo.text = CCBundle["cc.config.defaults.exportToPath.completed"]
81+
val pluginVersion = getPluginVersion()
82+
val schemaPath = "src/main/resources/defaults/conventionalcommit.schema.json"
83+
val schemaUrl = "https://github.com/lppedd/idea-conventional-commit/raw/${pluginVersion}/$schemaPath\""
84+
val sb = StringBuilder(jsonStr)
85+
sb.insert(4, $$"\"$schema\": \"$$schemaUrl\"\n")
86+
87+
runWriteAction {
88+
virtualFile.setBinaryContent("$sb".toByteArray())
7789
}
90+
91+
exportInfo.foreground = UIUtil.getLabelDisabledForeground()
92+
exportInfo.text = CCBundle["cc.config.defaults.exportToPath.completed"]
93+
}
94+
95+
private fun getPluginVersion(): String {
96+
val plugin = PluginManagerCore.getPlugin(PluginId.getId(CC.PluginId)) ?: error("plugin not found")
97+
return plugin.version
7898
}
7999
}

0 commit comments

Comments
 (0)