Permalink
Browse files

coafileHandler: Handle coafile

  • Loading branch information...
arafsheikh committed Jun 16, 2016
1 parent 6a1bdc9 commit 1d7809d4d215d0ab463b466363da46c01b692dbd
@@ -8,6 +8,9 @@ Bundle-ClassPath: .,
lib/commons-exec-1.3.jar,
lib/commons-logging-1.2.jar
Bundle-Vendor: COALA
Export-Package: com.coala.core.handlers;x-friends:="com.coala.core.tests",
com.coala.core.utils,
org.apache.commons.exec
Require-Bundle: org.eclipse.ui,
org.eclipse.core.resources;bundle-version="3.10.1",
org.eclipse.jdt.core;bundle-version="3.11.2",
@@ -18,6 +21,3 @@ Require-Bundle: org.eclipse.ui,
org.junit
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: javax.inject;version="1.0.0"
Export-Package: com.coala.core.handlers;x-friends:="com.coala.core.tests",
com.coala.core.utils,
org.apache.commons.exec
@@ -34,16 +34,12 @@
point="org.eclipse.ui.handlers">
<handler
commandId="com.coala.core.commands.runcoalaCommand"
class="com.coala.core.handlers.Plugin">
class="com.coala.core.handlers.coafileHandler">
</handler>
<handler
commandId="com.coala.core.commands.removeMarkersCommand"
class="com.coala.core.handlers.RemoveMarkers">
</handler>
<handler
commandId="com.coala.core.commands.bearList"
class="com.coala.core.handlers.Plugin">
</handler>
</extension>
<!--
<extension
@@ -22,7 +22,9 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
/**
* Remove all markers of the type "com.coala.core.coolproblem" from the given file.
* @param file IFile to remove markers from
*
* @param file
* IFile to remove markers from
*/
public void removeAllMarkers(IFile file) {
IMarker[] problems = null;
@@ -5,7 +5,11 @@
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import java.io.IOException;
@@ -16,28 +20,46 @@
* @see org.eclipse.core.commands.IHandler
* @see org.eclipse.core.commands.AbstractHandler
*/
public class Plugin extends AbstractHandler {
public class coafileHandler extends AbstractHandler {
/**
* The constructor.
*
* @throws IOException
* exception
*/
public Plugin() {
public coafileHandler() {
}
/**
* Execute analysis.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
IFile file = (IFile) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
.getActivePart().getSite().getPage().getActiveEditor().getEditorInput()
.getAdapter(IFile.class);
String folder = null;
IProject project = null;
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window != null) {
IStructuredSelection selection = (IStructuredSelection) window.getSelectionService()
.getSelection();
Object firstElement = selection.getFirstElement();
if (firstElement instanceof IAdaptable) {
project = (IProject) ((IAdaptable) firstElement).getAdapter(IProject.class);
if (project == null) {
System.out.println("Project returned null.");
} else {
IPath path = project.getLocation();
folder = path.toOSString();
}
}
}
new RemoveMarkers().execute(event);
try {
ExternalUtils.runBearOnFile(file, "CheckstyleBear");
} catch (IOException | InterruptedException ex) {
if (folder != null && project != null) {
ExternalUtils.runcoafile(folder, project);
} else {
System.out.println("Couldn't run analysis using .coafile");
}
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
@@ -9,12 +9,16 @@
import org.apache.commons.exec.PumpStreamHandler;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
public class ExternalUtils {
@@ -58,7 +62,7 @@ public void onProcessComplete(int exitValue) {
@Override
public void onProcessFailed(ExecuteException executeException) {
System.out.println("Running coala failed.");
System.out.println("Running coala failed");
executeException.printStackTrace();
}
};
@@ -73,6 +77,51 @@ public void onProcessFailed(ExecuteException executeException) {
executor.execute(cmdLine, resultHandler);
}
/**
* Run coala using the .coafile in the project directory.
*
* @param path
* The path to the project's root.
* @param project
* The IProject object of the current project.
*/
public static void runcoafile(final String path, final IProject project)
throws ExecuteException, IOException {
File cwd = new File(path);
CommandLine cmdLine = new CommandLine("coala-json");
final ByteArrayOutputStream stdout = new ByteArrayOutputStream();
PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(stdout);
// Asynchronously handle coala-json output
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler() {
@Override
public void onProcessComplete(int exitValue) {
try {
processJsonAndMark(stdout.toString(), project);
} catch (IOException ex) {
ex.printStackTrace();
}
}
@Override
public void onProcessFailed(ExecuteException executeException) {
System.out.println("Running coala failed with output: " + stdout.toString());
executeException.printStackTrace();
}
};
// Timeout command execution after 60 seconds.
ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
Executor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
executor.setExitValue(1);
executor.setStreamHandler(pumpStreamHandler);
executor.setWorkingDirectory(cwd);
executor.execute(cmdLine, resultHandler);
}
/**
* Process the JSON output of coala and add marker for each problem.
*
@@ -98,6 +147,36 @@ public static void processJsonAndMark(String json, IFile file) throws IOExceptio
}
}
/**
* Process the JSON output of coala and add marker for each problem.
*
* @param json
* Output of running coala-json.
* @param project
* The IProject containing files to add markers on.
* @throws IOException
* exception
*/
public static void processJsonAndMark(String json, IProject project) throws IOException {
JSONObject jsonObject = new JSONObject(json);
JSONArray result = jsonObject.getJSONObject("results").getJSONArray("default");
for (int i = 0; i < result.length(); i++) {
String projectPath = project.getLocation().toOSString();
String filePath = result.getJSONObject(i).getJSONArray("affected_code").getJSONObject(0)
.getString("file");
IPath path = new Path(filePath.substring(projectPath.length()));
IFile file = project.getFile(path);
String message = result.getJSONObject(i).getString("message");
String origin = result.getJSONObject(i).getString("origin");
int severity = result.getJSONObject(i).getInt("severity");
JSONArray affectedCodeArray = result.getJSONObject(i).getJSONArray("affected_code");
for (int j = 0; j < affectedCodeArray.length(); j++) {
int endLine = affectedCodeArray.getJSONObject(j).getJSONObject("end").getInt("line");
createCoolMarker(file, endLine, 3 - severity, message);
}
}
}
/**
* Creates a problem marker.
*

0 comments on commit 1d7809d

Please sign in to comment.