Created
October 6, 2022 20:24
-
-
Save mathieucarbou/ec7357fcc81e6171bc39e664eff087b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env groovy | |
import groovy.xml.MarkupBuilder | |
import java.util.stream.Stream | |
import static java.util.stream.Collectors.toList | |
def candidates = Stream.of( | |
new File(System.getProperty("user.home"), ".sdkman/candidates/java"), | |
new File('/Library/Java/JavaVirtualMachines')) | |
.flatMap({ Stream.of(it.listFiles()) }) | |
.filter({ new File(it, "bin/java").exists() }) | |
.collect(toList()) | |
String toolchain = new StringWriter().withCloseable { sw -> | |
new MarkupBuilder(sw).with { xml -> | |
xml.doubleQuotes = true | |
xml.mkp.xmlDeclaration([version: '1.0', encoding: 'UTF-8']) | |
xml.toolchains([ | |
'xmlns' : "http://maven.apache.org/TOOLCHAINS/1.1.0", | |
'xmlns:xsi' : "http://www.w3.org/2001/XMLSchema-instance", | |
'xsi:schemaLocation': "http://maven.apache.org/TOOLCHAINS/1.1.0 http://maven.apache.org/xsd/toolchains-1.1.0.xsd" | |
]) { toolchains -> | |
candidates.each { File jvm -> | |
def proc | |
if (new File(jvm, 'bin/jshell').exists()) { | |
proc = [new File(jvm, 'bin/jshell'), '-'].execute() | |
proc.outputStream.withCloseable { out -> | |
out << "System.out.println(System.getProperty(\"java.vendor\") + \"\\n\" + System.getProperty(\"java.version\"))" | |
} | |
} else { | |
proc = [new File(jvm, 'bin/jrunscript'), '-e', 'java.lang.System.out.println(java.lang.System.getProperty("java.vendor") + "\\n" + java.lang.System.getProperty("java.version"))'].execute() | |
} | |
def info = proc.inputStream.getText('UTF-8').split('\n') | |
def major = info[1].substring(0, info[1].indexOf('.')) | |
if (major == "1") { | |
major = info[1].substring(0, info[1].indexOf('.', 2)) | |
} | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor(info[0]) | |
version(info[1]) | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor(info[0]) | |
version(major) | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
// add special entries for our completely wrong requirements | |
if (info[0].contains('Azul Systems')) { | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor('zulu') | |
version(info[1]) | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor('zulu') | |
version(major) | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
} | |
if (major == "11") { | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor(info[0]) | |
version('1.11') | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
} | |
if (info[0].contains('Azul Systems') && major == "11") { | |
toolchain() { | |
type('jdk') | |
provides() { | |
vendor('zulu') | |
version('1.11') | |
} | |
configuration() { | |
jdkHome(jvm.absolutePath) | |
} | |
} | |
} | |
} | |
} | |
} | |
return sw.toString() | |
} | |
if (args.length && args[0] == 'install') { | |
new File(System.getProperty('user.home'), '.m2/toolchains.xml').text = toolchain | |
} else { | |
println toolchain | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment