This is an automated email from the ASF dual-hosted git repository.

lkishalmi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new bc8cf30  [NETBEANS-3279] Added RunUtils.cancelGradle() implementation
bc8cf30 is described below

commit bc8cf3071667b7606f7be0e361602229e5160507
Author: Laszlo Kishalmi <laszlo.kisha...@gmail.com>
AuthorDate: Tue Oct 22 23:00:14 2019 -0700

    [NETBEANS-3279] Added RunUtils.cancelGradle() implementation
---
 groovy/gradle/apichanges.xml                       | 15 +++++++++
 .../modules/gradle/api/execute/RunUtils.java       | 38 ++++++++++++++++++++++
 .../gradle/execute/AbstractGradleExecutor.java     |  4 +--
 .../gradle/execute/GradleDaemonExecutor.java       |  5 +--
 .../modules/gradle/execute/GradleExecutor.java     |  3 +-
 5 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/groovy/gradle/apichanges.xml b/groovy/gradle/apichanges.xml
index c717f54..39eea23 100644
--- a/groovy/gradle/apichanges.xml
+++ b/groovy/gradle/apichanges.xml
@@ -83,6 +83,21 @@ is the proper place.
     <!-- ACTUAL CHANGES BEGIN HERE: -->
 
     <changes>
+        <change>
+            <api name="execution"/>
+            <summary>Added <code>RunUtils.cancelGradle(RunConfig)</code> to 
allow plugins to cancel an executed Gradle process.</summary>
+            <version major="1" minor="4"/>
+            <date year="2019" month="10" day="22"/>
+            <author login="lkishalmi"/>
+            <compatibility source="compatible"/>
+            <description>
+                <p>
+                    Added <code>RunUtils.cancelGradle(RunConfig)</code> to 
allow plugins to cancel an executed Gradle process.
+                </p>
+            </description>
+            <class package="org.netbeans.modules.gradle.api.execute" 
name="RunUtils"/>
+            <issue number="NETBEANS-3279"/>
+        </change>
     </changes>
 
     <!-- Now the surrounding HTML text and document structure: -->
diff --git 
a/groovy/gradle/src/org/netbeans/modules/gradle/api/execute/RunUtils.java 
b/groovy/gradle/src/org/netbeans/modules/gradle/api/execute/RunUtils.java
index 3de0e54..8ab22fd 100644
--- a/groovy/gradle/src/org/netbeans/modules/gradle/api/execute/RunUtils.java
+++ b/groovy/gradle/src/org/netbeans/modules/gradle/api/execute/RunUtils.java
@@ -47,6 +47,7 @@ import java.util.Collections;
 import java.util.EnumSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.WeakHashMap;
 import java.util.prefs.Preferences;
 import org.netbeans.api.java.platform.JavaPlatform;
 import org.netbeans.api.java.platform.JavaPlatformManager;
@@ -77,6 +78,7 @@ public final class RunUtils {
     public static final String PROP_DEFAULT_CLI = "gradle.cli"; //NOI18N
 
     private RunUtils() {}
+    private static final Map<RunConfig, GradleExecutor> GRADLE_TASKS = new 
WeakHashMap<>();
 
     public static FileObject extractFileObjectfromLookup(Lookup lookup) {
         FileObject[] fos = extractFileObjectsfromLookup(lookup);
@@ -99,15 +101,37 @@ public final class RunUtils {
         return files.toArray(new FileObject[files.size()]);
     }
 
+    /**
+     * Executes a Gradle build with the given configuration. It can also take 
an
+     * initial message, which is printed to the output tab before the actual
+     * execution takes over the output handling.
+     *
+     * @param config the configuration of the Gradle execution
+     * @param initialOutput the initial message to be displayed,
+     *        can be {@code null} for no message.
+     * @return The Gradle Execution task
+     */
     public static ExecutorTask executeGradle(RunConfig config, String 
initialOutput) {
         LifecycleManager.getDefault().saveAll();
 
         GradleExecutor exec = new GradleDaemonExecutor(config);
         ExecutorTask task = executeGradleImpl(config.getTaskDisplayName(), 
exec, initialOutput);
+        GRADLE_TASKS.put(config, exec);
 
         return task;
     }
 
+    /**
+     * Create Gradle execution configuration (context). It applies the default
+     * setting from the project and the Global Gradle configuration on the
+     * command line.
+     *
+     * @param project The Gradle project
+     * @param action The name of the IDE action that's going to be executed
+     * @param displayName The display name of the output tab
+     * @param args Gradle command line arguments
+     * @return the Gradle execution configuration.
+     */
     public static RunConfig createRunConfig(Project project, String action, 
String displayName, String[] args) {
         GradleBaseProject gbp = GradleBaseProject.get(project);
 
@@ -129,6 +153,20 @@ public final class RunUtils {
         return ret;
     }
 
+    /**
+     * Enable plugins to Cancel a currently running Gradle execution.
+     * 
+     * @param config the RunConfig with which the Gradle execution has been 
started.
+     * @return {@code true} if the current execution was cancelled 
successfully,
+     *         {@code false} if the execution was already cancelled or it 
cannot
+     *         be cancelled for some reason.
+     * @since 1.4
+     */
+    public static boolean cancelGradle(RunConfig config) {
+        GradleExecutor exec = GRADLE_TASKS.get(config);
+        return exec != null ? exec.cancel() : false;
+    }
+
     private static ExecutorTask executeGradleImpl(String runtimeName, final 
GradleExecutor exec, String initialOutput) {
         InputOutput io = exec.getInputOutput();
         ExecutorTask task = ExecutionEngine.getDefault().execute(runtimeName, 
exec,
diff --git 
a/groovy/gradle/src/org/netbeans/modules/gradle/execute/AbstractGradleExecutor.java
 
b/groovy/gradle/src/org/netbeans/modules/gradle/execute/AbstractGradleExecutor.java
index 2b90468..99d87c8 100644
--- 
a/groovy/gradle/src/org/netbeans/modules/gradle/execute/AbstractGradleExecutor.java
+++ 
b/groovy/gradle/src/org/netbeans/modules/gradle/execute/AbstractGradleExecutor.java
@@ -29,7 +29,6 @@ import org.netbeans.api.options.OptionsDisplayer;
 import org.netbeans.spi.project.ui.support.BuildExecutionSupport;
 import org.openide.execution.ExecutorTask;
 import org.openide.filesystems.FileObject;
-import org.openide.util.Cancellable;
 import org.openide.util.ImageUtilities;
 import org.openide.util.NbBundle;
 import org.openide.util.RequestProcessor;
@@ -39,7 +38,6 @@ import java.io.File;
 import java.util.ArrayList;
 import javax.swing.SwingUtilities;
 import org.netbeans.api.project.Project;
-import org.netbeans.modules.gradle.actions.CustomActionRegistrationSupport;
 import org.netbeans.modules.gradle.api.execute.GradleCommandLine;
 import org.openide.DialogDescriptor;
 import org.openide.DialogDisplayer;
@@ -49,7 +47,7 @@ import org.openide.filesystems.FileUtil;
  *
  * @author Laszlo Kishalmi
  */
-public abstract class AbstractGradleExecutor extends 
OutputTabMaintainer<AbstractGradleExecutor.TabContext> implements 
GradleExecutor, Cancellable {
+public abstract class AbstractGradleExecutor extends 
OutputTabMaintainer<AbstractGradleExecutor.TabContext> implements 
GradleExecutor {
 
     public static final class TabContext {
 
diff --git 
a/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleDaemonExecutor.java
 
b/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleDaemonExecutor.java
index 84f5cd4..4312463 100644
--- 
a/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleDaemonExecutor.java
+++ 
b/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleDaemonExecutor.java
@@ -274,15 +274,16 @@ public final class GradleDaemonExecutor extends 
AbstractGradleExecutor {
     @Messages("LBL_ABORTING_BUILD=Aborting Build...")
     @Override
     public boolean cancel() {
-        if (cancelTokenSource != null) {
+        if (!cancelling && (cancelTokenSource != null)) {
             handle.switchToIndeterminate();
             handle.setDisplayName(Bundle.LBL_ABORTING_BUILD());
             // Closing out and err streams to prevent ambigous output 
NETBEANS-2038
             closeInOutErr();
             cancelling = true;
             cancelTokenSource.cancel();
+            return true;
         }
-        return true;
+        return false;
     }
 
 }
diff --git 
a/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleExecutor.java 
b/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleExecutor.java
index ead2400..0322a5c 100644
--- a/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleExecutor.java
+++ b/groovy/gradle/src/org/netbeans/modules/gradle/execute/GradleExecutor.java
@@ -20,13 +20,14 @@
 package org.netbeans.modules.gradle.execute;
 
 import org.openide.execution.ExecutorTask;
+import org.openide.util.Cancellable;
 import org.openide.windows.InputOutput;
 
 /**
  *
  * @author Laszlo Kishalmi
  */
-public interface GradleExecutor extends Runnable {
+public interface GradleExecutor extends Runnable, Cancellable {
     
     void setTask(ExecutorTask task);
     InputOutput getInputOutput();


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@netbeans.apache.org
For additional commands, e-mail: commits-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to