Permalink
Browse files

ExternalUtils: Check coala binary before execution

Fixes #13
  • Loading branch information...
arafsheikh committed Jul 7, 2016
1 parent 01af287 commit 9cf253df886320450d06645afe6992de1b74e5b2
@@ -0,0 +1,33 @@
package com.coala.core.utils;
import org.eclipse.jface.dialogs.MessageDialog;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class DialogUtils {
/**
* Dialog asking the user to install the coala framework.
* If the user answers in affirmative the coala website is opened
* in the default web browser.
*/
public static void installcoalaDialog() {
URI coalaWebsite = null;
try {
coalaWebsite = new URI("http://coala-analyzer.org");
} catch (URISyntaxException ex) {
ex.printStackTrace();
}
boolean ret = MessageDialog.openQuestion(null, "coala is not installed",
"The coala framework is required to run code analysis. "
+ "Do you want to know how to install coala?");
if (ret) {
try {
java.awt.Desktop.getDesktop().browse(coalaWebsite);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
@@ -17,9 +17,11 @@
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
public class ExternalUtils {
@@ -40,6 +42,10 @@
*/
public static void runBearOnFile(final IFile file, String bear)
throws ExecuteException, IOException, InterruptedException {
if (!checkPrerequisite("coala")) {
DialogUtils.installcoalaDialog();
return;
}
String path = file.getRawLocation().toOSString();
CommandLine cmdLine = new CommandLine("coala-json");
cmdLine.addArgument("-f" + path);
@@ -88,6 +94,10 @@ public void onProcessFailed(ExecuteException executeException) {
*/
public static void runcoafile(final String path, final IProject project)
throws ExecuteException, IOException {
if (!checkPrerequisite("coala")) {
DialogUtils.installcoalaDialog();
return;
}
File cwd = new File(path);
CommandLine cmdLine = new CommandLine("coala-json");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
@@ -209,6 +219,39 @@ public static void processJsonAndMark(String json, IProject project) throws IOEx
}
}
/**
* Searches for the given binary in the PATH environmental variable.
*
* @param binary
* The executable that needs to be checked
* @return True is the binary is present, false otherwise
*/
private static boolean checkPrerequisite(String binary) {
ProcessBuilder pb = new ProcessBuilder(isWindows() ? "where" : "which", binary);
boolean foundBinary = false;
try {
Process proc = pb.start();
int errCode = proc.waitFor();
if (errCode == 0) {
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(proc.getInputStream()))) {
foundBinary = true;
}
} else {
System.out.println(binary + " not in PATH");
}
} catch (IOException ex) {
System.out.println("Something went wrong while searching for " + binary);
} catch (InterruptedException ex) {
System.out.println("Something went wrong while searching for " + binary);
}
return foundBinary;
}
private static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("windows");
}
/**
* Creates a problem marker.
*

0 comments on commit 9cf253d

Please sign in to comment.