IDEA-168525 Possibility to listen to ant build events

This commit is contained in:
Eugene Zhuravlev
2017-05-15 14:33:57 +02:00
parent 3598fc3606
commit cb16f07461
5 changed files with 170 additions and 8 deletions
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.ant.config.execution;
import com.intellij.lang.ant.config.AntBuildFile;
public class AntBeforeExecutionEvent extends AntExecutionEvent{
private final AntBuildMessageView myMessageView;
AntBeforeExecutionEvent(AntBuildFile buildFile, AntBuildMessageView messageView) {
super(buildFile);
myMessageView = messageView;
}
public AntBuildMessageView getMessageView() {
return myMessageView;
}
}
@@ -0,0 +1,31 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.ant.config.execution;
import com.intellij.lang.ant.config.AntBuildFile;
class AntExecutionEvent {
private final AntBuildFile myBuildFile;
protected AntExecutionEvent(AntBuildFile buildFile) {
myBuildFile = buildFile;
}
public AntBuildFile getBuildFile() {
return myBuildFile;
}
}
@@ -0,0 +1,26 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.ant.config.execution;
import com.intellij.util.messages.Topic;
public interface AntExecutionListener {
Topic<AntExecutionListener> TOPIC = Topic.create("AntExecutionListener", AntExecutionListener.class);
void beforeExecution(AntBeforeExecutionEvent event);
void buildFinished(AntFinishedExecutionEvent event);
}
@@ -0,0 +1,41 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.lang.ant.config.execution;
import com.intellij.lang.ant.config.AntBuildFile;
public class AntFinishedExecutionEvent extends AntExecutionEvent{
private final Status myStatus;
private final int myErrorCount;
public enum Status {
SUCCESS, CANCELED, FAILURE;
}
AntFinishedExecutionEvent(AntBuildFile buildFile, Status status, int errorCount) {
super(buildFile);
myStatus = status;
myErrorCount = errorCount;
}
public Status getStatus() {
return myStatus;
}
public int getErrorCount() {
return myErrorCount;
}
}
@@ -26,6 +26,7 @@ import com.intellij.execution.util.ExecutionErrorDialog;
import com.intellij.history.LocalHistory;
import com.intellij.ide.macro.Macro;
import com.intellij.lang.ant.AntBundle;
import com.intellij.lang.ant.config.AntBuildFile;
import com.intellij.lang.ant.config.AntBuildFileBase;
import com.intellij.lang.ant.config.AntBuildListener;
import com.intellij.lang.ant.config.AntBuildTarget;
@@ -66,7 +67,9 @@ public final class ExecutionHandler {
List<BuildFileProperty> additionalProperties,
@NotNull final AntBuildListener antBuildListener) {
AntBuildTarget target = antRunConfiguration.getTarget();
if (target == null) return null;
if (target == null) {
return null;
}
FutureResult<ProcessHandler> result = runBuildImpl((AntBuildFileBase)target.getModel().getBuildFile(),
new String[]{target.getName()},
null,
@@ -106,6 +109,7 @@ public final class ExecutionHandler {
@NotNull final AntBuildListener antBuildListener, final boolean waitFor) {
final AntBuildMessageView messageView;
final GeneralCommandLine commandLine;
final AntBuildListenerWrapper listenerWrapper = new AntBuildListenerWrapper(buildFile, antBuildListener);
final Project project = buildFile.getProject();
try {
FileDocumentManager.getInstance().saveAllDocuments();
@@ -120,23 +124,25 @@ public final class ExecutionHandler {
messageView = prepareMessageView(buildMessageViewToReuse, buildFile, targets, additionalProperties);
commandLine = builder.getCommandLine().toCommandLine();
messageView.setBuildCommandLine(commandLine.getCommandLineString());
project.getMessageBus().syncPublisher(AntExecutionListener.TOPIC).beforeExecution(new AntBeforeExecutionEvent(buildFile, messageView));
}
catch (RunCanceledException e) {
e.showMessage(project, AntBundle.message("run.ant.error.dialog.title"));
antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
listenerWrapper.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
return null;
}
catch (CantRunException e) {
ExecutionErrorDialog.show(e, AntBundle.message("cant.run.ant.error.dialog.title"), project);
antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
listenerWrapper.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
return null;
}
catch (Macro.ExecutionCancelledException e) {
antBuildListener.buildFinished(AntBuildListener.ABORTED, 0);
listenerWrapper.buildFinished(AntBuildListener.ABORTED, 0);
return null;
}
catch (Throwable e) {
antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
listenerWrapper.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
LOG.error(e);
return null;
}
@@ -148,12 +154,12 @@ public final class ExecutionHandler {
}
public void onCancel() {
antBuildListener.buildFinished(AntBuildListener.ABORTED, 0);
listenerWrapper.buildFinished(AntBuildListener.ABORTED, 0);
}
public void run(@NotNull final ProgressIndicator indicator) {
try {
ProcessHandler handler = runBuild(indicator, messageView, buildFile, antBuildListener, commandLine);
ProcessHandler handler = runBuild(indicator, messageView, buildFile, listenerWrapper, commandLine);
future.set(handler);
if (waitFor && handler != null) {
handler.waitFor();
@@ -161,7 +167,7 @@ public final class ExecutionHandler {
}
catch (Throwable e) {
LOG.error(e);
antBuildListener.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
listenerWrapper.buildFinished(AntBuildListener.FAILED_TO_RUN, 0);
}
}
}.queue();
@@ -306,4 +312,31 @@ public final class ExecutionHandler {
}
return messageView;
}
private static class AntBuildListenerWrapper implements AntBuildListener {
@NotNull
private final AntBuildFile myBuildFile;
@NotNull
private final AntBuildListener myDelegate;
AntBuildListenerWrapper(@NotNull AntBuildFile buildFile, @NotNull AntBuildListener delegate) {
myBuildFile = buildFile;
myDelegate = delegate;
}
@Override
public void buildFinished(int state, int errorCount) {
try {
final AntFinishedExecutionEvent.Status status = state == AntBuildListener.ABORTED? AntFinishedExecutionEvent.Status.CANCELED :
state == AntBuildListener.FAILED_TO_RUN? AntFinishedExecutionEvent.Status.FAILURE :
AntFinishedExecutionEvent.Status.SUCCESS;
myBuildFile.getProject().getMessageBus().syncPublisher(AntExecutionListener.TOPIC).buildFinished(
new AntFinishedExecutionEvent(myBuildFile, status, errorCount)
);
}
finally {
myDelegate.buildFinished(state, errorCount);
}
}
}
}