Search in sources :

Example 11 with ExternalSystemException

use of com.intellij.openapi.externalSystem.model.ExternalSystemException 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 12 with ExternalSystemException

use of com.intellij.openapi.externalSystem.model.ExternalSystemException in project intellij-community by JetBrains.

the class GradleExecutionHelper method execute.

public <T> T execute(@NotNull String projectPath, @Nullable GradleExecutionSettings settings, @NotNull Function<ProjectConnection, T> f) {
    final String projectDir;
    final File projectPathFile = new File(projectPath);
    if (projectPathFile.isFile() && projectPath.endsWith(GradleConstants.EXTENSION) && projectPathFile.getParent() != null) {
        projectDir = projectPathFile.getParent();
    } else {
        projectDir = projectPath;
    }
    String userDir = null;
    if (!GradleEnvironment.ADJUST_USER_DIR) {
        try {
            userDir = System.getProperty("user.dir");
            if (userDir != null)
                System.setProperty("user.dir", projectDir);
        } catch (Exception ignore) {
        }
    }
    ProjectConnection connection = getConnection(projectDir, settings);
    try {
        return f.fun(connection);
    } catch (ExternalSystemException e) {
        throw e;
    } catch (Throwable e) {
        LOG.debug("Gradle execution error", e);
        Throwable rootCause = ExceptionUtil.getRootCause(e);
        throw new ExternalSystemException(ExceptionUtil.getMessage(rootCause));
    } finally {
        try {
            connection.close();
            if (userDir != null) {
                // restore original user.dir property
                System.setProperty("user.dir", userDir);
            }
        } catch (Throwable e) {
            LOG.debug("Gradle connection close error", e);
        }
    }
}
Also used : ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) File(java.io.File) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) IOException(java.io.IOException)

Example 13 with ExternalSystemException

use of com.intellij.openapi.externalSystem.model.ExternalSystemException in project android by JetBrains.

the class SimulatedSyncErrors method registerSyncErrorToSimulate.

public static void registerSyncErrorToSimulate(@NotNull String errorMessage) {
    verifyIsTestMode();
    ExternalSystemException exception = new ExternalSystemException(errorMessage);
    store(exception);
}
Also used : ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) LocationAwareExternalSystemException(com.intellij.openapi.externalSystem.model.LocationAwareExternalSystemException)

Example 14 with ExternalSystemException

use of com.intellij.openapi.externalSystem.model.ExternalSystemException in project android by JetBrains.

the class SimulatedSyncErrors method registerSyncErrorToSimulate.

public static void registerSyncErrorToSimulate(@NotNull Throwable cause) {
    verifyIsTestMode();
    ExternalSystemException exception = new ExternalSystemException(cause.getMessage());
    exception.initCause(cause);
    store(exception);
}
Also used : ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) LocationAwareExternalSystemException(com.intellij.openapi.externalSystem.model.LocationAwareExternalSystemException)

Example 15 with ExternalSystemException

use of com.intellij.openapi.externalSystem.model.ExternalSystemException in project android by JetBrains.

the class SimulatedSyncErrors method simulateRegisteredSyncError.

public static void simulateRegisteredSyncError() {
    Application application = ApplicationManager.getApplication();
    ExternalSystemException error = application.getUserData(SIMULATED_ERROR_KEY);
    if (error != null) {
        verifyIsTestMode();
        application.putUserData(SIMULATED_ERROR_KEY, null);
        throw error;
    }
}
Also used : ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) LocationAwareExternalSystemException(com.intellij.openapi.externalSystem.model.LocationAwareExternalSystemException) Application(com.intellij.openapi.application.Application)

Aggregations

ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)20 File (java.io.File)6 IOException (java.io.IOException)5 NotNull (org.jetbrains.annotations.NotNull)5 LocationAwareExternalSystemException (com.intellij.openapi.externalSystem.model.LocationAwareExternalSystemException)4 Nullable (org.jetbrains.annotations.Nullable)3 Application (com.intellij.openapi.application.Application)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 Message (com.android.ide.common.blame.Message)1 SyncErrorHandler (com.android.tools.idea.gradle.project.sync.errors.SyncErrorHandler)1 MessageType (com.android.tools.idea.gradle.project.sync.messages.MessageType)1 SyncMessage (com.android.tools.idea.gradle.project.sync.messages.SyncMessage)1 SyncMessages (com.android.tools.idea.gradle.project.sync.messages.SyncMessages)1 LocalProperties (com.android.tools.idea.gradle.util.LocalProperties)1 PositionInFile (com.android.tools.idea.gradle.util.PositionInFile)1 ValidationResult (com.android.tools.idea.sdk.SdkPaths.ValidationResult)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 AndroidStudioEvent (com.google.wireless.android.sdk.stats.AndroidStudioEvent)1