Search in sources :

Example 1 with BuildScriptDataBuilder

use of org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder 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 2 with BuildScriptDataBuilder

use of org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder in project intellij-community by JetBrains.

the class GradleModuleBuilder method setupRootModel.

@Override
public void setupRootModel(final ModifiableRootModel modifiableRootModel) throws ConfigurationException {
    String contentEntryPath = getContentEntryPath();
    if (StringUtil.isEmpty(contentEntryPath)) {
        return;
    }
    File contentRootDir = new File(contentEntryPath);
    FileUtilRt.createDirectory(contentRootDir);
    LocalFileSystem fileSystem = LocalFileSystem.getInstance();
    VirtualFile modelContentRootDir = fileSystem.refreshAndFindFileByIoFile(contentRootDir);
    if (modelContentRootDir == null) {
        return;
    }
    modifiableRootModel.addContentEntry(modelContentRootDir);
    // todo this should be moved to generic ModuleBuilder
    if (myJdk != null) {
        modifiableRootModel.setSdk(myJdk);
    } else {
        modifiableRootModel.inheritSdk();
    }
    final Project project = modifiableRootModel.getProject();
    if (myParentProject != null) {
        rootProjectPath = myParentProject.getLinkedExternalProjectPath();
    } else {
        rootProjectPath = FileUtil.toCanonicalPath(myWizardContext.isCreatingNewProject() ? project.getBasePath() : modelContentRootDir.getPath());
    }
    assert rootProjectPath != null;
    final VirtualFile gradleBuildFile = setupGradleBuildFile(modelContentRootDir);
    setupGradleSettingsFile(rootProjectPath, modelContentRootDir, modifiableRootModel.getProject().getName(), myProjectId == null ? modifiableRootModel.getModule().getName() : myProjectId.getArtifactId(), myWizardContext.isCreatingNewProject() || myParentProject == null);
    if (gradleBuildFile != null) {
        modifiableRootModel.getModule().putUserData(BUILD_SCRIPT_DATA, new BuildScriptDataBuilder(gradleBuildFile));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) LocalFileSystem(com.intellij.openapi.vfs.LocalFileSystem) BuildScriptDataBuilder(org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFile(com.intellij.psi.PsiFile) File(java.io.File)

Aggregations

Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 BuildScriptDataBuilder (org.jetbrains.plugins.gradle.frameworkSupport.BuildScriptDataBuilder)2 ImportSpec (com.intellij.openapi.externalSystem.importing.ImportSpec)1 ImportSpecBuilder (com.intellij.openapi.externalSystem.importing.ImportSpecBuilder)1 AbstractExternalSystemSettings (com.intellij.openapi.externalSystem.settings.AbstractExternalSystemSettings)1 LocalFileSystem (com.intellij.openapi.vfs.LocalFileSystem)1 File (java.io.File)1 IOException (java.io.IOException)1 GradleProjectSettings (org.jetbrains.plugins.gradle.settings.GradleProjectSettings)1