Skip to content

Commit

Permalink
[NETBEANS-3279] Added RunUtils.cancelGradle() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
lkishalmi committed Nov 2, 2019
1 parent 0d1d701 commit bc8cf30
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
15 changes: 15 additions & 0 deletions groovy/gradle/apichanges.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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: -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
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;
Expand Down Expand Up @@ -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);
Expand All @@ -99,15 +101,37 @@ public static FileObject[] extractFileObjectsfromLookup(Lookup lookup) {
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);

Expand All @@ -129,6 +153,20 @@ public static RunConfig createRunConfig(Project project, String action, String d
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
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;
Expand All @@ -39,7 +38,6 @@
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;
Expand All @@ -49,7 +47,7 @@
*
* @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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,16 @@ private void showAbort() {
@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit bc8cf30

Please sign in to comment.