Search in sources :

Example 1 with ExternalProjectRefreshCallback

use of com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback in project intellij-community by JetBrains.

the class ExternalSystemUtil method refreshProjects.

/**
   * Asks to refresh all external projects of the target external system linked to the given ide project based on provided spec
   *
   * @param spec import specification
   */
public static void refreshProjects(@NotNull final ImportSpec spec) {
    ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(spec.getExternalSystemId());
    if (manager == null) {
        return;
    }
    AbstractExternalSystemSettings<?, ?, ?> settings = manager.getSettingsProvider().fun(spec.getProject());
    final Collection<? extends ExternalProjectSettings> projectsSettings = settings.getLinkedProjectsSettings();
    if (projectsSettings.isEmpty()) {
        return;
    }
    final ProjectDataManager projectDataManager = ServiceManager.getService(ProjectDataManager.class);
    final ExternalProjectRefreshCallback callback;
    if (spec.getCallback() == null) {
        callback = new MyMultiExternalProjectRefreshCallback(spec.getProject(), projectDataManager, spec.getExternalSystemId());
    } else {
        callback = spec.getCallback();
    }
    Set<String> toRefresh = ContainerUtilRt.newHashSet();
    for (ExternalProjectSettings setting : projectsSettings) {
        // don't refresh project when auto-import is disabled if such behavior needed (e.g. on project opening when auto-import is disabled)
        if (!setting.isUseAutoImport() && spec.whenAutoImportEnabled())
            continue;
        toRefresh.add(setting.getExternalProjectPath());
    }
    if (!toRefresh.isEmpty()) {
        ExternalSystemNotificationManager.getInstance(spec.getProject()).clearNotifications(null, NotificationSource.PROJECT_SYNC, spec.getExternalSystemId());
        for (String path : toRefresh) {
            refreshProject(path, new ImportSpecBuilder(spec).callback(callback).build());
        }
    }
}
Also used : ProjectDataManager(com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) ImportSpecBuilder(com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)

Example 2 with ExternalProjectRefreshCallback

use of com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback in project intellij-community by JetBrains.

the class ExternalSystemImportingTestCase method doImportProject.

private void doImportProject() {
    AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(myProject, getExternalSystemId());
    final ExternalProjectSettings projectSettings = getCurrentExternalProjectSettings();
    projectSettings.setExternalProjectPath(getProjectPath());
    Set<ExternalProjectSettings> projects = ContainerUtilRt.newHashSet(systemSettings.getLinkedProjectsSettings());
    projects.remove(projectSettings);
    projects.add(projectSettings);
    systemSettings.setLinkedProjectsSettings(projects);
    final Ref<Couple<String>> error = Ref.create();
    ExternalSystemUtil.refreshProjects(new ImportSpecBuilder(myProject, getExternalSystemId()).use(ProgressExecutionMode.MODAL_SYNC).callback(new ExternalProjectRefreshCallback() {

        @Override
        public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
            if (externalProject == null) {
                System.err.println("Got null External project after import");
                return;
            }
            ServiceManager.getService(ProjectDataManager.class).importData(externalProject, myProject, true);
            System.out.println("External project was successfully imported");
        }

        @Override
        public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
            error.set(Couple.of(errorMessage, errorDetails));
        }
    }).forceWhenUptodate());
    if (!error.isNull()) {
        String failureMsg = "Import failed: " + error.get().first;
        if (StringUtil.isNotEmpty(error.get().second)) {
            failureMsg += "\nError details: \n" + error.get().second;
        }
        fail(failureMsg);
    }
}
Also used : AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) Couple(com.intellij.openapi.util.Couple) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData) ImportSpecBuilder(com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)

Example 3 with ExternalProjectRefreshCallback

use of com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback in project intellij-community by JetBrains.

the class ImportSpecBuilder method build.

public ImportSpec build() {
    ImportSpecImpl mySpec = new ImportSpecImpl(myProject, myExternalSystemId);
    mySpec.setWhenAutoImportEnabled(myWhenAutoImportEnabled);
    mySpec.setProgressExecutionMode(myProgressExecutionMode);
    mySpec.setForceWhenUptodate(myForceWhenUptodate);
    mySpec.setCreateDirectoriesForEmptyContentRoots(myCreateDirectoriesForEmptyContentRoots);
    if (myUseDefaultCallback) {
        mySpec.setCallback(new ExternalProjectRefreshCallback() {

            @Override
            public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
                if (externalProject == null) {
                    return;
                }
                final boolean synchronous = mySpec.getProgressExecutionMode() == ProgressExecutionMode.MODAL_SYNC;
                ServiceManager.getService(ProjectDataManager.class).importData(externalProject, mySpec.getProject(), synchronous);
            }

            @Override
            public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
            }
        });
    } else {
        mySpec.setCallback(myCallback);
    }
    mySpec.setPreviewMode(isPreviewMode);
    mySpec.setReportRefreshError(isReportRefreshError);
    mySpec.setArguments(myArguments);
    mySpec.setVmOptions(myVmOptions);
    return mySpec;
}
Also used : ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData)

Example 4 with ExternalProjectRefreshCallback

use of com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback in project intellij-community by JetBrains.

the class GradleProjectImportBuilder method createFinalImportCallback.

@Override
protected ExternalProjectRefreshCallback createFinalImportCallback(@NotNull final Project project, @NotNull ExternalProjectSettings projectSettings) {
    return new ExternalProjectRefreshCallback() {

        @Override
        public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
            if (externalProject == null)
                return;
            Runnable selectDataTask = () -> {
                ExternalProjectDataSelectorDialog dialog = new ExternalProjectDataSelectorDialog(project, new InternalExternalProjectInfo(GradleConstants.SYSTEM_ID, projectSettings.getExternalProjectPath(), externalProject));
                if (dialog.hasMultipleDataToSelect()) {
                    dialog.showAndGet();
                } else {
                    Disposer.dispose(dialog.getDisposable());
                }
            };
            Runnable importTask = () -> ServiceManager.getService(ProjectDataManager.class).importData(externalProject, project, false);
            if (!ApplicationManager.getApplication().isHeadlessEnvironment()) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    selectDataTask.run();
                    ApplicationManager.getApplication().executeOnPooledThread(importTask);
                });
            } else {
                importTask.run();
            }
        }

        @Override
        public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
        }
    };
}
Also used : ProjectDataManager(com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ExternalProjectDataSelectorDialog(com.intellij.openapi.externalSystem.service.ui.ExternalProjectDataSelectorDialog) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) InternalExternalProjectInfo(com.intellij.openapi.externalSystem.model.internal.InternalExternalProjectInfo) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with ExternalProjectRefreshCallback

use of com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback in project intellij-community by JetBrains.

the class ExternalSystemUtil method refreshProject.

public static void refreshProject(@NotNull final String externalProjectPath, @NotNull final ImportSpec importSpec) {
    Project project = importSpec.getProject();
    ProjectSystemId externalSystemId = importSpec.getExternalSystemId();
    ExternalProjectRefreshCallback callback = importSpec.getCallback();
    boolean isPreviewMode = importSpec.isPreviewMode();
    ProgressExecutionMode progressExecutionMode = importSpec.getProgressExecutionMode();
    boolean reportRefreshError = importSpec.isReportRefreshError();
    String arguments = importSpec.getArguments();
    String vmOptions = importSpec.getVmOptions();
    File projectFile = new File(externalProjectPath);
    final String projectName;
    if (projectFile.isFile()) {
        projectName = projectFile.getParentFile().getName();
    } else {
        projectName = projectFile.getName();
    }
    final TaskUnderProgress refreshProjectStructureTask = new TaskUnderProgress() {

        private final ExternalSystemResolveProjectTask myTask = new ExternalSystemResolveProjectTask(externalSystemId, project, externalProjectPath, vmOptions, arguments, isPreviewMode);

        @SuppressWarnings({ "ThrowableResultOfMethodCallIgnored", "IOResourceOpenedButNotSafelyClosed" })
        @Override
        public void execute(@NotNull ProgressIndicator indicator) {
            if (project.isDisposed())
                return;
            if (indicator instanceof ProgressIndicatorEx) {
                ((ProgressIndicatorEx) indicator).addStateDelegate(new AbstractProgressIndicatorExBase() {

                    @Override
                    public void cancel() {
                        super.cancel();
                        ApplicationManager.getApplication().executeOnPooledThread((Runnable) () -> myTask.cancel(ExternalSystemTaskNotificationListener.EP_NAME.getExtensions()));
                    }
                });
            }
            ExternalSystemProcessingManager processingManager = ServiceManager.getService(ExternalSystemProcessingManager.class);
            if (processingManager.findTask(ExternalSystemTaskType.RESOLVE_PROJECT, externalSystemId, externalProjectPath) != null) {
                if (callback != null) {
                    callback.onFailure(ExternalSystemBundle.message("error.resolve.already.running", externalProjectPath), null);
                }
                return;
            }
            if (!(callback instanceof MyMultiExternalProjectRefreshCallback)) {
                ExternalSystemNotificationManager.getInstance(project).clearNotifications(null, NotificationSource.PROJECT_SYNC, externalSystemId);
            }
            final ExternalSystemTaskActivator externalSystemTaskActivator = ExternalProjectsManager.getInstance(project).getTaskActivator();
            if (!isPreviewMode && !externalSystemTaskActivator.runTasks(externalProjectPath, ExternalSystemTaskActivator.Phase.BEFORE_SYNC)) {
                return;
            }
            myTask.execute(indicator, ExternalSystemTaskNotificationListener.EP_NAME.getExtensions());
            if (project.isDisposed())
                return;
            final Throwable error = myTask.getError();
            if (error == null) {
                if (callback != null) {
                    DataNode<ProjectData> externalProject = myTask.getExternalProject();
                    if (externalProject != null && importSpec.shouldCreateDirectoriesForEmptyContentRoots()) {
                        externalProject.putUserData(ContentRootDataService.CREATE_EMPTY_DIRECTORIES, Boolean.TRUE);
                    }
                    callback.onSuccess(externalProject);
                }
                if (!isPreviewMode) {
                    externalSystemTaskActivator.runTasks(externalProjectPath, ExternalSystemTaskActivator.Phase.AFTER_SYNC);
                }
                return;
            }
            if (error instanceof ImportCanceledException) {
                // stop refresh task
                return;
            }
            String message = ExternalSystemApiUtil.buildErrorMessage(error);
            if (StringUtil.isEmpty(message)) {
                message = String.format("Can't resolve %s project at '%s'. Reason: %s", externalSystemId.getReadableName(), externalProjectPath, message);
            }
            if (callback != null) {
                callback.onFailure(message, extractDetails(error));
            }
            ExternalSystemManager<?, ?, ?, ?, ?> manager = ExternalSystemApiUtil.getManager(externalSystemId);
            if (manager == null) {
                return;
            }
            AbstractExternalSystemSettings<?, ?, ?> settings = manager.getSettingsProvider().fun(project);
            ExternalProjectSettings projectSettings = settings.getLinkedProjectSettings(externalProjectPath);
            if (projectSettings == null || !reportRefreshError) {
                return;
            }
            ExternalSystemNotificationManager.getInstance(project).processExternalProjectRefreshError(error, projectName, externalSystemId);
        }
    };
    final String title;
    switch(progressExecutionMode) {
        case NO_PROGRESS_SYNC:
        case NO_PROGRESS_ASYNC:
            throw new ExternalSystemException("Please, use progress for the project import!");
        case MODAL_SYNC:
            title = ExternalSystemBundle.message("progress.import.text", projectName, externalSystemId.getReadableName());
            new Task.Modal(project, title, true) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    refreshProjectStructureTask.execute(indicator);
                }
            }.queue();
            break;
        case IN_BACKGROUND_ASYNC:
            title = ExternalSystemBundle.message("progress.refresh.text", projectName, externalSystemId.getReadableName());
            new Task.Backgroundable(project, title) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    refreshProjectStructureTask.execute(indicator);
                }
            }.queue();
            break;
        case START_IN_FOREGROUND_ASYNC:
            title = ExternalSystemBundle.message("progress.refresh.text", projectName, externalSystemId.getReadableName());
            new Task.Backgroundable(project, title, true, PerformInBackgroundOption.DEAF) {

                @Override
                public void run(@NotNull ProgressIndicator indicator) {
                    refreshProjectStructureTask.execute(indicator);
                }
            }.queue();
    }
}
Also used : ExternalSystemTaskActivator(com.intellij.openapi.externalSystem.service.project.manage.ExternalSystemTaskActivator) Task(com.intellij.openapi.progress.Task) ExternalSystemResolveProjectTask(com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask) ExternalSystemProcessingManager(com.intellij.openapi.externalSystem.service.internal.ExternalSystemProcessingManager) ImportCanceledException(com.intellij.openapi.externalSystem.service.ImportCanceledException) NotNull(org.jetbrains.annotations.NotNull) ExternalSystemResolveProjectTask(com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData) ProgressExecutionMode(com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) Project(com.intellij.openapi.project.Project) AbstractProgressIndicatorExBase(com.intellij.openapi.progress.util.AbstractProgressIndicatorExBase) DisposeAwareRunnable(com.intellij.util.DisposeAwareRunnable) ProgressIndicatorEx(com.intellij.openapi.wm.ex.ProgressIndicatorEx) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Aggregations

ExternalProjectRefreshCallback (com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback)9 ExternalProjectSettings (com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)5 NotNull (org.jetbrains.annotations.NotNull)5 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)4 Nullable (org.jetbrains.annotations.Nullable)4 ImportSpecBuilder (com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)3 DataNode (com.intellij.openapi.externalSystem.model.DataNode)3 ProjectDataManager (com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager)3 AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)3 InternalExternalProjectInfo (com.intellij.openapi.externalSystem.model.internal.InternalExternalProjectInfo)2 ExternalProjectDataSelectorDialog (com.intellij.openapi.externalSystem.service.ui.ExternalProjectDataSelectorDialog)2 Project (com.intellij.openapi.project.Project)2 Couple (com.intellij.openapi.util.Couple)2 File (java.io.File)2 Application (com.intellij.openapi.application.Application)1 ImportCanceledException (com.intellij.openapi.externalSystem.service.ImportCanceledException)1 ProgressExecutionMode (com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode)1 ExternalSystemProcessingManager (com.intellij.openapi.externalSystem.service.internal.ExternalSystemProcessingManager)1 ExternalSystemResolveProjectTask (com.intellij.openapi.externalSystem.service.internal.ExternalSystemResolveProjectTask)1 ProjectDataManager (com.intellij.openapi.externalSystem.service.project.ProjectDataManager)1