1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 06:02:11 +02:00

Bug 574519: Always collect WSL entries in a Job

Change-Id: I35abec7ca075bf525e972e4cbc03af4361d1d490
This commit is contained in:
Jonah Graham 2021-07-24 12:52:33 -04:00
parent da616bcc43
commit 1071e55fb1
2 changed files with 93 additions and 53 deletions

View file

@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2 Bundle-ManifestVersion: 2
Bundle-Name: %pluginName Bundle-Name: %pluginName
Bundle-SymbolicName: org.eclipse.tm.terminal.view.ui;singleton:=true Bundle-SymbolicName: org.eclipse.tm.terminal.view.ui;singleton:=true
Bundle-Version: 4.9.0.qualifier Bundle-Version: 4.9.1.qualifier
Bundle-Activator: org.eclipse.tm.terminal.view.ui.activator.UIPlugin Bundle-Activator: org.eclipse.tm.terminal.view.ui.activator.UIPlugin
Bundle-Vendor: %providerName Bundle-Vendor: %providerName
Require-Bundle: org.eclipse.core.expressions;bundle-version="3.4.400", Require-Bundle: org.eclipse.core.expressions;bundle-version="3.4.400",

View file

@ -24,16 +24,19 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Predicate; import java.util.function.Predicate;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.tm.terminal.view.ui.interfaces.IExternalExecutablesProperties; import org.eclipse.tm.terminal.view.ui.interfaces.IExternalExecutablesProperties;
import org.eclipse.tm.terminal.view.ui.local.showin.IDetectExternalExecutable; import org.eclipse.tm.terminal.view.ui.local.showin.IDetectExternalExecutable;
public class DetectWSL implements IDetectExternalExecutable { public class DetectWSL implements IDetectExternalExecutable {
/** private List<Map<String, String>> result = null;
* Don't access directly, use {@link #getEntries()} private WslDetectJob detectJob = null;
*/
List<Map<String, String>> result = null;
@Override @Override
public boolean hasEntries() { public boolean hasEntries() {
@ -55,57 +58,94 @@ public class DetectWSL implements IDetectExternalExecutable {
} }
private synchronized List<Map<String, String>> getEntries() { private synchronized List<Map<String, String>> getEntries() {
if (result == null) { // getEntries can be called in many contexts, even from within
result = Collections.emptyList(); // menu creation (see Bug 574519). Therefore we spawn a job to
if (Platform.OS_WIN32.equals(Platform.getOS())) { // get the real entries, which means until the job is done, this
String windir = System.getenv("windir"); //$NON-NLS-1$ // method will return no entries.
if (windir == null) { if (result != null) {
return result; return result;
} }
String wsl = windir + "\\System32\\wsl.exe"; //$NON-NLS-1$ if (detectJob == null) {
if (!Files.isExecutable(Paths.get(wsl))) { detectJob = new WslDetectJob();
return result; detectJob.schedule();
} }
try {
ProcessBuilder pb = new ProcessBuilder(wsl, "--list", "--quiet"); //$NON-NLS-1$ //$NON-NLS-2$ if (detectJob.join(10, null)) {
try { result = detectJob.result;
Process process = pb.start(); detectJob = null;
try (InputStream is = process.getErrorStream()) { } else {
// drain the error stream return Collections.emptyList();
if (is.readAllBytes().length != 0) {
return result;
}
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_16LE))) {
result = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
String distribution = line.trim();
if (distribution.isBlank()) {
continue;
}
// docker-desktop entries are not "real" so shouldn't be shown in UI
if (distribution.startsWith("docker-desktop")) { //$NON-NLS-1$
continue;
}
String name = distribution + " (WSL)"; //$NON-NLS-1$
Map<String, String> m = new HashMap<>();
m.put(IExternalExecutablesProperties.PROP_NAME, name);
m.put(IExternalExecutablesProperties.PROP_PATH, wsl);
m.put(IExternalExecutablesProperties.PROP_ARGS, "--distribution " + distribution); //$NON-NLS-1$
m.put(IExternalExecutablesProperties.PROP_TRANSLATE, Boolean.TRUE.toString());
result.add(m);
}
}
} catch (IOException e) {
}
} }
} catch (OperationCanceledException | InterruptedException e) {
result = Collections.emptyList();
detectJob = null;
} }
return result; return result;
} }
private static class WslDetectJob extends Job {
private List<Map<String, String>> result = null;
public WslDetectJob() {
super("Detect WSL Instances"); //$NON-NLS-1$
setSystem(true);
}
@Override
protected IStatus run(IProgressMonitor monitor) {
if (result == null) {
result = Collections.emptyList();
if (Platform.OS_WIN32.equals(Platform.getOS())) {
String windir = System.getenv("windir"); //$NON-NLS-1$
if (windir == null) {
return Status.OK_STATUS;
}
String wsl = windir + "\\System32\\wsl.exe"; //$NON-NLS-1$
if (!Files.isExecutable(Paths.get(wsl))) {
return Status.OK_STATUS;
}
ProcessBuilder pb = new ProcessBuilder(wsl, "--list", "--quiet"); //$NON-NLS-1$ //$NON-NLS-2$
try {
Process process = pb.start();
try (InputStream is = process.getErrorStream()) {
// drain the error stream
if (is.readAllBytes().length != 0) {
return Status.OK_STATUS;
}
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_16LE))) {
result = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
String distribution = line.trim();
if (distribution.isBlank()) {
continue;
}
// docker-desktop entries are not "real" so shouldn't be shown in UI
if (distribution.startsWith("docker-desktop")) { //$NON-NLS-1$
continue;
}
String name = distribution + " (WSL)"; //$NON-NLS-1$
Map<String, String> m = new HashMap<>();
m.put(IExternalExecutablesProperties.PROP_NAME, name);
m.put(IExternalExecutablesProperties.PROP_PATH, wsl);
m.put(IExternalExecutablesProperties.PROP_ARGS, "--distribution " + distribution); //$NON-NLS-1$
m.put(IExternalExecutablesProperties.PROP_TRANSLATE, Boolean.TRUE.toString());
result.add(m);
}
}
} catch (IOException e) {
}
}
}
return Status.OK_STATUS;
}
}
} }