Skip to content

Instantly share code, notes, and snippets.

@douira
Created August 12, 2022 02:28
Show Gist options
  • Save douira/82f0d30204c6e6b771a07d00e20b6aab to your computer and use it in GitHub Desktop.
Save douira/82f0d30204c6e6b771a07d00e20b6aab to your computer and use it in GitHub Desktop.
Script to update the JDK Version for VSCode to match the one defined in the Brachyura Buildscript.java after switching branches
#!/usr/bin/env node
// copy this file into .git/hooks/post-checkout
const path = require("path")
const fs = require("fs")
//find the required java version from the build script
const javaVersion = parseInt(
fs
.readFileSync(
path.join(__dirname, "../../buildscript/src/main/java/Buildscript.java")
)
.toString()
.match(/public int getJavaVersion\(\) \{.*?return (\d)+;.*?\}/s)?.[1] ??
"8",
10
)
//switch the defalt runtime to the correct version in settings.json
const settingsPath = path.join(__dirname, "../../.vscode/settings.json")
const settings = JSON.parse(
fs
.readFileSync(settingsPath)
.toString()
.replace(/,(\s*[}\]])/gs, "$1")
)
let modified = false
settings["java.configuration.runtimes"].forEach(runtime => {
if (runtime.name.replace("1.", "").includes("-" + javaVersion.toString())) {
if (!runtime.default) {
runtime.default = true
modified = true
}
} else if (runtime.default) {
runtime.default = false
modified = true
}
})
// write the modified settings.json back to the file
if (modified) {
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, " "))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment