Search in sources :

Example 6 with AbstractExternalSystemSettings

use of com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings in project intellij-community by JetBrains.

the class ExternalSystemApiUtil method isOneToOneMapping.

/**
   * Allows to answer if given ide project has 1-1 mapping with the given external project, i.e. the ide project has been
   * imported from external system and no other external projects have been added.
   * <p/>
   * This might be necessary in a situation when project-level setting is changed (e.g. project name). We don't want to rename
   * ide project if it doesn't completely corresponds to the given ide project then.
   *
   * @param ideProject       target ide project
   * @param projectData      target external project
   * @return                 <code>true</code> if given ide project has 1-1 mapping to the given external project;
   *                         <code>false</code> otherwise
   */
public static boolean isOneToOneMapping(@NotNull Project ideProject, @NotNull ProjectData projectData) {
    String linkedExternalProjectPath = null;
    for (ExternalSystemManager<?, ?, ?, ?, ?> manager : getAllManagers()) {
        ProjectSystemId externalSystemId = manager.getSystemId();
        AbstractExternalSystemSettings systemSettings = getSettings(ideProject, externalSystemId);
        Collection projectsSettings = systemSettings.getLinkedProjectsSettings();
        int linkedProjectsNumber = projectsSettings.size();
        if (linkedProjectsNumber > 1) {
            // More than one external project of the same external system type is linked to the given ide project.
            return false;
        } else if (linkedProjectsNumber == 1) {
            if (linkedExternalProjectPath == null) {
                // More than one external project of different external system types is linked to the current ide project.
                linkedExternalProjectPath = ((ExternalProjectSettings) projectsSettings.iterator().next()).getExternalProjectPath();
            } else {
                return false;
            }
        }
    }
    if (linkedExternalProjectPath != null && !linkedExternalProjectPath.equals(projectData.getLinkedExternalProjectPath())) {
        // New external project is being linked.
        return false;
    }
    for (Module module : ModuleManager.getInstance(ideProject).getModules()) {
        if (!isExternalSystemAwareModule(projectData.getOwner(), module)) {
            return false;
        }
    }
    return true;
}
Also used : AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) ProjectSystemId(com.intellij.openapi.externalSystem.model.ProjectSystemId) Module(com.intellij.openapi.module.Module) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)

Example 7 with AbstractExternalSystemSettings

use of com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings in project intellij-community by JetBrains.

the class GradleModuleBuilder method setupModule.

@Override
protected void setupModule(Module module) throws ConfigurationException {
    super.setupModule(module);
    assert rootProjectPath != null;
    VirtualFile buildScriptFile = null;
    final BuildScriptDataBuilder buildScriptDataBuilder = getBuildScriptData(module);
    try {
        if (buildScriptDataBuilder != null) {
            buildScriptFile = buildScriptDataBuilder.getBuildScriptFile();
            final String text = buildScriptDataBuilder.build();
            appendToFile(buildScriptFile, "\n" + text);
        }
    } catch (IOException e) {
        LOG.warn("Unexpected exception on applying frameworks templates", e);
    }
    final Project project = module.getProject();
    if (myWizardContext.isCreatingNewProject()) {
        getExternalProjectSettings().setExternalProjectPath(rootProjectPath);
        AbstractExternalSystemSettings settings = ExternalSystemApiUtil.getSettings(project, GradleConstants.SYSTEM_ID);
        project.putUserData(ExternalSystemDataKeys.NEWLY_CREATED_PROJECT, Boolean.TRUE);
        //noinspection unchecked
        settings.linkProject(getExternalProjectSettings());
    } else {
        FileDocumentManager.getInstance().saveAllDocuments();
        final GradleProjectSettings gradleProjectSettings = getExternalProjectSettings();
        final VirtualFile finalBuildScriptFile = buildScriptFile;
        Runnable runnable = () -> {
            if (myParentProject == null) {
                gradleProjectSettings.setExternalProjectPath(rootProjectPath);
                AbstractExternalSystemSettings settings = ExternalSystemApiUtil.getSettings(project, GradleConstants.SYSTEM_ID);
                //noinspection unchecked
                settings.linkProject(gradleProjectSettings);
            }
            ImportSpec importSpec = new ImportSpecBuilder(project, GradleConstants.SYSTEM_ID).use(ProgressExecutionMode.IN_BACKGROUND_ASYNC).createDirectoriesForEmptyContentRoots().useDefaultCallback().build();
            ExternalSystemUtil.refreshProject(rootProjectPath, importSpec);
            final PsiFile psiFile;
            if (finalBuildScriptFile != null) {
                psiFile = PsiManager.getInstance(project).findFile(finalBuildScriptFile);
                if (psiFile != null) {
                    EditorHelper.openInEditor(psiFile);
                }
            }
        };
        // execute when current dialog is closed
        ExternalSystemUtil.invokeLater(project, ModalityState.NON_MODAL, runnable);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) GradleProjectSettings(org.jetbrains.plugins.gradle.settings.GradleProjectSettings) BuildScriptDataBuilder(org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder) PsiFile(com.intellij.psi.PsiFile) IOException(java.io.IOException) ImportSpec(com.intellij.openapi.externalSystem.importing.ImportSpec) ImportSpecBuilder(com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)

Example 8 with AbstractExternalSystemSettings

use of com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings in project intellij-community by JetBrains.

the class ExternalToolWindowManager method handle.

@SuppressWarnings("unchecked")
public static void handle(@NotNull final Project project) {
    for (final ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
        final AbstractExternalSystemSettings settings = manager.getSettingsProvider().fun(project);
        settings.subscribe(new ExternalSystemSettingsListenerAdapter() {

            @Override
            public void onProjectsLinked(@NotNull Collection linked) {
                if (settings.getLinkedProjectsSettings().size() != 1) {
                    return;
                }
                ToolWindow toolWindow = getToolWindow(project, manager.getSystemId());
                if (toolWindow != null) {
                    toolWindow.setAvailable(true, null);
                } else {
                    StartupManager.getInstance(project).runWhenProjectIsInitialized(new DumbAwareRunnable() {

                        @Override
                        public void run() {
                            if (project.isDisposed())
                                return;
                            ExternalSystemUtil.ensureToolWindowInitialized(project, manager.getSystemId());
                            ToolWindowManager.getInstance(project).invokeLater(() -> {
                                if (project.isDisposed())
                                    return;
                                ToolWindow toolWindow1 = getToolWindow(project, manager.getSystemId());
                                if (toolWindow1 != null) {
                                    toolWindow1.setAvailable(true, null);
                                }
                            });
                        }
                    });
                }
            }

            @Override
            public void onProjectsUnlinked(@NotNull Set linkedProjectPaths) {
                if (!settings.getLinkedProjectsSettings().isEmpty()) {
                    return;
                }
                final ToolWindow toolWindow = getToolWindow(project, manager.getSystemId());
                if (toolWindow != null) {
                    UIUtil.invokeLaterIfNeeded(() -> toolWindow.setAvailable(false, null));
                }
            }
        });
    }
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) ExternalSystemSettingsListenerAdapter(com.intellij.openapi.externalSystem.settings.ExternalSystemSettingsListenerAdapter) Set(java.util.Set) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) Collection(java.util.Collection) DumbAwareRunnable(com.intellij.openapi.project.DumbAwareRunnable)

Example 9 with AbstractExternalSystemSettings

use of com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings in project intellij-community by JetBrains.

the class ExternalSystemVcsRegistrar method handle.

@SuppressWarnings("unchecked")
public static void handle(@NotNull final Project project) {
    for (final ExternalSystemManager<?, ?, ?, ?, ?> manager : ExternalSystemApiUtil.getAllManagers()) {
        final AbstractExternalSystemSettings settings = manager.getSettingsProvider().fun(project);
        settings.subscribe(new ExternalSystemSettingsListenerAdapter() {

            @Override
            public void onProjectsLinked(@NotNull final Collection linked) {
                List<VcsDirectoryMapping> newMappings = ContainerUtilRt.newArrayList();
                final LocalFileSystem fileSystem = LocalFileSystem.getInstance();
                ProjectLevelVcsManager vcsManager = ProjectLevelVcsManager.getInstance(project);
                for (Object o : linked) {
                    final ExternalProjectSettings settings = (ExternalProjectSettings) o;
                    VirtualFile dir = fileSystem.refreshAndFindFileByPath(settings.getExternalProjectPath());
                    if (dir == null) {
                        continue;
                    }
                    if (!dir.isDirectory()) {
                        dir = dir.getParent();
                    }
                    newMappings.addAll(VcsUtil.findRoots(dir, project));
                }
                // There is a possible case that no VCS mappings are configured for the current project. There is a single
                // mapping like <Project> - <No VCS> then. We want to replace it if only one mapping to the project root dir
                // has been detected then.
                List<VcsDirectoryMapping> oldMappings = vcsManager.getDirectoryMappings();
                if (oldMappings.size() == 1 && newMappings.size() == 1 && StringUtil.isEmpty(oldMappings.get(0).getVcs())) {
                    VcsDirectoryMapping newMapping = newMappings.iterator().next();
                    String detectedDirPath = newMapping.getDirectory();
                    VirtualFile detectedDir = fileSystem.findFileByPath(detectedDirPath);
                    if (detectedDir != null && detectedDir.equals(project.getBaseDir())) {
                        newMappings.clear();
                        newMappings.add(new VcsDirectoryMapping("", newMapping.getVcs()));
                        vcsManager.setDirectoryMappings(newMappings);
                        return;
                    }
                }
                newMappings.addAll(oldMappings);
                vcsManager.setDirectoryMappings(newMappings);
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ExternalSystemSettingsListenerAdapter(com.intellij.openapi.externalSystem.settings.ExternalSystemSettingsListenerAdapter) VcsDirectoryMapping(com.intellij.openapi.vcs.VcsDirectoryMapping) ProjectLevelVcsManager(com.intellij.openapi.vcs.ProjectLevelVcsManager) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) Collection(java.util.Collection) List(java.util.List)

Example 10 with AbstractExternalSystemSettings

use of com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings in project intellij-community by JetBrains.

the class ExternalSystemUtil method linkExternalProject.

/**
   * Tries to obtain external project info implied by the given settings and link that external project to the given ide project.
   *
   * @param externalSystemId        target external system
   * @param projectSettings         settings of the external project to link
   * @param project                 target ide project to link external project to
   * @param executionResultCallback it might take a while to resolve external project info, that's why it's possible to provide
   *                                a callback to be notified on processing result. It receives <code>true</code> if an external
   *                                project has been successfully linked to the given ide project;
   *                                <code>false</code> otherwise (note that corresponding notification with error details is expected
   *                                to be shown to the end-user then)
   * @param isPreviewMode           flag which identifies if missing external project binaries should be downloaded
   * @param progressExecutionMode   identifies how progress bar will be represented for the current processing
   */
@SuppressWarnings("UnusedDeclaration")
public static void linkExternalProject(@NotNull final ProjectSystemId externalSystemId, @NotNull final ExternalProjectSettings projectSettings, @NotNull final Project project, @Nullable final Consumer<Boolean> executionResultCallback, boolean isPreviewMode, @NotNull final ProgressExecutionMode progressExecutionMode) {
    ExternalProjectRefreshCallback callback = new ExternalProjectRefreshCallback() {

        @SuppressWarnings("unchecked")
        @Override
        public void onSuccess(@Nullable final DataNode<ProjectData> externalProject) {
            if (externalProject == null) {
                if (executionResultCallback != null) {
                    executionResultCallback.consume(false);
                }
                return;
            }
            AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(project, externalSystemId);
            Set<ExternalProjectSettings> projects = ContainerUtilRt.newHashSet(systemSettings.getLinkedProjectsSettings());
            projects.add(projectSettings);
            systemSettings.setLinkedProjectsSettings(projects);
            ensureToolWindowInitialized(project, externalSystemId);
            ServiceManager.getService(ProjectDataManager.class).importData(externalProject, project, true);
            if (executionResultCallback != null) {
                executionResultCallback.consume(true);
            }
        }

        @Override
        public void onFailure(@NotNull String errorMessage, @Nullable String errorDetails) {
            if (executionResultCallback != null) {
                executionResultCallback.consume(false);
            }
        }
    };
    refreshProject(project, externalSystemId, projectSettings.getExternalProjectPath(), callback, isPreviewMode, progressExecutionMode);
}
Also used : ProjectDataManager(com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager) AbstractExternalSystemSettings(com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings) ExternalProjectRefreshCallback(com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable) ExternalProjectSettings(com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)

Aggregations

AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)18 ExternalProjectSettings (com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)13 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)4 Collection (java.util.Collection)4 ImportSpecBuilder (com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)3 ProjectSystemId (com.intellij.openapi.externalSystem.model.ProjectSystemId)3 ExternalProjectRefreshCallback (com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback)3 ExternalSystemSettingsListenerAdapter (com.intellij.openapi.externalSystem.settings.ExternalSystemSettingsListenerAdapter)3 Module (com.intellij.openapi.module.Module)2 ModulesConfigurator (com.intellij.openapi.roots.ui.configuration.ModulesConfigurator)2 Couple (com.intellij.openapi.util.Couple)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 HashMap (java.util.HashMap)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 PersistentStateComponent (com.intellij.openapi.components.PersistentStateComponent)1 ImportSpec (com.intellij.openapi.externalSystem.importing.ImportSpec)1 DataNode (com.intellij.openapi.externalSystem.model.DataNode)1 InternalExternalProjectInfo (com.intellij.openapi.externalSystem.model.internal.InternalExternalProjectInfo)1