Skip to content

Commit

Permalink
Refactored external tools process run
Browse files Browse the repository at this point in the history
  • Loading branch information
maccasoft committed May 11, 2024
1 parent ea5a4a5 commit cd42031
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
2 changes: 1 addition & 1 deletion build/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools-runtime</artifactId>
<version>0.36.0</version>
<version>0.36.1</version>
<packaging>pom</packaging>
<build>
<plugins>
Expand Down
2 changes: 1 addition & 1 deletion modules/spin-tools/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools</artifactId>
<version>0.36.0</version>
<version>0.36.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public void close() throws IOException {
}

private void append(String text) {
display.syncExec(new Runnable() {
display.asyncExec(new Runnable() {

@Override
public void run() {
Expand Down
50 changes: 40 additions & 10 deletions modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Stack;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -114,7 +115,7 @@
public class SpinTools {

public static final String APP_TITLE = "Spin Tools IDE";
public static final String APP_VERSION = "0.36.0";
public static final String APP_VERSION = "0.36.1";

static final File defaultSpin1Examples = new File(System.getProperty("APP_DIR"), "examples/P1").getAbsoluteFile();
static final File defaultSpin2Examples = new File(System.getProperty("APP_DIR"), "examples/P2").getAbsoluteFile();
Expand All @@ -137,6 +138,7 @@ public class SpinTools {
StatusLine statusLine;

Menu runMenu;
Process process;

MenuItem topObjectItem;
MenuItem blockSelectionItem;
Expand Down Expand Up @@ -567,7 +569,10 @@ public void propertyChange(PropertyChangeEvent evt) {

@Override
public void handleEvent(Event event) {
event.doit = handleUnsavedContent();
event.doit = handleRunningProcess();
if (event.doit) {
event.doit = handleUnsavedContent();
}
}
});
shell.addDisposeListener(new DisposeListener() {
Expand Down Expand Up @@ -2544,6 +2549,10 @@ void populateRunMenu() {

@Override
public void handleEvent(Event event) {
if (!handleRunningProcess()) {
return;
}

consoleView.clear();
if (!consoleView.getVisible()) {
consoleView.setVisible(true);
Expand Down Expand Up @@ -2608,7 +2617,30 @@ public void handleEvent(Event event) {
}
}

protected int runCommand(List<String> cmd, File outDir, OutputStream stdout) throws IOException, InterruptedException {
private boolean handleRunningProcess() {
if (process != null && process.isAlive()) {
int style = SWT.APPLICATION_MODAL | SWT.ICON_QUESTION | SWT.YES | SWT.NO;
MessageBox messageBox = new MessageBox(shell, style);
messageBox.setText(APP_TITLE);
messageBox.setMessage("An external tool is still running. Terminate?");
if (messageBox.open() != SWT.YES) {
return false;
}
try {
process.destroyForcibly();
process.waitFor(10, TimeUnit.SECONDS);
} catch (Exception e) {
// Do nothing
}
if (process.isAlive()) {
MessageDialog.openError(shell, APP_TITLE, "Can't terminate process " + process.pid());
return false;
}
}
return true;
}

protected void runCommand(List<String> cmd, File outDir, OutputStream stdout) throws IOException, InterruptedException {
ProcessBuilder builder = new ProcessBuilder(cmd);
builder.redirectErrorStream(true);
builder.directory(outDir);
Expand All @@ -2623,17 +2655,17 @@ protected int runCommand(List<String> cmd, File outDir, OutputStream stdout) thr
sb.append("\n");
stdout.write(sb.toString().getBytes());

final Process p = builder.start();
process = builder.start();

Thread ioStream = new Thread() {
Thread ioThread = new Thread() {

int count;
byte[] buf = new byte[1024];
byte[] buf = new byte[4096];

@Override
public void run() {
try {
InputStream out = p.getInputStream();
InputStream out = process.getInputStream();

do {
count = out.read(buf);
Expand All @@ -2648,9 +2680,7 @@ public void run() {
}
}
};
ioStream.start();

return p.waitFor();
ioThread.start();
}

void createPortMenu(Menu parent) {
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools-ide</artifactId>
<version>0.36.0</version>
<version>0.36.1</version>
<packaging>pom</packaging>
<description>Integrated Development Environment for Parallax Propeller microcontrollers.</description>
<developers>
Expand All @@ -20,7 +20,7 @@
<dependency>
<groupId>com.maccasoft</groupId>
<artifactId>spin-tools</artifactId>
<version>0.36.0</version>
<version>0.36.1</version>
<scope>runtime</scope>
</dependency>
</dependencies>
Expand Down

0 comments on commit cd42031

Please sign in to comment.