Search in sources :

Example 6 with ImportSpecBuilder

use of com.intellij.openapi.externalSystem.importing.ImportSpecBuilder 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 7 with ImportSpecBuilder

use of com.intellij.openapi.externalSystem.importing.ImportSpecBuilder in project kotlin by JetBrains.

the class ExternalSystemImportingTestCase method doImportProject.

private void doImportProject() {
    AbstractExternalSystemSettings systemSettings = ExternalSystemApiUtil.getSettings(myProject, getExternalSystemId());
    ExternalProjectSettings projectSettings = getCurrentExternalProjectSettings();
    projectSettings.setExternalProjectPath(getProjectPath());
    @SuppressWarnings("unchecked") Set<ExternalProjectSettings> projects = ContainerUtilRt.newHashSet(systemSettings.getLinkedProjectsSettings());
    projects.remove(projectSettings);
    projects.add(projectSettings);
    //noinspection unchecked
    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 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)

Aggregations

ImportSpecBuilder (com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)7 ExternalProjectSettings (com.intellij.openapi.externalSystem.settings.ExternalProjectSettings)4 ExternalProjectRefreshCallback (com.intellij.openapi.externalSystem.service.project.ExternalProjectRefreshCallback)3 AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)3 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)2 Couple (com.intellij.openapi.util.Couple)2 ExternalSystemManager (com.intellij.openapi.externalSystem.ExternalSystemManager)1 ImportSpec (com.intellij.openapi.externalSystem.importing.ImportSpec)1 ExternalConfigPathAware (com.intellij.openapi.externalSystem.model.project.ExternalConfigPathAware)1 ProjectDataManager (com.intellij.openapi.externalSystem.service.project.manage.ProjectDataManager)1 ExternalSystemNode (com.intellij.openapi.externalSystem.view.ExternalSystemNode)1 Project (com.intellij.openapi.project.Project)1 StartupActivity (com.intellij.openapi.startup.StartupActivity)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 DisposeAwareRunnable (com.intellij.util.DisposeAwareRunnable)1 IOException (java.io.IOException)1 BuildScriptDataBuilder (org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder)1 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)1