Search in sources :

Example 6 with ProjectManagerEx

use of com.intellij.openapi.project.ex.ProjectManagerEx in project intellij-community by JetBrains.

the class PlatformProjectOpenProcessor method doOpenProject.

@Nullable
public static Project doOpenProject(@NotNull VirtualFile virtualFile, @Nullable Project projectToClose, int line, @Nullable ProjectOpenedCallback callback, @NotNull EnumSet<Option> options) {
    VirtualFile baseDir = virtualFile;
    boolean dummyProject = false;
    String dummyProjectName = null;
    boolean forceOpenInNewFrame = options.contains(Option.FORCE_NEW_FRAME);
    boolean isReopen = options.contains(Option.REOPEN);
    boolean tempProject = options.contains(Option.TEMP_PROJECT);
    if (!baseDir.isDirectory()) {
        if (tempProject) {
            baseDir = null;
        } else {
            baseDir = virtualFile.getParent();
            while (baseDir != null && !ProjectKt.isProjectDirectoryExistsUsingIo(baseDir)) {
                baseDir = baseDir.getParent();
            }
        }
        if (baseDir == null) {
            // no reasonable directory -> create new temp one or use parent
            if (tempProject || Registry.is("ide.open.file.in.temp.project.dir")) {
                try {
                    dummyProjectName = virtualFile.getName();
                    File directory = FileUtil.createTempDirectory(dummyProjectName, null, true);
                    baseDir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(directory);
                    dummyProject = true;
                } catch (IOException ex) {
                    LOG.error(ex);
                }
            }
            if (baseDir == null) {
                baseDir = virtualFile.getParent();
            }
        }
    }
    final Path projectDir = Paths.get(FileUtil.toSystemDependentName(baseDir.getPath()), Project.DIRECTORY_STORE_FOLDER);
    Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
    if (!forceOpenInNewFrame && openProjects.length > 0) {
        if (projectToClose == null) {
            projectToClose = openProjects[openProjects.length - 1];
        }
        if (ProjectAttachProcessor.canAttachToProject() && GeneralSettings.getInstance().getConfirmOpenNewProject() == GeneralSettings.OPEN_PROJECT_ASK) {
            final OpenOrAttachDialog dialog = new OpenOrAttachDialog(projectToClose, isReopen, isReopen ? "Reopen Project" : "Open Project");
            if (!dialog.showAndGet()) {
                return null;
            }
            if (dialog.isReplace()) {
                if (!ProjectUtil.closeAndDispose(projectToClose)) {
                    return null;
                }
            } else if (dialog.isAttach()) {
                if (attachToProject(projectToClose, Paths.get(FileUtil.toSystemDependentName(baseDir.getPath())), callback)) {
                    return null;
                }
            }
            // process all pending events that can interrupt focus flow
            // todo this can be removed after taming the focus beast
            IdeEventQueue.getInstance().flushQueue();
        } else {
            int exitCode = ProjectUtil.confirmOpenNewProject(false);
            if (exitCode == GeneralSettings.OPEN_PROJECT_SAME_WINDOW) {
                if (!ProjectUtil.closeAndDispose(projectToClose)) {
                    return null;
                }
            } else if (exitCode != GeneralSettings.OPEN_PROJECT_NEW_WINDOW) {
                // not in a new window
                return null;
            }
        }
    }
    boolean runConfigurators = true;
    boolean newProject = false;
    ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
    Project project = null;
    if (PathKt.exists(projectDir)) {
        try {
            File baseDirIo = VfsUtilCore.virtualToIoFile(baseDir);
            for (ProjectOpenProcessor processor : ProjectOpenProcessor.EXTENSION_POINT_NAME.getExtensions()) {
                processor.refreshProjectFiles(baseDirIo);
            }
            project = projectManager.convertAndLoadProject(baseDir.getPath());
            if (project != null) {
                Module[] modules = ModuleManager.getInstance(project).getModules();
                if (modules.length > 0) {
                    runConfigurators = false;
                }
            }
        } catch (Exception e) {
            LOG.error(e);
        }
    } else {
        PathKt.createDirectories(projectDir);
        project = projectManager.newProject(dummyProject ? dummyProjectName : baseDir.getName(), baseDir.getPath(), true, dummyProject);
        newProject = true;
    }
    if (project == null) {
        WelcomeFrame.showIfNoProjectOpened();
        return null;
    }
    ProjectBaseDirectory.getInstance(project).setBaseDir(baseDir);
    Module module = runConfigurators ? runDirectoryProjectConfigurators(baseDir, project) : ModuleManager.getInstance(project).getModules()[0];
    if (runConfigurators && dummyProject) {
        // add content root for chosen (single) file
        ModuleRootModificationUtil.updateModel(module, model -> {
            ContentEntry[] entries = model.getContentEntries();
            if (entries.length == 1)
                model.removeContentEntry(entries[0]);
            model.addContentEntry(virtualFile);
        });
    }
    if (newProject) {
        project.save();
    }
    openFileFromCommandLine(project, virtualFile, line);
    if (!projectManager.openProject(project)) {
        WelcomeFrame.showIfNoProjectOpened();
        final Project finalProject = project;
        ApplicationManager.getApplication().runWriteAction(() -> Disposer.dispose(finalProject));
        return project;
    }
    if (callback != null) {
        callback.projectOpened(project, module);
    }
    return project;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Path(java.nio.file.Path) IOException(java.io.IOException) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) ContentEntry(com.intellij.openapi.roots.ContentEntry) Module(com.intellij.openapi.module.Module) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) ProjectOpenProcessor(com.intellij.projectImport.ProjectOpenProcessor) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with ProjectManagerEx

use of com.intellij.openapi.project.ex.ProjectManagerEx in project intellij-community by JetBrains.

the class ProjectOpeningTest method testOpenProjectCancelling.

public void testOpenProjectCancelling() throws Exception {
    File foo = PlatformTestCase.createTempDir("foo");
    Project project = null;
    MyStartupActivity activity = new MyStartupActivity();
    PlatformTestUtil.registerExtension(POST_STARTUP_ACTIVITY, activity, getTestRootDisposable());
    try {
        ProjectManagerEx manager = ProjectManagerEx.getInstanceEx();
        project = manager.createProject(null, foo.getPath());
        assertFalse(manager.openProject(project));
        assertFalse(project.isOpen());
        assertTrue(activity.passed);
    } finally {
        closeProject(project);
    }
}
Also used : Project(com.intellij.openapi.project.Project) File(java.io.File) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx)

Example 8 with ProjectManagerEx

use of com.intellij.openapi.project.ex.ProjectManagerEx in project intellij-community by JetBrains.

the class ProjectOpeningTest method testCancelOnLoadingModules.

public void testCancelOnLoadingModules() throws Exception {
    File foo = PlatformTestCase.createTempDir("foo");
    Project project = null;
    try {
        ProjectManagerEx manager = ProjectManagerEx.getInstanceEx();
        project = manager.createProject(null, foo.getPath());
        project.save();
        closeProject(project);
        ApplicationManager.getApplication().getMessageBus().connect(getTestRootDisposable()).subscribe(ProjectLifecycleListener.TOPIC, new ProjectLifecycleListener() {

            @Override
            public void projectComponentsInitialized(@NotNull Project project) {
                ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
                assertNotNull(indicator);
                indicator.cancel();
                indicator.checkCanceled();
            }
        });
        project = manager.loadAndOpenProject(foo.getPath());
        assertFalse(project.isOpen());
        assertTrue(project.isDisposed());
    } finally {
        closeProject(project);
    }
}
Also used : Project(com.intellij.openapi.project.Project) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) File(java.io.File) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx)

Example 9 with ProjectManagerEx

use of com.intellij.openapi.project.ex.ProjectManagerEx in project intellij-community by JetBrains.

the class LightPlatformTestCase method doSetup.

public static void doSetup(@NotNull LightProjectDescriptor descriptor, @NotNull LocalInspectionTool[] localInspectionTools, @NotNull Disposable parentDisposable) throws Exception {
    assertNull("Previous test " + ourTestCase + " hasn't called tearDown(). Probably overridden without super call.", ourTestCase);
    IdeaLogger.ourErrorsOccurred = null;
    ApplicationManager.getApplication().assertIsDispatchThread();
    boolean reusedProject = true;
    if (ourProject == null || ourProjectDescriptor == null || !ourProjectDescriptor.equals(descriptor)) {
        initProject(descriptor);
        reusedProject = false;
    }
    ProjectManagerEx projectManagerEx = ProjectManagerEx.getInstanceEx();
    projectManagerEx.openTestProject(ourProject);
    if (reusedProject) {
        DumbService.getInstance(ourProject).queueTask(new UnindexedFilesUpdater(ourProject));
    }
    MessageBusConnection connection = ourProject.getMessageBus().connect(parentDisposable);
    connection.subscribe(ProjectTopics.MODULES, new ModuleListener() {

        @Override
        public void moduleAdded(@NotNull Project project, @NotNull Module module) {
            fail("Adding modules is not permitted in LightIdeaTestCase.");
        }
    });
    clearUncommittedDocuments(getProject());
    InspectionsKt.configureInspections(localInspectionTools, getProject(), parentDisposable);
    assertFalse(getPsiManager().isDisposed());
    Boolean passed = null;
    try {
        passed = StartupManagerEx.getInstanceEx(getProject()).startupActivityPassed();
    } catch (Exception ignored) {
    }
    assertTrue("open: " + getProject().isOpen() + "; disposed:" + getProject().isDisposed() + "; startup passed:" + passed + "; all open projects: " + Arrays.asList(ProjectManager.getInstance().getOpenProjects()), getProject().isInitialized());
    CodeStyleSettingsManager.getInstance(getProject()).setTemporarySettings(new CodeStyleSettings());
    final FileDocumentManager manager = FileDocumentManager.getInstance();
    if (manager instanceof FileDocumentManagerImpl) {
        Document[] unsavedDocuments = manager.getUnsavedDocuments();
        manager.saveAllDocuments();
        ApplicationManager.getApplication().runWriteAction(((FileDocumentManagerImpl) manager)::dropAllUnsavedDocuments);
        assertEmpty("There are unsaved documents", Arrays.asList(unsavedDocuments));
    }
    // startup activities
    UIUtil.dispatchAllInvocationEvents();
    ((FileTypeManagerImpl) FileTypeManager.getInstance()).drainReDetectQueue();
}
Also used : MessageBusConnection(com.intellij.util.messages.MessageBusConnection) ModuleListener(com.intellij.openapi.project.ModuleListener) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) FileDocumentManagerImpl(com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl) Document(com.intellij.openapi.editor.Document) FileTypeManagerImpl(com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException) Project(com.intellij.openapi.project.Project) CodeStyleSettings(com.intellij.psi.codeStyle.CodeStyleSettings) Module(com.intellij.openapi.module.Module) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) UnindexedFilesUpdater(com.intellij.util.indexing.UnindexedFilesUpdater)

Example 10 with ProjectManagerEx

use of com.intellij.openapi.project.ex.ProjectManagerEx in project android by JetBrains.

the class AndroidImportProjectAction method doCreate.

private static void doCreate(@NotNull AddModuleWizard wizard) throws IOException {
    // TODO: Now we need to add as module if file does not exist
    ProjectBuilder projectBuilder = wizard.getProjectBuilder();
    try {
        File projectFilePath = new File(wizard.getNewProjectFilePath());
        File projectDirPath = projectFilePath.isDirectory() ? projectFilePath : projectFilePath.getParentFile();
        LOG.assertTrue(projectDirPath != null, "Cannot create project in '" + projectFilePath + "': no parent file exists");
        ensureExists(projectDirPath);
        if (StorageScheme.DIRECTORY_BASED == wizard.getStorageScheme()) {
            ensureExists(new File(projectDirPath, DIRECTORY_STORE_FOLDER));
        }
        boolean unitTestMode = ApplicationManager.getApplication().isUnitTestMode();
        ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
        Project project = projectManager.newProject(wizard.getProjectName(), projectDirPath.getPath(), true, false);
        if (project == null) {
            return;
        }
        if (!unitTestMode) {
            project.save();
        }
        if (projectBuilder != null) {
            if (!projectBuilder.validate(null, project)) {
                return;
            }
            projectBuilder.commit(project, null, EMPTY_MODULES_PROVIDER);
        }
        if (!unitTestMode) {
            project.save();
        }
    } finally {
        if (projectBuilder != null) {
            projectBuilder.cleanup();
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) Projects.canImportAsGradleProject(com.android.tools.idea.gradle.util.Projects.canImportAsGradleProject) ProjectBuilder(com.intellij.ide.util.projectWizard.ProjectBuilder) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx)

Aggregations

ProjectManagerEx (com.intellij.openapi.project.ex.ProjectManagerEx)18 Project (com.intellij.openapi.project.Project)17 File (java.io.File)9 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 IOException (java.io.IOException)6 ProjectManagerImpl (com.intellij.openapi.project.impl.ProjectManagerImpl)3 ProjectBuilder (com.intellij.ide.util.projectWizard.ProjectBuilder)2 Module (com.intellij.openapi.module.Module)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 GradleBuildFile (com.android.tools.idea.gradle.parser.GradleBuildFile)1 AndroidGradleProjectComponent (com.android.tools.idea.gradle.project.AndroidGradleProjectComponent)1 GradleUtil.getGradleBuildFile (com.android.tools.idea.gradle.util.GradleUtil.getGradleBuildFile)1 Projects.canImportAsGradleProject (com.android.tools.idea.gradle.util.Projects.canImportAsGradleProject)1 Projects.isLegacyIdeaAndroidProject (com.android.tools.idea.gradle.util.Projects.isLegacyIdeaAndroidProject)1 Document (com.intellij.openapi.editor.Document)1 FileDocumentManager (com.intellij.openapi.fileEditor.FileDocumentManager)1 FileDocumentManagerImpl (com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl)1 FileTypeManagerImpl (com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1