Pass available JDKs to build server (#1387)

Signed-off-by: Sheng Chen <sheche@microsoft.com>
This commit is contained in:
Sheng Chen 2023-08-23 15:23:39 +08:00
Родитель bcce99295e
Коммит 116fc4a0d4
3 изменённых файлов: 120 добавлений и 0 удалений

Просмотреть файл

@ -17,16 +17,26 @@ package com.microsoft.gradle.bs.importer;
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.internal.launching.StandardVMType;
import org.eclipse.jdt.launching.AbstractVMInstall;
import org.eclipse.jdt.launching.IVMInstall;
import org.eclipse.jdt.launching.IVMInstall2;
import org.eclipse.jdt.launching.IVMInstallType;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.VMStandin;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.RuntimeEnvironment;
import org.eclipse.jdt.ls.core.internal.preferences.Preferences;
public class EclipseVmUtil {
@ -123,6 +133,93 @@ public class EclipseVmUtil {
return Optional.empty();
}
/**
* Get all the available JDK installations in the Eclipse VM registry. If multiple installations
* are found for the same major version, the first found one is return.
*
* The results are returned as map, where key is the major version and value is the uri string of
* the installation path.
* <p> Note: The embedded JRE is excluded.
*/
public static Map<String, String> getAllVmInstalls() {
List<IVMInstall> vmList = Stream.of(JavaRuntime.getVMInstallTypes())
.map(IVMInstallType::getVMInstalls)
.flatMap(Arrays::stream)
.toList();
Map<String, File> vmInstalls = new HashMap<>();
for (IVMInstall vmInstall : vmList) {
if (vmInstall instanceof AbstractVMInstall vm) {
String javaVersion = getMajorJavaVersion(vm.getJavaVersion());
if (javaVersion == null || vm.getInstallLocation() == null
|| isEmbeddedJre(vm.getInstallLocation().getAbsolutePath())) {
continue;
}
vmInstalls.putIfAbsent(javaVersion, vm.getInstallLocation());
}
}
Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
Set<RuntimeEnvironment> runtimes = preferences.getRuntimes();
for (RuntimeEnvironment runtime : runtimes) {
if (StringUtils.isBlank(runtime.getPath())) {
continue;
}
File javaHome = new File(runtime.getPath());
if (vmInstalls.containsValue(javaHome)) {
continue;
}
String javaVersion = new StandardVMType().readReleaseVersion(javaHome);
// TODO: cannot get version is release file is not present.
if (StringUtils.isNotBlank(javaVersion)) {
// the user preference should have higher priority and replace
// the existing one if the major version is the same.
vmInstalls.put(getMajorJavaVersion(javaVersion), javaHome);
}
}
Map<String, String> vmInstallsUri = new HashMap<>();
for (Map.Entry<String, File> entry : vmInstalls.entrySet()) {
vmInstallsUri.put(entry.getKey(), entry.getValue().toURI().toString());
}
return vmInstallsUri;
}
private static String getMajorJavaVersion(String version) {
if (version == null) {
return null;
} else if (version.startsWith(JavaCore.VERSION_1_8)) {
return JavaCore.VERSION_1_8;
} else if (version.startsWith(JavaCore.VERSION_9)) {
return JavaCore.VERSION_9;
} else if (version.startsWith(JavaCore.VERSION_10)) {
return JavaCore.VERSION_10;
} else if (version.startsWith(JavaCore.VERSION_11)) {
return JavaCore.VERSION_11;
} else if (version.startsWith(JavaCore.VERSION_12)) {
return JavaCore.VERSION_12;
} else if (version.startsWith(JavaCore.VERSION_13)) {
return JavaCore.VERSION_13;
} else if (version.startsWith(JavaCore.VERSION_14)) {
return JavaCore.VERSION_14;
} else if (version.startsWith(JavaCore.VERSION_15)) {
return JavaCore.VERSION_15;
} else if (version.startsWith(JavaCore.VERSION_16)) {
return JavaCore.VERSION_16;
} else if (version.startsWith(JavaCore.VERSION_17)) {
return JavaCore.VERSION_17;
} else if (version.startsWith(JavaCore.VERSION_18)) {
return JavaCore.VERSION_18;
} else if (version.startsWith(JavaCore.VERSION_19)) {
return JavaCore.VERSION_19;
} else if (version.startsWith(JavaCore.VERSION_20)) {
return JavaCore.VERSION_20;
}
return null;
}
private static String getExecutionEnvironmentId(String version) {
// the result values correspond to the standard execution environment definitions in the
// org.eclipse.jdt.launching/plugin.xml file

Просмотреть файл

@ -219,12 +219,14 @@ public class GradleBuildServerProjectImporter extends AbstractProjectImporter {
private BuildServerPreferences getBuildServerPreferences() {
BuildServerPreferences pref = new BuildServerPreferences();
Preferences jdtlsPreferences = getPreferences();
pref.setWrapperEnabled(jdtlsPreferences.isGradleWrapperEnabled());
pref.setGradleArguments(jdtlsPreferences.getGradleArguments());
pref.setGradleHome(jdtlsPreferences.getGradleHome());
pref.setGradleJavaHome(jdtlsPreferences.getGradleJavaHome());
pref.setGradleJvmArguments(jdtlsPreferences.getGradleJvmArguments());
pref.setGradleUserHome(jdtlsPreferences.getGradleUserHome());
pref.setGradleVersion(jdtlsPreferences.getGradleVersion());
pref.setJdks(EclipseVmUtil.getAllVmInstalls());
return pref;
}

Просмотреть файл

@ -2,21 +2,26 @@ package com.microsoft.gradle.bs.importer.model;
import java.util.Collections;
import java.util.List;
import java.util.Map;
/**
* The data used in 'build/initialize' request.
*/
public class BuildServerPreferences {
private String gradleJavaHome;
private boolean isWrapperEnabled;
private String gradleVersion;
private String gradleHome;
private String gradleUserHome;
private List<String> gradleArguments;
private List<String> gradleJvmArguments;
private Map<String, String> jdks;
public BuildServerPreferences() {
isWrapperEnabled = true;
gradleArguments = Collections.emptyList();
gradleJvmArguments = Collections.emptyList();
jdks = Collections.emptyMap();
}
public String getGradleJavaHome() {
@ -27,6 +32,14 @@ public class BuildServerPreferences {
this.gradleJavaHome = gradleJavaHome;
}
public boolean isWrapperEnabled() {
return isWrapperEnabled;
}
public void setWrapperEnabled(boolean isWrapperEnabled) {
this.isWrapperEnabled = isWrapperEnabled;
}
public String getGradleVersion() {
return gradleVersion;
}
@ -66,4 +79,12 @@ public class BuildServerPreferences {
public void setGradleJvmArguments(List<String> gradleJvmArguments) {
this.gradleJvmArguments = gradleJvmArguments;
}
public Map<String, String> getJdks() {
return jdks;
}
public void setJdks(Map<String, String> jdks) {
this.jdks = jdks;
}
}