Skip to content

Commit c856301

Browse files
committed
refactored API to match behavior of compilation logging
1 parent a2a265e commit c856301

5 files changed

Lines changed: 33 additions & 29 deletions

File tree

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package com.sandwich.util.io;
22

3+
import java.io.File;
4+
5+
import com.sandwich.koan.constant.KoanConstants;
36
import com.sandwich.koan.ui.SuitePresenter;
47
import com.sandwich.koan.util.ApplicationUtils;
58

6-
import java.io.File;
7-
import java.util.Arrays;
8-
import java.util.Scanner;
9-
109
public class CompilationFailureLogger implements CompilationListener {
11-
public void compilationFailed(File src, String[] command, Process p, Throwable x) {
10+
public void compilationFailed(File src, String[] command, int exitCode, String errorMessage, Throwable x) {
1211
SuitePresenter presenter = ApplicationUtils.getPresenter();
13-
presenter.displayError("\n*****************************************************************");
14-
presenter.displayError("COMPILE FAILED! Exit status was: " + p.exitValue());
15-
presenter.displayError("\nCompile Output:");
16-
presenter.displayError(StreamUtils.convertStreamToString(p.getErrorStream()));
17-
presenter.displayError("Compile Command:");
18-
presenter.displayError(Arrays.toString(command));
19-
presenter.displayError("*****************************************************************\n");
20-
System.exit(1);
12+
String lineSeparator = KoanConstants.EOL;
13+
presenter.displayError(lineSeparator+
14+
"*****************************************************************");
15+
presenter.displayError(lineSeparator+"Compile Output:");
16+
presenter.displayError(errorMessage.
17+
replace(lineSeparator, lineSeparator + " ") + lineSeparator); // indent compiler output
18+
presenter.displayError("Compiling \""+src.getAbsolutePath() +"\" failed.");
19+
presenter.displayError("The exit status was: " + exitCode);
20+
presenter.displayError(
21+
"*****************************************************************"+lineSeparator);
2122
}
22-
public void compilationSucceeded(File src, String[] command, Process p, Throwable x) { }
23-
}
23+
public void compilationSucceeded(File src, String[] command, String stdIo, Throwable x) { }
24+
}

‎koans-lib/src/com/sandwich/util/io/CompilationListener.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
import java.io.File;
44

55
public interface CompilationListener {
6-
void compilationFailed(File src, String[] command, Process p, Throwable x);
7-
void compilationSucceeded(File src, String[] command, Process p, Throwable x);
6+
void compilationFailed(File src, String[] command, int exitCode, String errorMessage, Throwable x);
7+
void compilationSucceeded(File src, String[] command, String stdIo, Throwable x);
88
}

‎koans-lib/src/com/sandwich/util/io/FileCompilerAction.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FileCompilerAction implements FileAction {
1111

1212
private final String destinationPath;
1313
private final String[] classPaths;
14-
private CompilationListener errorHandler;
14+
private CompilationListener compilationListner;
1515
static final CompilationListener LOGGING_HANDLER = new CompilationFailureLogger();
1616

1717
public FileCompilerAction(File destinationPath, CompilationListener errorHandler, String...classPaths){
@@ -20,7 +20,7 @@ public FileCompilerAction(File destinationPath, CompilationListener errorHandler
2020
}
2121
this.destinationPath = destinationPath.getAbsolutePath();
2222
this.classPaths = classPaths;
23-
this.errorHandler = errorHandler == null ? LOGGING_HANDLER : errorHandler;
23+
this.compilationListner = errorHandler == null ? LOGGING_HANDLER : errorHandler;
2424
}
2525

2626
public void sourceToDestination(File src, File bin) throws IOException {
@@ -40,13 +40,14 @@ public void sourceToDestination(File src, File bin) throws IOException {
4040
presenter.displayMessage("compiling file: " + src.getAbsolutePath());
4141
}
4242
if (p.waitFor() != 0) {
43-
errorHandler.compilationFailed(src, command, p, null);
43+
compilationListner.compilationFailed(src, command, p.exitValue(), StreamUtils.convertStreamToString(p.getErrorStream()), null);
4444
}else{
45-
errorHandler.compilationSucceeded(src, command, p, null);
45+
compilationListner.compilationSucceeded(src, command, StreamUtils.convertStreamToString(p.getInputStream()), null);
4646
}
4747
} catch (Exception x) {
4848
x.printStackTrace();
49-
errorHandler.compilationFailed(src, command, p, x);
49+
compilationListner.compilationFailed(src, command,
50+
p.exitValue(), StreamUtils.convertStreamToString(p.getErrorStream()), x);
5051
}
5152
}
5253
}

‎koans-lib/src/com/sandwich/util/io/KoanSuiteCompilationListener.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
public class KoanSuiteCompilationListener implements CompilationListener {
66

77
private boolean lastCompilationAttemptFailed = false;
8-
private boolean messageShown = false;
8+
private String lastMessageShown = null;
99

10-
public void compilationFailed(File src, String[] command, Process p, Throwable x) {
11-
if(!messageShown){
12-
FileCompilerAction.LOGGING_HANDLER.compilationFailed(src, command, p, x);
10+
public void compilationFailed(File src, String[] command, int exitCode, String errorMessage, Throwable x) {
11+
if(!errorMessage.equals(lastMessageShown)){
12+
FileCompilerAction.LOGGING_HANDLER.compilationFailed(src, command, exitCode, errorMessage, x);
1313
}
14-
messageShown = lastCompilationAttemptFailed = true;
14+
lastMessageShown = errorMessage;
15+
lastCompilationAttemptFailed = true;
1516
}
1617

17-
public void compilationSucceeded(File src, String[] command, Process p, Throwable x) {
18-
messageShown = lastCompilationAttemptFailed = false;
18+
public void compilationSucceeded(File src, String[] command, String stdIo, Throwable x) {
19+
lastMessageShown = null; // reset last failed compilation message
20+
lastCompilationAttemptFailed = false;
1921
}
2022

2123
public boolean isLastCompilationAttemptFailure(){

‎koans/lib/koans.jar‎

-3 KB
Binary file not shown.

0 commit comments

Comments
 (0)