Search in sources :

Example 1 with CancellationTokenSource

use of org.gradle.tooling.CancellationTokenSource in project android by JetBrains.

the class BuildStopper method attemptToStopBuild.

void attemptToStopBuild(@NotNull ExternalSystemTaskId id, @Nullable ProgressIndicator progressIndicator) {
    if (progressIndicator != null) {
        if (progressIndicator.isCanceled()) {
            return;
        }
        if (progressIndicator.isRunning()) {
            progressIndicator.setText("Stopping Gradle build...");
            progressIndicator.cancel();
        }
    }
    CancellationTokenSource token = remove(id);
    if (token != null) {
        token.cancel();
    }
}
Also used : CancellationTokenSource(org.gradle.tooling.CancellationTokenSource)

Example 2 with CancellationTokenSource

use of org.gradle.tooling.CancellationTokenSource in project android by JetBrains.

the class BuildStopper method createAndRegisterTokenSource.

@NotNull
CancellationTokenSource createAndRegisterTokenSource(@NotNull ExternalSystemTaskId id) {
    CancellationTokenSource tokenSource = GradleConnector.newCancellationTokenSource();
    myMap.put(id, tokenSource);
    return tokenSource;
}
Also used : CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with CancellationTokenSource

use of org.gradle.tooling.CancellationTokenSource in project android by JetBrains.

the class BuildStopperTest method createAndRegisterTokenSource.

@Test
public void createAndRegisterTokenSource() {
    CancellationTokenSource tokenSource = myMapping.createAndRegisterTokenSource(myId);
    assertSame(tokenSource, myMapping.get(myId));
}
Also used : CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) Test(org.junit.Test)

Example 4 with CancellationTokenSource

use of org.gradle.tooling.CancellationTokenSource in project intellij-community by JetBrains.

the class GradleTaskManager method executeTasks.

@Override
public void executeTasks(@NotNull final ExternalSystemTaskId id, @NotNull final List<String> taskNames, @NotNull String projectPath, @Nullable GradleExecutionSettings settings, @Nullable final String jvmAgentSetup, @NotNull final ExternalSystemTaskNotificationListener listener) throws ExternalSystemException {
    // TODO add support for external process mode
    if (ExternalSystemApiUtil.isInProcessMode(GradleConstants.SYSTEM_ID)) {
        for (GradleTaskManagerExtension gradleTaskManagerExtension : GradleTaskManagerExtension.EP_NAME.getExtensions()) {
            if (gradleTaskManagerExtension.executeTasks(id, taskNames, projectPath, settings, jvmAgentSetup, listener)) {
                return;
            }
        }
    }
    GradleExecutionSettings effectiveSettings = settings == null ? new GradleExecutionSettings(null, null, DistributionType.BUNDLED, false) : settings;
    Function<ProjectConnection, Void> f = connection -> {
        final List<String> initScripts = ContainerUtil.newArrayList();
        final GradleProjectResolverExtension projectResolverChain = GradleProjectResolver.createProjectResolverChain(effectiveSettings);
        for (GradleProjectResolverExtension resolverExtension = projectResolverChain; resolverExtension != null; resolverExtension = resolverExtension.getNext()) {
            final String resolverClassName = resolverExtension.getClass().getName();
            resolverExtension.enhanceTaskProcessing(taskNames, jvmAgentSetup, script -> {
                if (StringUtil.isNotEmpty(script)) {
                    ContainerUtil.addAllNotNull(initScripts, "//-- Generated by " + resolverClassName, script, "//");
                }
            });
        }
        final String initScript = effectiveSettings.getUserData(INIT_SCRIPT_KEY);
        if (StringUtil.isNotEmpty(initScript)) {
            ContainerUtil.addAll(initScripts, "//-- Additional script", initScript, "//");
        }
        if (!initScripts.isEmpty()) {
            try {
                File tempFile = GradleExecutionHelper.writeToFileGradleInitScript(StringUtil.join(initScripts, SystemProperties.getLineSeparator()));
                effectiveSettings.withArguments(GradleConstants.INIT_SCRIPT_CMD_OPTION, tempFile.getAbsolutePath());
            } catch (IOException e) {
                throw new ExternalSystemException(e);
            }
        }
        GradleVersion gradleVersion = GradleExecutionHelper.getGradleVersion(connection);
        if (gradleVersion != null && gradleVersion.compareTo(GradleVersion.version("2.5")) < 0) {
            listener.onStatusChange(new ExternalSystemTaskExecutionEvent(id, new ExternalSystemProgressEventUnsupportedImpl(gradleVersion + " does not support executions view")));
        }
        for (GradleBuildParticipant buildParticipant : effectiveSettings.getExecutionWorkspace().getBuildParticipants()) {
            effectiveSettings.withArguments(GradleConstants.INCLUDE_BUILD_CMD_OPTION, buildParticipant.getProjectPath());
        }
        BuildLauncher launcher = myHelper.getBuildLauncher(id, connection, effectiveSettings, listener);
        launcher.forTasks(ArrayUtil.toStringArray(taskNames));
        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();
        } finally {
            myCancellationMap.remove(id);
        }
        return null;
    };
    myHelper.execute(projectPath, effectiveSettings, f);
}
Also used : GradleExecutionSettings(org.jetbrains.plugins.gradle.settings.GradleExecutionSettings) ExternalSystemTaskNotificationListener(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener) ArrayUtil(com.intellij.util.ArrayUtil) GradleExecutionSettings(org.jetbrains.plugins.gradle.settings.GradleExecutionSettings) GradleProjectResolver(org.jetbrains.plugins.gradle.service.project.GradleProjectResolver) ContainerUtil(com.intellij.util.containers.ContainerUtil) GradleExecutionHelper(org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper) ExternalSystemApiUtil(com.intellij.openapi.externalSystem.util.ExternalSystemApiUtil) UnsupportedCancellationToken(org.jetbrains.plugins.gradle.service.execution.UnsupportedCancellationToken) Map(java.util.Map) GradleConnector(org.gradle.tooling.GradleConnector) GradleBuildParticipant(org.jetbrains.plugins.gradle.settings.GradleBuildParticipant) GradleProjectResolverExtension(org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension) GradleVersion(org.gradle.util.GradleVersion) BuildLauncher(org.gradle.tooling.BuildLauncher) ExternalSystemTaskManager(com.intellij.openapi.externalSystem.task.ExternalSystemTaskManager) StringUtil(com.intellij.openapi.util.text.StringUtil) ExternalSystemTaskExecutionEvent(com.intellij.openapi.externalSystem.model.task.event.ExternalSystemTaskExecutionEvent) Key(com.intellij.openapi.util.Key) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) IOException(java.io.IOException) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable) ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) DistributionType(org.jetbrains.plugins.gradle.settings.DistributionType) List(java.util.List) SystemProperties(com.intellij.util.SystemProperties) Function(com.intellij.util.Function) ExternalSystemProgressEventUnsupportedImpl(com.intellij.openapi.externalSystem.model.task.event.ExternalSystemProgressEventUnsupportedImpl) GradleConstants(org.jetbrains.plugins.gradle.util.GradleConstants) ProjectConnection(org.gradle.tooling.ProjectConnection) NotNull(org.jetbrains.annotations.NotNull) GradleProjectResolverExtension(org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension) UnsupportedCancellationToken(org.jetbrains.plugins.gradle.service.execution.UnsupportedCancellationToken) ProjectConnection(org.gradle.tooling.ProjectConnection) ExternalSystemTaskExecutionEvent(com.intellij.openapi.externalSystem.model.task.event.ExternalSystemTaskExecutionEvent) IOException(java.io.IOException) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) BuildLauncher(org.gradle.tooling.BuildLauncher) CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) List(java.util.List) GradleVersion(org.gradle.util.GradleVersion) File(java.io.File) GradleBuildParticipant(org.jetbrains.plugins.gradle.settings.GradleBuildParticipant) ExternalSystemProgressEventUnsupportedImpl(com.intellij.openapi.externalSystem.model.task.event.ExternalSystemProgressEventUnsupportedImpl)

Example 5 with CancellationTokenSource

use of org.gradle.tooling.CancellationTokenSource in project android by JetBrains.

the class NewGradleSync method sync.

@VisibleForTesting
@NotNull
Callback sync(@NotNull Project project) {
    Callback callback = new Callback();
    if (project.isDisposed()) {
        callback.reject(String.format("Project '%1$s' is already disposed", project.getName()));
    }
    // TODO: Handle sync cancellation.
    // TODO: Show Gradle output.
    GradleExecutionSettings executionSettings = getOrCreateGradleExecutionSettings(project, useEmbeddedGradle());
    Function<ProjectConnection, Void> syncFunction = connection -> {
        List<Class<?>> modelTypes = Lists.newArrayList(AndroidProject.class, NativeAndroidProject.class, GradleBuild.class, ModuleExtendedModel.class);
        BuildActionExecuter<SyncAction.ProjectModels> executor = connection.action(new SyncAction(modelTypes));
        ExternalSystemTaskNotificationListener listener = new ExternalSystemTaskNotificationListenerAdapter() {
        };
        List<String> commandLineArgs = myCommandLineArgs.get(project);
        ExternalSystemTaskId id = createId(project);
        prepare(executor, id, executionSettings, listener, Collections.emptyList(), commandLineArgs, connection);
        CancellationTokenSource cancellationTokenSource = newCancellationTokenSource();
        executor.withCancellationToken(cancellationTokenSource.token());
        try {
            SyncAction.ProjectModels models = executor.run();
            callback.setDone(models);
        } catch (RuntimeException e) {
            callback.setRejected(e);
        }
        return null;
    };
    myHelper.execute(getBaseDirPath(project).getPath(), executionSettings, syncFunction);
    return callback;
}
Also used : GradleExecutionSettings(org.jetbrains.plugins.gradle.settings.GradleExecutionSettings) GradleUtil.getOrCreateGradleExecutionSettings(com.android.tools.idea.gradle.util.GradleUtil.getOrCreateGradleExecutionSettings) ExternalSystemTaskNotificationListener(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener) GradleExecutionSettings(org.jetbrains.plugins.gradle.settings.GradleExecutionSettings) CommandLineArgs(com.android.tools.idea.gradle.project.sync.common.CommandLineArgs) ExceptionUtil.getRootCause(com.intellij.util.ExceptionUtil.getRootCause) GradleUtil.getOrCreateGradleExecutionSettings(com.android.tools.idea.gradle.util.GradleUtil.getOrCreateGradleExecutionSettings) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter) GradleExecutionHelper(org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper) Lists(com.google.common.collect.Lists) Task(com.intellij.openapi.progress.Task) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) GRADLE_SYSTEM_ID(com.android.tools.idea.gradle.util.GradleUtil.GRADLE_SYSTEM_ID) RESOLVE_PROJECT(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType.RESOLVE_PROJECT) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) GradleConnector.newCancellationTokenSource(org.gradle.tooling.GradleConnector.newCancellationTokenSource) GradleBuild(org.gradle.tooling.model.gradle.GradleBuild) ActionCallback(com.intellij.openapi.util.ActionCallback) AndroidProject(com.android.builder.model.AndroidProject) ProgressExecutionMode(com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode) StringUtil.isEmpty(com.intellij.openapi.util.text.StringUtil.isEmpty) UIUtil.invokeAndWaitIfNeeded(com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded) ModuleExtendedModel(org.jetbrains.plugins.gradle.model.ModuleExtendedModel) GradleExecutionHelper.prepare(org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper.prepare) Nullable(org.jetbrains.annotations.Nullable) ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) List(java.util.List) Projects.getBaseDirPath(com.android.tools.idea.gradle.util.Projects.getBaseDirPath) Function(com.intellij.util.Function) BuildActionExecuter(org.gradle.tooling.BuildActionExecuter) ProjectConnection(org.gradle.tooling.ProjectConnection) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) ProjectConnection(org.gradle.tooling.ProjectConnection) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) AndroidProject(com.android.builder.model.AndroidProject) BuildActionExecuter(org.gradle.tooling.BuildActionExecuter) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) ExternalSystemTaskNotificationListener(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener) ActionCallback(com.intellij.openapi.util.ActionCallback) ModuleExtendedModel(org.jetbrains.plugins.gradle.model.ModuleExtendedModel) GradleConnector.newCancellationTokenSource(org.gradle.tooling.GradleConnector.newCancellationTokenSource) CancellationTokenSource(org.gradle.tooling.CancellationTokenSource) List(java.util.List) GradleBuild(org.gradle.tooling.model.gradle.GradleBuild) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CancellationTokenSource (org.gradle.tooling.CancellationTokenSource)5 NotNull (org.jetbrains.annotations.NotNull)3 ExternalSystemTaskId (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId)2 ExternalSystemTaskNotificationListener (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener)2 Function (com.intellij.util.Function)2 List (java.util.List)2 ProjectConnection (org.gradle.tooling.ProjectConnection)2 Nullable (org.jetbrains.annotations.Nullable)2 GradleExecutionHelper (org.jetbrains.plugins.gradle.service.execution.GradleExecutionHelper)2 GradleExecutionSettings (org.jetbrains.plugins.gradle.settings.GradleExecutionSettings)2 AndroidProject (com.android.builder.model.AndroidProject)1 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 CommandLineArgs (com.android.tools.idea.gradle.project.sync.common.CommandLineArgs)1 GRADLE_SYSTEM_ID (com.android.tools.idea.gradle.util.GradleUtil.GRADLE_SYSTEM_ID)1 GradleUtil.getOrCreateGradleExecutionSettings (com.android.tools.idea.gradle.util.GradleUtil.getOrCreateGradleExecutionSettings)1 Projects.getBaseDirPath (com.android.tools.idea.gradle.util.Projects.getBaseDirPath)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Lists (com.google.common.collect.Lists)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)1