Search in sources :

Example 1 with ExternalSystemTaskNotificationListenerAdapter

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

the class GradleDependenciesImportingTest method assertCompileClasspathOrdering.

protected void assertCompileClasspathOrdering(String moduleName) {
    Module module = getModule(moduleName);
    String sourceSetName = getSourceSetName(module);
    assertNotNull("Can not find the sourceSet for the module", sourceSetName);
    ExternalSystemTaskExecutionSettings settings = new ExternalSystemTaskExecutionSettings();
    settings.setExternalProjectPath(getExternalProjectPath(module));
    String id = getExternalProjectId(module);
    String gradlePath = id.startsWith(":") ? trimEnd(id, sourceSetName) : "";
    settings.setTaskNames(Collections.singletonList(gradlePath + ":print" + capitalize(sourceSetName) + "CompileDependencies"));
    settings.setExternalSystemIdString(GradleConstants.SYSTEM_ID.getId());
    settings.setScriptParameters("--quiet");
    ExternalSystemProgressNotificationManager notificationManager = ServiceManager.getService(ExternalSystemProgressNotificationManager.class);
    StringBuilder gradleClasspath = new StringBuilder();
    ExternalSystemTaskNotificationListenerAdapter listener = new ExternalSystemTaskNotificationListenerAdapter() {

        @Override
        public void onTaskOutput(@NotNull ExternalSystemTaskId id, @NotNull String text, boolean stdOut) {
            gradleClasspath.append(text);
        }
    };
    notificationManager.addNotificationListener(listener);
    ExternalSystemUtil.runTask(settings, DefaultRunExecutor.EXECUTOR_ID, myProject, GradleConstants.SYSTEM_ID, null, ProgressExecutionMode.NO_PROGRESS_SYNC);
    notificationManager.removeNotificationListener(listener);
    List<String> ideClasspath = ContainerUtil.newArrayList();
    ModuleRootManager.getInstance(module).orderEntries().withoutSdk().withoutModuleSourceEntries().compileOnly().productionOnly().forEach(entry -> {
        if (entry instanceof ModuleOrderEntry) {
            Module moduleDep = ((ModuleOrderEntry) entry).getModule();
            String sourceSetDepName = getSourceSetName(moduleDep);
            assert sourceSetDepName != "main";
            String gradleProjectDepName = trimStart(trimEnd(getExternalProjectId(moduleDep), ":main"), ":");
            String version = getExternalProjectVersion(moduleDep);
            version = "unspecified".equals(version) ? "" : "-" + version;
            ideClasspath.add(gradleProjectDepName + version + ".jar");
        } else {
            ideClasspath.add(entry.getFiles(OrderRootType.CLASSES)[0].getName());
        }
        return true;
    });
    assertEquals(join(ideClasspath, " "), gradleClasspath.toString().trim());
}
Also used : ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) Module(com.intellij.openapi.module.Module) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter) NotNull(org.jetbrains.annotations.NotNull) ExternalSystemProgressNotificationManager(com.intellij.openapi.externalSystem.service.notification.ExternalSystemProgressNotificationManager) ExternalSystemTaskExecutionSettings(com.intellij.openapi.externalSystem.model.execution.ExternalSystemTaskExecutionSettings)

Example 2 with ExternalSystemTaskNotificationListenerAdapter

use of com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter 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)

Example 3 with ExternalSystemTaskNotificationListenerAdapter

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

the class AndroidGradleProjectResolverIdeaTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    initMocks(this);
    myProjectModel = new IdeaProjectStub("multiProject");
    myAndroidProject = TestProjects.createBasicProject(myProjectModel.getRootDir());
    myNativeAndroidProject = TestProjects.createNativeProject(myProjectModel.getRootDir());
    myAndroidModuleModel = myProjectModel.addModule(myAndroidProject.getName(), "androidTask");
    myNativeAndroidModuleModel = myProjectModel.addModule(myNativeAndroidProject.getName(), "nativeAndroidTask");
    myJavaModuleModel = myProjectModel.addModule("util", "compileJava", "jar", "classes");
    myProjectModel.addModule("notReallyAGradleProject");
    ProjectImportAction.AllModels allModels = new ProjectImportAction.AllModels(myProjectModel);
    allModels.addExtraProject(myAndroidProject, AndroidProject.class, myAndroidModuleModel);
    allModels.addExtraProject(myNativeAndroidProject, NativeAndroidProject.class, myNativeAndroidModuleModel);
    ExternalSystemTaskId id = ExternalSystemTaskId.create(SYSTEM_ID, RESOLVE_PROJECT, myProjectModel.getName());
    String projectPath = toSystemDependentName(myProjectModel.getBuildFile().getParent());
    ExternalSystemTaskNotificationListener notificationListener = new ExternalSystemTaskNotificationListenerAdapter() {
    };
    myResolverCtx = new DefaultProjectResolverContext(id, projectPath, null, mock(ProjectConnection.class), notificationListener, true);
    myResolverCtx.setModels(allModels);
    myProjectResolver = new AndroidGradleProjectResolver(myCommandLineArgs, myErrorHandler, myProjectFinder, myVariantSelector);
    myProjectResolver.setProjectResolverContext(myResolverCtx);
    GradleProjectResolverExtension next = new BaseGradleProjectResolverExtension();
    next.setProjectResolverContext(myResolverCtx);
    myProjectResolver.setNext(next);
}
Also used : GradleProjectResolverExtension(org.jetbrains.plugins.gradle.service.project.GradleProjectResolverExtension) BaseGradleProjectResolverExtension(org.jetbrains.plugins.gradle.service.project.BaseGradleProjectResolverExtension) ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) BaseGradleProjectResolverExtension(org.jetbrains.plugins.gradle.service.project.BaseGradleProjectResolverExtension) ExternalSystemTaskNotificationListener(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener) IdeaProjectStub(com.android.tools.idea.gradle.stubs.gradle.IdeaProjectStub) DefaultProjectResolverContext(org.jetbrains.plugins.gradle.service.project.DefaultProjectResolverContext) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter) ProjectImportAction(org.jetbrains.plugins.gradle.model.ProjectImportAction)

Example 4 with ExternalSystemTaskNotificationListenerAdapter

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

the class GradleRunnerUtil method attachTaskExecutionView.

public static DuplexConsoleView attachTaskExecutionView(@NotNull final Project project, @NotNull final ConsoleView consoleView, final boolean isTaskConsoleEnabledByDefault, @Nullable final String stateStorageKey, @NotNull final ProcessHandler processHandler, @NotNull final ExternalSystemTaskId taskId) {
    final String tripleStateStorageKey = stateStorageKey != null ? stateStorageKey + "_str" : null;
    if (stateStorageKey != null && isTaskConsoleEnabledByDefault && !PropertiesComponent.getInstance().isValueSet(tripleStateStorageKey)) {
        PropertiesComponent.getInstance().setValue(tripleStateStorageKey, Boolean.TRUE.toString());
        PropertiesComponent.getInstance().setValue(stateStorageKey, Boolean.TRUE);
    }
    final TaskExecutionView gradleExecutionConsole = new TaskExecutionView(project);
    final Ref<DuplexConsoleView> duplexConsoleViewRef = Ref.create();
    final DuplexConsoleView duplexConsoleView = new DuplexConsoleView<ConsoleView, ConsoleView>(gradleExecutionConsole, consoleView, stateStorageKey) {

        @Override
        public void enableConsole(boolean primary) {
            super.enableConsole(primary);
            if (stateStorageKey != null) {
                PropertiesComponent.getInstance().setValue(tripleStateStorageKey, Boolean.toString(primary));
            }
        }

        @NotNull
        @Override
        public AnAction[] createConsoleActions() {
            final DefaultActionGroup textActionGroup = new DefaultActionGroup() {

                @Override
                public void update(AnActionEvent e) {
                    super.update(e);
                    if (duplexConsoleViewRef.get() != null) {
                        e.getPresentation().setVisible(!duplexConsoleViewRef.get().isPrimaryConsoleEnabled());
                    }
                }
            };
            final AnAction[] consoleActions = consoleView.createConsoleActions();
            for (AnAction anAction : consoleActions) {
                textActionGroup.add(anAction);
            }
            final List<AnAction> anActions = ContainerUtil.newArrayList(super.createConsoleActions());
            anActions.add(textActionGroup);
            return ArrayUtil.toObjectArray(anActions, AnAction.class);
        }
    };
    duplexConsoleViewRef.set(duplexConsoleView);
    duplexConsoleView.setDisableSwitchConsoleActionOnProcessEnd(false);
    duplexConsoleView.getSwitchConsoleActionPresentation().setIcon(AllIcons.Actions.ChangeView);
    duplexConsoleView.getSwitchConsoleActionPresentation().setText(GradleBundle.message("gradle.runner.toggle.tree.text.action.name"));
    final ExternalSystemProgressNotificationManager progressManager = ServiceManager.getService(ExternalSystemProgressNotificationManager.class);
    final ExternalSystemTaskNotificationListenerAdapter taskListener = new ExternalSystemTaskNotificationListenerAdapter() {

        @Override
        public void onStatusChange(@NotNull final ExternalSystemTaskNotificationEvent event) {
            if (event instanceof ExternalSystemTaskExecutionEvent) {
                UIUtil.invokeLaterIfNeeded(() -> {
                    if (((ExternalSystemTaskExecutionEvent) event).getProgressEvent() instanceof ExternalSystemProgressEventUnsupported) {
                        duplexConsoleView.enableConsole(false);
                    }
                    gradleExecutionConsole.onStatusChange((ExternalSystemTaskExecutionEvent) event);
                });
            }
        }

        @Override
        public void onQueued(@NotNull ExternalSystemTaskId id, final String workingDir) {
            UIUtil.invokeLaterIfNeeded(() -> gradleExecutionConsole.setWorkingDir(workingDir));
        }

        @Override
        public void onFailure(@NotNull ExternalSystemTaskId id, @NotNull final Exception e) {
            UIUtil.invokeLaterIfNeeded(() -> gradleExecutionConsole.onFailure(e));
        }

        @Override
        public void onEnd(@NotNull ExternalSystemTaskId id) {
            progressManager.removeNotificationListener(this);
        }
    };
    progressManager.addNotificationListener(taskId, taskListener);
    return duplexConsoleView;
}
Also used : ExternalSystemTaskId(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) DefaultActionGroup(com.intellij.openapi.actionSystem.DefaultActionGroup) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter) NotNull(org.jetbrains.annotations.NotNull) ExternalSystemProgressNotificationManager(com.intellij.openapi.externalSystem.service.notification.ExternalSystemProgressNotificationManager) ExternalSystemTaskNotificationEvent(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationEvent) DuplexConsoleView(com.intellij.execution.console.DuplexConsoleView)

Example 5 with ExternalSystemTaskNotificationListenerAdapter

use of com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter in project freeline by alibaba.

the class GradleUtil method executeTask.

/**
 * 执行task
 *
 * @param project
 * @param taskName
 * @param args
 * @param listener
 */
public static void executeTask(Project project, String taskName, String args, ExternalSystemTaskNotificationListener listener) {
    AndroidGradleTaskManager manager = new AndroidGradleTaskManager();
    List<String> taskNames = new ArrayList<String>();
    if (taskName != null) {
        taskNames.add(taskName);
    }
    List<String> vmOptions = new ArrayList<String>();
    List<String> params = new ArrayList<String>();
    if (args != null) {
        params.add(args);
    }
    if (listener == null) {
        listener = new ExternalSystemTaskNotificationListenerAdapter() {
        };
    }
    manager.executeTasks(ExternalSystemTaskId.create(GradleConstants.SYSTEM_ID, ExternalSystemTaskType.EXECUTE_TASK, project), taskNames, project.getBasePath(), null, vmOptions, params, null, listener);
}
Also used : AndroidGradleTaskManager(com.android.tools.idea.gradle.task.AndroidGradleTaskManager) ExternalSystemTaskNotificationListenerAdapter(com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter)

Aggregations

ExternalSystemTaskNotificationListenerAdapter (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter)6 ExternalSystemTaskId (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId)5 NotNull (org.jetbrains.annotations.NotNull)3 ExternalSystemTaskNotificationListener (com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener)2 ExternalSystemProgressNotificationManager (com.intellij.openapi.externalSystem.service.notification.ExternalSystemProgressNotificationManager)2 AndroidProject (com.android.builder.model.AndroidProject)1 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 GradleBuildModel (com.android.tools.idea.gradle.dsl.model.GradleBuildModel)1 ArtifactDependencyModel (com.android.tools.idea.gradle.dsl.model.dependencies.ArtifactDependencyModel)1 CommandLineArgs (com.android.tools.idea.gradle.project.sync.common.CommandLineArgs)1 IdeaProjectStub (com.android.tools.idea.gradle.stubs.gradle.IdeaProjectStub)1 AndroidGradleTaskManager (com.android.tools.idea.gradle.task.AndroidGradleTaskManager)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 ArtifactDependencyModelWrapper (com.antfortune.freeline.idea.models.ArtifactDependencyModelWrapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Lists (com.google.common.collect.Lists)1 DuplexConsoleView (com.intellij.execution.console.DuplexConsoleView)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1