gradle: check for gradle cancellation support reworked

This commit is contained in:
Vladislav.Soroka
2015-03-25 18:12:13 +03:00
parent 3338697d40
commit 5faa3dfd9c
9 changed files with 121 additions and 97 deletions
@@ -0,0 +1,42 @@
/*
* Copyright 2000-2015 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.openapi.externalSystem.service.execution;
import com.intellij.openapi.externalSystem.model.ExternalSystemException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Vladislav.Soroka
* @since 12/12/2014
*/
public class NotSupportedException extends ExternalSystemException {
public NotSupportedException(@Nullable String message) {
super(message);
}
public NotSupportedException(@Nullable Throwable cause) {
super(cause);
}
public NotSupportedException(@Nullable String message, @NotNull String... quickFixes) {
super(message, quickFixes);
}
public NotSupportedException(@Nullable String message, @Nullable Throwable cause, @NotNull String... quickFixes) {
super(message, cause, quickFixes);
}
}
@@ -6,7 +6,8 @@ import com.intellij.openapi.externalSystem.model.ProjectSystemId;
import com.intellij.openapi.externalSystem.model.task.*;
import com.intellij.openapi.externalSystem.service.ExternalSystemFacadeManager;
import com.intellij.openapi.externalSystem.service.RemoteExternalSystemFacade;
import com.intellij.openapi.externalSystem.service.notification.ExternalSystemProgressNotificationManager;
import com.intellij.openapi.externalSystem.service.execution.NotSupportedException;
import com.intellij.openapi.externalSystem.service.notification.*;
import com.intellij.openapi.externalSystem.util.ExternalSystemBundle;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
@@ -190,6 +191,12 @@ public abstract class AbstractExternalSystemTask implements ExternalSystemTask {
setState(result ? ExternalSystemTaskState.CANCELED : ExternalSystemTaskState.CANCELLATION_FAILED);
return result;
}
catch (NotSupportedException e) {
NotificationData notification =
new NotificationData("Cancellation failed", e.getMessage(), NotificationCategory.WARNING, NotificationSource.PROJECT_SYNC);
notification.setBalloonNotification(true);
ExternalSystemNotificationManager.getInstance(getIdeProject()).showNotification(getExternalSystemId(), notification);
}
catch (Throwable e) {
setState(ExternalSystemTaskState.CANCELLATION_FAILED);
myError.set(e);
@@ -80,7 +80,7 @@ public class GradleBuildProcessParametersProvider extends BuildProcessParameters
File[] children = FileUtil.notNullize(gradleLibDir.listFiles());
for (File child : children) {
final String fileName = child.getName();
if (fileName.endsWith(".jar") && !fileName.startsWith("logback-") && child.isFile()) {
if (fileName.endsWith(".jar") && child.isFile()) {
classpath.add(child.getAbsolutePath());
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2015 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 org.jetbrains.plugins.gradle.service.execution;
import com.intellij.openapi.externalSystem.service.execution.NotSupportedException;
import org.gradle.tooling.CancellationToken;
import org.gradle.tooling.CancellationTokenSource;
/**
* @author Vladislav.Soroka
* @since 3/25/2015
*/
public class UnsupportedCancellationToken implements CancellationTokenSource {
@Override
public void cancel() {
throw new NotSupportedException("Configured version of Gradle does not support cancellation. \nPlease, use Gradle 2.1 or newer.");
}
@Override
public CancellationToken token() {
return null;
}
}
@@ -35,6 +35,7 @@ import org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory;
import org.gradle.tooling.internal.consumer.DefaultGradleConnector;
import org.gradle.tooling.internal.consumer.Distribution;
import org.gradle.tooling.model.build.BuildEnvironment;
import org.gradle.util.GradleVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.settings.DistributionType;
@@ -443,7 +444,18 @@ public class GradleExecutionHelper {
}
@Nullable
private static BuildEnvironment getBuildEnvironment(@NotNull ProjectConnection connection) {
public static GradleVersion getGradleVersion(@NotNull ProjectConnection connection) {
final BuildEnvironment buildEnvironment = getBuildEnvironment(connection);
GradleVersion gradleVersion = null;
if (buildEnvironment != null) {
gradleVersion = GradleVersion.version(buildEnvironment.getGradle().getGradleVersion());
}
return gradleVersion;
}
@Nullable
public static BuildEnvironment getBuildEnvironment(@NotNull ProjectConnection connection) {
try {
return connection.getModel(BuildEnvironment.class);
}
@@ -44,10 +44,12 @@ import org.gradle.tooling.model.build.BuildEnvironment;
import org.gradle.tooling.model.idea.BasicIdeaProject;
import org.gradle.tooling.model.idea.IdeaModule;
import org.gradle.tooling.model.idea.IdeaProject;
import org.gradle.util.GradleVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.model.ProjectImportAction;
import org.jetbrains.plugins.gradle.remote.impl.GradleLibraryNamesMixer;
import org.jetbrains.plugins.gradle.service.execution.UnsupportedCancellationToken;
import org.jetbrains.plugins.gradle.settings.ClassHolder;
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings;
import org.jetbrains.plugins.gradle.util.GradleConstants;
@@ -150,6 +152,11 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
parametersList.addProperty(jvmArg.getKey(), jvmArg.getValue());
}
final BuildEnvironment buildEnvironment = GradleExecutionHelper.getBuildEnvironment(resolverCtx.getConnection());
GradleVersion gradleVersion = null;
if (buildEnvironment != null) {
gradleVersion = GradleVersion.version(buildEnvironment.getGradle().getGradleVersion());
}
BuildActionExecuter<ProjectImportAction.AllModels> buildActionExecutor = resolverCtx.getConnection().action(projectImportAction);
@@ -169,6 +176,9 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
buildActionExecutor.withCancellationToken(cancellationTokenSource.token());
synchronized (myCancellationMap) {
myCancellationMap.putValue(resolverCtx.getExternalSystemTaskId(), cancellationTokenSource);
if (gradleVersion != null && gradleVersion.compareTo(GradleVersion.version("2.1")) < 0) {
myCancellationMap.putValue(resolverCtx.getExternalSystemTaskId(), new UnsupportedCancellationToken());
}
}
allModels = buildActionExecutor.run();
if (allModels == null) {
@@ -196,7 +206,6 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
}
}
final BuildEnvironment buildEnvironment = getBuildEnvironment(resolverCtx);
allModels.setBuildEnvironment(buildEnvironment);
resolverCtx.setModels(allModels);
@@ -280,16 +289,6 @@ public class GradleProjectResolver implements ExternalSystemProjectResolver<Grad
return projectDataNode;
}
@Nullable
private static BuildEnvironment getBuildEnvironment(@NotNull ProjectResolverContext resolverCtx) {
try {
return resolverCtx.getConnection().getModel(BuildEnvironment.class);
}
catch (Exception e) {
return null;
}
}
private void handleBuildSrcProject(@NotNull final DataNode<ProjectData> resultProjectDataNode,
@NotNull final ProjectConnectionDataNodeFunction projectConnectionDataNodeFunction) {
@@ -32,11 +32,13 @@ import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.CancellationTokenSource;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.util.GradleVersion;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.service.project.GradleExecutionHelper;
import org.jetbrains.plugins.gradle.service.project.GradleProjectResolver;
import org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension;
import org.jetbrains.plugins.gradle.service.execution.UnsupportedCancellationToken;
import org.jetbrains.plugins.gradle.settings.GradleExecutionSettings;
import org.jetbrains.plugins.gradle.util.GradleConstants;
@@ -118,11 +120,17 @@ public class GradleTaskManager extends AbstractExternalSystemTaskManager<GradleE
}
}
GradleVersion gradleVersion = GradleExecutionHelper.getGradleVersion(connection);
BuildLauncher launcher = myHelper.getBuildLauncher(id, connection, settings, listener, vmOptions, scriptParameters);
launcher.forTasks(ArrayUtil.toStringArray(taskNames));
final CancellationTokenSource cancellationTokenSource = GradleConnector.newCancellationTokenSource();
launcher.withCancellationToken(cancellationTokenSource.token());
myCancellationMap.put(id, cancellationTokenSource);
if (gradleVersion != null && gradleVersion.compareTo(GradleVersion.version("2.1")) < 0) {
myCancellationMap.put(id, new UnsupportedCancellationToken());
} else {
final CancellationTokenSource cancellationTokenSource = GradleConnector.newCancellationTokenSource();
launcher.withCancellationToken(cancellationTokenSource.token());
myCancellationMap.put(id, cancellationTokenSource);
}
try {
launcher.run();
}
@@ -1,23 +1,6 @@
package org.jetbrains.plugins.gradle.util;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import ch.qos.logback.core.AppenderBase;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTask;
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskState;
import com.intellij.openapi.externalSystem.service.internal.ExternalSystemProcessingManager;
import com.intellij.openapi.externalSystem.service.notification.ExternalSystemNotificationManager;
import com.intellij.openapi.externalSystem.service.notification.NotificationCategory;
import com.intellij.openapi.externalSystem.service.notification.NotificationData;
import com.intellij.openapi.externalSystem.service.notification.NotificationSource;
import com.intellij.openapi.project.Project;
import org.gradle.tooling.internal.consumer.connection.NonCancellableConsumerConnectionAdapter;
import org.jetbrains.plugins.gradle.service.notification.OpenGradleSettingsCallback;
import java.util.List;
/**
* @author Denis Zhdanov
@@ -27,65 +10,6 @@ public class GradleLog {
public static final Logger LOG = Logger.getInstance(GradleLog.class);
private static final String GRADLE_PROVIDER_DOES_NOT_SUPPORT_CANCELLATION_MESSAGE =
"Note: Version of Gradle provider does not support cancellation. Upgrade your Gradle build.";
private GradleLog() {
}
public static class GradleLogbackAppender extends AppenderBase<ILoggingEvent> {
@Override
protected void append(ILoggingEvent event) {
ThrowableProxy throwableProxy =
event.getThrowableProxy() instanceof ThrowableProxy ? (ThrowableProxy)event.getThrowableProxy() : null;
Throwable throwable = throwableProxy == null ? null : throwableProxy.getThrowable();
switch (event.getLevel().toInt()) {
case Level.ALL_INT:
case Level.TRACE_INT:
case Level.DEBUG_INT:
LOG.debug(event.getFormattedMessage(), throwable);
break;
case Level.INFO_INT:
LOG.info(event.getFormattedMessage(), throwable);
break;
case Level.WARN_INT:
LOG.warn(event.getFormattedMessage(), throwable);
break;
case Level.ERROR_INT:
LOG.error(event.getFormattedMessage(), throwable);
break;
case Level.OFF_INT:
break;
default:
LOG.debug(event.getFormattedMessage(), throwable);
break;
}
if (NonCancellableConsumerConnectionAdapter.class.getName().equals(event.getLoggerName()) &&
GRADLE_PROVIDER_DOES_NOT_SUPPORT_CANCELLATION_MESSAGE.equals(event.getMessage())) {
// see org.gradle.tooling.internal.consumer.connection.NonCancellableConsumerConnectionAdapter#handleCancellationPreOperation
ExternalSystemProcessingManager processingManager = ServiceManager.getService(ExternalSystemProcessingManager.class);
final List<ExternalSystemTask> canceledTasks =
processingManager
.findTasksOfState(GradleConstants.SYSTEM_ID, ExternalSystemTaskState.CANCELING, ExternalSystemTaskState.CANCELED);
for (ExternalSystemTask canceledTask : canceledTasks) {
final Project project = canceledTask.getId().findProject();
if (project != null) {
String errorMessage = String.format("%s <a href=\"%s\">Open Gradle settings</a>",
"Configured version of Gradle does not support cancellation. Please, use Gradle 2.1 or newer.\n",
OpenGradleSettingsCallback.ID);
NotificationData notification = new NotificationData(
"Gradle supports cancellation since 2.1 version", errorMessage, NotificationCategory.WARNING,
NotificationSource.PROJECT_SYNC);
notification.setListener(OpenGradleSettingsCallback.ID, new OpenGradleSettingsCallback(project));
notification.setBalloonNotification(true);
ExternalSystemNotificationManager.getInstance(project).showNotification(GradleConstants.SYSTEM_ID, notification);
}
}
}
}
}
}
@@ -55,10 +55,6 @@ public class ProjectImportAction implements BuildAction<ProjectImportAction.AllM
}
AllModels allModels = new AllModels(ideaProject);
// TODO ask gradle guys why there is always null got for BuildEnvironment model
//allModels.setBuildEnvironment(controller.findModel(BuildEnvironment.class));
addExtraProject(controller, allModels, null);
for (IdeaModule module : ideaProject.getModules()) {
addExtraProject(controller, allModels, module);