From cd4203187d9e6cafa487078bbaf2b430f378ab1f Mon Sep 17 00:00:00 2001 From: Marco Maccaferri Date: Sat, 11 May 2024 18:07:49 +0200 Subject: [PATCH] Refactored external tools process run --- build/pom.xml | 2 +- modules/spin-tools/pom.xml | 2 +- .../com/maccasoft/propeller/ConsoleView.java | 2 +- .../com/maccasoft/propeller/SpinTools.java | 50 +++++++++++++++---- pom.xml | 4 +- 5 files changed, 45 insertions(+), 15 deletions(-) diff --git a/build/pom.xml b/build/pom.xml index 896e16d8..db39e4b2 100644 --- a/build/pom.xml +++ b/build/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.maccasoft spin-tools-runtime - 0.36.0 + 0.36.1 pom diff --git a/modules/spin-tools/pom.xml b/modules/spin-tools/pom.xml index 97343e30..ce337d7f 100644 --- a/modules/spin-tools/pom.xml +++ b/modules/spin-tools/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.maccasoft spin-tools - 0.36.0 + 0.36.1 jar UTF-8 diff --git a/modules/spin-tools/src/com/maccasoft/propeller/ConsoleView.java b/modules/spin-tools/src/com/maccasoft/propeller/ConsoleView.java index 65aa4b22..fcf929eb 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/ConsoleView.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/ConsoleView.java @@ -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() { diff --git a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java index a4723483..43e46b11 100644 --- a/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java +++ b/modules/spin-tools/src/com/maccasoft/propeller/SpinTools.java @@ -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; @@ -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(); @@ -137,6 +138,7 @@ public class SpinTools { StatusLine statusLine; Menu runMenu; + Process process; MenuItem topObjectItem; MenuItem blockSelectionItem; @@ -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() { @@ -2544,6 +2549,10 @@ void populateRunMenu() { @Override public void handleEvent(Event event) { + if (!handleRunningProcess()) { + return; + } + consoleView.clear(); if (!consoleView.getVisible()) { consoleView.setVisible(true); @@ -2608,7 +2617,30 @@ public void handleEvent(Event event) { } } - protected int runCommand(List 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 cmd, File outDir, OutputStream stdout) throws IOException, InterruptedException { ProcessBuilder builder = new ProcessBuilder(cmd); builder.redirectErrorStream(true); builder.directory(outDir); @@ -2623,17 +2655,17 @@ protected int runCommand(List 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); @@ -2648,9 +2680,7 @@ public void run() { } } }; - ioStream.start(); - - return p.waitFor(); + ioThread.start(); } void createPortMenu(Menu parent) { diff --git a/pom.xml b/pom.xml index 59489f2c..377422d9 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 com.maccasoft spin-tools-ide - 0.36.0 + 0.36.1 pom Integrated Development Environment for Parallax Propeller microcontrollers. @@ -20,7 +20,7 @@ com.maccasoft spin-tools - 0.36.0 + 0.36.1 runtime