Search in sources :

Example 6 with ProjectBuilder

use of com.intellij.ide.util.projectWizard.ProjectBuilder in project intellij-community by JetBrains.

the class NewProjectUtil method doCreate.

private static Project doCreate(final AbstractProjectWizard dialog, @Nullable Project projectToClose) throws IOException {
    final ProjectManagerEx projectManager = ProjectManagerEx.getInstanceEx();
    final String projectFilePath = dialog.getNewProjectFilePath();
    for (Project p : ProjectManager.getInstance().getOpenProjects()) {
        if (ProjectUtil.isSameProject(projectFilePath, p)) {
            ProjectUtil.focusProjectWindow(p, false);
            return null;
        }
    }
    final ProjectBuilder projectBuilder = dialog.getProjectBuilder();
    try {
        File projectDir = new File(projectFilePath).getParentFile();
        LOG.assertTrue(projectDir != null, "Cannot create project in '" + projectFilePath + "': no parent file exists");
        FileUtil.ensureExists(projectDir);
        if (StorageScheme.DIRECTORY_BASED == dialog.getStorageScheme()) {
            FileUtil.ensureExists(new File(projectFilePath, Project.DIRECTORY_STORE_FOLDER));
        }
        final Project newProject;
        if (projectBuilder == null || !projectBuilder.isUpdate()) {
            String name = dialog.getProjectName();
            newProject = projectBuilder == null ? projectManager.newProject(name, projectFilePath, true, false) : projectBuilder.createProject(name, projectFilePath);
        } else {
            newProject = projectToClose;
        }
        if (newProject == null)
            return projectToClose;
        final Sdk jdk = dialog.getNewProjectJdk();
        if (jdk != null) {
            CommandProcessor.getInstance().executeCommand(newProject, () -> ApplicationManager.getApplication().runWriteAction(() -> applyJdkToProject(newProject, jdk)), null, null);
        }
        final String compileOutput = dialog.getNewCompileOutput();
        CommandProcessor.getInstance().executeCommand(newProject, () -> ApplicationManager.getApplication().runWriteAction(() -> {
            String canonicalPath = compileOutput;
            try {
                canonicalPath = FileUtil.resolveShortWindowsName(compileOutput);
            } catch (IOException e) {
            //file doesn't exist
            }
            canonicalPath = FileUtil.toSystemIndependentName(canonicalPath);
            CompilerProjectExtension.getInstance(newProject).setCompilerOutputUrl(VfsUtilCore.pathToUrl(canonicalPath));
        }), null, null);
        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }
        if (projectBuilder != null && !projectBuilder.validate(projectToClose, newProject)) {
            return projectToClose;
        }
        if (newProject != projectToClose && !ApplicationManager.getApplication().isUnitTestMode()) {
            closePreviousProject(projectToClose);
        }
        if (projectBuilder != null) {
            projectBuilder.commit(newProject, null, ModulesProvider.EMPTY_MODULES_PROVIDER);
        }
        final boolean need2OpenProjectStructure = projectBuilder == null || projectBuilder.isOpenProjectSettingsAfter();
        StartupManager.getInstance(newProject).registerPostStartupActivity(() -> {
            // ensure the dialog is shown after all startup activities are done
            ApplicationManager.getApplication().invokeLater(() -> {
                if (newProject.isDisposed() || ApplicationManager.getApplication().isUnitTestMode())
                    return;
                if (need2OpenProjectStructure) {
                    ModulesConfigurator.showDialog(newProject, null, null);
                }
                ApplicationManager.getApplication().invokeLater(() -> {
                    if (newProject.isDisposed())
                        return;
                    final ToolWindow toolWindow = ToolWindowManager.getInstance(newProject).getToolWindow(ToolWindowId.PROJECT_VIEW);
                    if (toolWindow != null) {
                        toolWindow.activate(null);
                    }
                }, ModalityState.NON_MODAL);
            }, ModalityState.NON_MODAL);
        });
        if (newProject != projectToClose) {
            ProjectUtil.updateLastProjectLocation(projectFilePath);
            if (WindowManager.getInstance().isFullScreenSupportedInCurrentOS()) {
                IdeFocusManager instance = IdeFocusManager.findInstance();
                IdeFrame lastFocusedFrame = instance.getLastFocusedFrame();
                if (lastFocusedFrame instanceof IdeFrameEx) {
                    boolean fullScreen = ((IdeFrameEx) lastFocusedFrame).isInFullScreen();
                    if (fullScreen) {
                        newProject.putUserData(IdeFrameImpl.SHOULD_OPEN_IN_FULL_SCREEN, Boolean.TRUE);
                    }
                }
            }
            projectManager.openProject(newProject);
        }
        if (!ApplicationManager.getApplication().isUnitTestMode()) {
            newProject.save();
        }
        return newProject;
    } finally {
        if (projectBuilder != null) {
            projectBuilder.cleanup();
        }
    }
}
Also used : Project(com.intellij.openapi.project.Project) ProjectBuilder(com.intellij.ide.util.projectWizard.ProjectBuilder) JavaSdk(com.intellij.openapi.projectRoots.JavaSdk) Sdk(com.intellij.openapi.projectRoots.Sdk) IOException(java.io.IOException) ProjectManagerEx(com.intellij.openapi.project.ex.ProjectManagerEx) File(java.io.File) IdeFrameEx(com.intellij.openapi.wm.ex.IdeFrameEx)

Example 7 with ProjectBuilder

use of com.intellij.ide.util.projectWizard.ProjectBuilder in project intellij-community by JetBrains.

the class ModulesConfigurator method addModule.

@Nullable
public List<Module> addModule(Component parent, boolean anImport) {
    if (myProject.isDefault())
        return null;
    final ProjectBuilder builder = runModuleWizard(parent, anImport);
    if (builder != null) {
        final List<Module> modules = new ArrayList<>();
        final List<Module> committedModules;
        if (builder instanceof ProjectImportBuilder<?>) {
            final ModifiableArtifactModel artifactModel = ProjectStructureConfigurable.getInstance(myProject).getArtifactsStructureConfigurable().getModifiableArtifactModel();
            committedModules = ((ProjectImportBuilder<?>) builder).commit(myProject, myModuleModel, this, artifactModel);
        } else {
            committedModules = builder.commit(myProject, myModuleModel, this);
        }
        if (committedModules != null) {
            modules.addAll(committedModules);
        }
        ApplicationManager.getApplication().runWriteAction(() -> {
            for (Module module : modules) {
                getOrCreateModuleEditor(module);
            }
        });
        return modules;
    }
    return null;
}
Also used : ProjectBuilder(com.intellij.ide.util.projectWizard.ProjectBuilder) ProjectImportBuilder(com.intellij.projectImport.ProjectImportBuilder) ModifiableArtifactModel(com.intellij.packaging.artifacts.ModifiableArtifactModel) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ProjectBuilder

use of com.intellij.ide.util.projectWizard.ProjectBuilder in project intellij-community by JetBrains.

the class GradleProjectWizardTest method testGradleProject.

public void testGradleProject() throws Exception {
    final String projectName = "testProject";
    Project project = createProject(new Consumer<Step>() {

        @Override
        public void consume(Step step) {
            if (step instanceof ProjectTypeStep) {
                assertTrue(((ProjectTypeStep) step).setSelectedTemplate("Gradle", null));
                List<ModuleWizardStep> steps = myWizard.getSequence().getSelectedSteps();
                assertEquals(5, steps.size());
                final ProjectBuilder projectBuilder = myWizard.getProjectBuilder();
                assertInstanceOf(projectBuilder, GradleModuleBuilder.class);
                ((GradleModuleBuilder) projectBuilder).setName(projectName);
            }
        }
    });
    assertEquals(projectName, project.getName());
    Module[] modules = ModuleManager.getInstance(project).getModules();
    assertEquals(1, modules.length);
    final Module module = modules[0];
    assertTrue(ModuleRootManager.getInstance(module).isSdkInherited());
    assertEquals(projectName, module.getName());
    VirtualFile root = ProjectRootManager.getInstance(project).getContentRoots()[0];
    VirtualFile settingsScript = VfsUtilCore.findRelativeFile("settings.gradle", root);
    assertNotNull(settingsScript);
    assertEquals(String.format("rootProject.name = '%s'\n\n", projectName), StringUtil.convertLineSeparators(VfsUtilCore.loadText(settingsScript)));
    VirtualFile buildScript = VfsUtilCore.findRelativeFile("build.gradle", root);
    assertNotNull(buildScript);
    assertEquals("group '" + projectName + "'\n" + "version '1.0-SNAPSHOT'\n" + "\n" + "apply plugin: 'java'\n" + "\n" + "sourceCompatibility = 1.8\n" + "\n" + "repositories {\n" + "    mavenCentral()\n" + "}\n" + "\n" + "dependencies {\n" + "    testCompile group: 'junit', name: 'junit', version: '4.12'\n" + "}\n", StringUtil.convertLineSeparators(VfsUtilCore.loadText(buildScript)));
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GradleModuleBuilder(org.jetbrains.plugins.gradle.service.project.wizard.GradleModuleBuilder) ProjectBuilder(com.intellij.ide.util.projectWizard.ProjectBuilder) List(java.util.List) ProjectTypeStep(com.intellij.ide.projectWizard.ProjectTypeStep) ModuleWizardStep(com.intellij.ide.util.projectWizard.ModuleWizardStep) Step(com.intellij.ide.wizard.Step) Module(com.intellij.openapi.module.Module) ProjectTypeStep(com.intellij.ide.projectWizard.ProjectTypeStep)

Aggregations

ProjectBuilder (com.intellij.ide.util.projectWizard.ProjectBuilder)8 Project (com.intellij.openapi.project.Project)4 ModuleBuilder (com.intellij.ide.util.projectWizard.ModuleBuilder)3 Module (com.intellij.openapi.module.Module)3 Nullable (org.jetbrains.annotations.Nullable)3 ModuleWizardStep (com.intellij.ide.util.projectWizard.ModuleWizardStep)2 ProjectManagerEx (com.intellij.openapi.project.ex.ProjectManagerEx)2 DefaultModulesProvider (com.intellij.openapi.roots.ui.configuration.DefaultModulesProvider)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 File (java.io.File)2 Projects.canImportAsGradleProject (com.android.tools.idea.gradle.util.Projects.canImportAsGradleProject)1 NewProjectWizard (com.intellij.ide.projectWizard.NewProjectWizard)1 ProjectTypeStep (com.intellij.ide.projectWizard.ProjectTypeStep)1 AbstractProjectWizard (com.intellij.ide.util.newProjectWizard.AbstractProjectWizard)1 StepSequence (com.intellij.ide.util.newProjectWizard.StepSequence)1 CreateFromSourcesMode (com.intellij.ide.util.newProjectWizard.modes.CreateFromSourcesMode)1 WizardContext (com.intellij.ide.util.projectWizard.WizardContext)1 Step (com.intellij.ide.wizard.Step)1 Disposable (com.intellij.openapi.Disposable)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1