Skip to content

Commit

Permalink
ExternalUtils: Check coala binary before execution
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
arafsheikh committed Jul 7, 2016
1 parent 01af287 commit 9cf253d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
33 changes: 33 additions & 0 deletions com.coala.core/src/com/coala/core/utils/DialogUtils.java
@@ -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();
}
}
}
}
43 changes: 43 additions & 0 deletions com.coala.core/src/com/coala/core/utils/ExternalUtils.java
Expand Up @@ -17,9 +17,11 @@
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator; import java.util.Iterator;


public class ExternalUtils { public class ExternalUtils {
Expand All @@ -40,6 +42,10 @@ public class ExternalUtils {
*/ */
public static void runBearOnFile(final IFile file, String bear) public static void runBearOnFile(final IFile file, String bear)
throws ExecuteException, IOException, InterruptedException { throws ExecuteException, IOException, InterruptedException {
if (!checkPrerequisite("coala")) {
DialogUtils.installcoalaDialog();
return;
}
String path = file.getRawLocation().toOSString(); String path = file.getRawLocation().toOSString();
CommandLine cmdLine = new CommandLine("coala-json"); CommandLine cmdLine = new CommandLine("coala-json");
cmdLine.addArgument("-f" + path); cmdLine.addArgument("-f" + path);
Expand Down Expand Up @@ -88,6 +94,10 @@ public void onProcessFailed(ExecuteException executeException) {
*/ */
public static void runcoafile(final String path, final IProject project) public static void runcoafile(final String path, final IProject project)
throws ExecuteException, IOException { throws ExecuteException, IOException {
if (!checkPrerequisite("coala")) {
DialogUtils.installcoalaDialog();
return;
}
File cwd = new File(path); File cwd = new File(path);
CommandLine cmdLine = new CommandLine("coala-json"); CommandLine cmdLine = new CommandLine("coala-json");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream(); final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
Expand Down Expand Up @@ -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. * Creates a problem marker.
* *
Expand Down

0 comments on commit 9cf253d

Please sign in to comment.