Search in sources :

Example 1 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class SkeletonTestTask method runTestOn.

@Override
public void runTestOn(@NotNull final String sdkHome) throws IOException, InvalidSdkException {
    final Sdk sdk = createTempSdk(sdkHome, SdkCreationType.SDK_PACKAGES_ONLY);
    final File skeletonsPath = new File(PythonSdkType.getSkeletonsPath(PathManager.getSystemPath(), sdk.getHomePath()));
    // File with module skeleton
    File skeletonFileOrDirectory = new File(skeletonsPath, myModuleNameToBeGenerated);
    // Module may be stored in "moduleName.py" or "moduleName/__init__.py"
    if (skeletonFileOrDirectory.isDirectory()) {
        skeletonFileOrDirectory = new File(skeletonFileOrDirectory, PyNames.INIT_DOT_PY);
    } else {
        skeletonFileOrDirectory = new File(skeletonFileOrDirectory.getAbsolutePath() + PyNames.DOT_PY);
    }
    final File skeletonFile = skeletonFileOrDirectory;
    if (skeletonFile.exists()) {
        // To make sure we do not reuse it
        assert skeletonFile.delete() : "Failed to delete file " + skeletonFile;
    }
    ApplicationManager.getApplication().invokeAndWait(() -> {
        // File that uses CLR library
        myFixture.copyFileToProject("dotNet/" + mySourceFileToRunGenerationOn, mySourceFileToRunGenerationOn);
        // Library itself
        myFixture.copyFileToProject("dotNet/PythonLibs.dll", "PythonLibs.dll");
        // Another library
        myFixture.copyFileToProject("dotNet/SingleNameSpace.dll", "SingleNameSpace.dll");
        myFixture.configureByFile(mySourceFileToRunGenerationOn);
    }, ModalityState.NON_MODAL);
    // This inspection should suggest us to generate stubs
    myFixture.enableInspections(PyUnresolvedReferencesInspection.class);
    UIUtil.invokeAndWaitIfNeeded(new Runnable() {

        @Override
        public void run() {
            PsiDocumentManager.getInstance(myFixture.getProject()).commitAllDocuments();
            final String intentionName = PyBundle.message("sdk.gen.stubs.for.binary.modules", myUseQuickFixWithThisModuleOnly);
            final IntentionAction intention = myFixture.findSingleIntention(intentionName);
            Assert.assertNotNull("No intention found to generate skeletons!", intention);
            Assert.assertThat("Intention should be quick fix to run", intention, Matchers.instanceOf(QuickFixWrapper.class));
            final LocalQuickFix quickFix = ((QuickFixWrapper) intention).getFix();
            Assert.assertThat("Quick fix should be 'generate binary skeletons' fix to run", quickFix, Matchers.instanceOf(GenerateBinaryStubsFix.class));
            final Task fixTask = ((GenerateBinaryStubsFix) quickFix).getFixTask(myFixture.getFile());
            fixTask.run(new AbstractProgressIndicatorBase());
        }
    });
    FileUtil.copy(skeletonFile, new File(myFixture.getTempDirPath(), skeletonFile.getName()));
    if (myExpectedSkeletonFile != null) {
        final String actual = StreamUtil.readText(new FileInputStream(skeletonFile), Charset.defaultCharset());
        final String skeletonText = StreamUtil.readText(new FileInputStream(new File(getTestDataPath(), myExpectedSkeletonFile)), Charset.defaultCharset());
        // TODO: Move to separate method ?
        if (!Matchers.equalToIgnoringWhiteSpace(removeGeneratorVersion(skeletonText)).matches(removeGeneratorVersion(actual))) {
            throw new FileComparisonFailure("asd", skeletonText, actual, skeletonFile.getAbsolutePath());
        }
    }
    myFixture.configureByFile(skeletonFile.getName());
}
Also used : Task(com.intellij.openapi.progress.Task) PyExecutionFixtureTestTask(com.jetbrains.env.PyExecutionFixtureTestTask) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) AbstractProgressIndicatorBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorBase) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File) FileInputStream(java.io.FileInputStream) FileComparisonFailure(com.intellij.rt.execution.junit.FileComparisonFailure)

Example 2 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class MavenBeforeRunTasksProvider method executeTask.

public boolean executeTask(final DataContext context, RunConfiguration configuration, ExecutionEnvironment env, final MavenBeforeRunTask task) {
    final Semaphore targetDone = new Semaphore();
    final boolean[] result = new boolean[] { true };
    try {
        ApplicationManager.getApplication().invokeAndWait(() -> {
            final Project project = CommonDataKeys.PROJECT.getData(context);
            final MavenProject mavenProject = getMavenProject(task);
            if (project == null || project.isDisposed() || mavenProject == null)
                return;
            FileDocumentManager.getInstance().saveAllDocuments();
            final MavenExplicitProfiles explicitProfiles = MavenProjectsManager.getInstance(project).getExplicitProfiles();
            final MavenRunner mavenRunner = MavenRunner.getInstance(project);
            targetDone.down();
            new Task.Backgroundable(project, TasksBundle.message("maven.tasks.executing"), true) {

                public void run(@NotNull ProgressIndicator indicator) {
                    try {
                        MavenRunnerParameters params = new MavenRunnerParameters(true, mavenProject.getDirectory(), ParametersListUtil.parse(task.getGoal()), explicitProfiles.getEnabledProfiles(), explicitProfiles.getDisabledProfiles());
                        result[0] = mavenRunner.runBatch(Collections.singletonList(params), null, null, TasksBundle.message("maven.tasks.executing"), indicator);
                    } finally {
                        targetDone.up();
                    }
                }

                @Override
                public boolean shouldStartInBackground() {
                    return MavenRunner.getInstance(project).getSettings().isRunMavenInBackground();
                }

                @Override
                public void processSentToBackground() {
                    MavenRunner.getInstance(project).getSettings().setRunMavenInBackground(true);
                }
            }.queue();
        }, ModalityState.NON_MODAL);
    } catch (Exception e) {
        MavenLog.LOG.error(e);
        return false;
    }
    targetDone.waitFor();
    return result[0];
}
Also used : Project(com.intellij.openapi.project.Project) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Task(com.intellij.openapi.progress.Task) MavenProject(org.jetbrains.idea.maven.project.MavenProject) MavenRunner(org.jetbrains.idea.maven.execution.MavenRunner) MavenExplicitProfiles(org.jetbrains.idea.maven.model.MavenExplicitProfiles) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) Semaphore(com.intellij.util.concurrency.Semaphore) MavenRunnerParameters(org.jetbrains.idea.maven.execution.MavenRunnerParameters)

Example 3 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class MavenUtil method runInBackground.

public static MavenTaskHandler runInBackground(final Project project, final String title, final boolean cancellable, final MavenTask task) {
    final MavenProgressIndicator indicator = new MavenProgressIndicator();
    Runnable runnable = () -> {
        if (project.isDisposed())
            return;
        try {
            task.run(indicator);
        } catch (MavenProcessCanceledException | ProcessCanceledException ignore) {
            indicator.cancel();
        }
    };
    if (isNoBackgroundMode()) {
        runnable.run();
        return new MavenTaskHandler() {

            public void waitFor() {
            }
        };
    } else {
        final Future<?> future = ApplicationManager.getApplication().executeOnPooledThread(runnable);
        final MavenTaskHandler handler = new MavenTaskHandler() {

            public void waitFor() {
                try {
                    future.get();
                } catch (InterruptedException | ExecutionException e) {
                    MavenLog.LOG.error(e);
                }
            }
        };
        invokeLater(project, () -> {
            if (future.isDone())
                return;
            new Task.Backgroundable(project, title, cancellable) {

                public void run(@NotNull ProgressIndicator i) {
                    indicator.setIndicator(i);
                    handler.waitFor();
                }
            }.queue();
        });
        return handler;
    }
}
Also used : Task(com.intellij.openapi.progress.Task) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) DisposeAwareRunnable(com.intellij.util.DisposeAwareRunnable) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class ExternalSystemUtil method runTask.

public static void runTask(@NotNull final ExternalSystemTaskExecutionSettings taskSettings, @NotNull final String executorId, @NotNull final Project project, @NotNull final ProjectSystemId externalSystemId, @Nullable final TaskCallback callback, @NotNull final ProgressExecutionMode progressExecutionMode, boolean activateToolWindowBeforeRun) {
    ExecutionEnvironment environment = createExecutionEnvironment(project, externalSystemId, taskSettings, executorId);
    if (environment == null)
        return;
    RunnerAndConfigurationSettings runnerAndConfigurationSettings = environment.getRunnerAndConfigurationSettings();
    assert runnerAndConfigurationSettings != null;
    runnerAndConfigurationSettings.setActivateToolWindowBeforeRun(activateToolWindowBeforeRun);
    final TaskUnderProgress task = new TaskUnderProgress() {

        @Override
        public void execute(@NotNull ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            final Semaphore targetDone = new Semaphore();
            final Ref<Boolean> result = new Ref<>(false);
            final Disposable disposable = Disposer.newDisposable();
            project.getMessageBus().connect(disposable).subscribe(ExecutionManager.EXECUTION_TOPIC, new ExecutionListener() {

                public void processStartScheduled(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
                    if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                        targetDone.down();
                    }
                }

                public void processNotStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
                    if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                        targetDone.up();
                    }
                }

                public void processStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal, @NotNull final ProcessHandler handler) {
                    if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
                        handler.addProcessListener(new ProcessAdapter() {

                            public void processTerminated(ProcessEvent event) {
                                result.set(event.getExitCode() == 0);
                                targetDone.up();
                            }
                        });
                    }
                }
            });
            try {
                ApplicationManager.getApplication().invokeAndWait(() -> {
                    try {
                        environment.getRunner().execute(environment);
                    } catch (ExecutionException e) {
                        targetDone.up();
                        LOG.error(e);
                    }
                }, ModalityState.defaultModalityState());
            } catch (Exception e) {
                LOG.error(e);
                Disposer.dispose(disposable);
                return;
            }
            targetDone.waitFor();
            Disposer.dispose(disposable);
            if (callback != null) {
                if (result.get()) {
                    callback.onSuccess();
                } else {
                    callback.onFailure();
                }
            }
        }
    };
    final String title = AbstractExternalSystemTaskConfigurationType.generateName(project, taskSettings);
    switch(progressExecutionMode) {
        case NO_PROGRESS_SYNC:
            task.execute(new EmptyProgressIndicator());
            break;
        case MODAL_SYNC:
            new Task.Modal(project, title, true) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    task.execute(indicator);
                }
            }.queue();
            break;
        case NO_PROGRESS_ASYNC:
            ApplicationManager.getApplication().executeOnPooledThread(() -> task.execute(new EmptyProgressIndicator()));
            break;
        case IN_BACKGROUND_ASYNC:
            new Task.Backgroundable(project, title) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    task.execute(indicator);
                }
            }.queue();
            break;
        case START_IN_FOREGROUND_ASYNC:
            new Task.Backgroundable(project, title, true, PerformInBackgroundOption.DEAF) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    task.execute(indicator);
                }
            }.queue();
    }
}
Also used : Disposable(com.intellij.openapi.Disposable) ExecutionEnvironment(com.intellij.execution.runners.ExecutionEnvironment) Task(com.intellij.openapi.progress.Task) ExternalSystemResolveProjectTask(com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProcessEvent(com.intellij.execution.process.ProcessEvent) Semaphore(com.intellij.util.concurrency.Semaphore) NotNull(org.jetbrains.annotations.NotNull) ImportCanceledException(com.intellij.openapi.externalSystem.service.ImportCanceledException) IOException(java.io.IOException) Ref(com.intellij.openapi.util.Ref) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ProcessHandler(com.intellij.execution.process.ProcessHandler)

Example 5 with Task

use of com.intellij.openapi.progress.Task in project intellij-community by JetBrains.

the class LogConsoleBase method getSearchComponent.

@Override
public JComponent getSearchComponent() {
    myLogFilterCombo.setModel(new DefaultComboBoxModel(myFilters.toArray(new LogFilter[myFilters.size()])));
    resetLogFilter();
    myLogFilterCombo.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            final LogFilter filter = (LogFilter) myLogFilterCombo.getSelectedItem();
            final Task.Backgroundable task = new Task.Backgroundable(myProject, APPLYING_FILTER_TITLE) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    myModel.selectFilter(filter);
                }
            };
            ProgressManager.getInstance().run(task);
        }
    });
    myTextFilterWrapper.removeAll();
    myTextFilterWrapper.add(getTextFilterComponent());
    return mySearchComponent;
}
Also used : Task(com.intellij.openapi.progress.Task) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator)

Aggregations

Task (com.intellij.openapi.progress.Task)33 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)28 NotNull (org.jetbrains.annotations.NotNull)20 Project (com.intellij.openapi.project.Project)10 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 File (java.io.File)6 IOException (java.io.IOException)5 Editor (com.intellij.openapi.editor.Editor)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)3 Ref (com.intellij.openapi.util.Ref)3 Semaphore (com.intellij.util.concurrency.Semaphore)3 ArrayList (java.util.ArrayList)3 Nullable (org.jetbrains.annotations.Nullable)3 Disposable (com.intellij.openapi.Disposable)2 ProgressExecutionMode (com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode)2 BackgroundableProcessIndicator (com.intellij.openapi.progress.impl.BackgroundableProcessIndicator)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 Installer (com.android.repository.api.Installer)1 InstallerFactory (com.android.repository.api.InstallerFactory)1