Search in sources :

Example 1 with ProjectOpenProcessor

use of com.intellij.projectImport.ProjectOpenProcessor in project intellij-community by JetBrains.

the class ProjectUtil method openOrImport.

/**
   * @param path                project file path
   * @param projectToClose      currently active project
   * @param forceOpenInNewFrame forces opening in new frame
   * @return project by path if the path was recognized as IDEA project file or one of the project formats supported by
   *         installed importers (regardless of opening/import result)
   *         null otherwise
   */
@Nullable
public static Project openOrImport(@NotNull String path, Project projectToClose, boolean forceOpenInNewFrame) {
    VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(path);
    if (virtualFile == null)
        return null;
    virtualFile.refresh(false, false);
    Project existing = findAndFocusExistingProjectForPath(path);
    if (existing != null)
        return existing;
    ProjectOpenProcessor strong = ProjectOpenProcessor.getStrongImportProvider(virtualFile);
    if (strong != null) {
        return strong.doOpenProject(virtualFile, projectToClose, forceOpenInNewFrame);
    }
    if (ProjectKt.isValidProjectPath(path)) {
        return openProject(path, projectToClose, forceOpenInNewFrame);
    }
    if (virtualFile.isDirectory()) {
        for (VirtualFile child : virtualFile.getChildren()) {
            final String childPath = child.getPath();
            if (childPath.endsWith(ProjectFileType.DOT_DEFAULT_EXTENSION)) {
                return openProject(childPath, projectToClose, forceOpenInNewFrame);
            }
        }
    }
    ProjectOpenProcessor provider = ProjectOpenProcessor.getImportProvider(virtualFile);
    if (provider != null) {
        final Project project = provider.doOpenProject(virtualFile, projectToClose, forceOpenInNewFrame);
        if (project != null) {
            ApplicationManager.getApplication().invokeLater(() -> {
                if (!project.isDisposed()) {
                    final ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(ToolWindowId.PROJECT_VIEW);
                    if (toolWindow != null) {
                        toolWindow.activate(null);
                    }
                }
            }, ModalityState.NON_MODAL);
        }
        return project;
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) ProjectOpenProcessor(com.intellij.projectImport.ProjectOpenProcessor) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with ProjectOpenProcessor

use of com.intellij.projectImport.ProjectOpenProcessor 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 3 with ProjectOpenProcessor

use of com.intellij.projectImport.ProjectOpenProcessor in project intellij-community by JetBrains.

the class UpdateReceivedFileProcessor method isProjectOrModuleFile.

private static boolean isProjectOrModuleFile(VirtualFile virtualFile) {
    if (virtualFile == null)
        return false;
    final ProjectOpenProcessor importProvider = ProjectOpenProcessor.getImportProvider(virtualFile);
    if (importProvider != null && importProvider.isProjectFile(virtualFile))
        return true;
    FileType fileType = virtualFile.getFileType();
    return fileType == StdFileTypes.IDEA_PROJECT || fileType == StdFileTypes.IDEA_MODULE || fileType == StdFileTypes.IDEA_WORKSPACE;
}
Also used : FileType(com.intellij.openapi.fileTypes.FileType) ProjectOpenProcessor(com.intellij.projectImport.ProjectOpenProcessor)

Example 4 with ProjectOpenProcessor

use of com.intellij.projectImport.ProjectOpenProcessor in project intellij-community by JetBrains.

the class CommandLineProcessor method doOpenFileOrProject.

@Nullable
private static Project doOpenFileOrProject(String name) {
    final VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByPath(name);
    if (virtualFile == null) {
        Messages.showErrorDialog("Cannot find file '" + name + "'", "Cannot Find File");
        return null;
    }
    ProjectOpenProcessor provider = ProjectOpenProcessor.getImportProvider(virtualFile);
    if (provider instanceof PlatformProjectOpenProcessor && !virtualFile.isDirectory()) {
        // HACK: PlatformProjectOpenProcessor agrees to open anything
        provider = null;
    }
    if (provider != null || ProjectKt.isValidProjectPath(name)) {
        final Project result = ProjectUtil.openOrImport(name, null, true);
        if (result == null) {
            Messages.showErrorDialog("Cannot open project '" + name + "'", "Cannot Open Project");
        }
        return result;
    } else {
        return doOpenFile(virtualFile, -1, false);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) PlatformProjectOpenProcessor(com.intellij.platform.PlatformProjectOpenProcessor) ProjectOpenProcessor(com.intellij.projectImport.ProjectOpenProcessor) PlatformProjectOpenProcessor(com.intellij.platform.PlatformProjectOpenProcessor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ProjectOpenProcessor (com.intellij.projectImport.ProjectOpenProcessor)4 Project (com.intellij.openapi.project.Project)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 Nullable (org.jetbrains.annotations.Nullable)3 FileType (com.intellij.openapi.fileTypes.FileType)1 Module (com.intellij.openapi.module.Module)1 ProjectManagerEx (com.intellij.openapi.project.ex.ProjectManagerEx)1 ContentEntry (com.intellij.openapi.roots.ContentEntry)1 PlatformProjectOpenProcessor (com.intellij.platform.PlatformProjectOpenProcessor)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1