Skip to content

Commit

Permalink
Provide more helpful executable and directory defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
PyvesB committed Feb 20, 2022
1 parent ecb6e16 commit d3f9fd1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class ReadaptDebugDelegate extends DSPLaunchDelegate {
@Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor)
throws CoreException {
if (!new File(READAPT_PATH.getValue()).exists()) {
String readaptPath = READAPT_PATH.getValue();
if (readaptPath == null || !new File(readaptPath).exists()) {
displayNotFoundWarning();
return;
}
Expand All @@ -56,7 +57,7 @@ public void launch(ILaunchConfiguration configuration, String mode, ILaunch laun
parameters.put("request", "launch");

DSPLaunchDelegateLaunchBuilder builder = new DSPLaunchDelegateLaunchBuilder(configuration, mode, launch, monitor);
builder.setLaunchDebugAdapter("\"" + READAPT_PATH.getValue() + "\"", Collections.singletonList("stdio"));
builder.setLaunchDebugAdapter("\"" + readaptPath + "\"", Collections.singletonList("stdio"));
builder.setMonitorDebugAdapter(DEBUG_READAPT.getValue());
builder.setDspParameters(parameters);
super.launch(builder);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Pierre-Yves B. and others.
* Copyright (c) 2019-2022 Pierre-Yves B. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -12,11 +12,13 @@
*******************************************************************************/
package io.github.pyvesb.eclipse_solargraph.preferences;

import io.github.pyvesb.eclipse_solargraph.utils.CommandHelper;

public enum StringPreferences implements Preference<String> {

GEM_PATH("GemPath", "Solargraph executable:", ""),
RUBY_DIR("RubyDir", "Ruby bin directory:", ""),
READAPT_PATH("ReadaptPath", "Readapt executable:", "");
GEM_PATH("GemPath", "Solargraph executable:", CommandHelper.findPath("solargraph")),
RUBY_DIR("RubyDir", "Ruby bin directory:", CommandHelper.findDirectory("ruby")),
READAPT_PATH("ReadaptPath", "Readapt executable:", CommandHelper.findPath("readapt"));

private final String key;
private final String desc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ public void start() throws IOException {
}

private static List<String> getSolargraphCommand() {
if (new File(GEM_PATH.getValue()).exists()) {
return Arrays.asList(CommandHelper.getPlatformCommand("\"" + GEM_PATH.getValue() + "\" stdio"));
String gemPath = GEM_PATH.getValue();
if (gemPath != null && new File(gemPath).exists()) {
return Arrays.asList(CommandHelper.getPlatformCommand("\"" + gemPath + "\" stdio"));
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Pierre-Yves B. and others.
* Copyright (c) 2019-2022 Pierre-Yves B. and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
Expand All @@ -15,16 +15,46 @@
import static io.github.pyvesb.eclipse_solargraph.preferences.BooleanPreferences.SYSTEM_RUBY;
import static io.github.pyvesb.eclipse_solargraph.preferences.StringPreferences.RUBY_DIR;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.eclipse.core.runtime.Platform;

public class CommandHelper {

public static String findDirectory(String executable) {
String executablePath = findPath(executable);
if (executablePath != null) {
File executableDirectory = new File(executablePath).getParentFile();
if (executableDirectory != null && executableDirectory.isDirectory()) {
return executableDirectory.getAbsolutePath();
}
}
return null;
}

public static String findPath(String executable) {
String locationCommand = (isWindows() ? "where " : "which ") + executable;
try {
Process process = new ProcessBuilder(getPlatformCommand(locationCommand)).redirectErrorStream(true).start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String path = reader.readLine();
if (path != null && new File(path).exists()) {
return path;
}
}
} catch (IOException e) {
LogHelper.error("Failed to find location of " + executable, e);
}
return null;
}

public static String[] getAbsolutePlatformCommand(String command) {
if (!SYSTEM_RUBY.getValue()) {
String rubyDir = RUBY_DIR.getValue();
if (rubyDir.length() > 0) {
if (rubyDir != null && !rubyDir.isEmpty()) {
return CommandHelper.getPlatformCommand(rubyDir + File.separator + command);
}
}
Expand Down

0 comments on commit d3f9fd1

Please sign in to comment.