Search in sources :

Example 1 with GradleProjectImporter

use of com.android.tools.idea.gradle.project.importing.GradleProjectImporter in project android by JetBrains.

the class AndroidOpenFileAction method openOrImportProject.

private static boolean openOrImportProject(@NotNull VirtualFile file, @Nullable Project project) {
    if (canImportAsGradleProject(file)) {
        VirtualFile target = findImportTarget(file);
        if (target != null) {
            Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
            if (openProjects.length > 0) {
                int exitCode = confirmOpenNewProject(false);
                if (exitCode == GeneralSettings.OPEN_PROJECT_SAME_WINDOW) {
                    Project toClose = ((project != null) && !project.isDefault()) ? project : openProjects[openProjects.length - 1];
                    if (!closeAndDispose(toClose)) {
                        return false;
                    }
                } else if (exitCode != GeneralSettings.OPEN_PROJECT_NEW_WINDOW) {
                    return false;
                }
            }
            GradleProjectImporter gradleImporter = GradleProjectImporter.getInstance();
            gradleImporter.openProject(file);
            return true;
        }
    }
    Project opened = openOrImport(file.getPath(), project, false);
    if (opened != null) {
        setLastOpenedFile(opened, file);
        return true;
    }
    return false;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) Projects.canImportAsGradleProject(com.android.tools.idea.gradle.util.Projects.canImportAsGradleProject) GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter)

Example 2 with GradleProjectImporter

use of com.android.tools.idea.gradle.project.importing.GradleProjectImporter in project android by JetBrains.

the class AdtImportBuilder method commit.

@Nullable
@Override
public List<Module> commit(Project project, @Nullable ModifiableModuleModel model, ModulesProvider modulesProvider, @Nullable ModifiableArtifactModel artifactModel) {
    File destDir = getBaseDirPath(project);
    try {
        if (!destDir.exists()) {
            boolean ok = destDir.mkdirs();
            if (!ok) {
                throw new IOException("Could not create destination directory");
            }
        }
        // Re-read the project here since one of the wizard steps can have modified the importer options,
        // and that affects the imported state (for example, if you enable/disable the replace-lib-with-dependency
        // options, the set of modules can change)
        readProjects();
        if (!myImporter.getErrors().isEmpty()) {
            return null;
        }
        myImporter.exportProject(destDir, true);
        project.getBaseDir().refresh(false, true);
    } catch (IOException e) {
        Logger.getInstance(AdtImportBuilder.class).error(e);
        return null;
    }
    try {
        NewProjectImportGradleSyncListener syncListener = new NewProjectImportGradleSyncListener() {

            @Override
            public void syncSucceeded(@NotNull Project project) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    activateProjectView(project);
                    openSummary(project);
                });
            }

            @Override
            public void syncFailed(@NotNull Project project, @NotNull String errorMessage) {
                ApplicationManager.getApplication().invokeLater(() -> {
                    createTopLevelProjectAndOpen(project);
                    openSummary(project);
                });
            }
        };
        GradleProjectImporter importer = GradleProjectImporter.getInstance();
        if (myCreateProject) {
            GradleProjectImporter.Request request = new GradleProjectImporter.Request().setProject(project);
            importer.importProject(project.getName(), destDir, request, syncListener);
        } else {
            GradleSyncInvoker.getInstance().requestProjectSyncAndSourceGeneration(project, syncListener);
        }
    } catch (ConfigurationException e) {
        Messages.showErrorDialog(project, e.getMessage(), e.getTitle());
    } catch (Throwable e) {
        Messages.showErrorDialog(project, e.getMessage(), "ADT Project Import");
    }
    return Collections.emptyList();
}
Also used : NewProjectImportGradleSyncListener(com.android.tools.idea.gradle.project.importing.NewProjectImportGradleSyncListener) Project(com.intellij.openapi.project.Project) ConfigurationException(com.intellij.openapi.options.ConfigurationException) GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter) IOException(java.io.IOException) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GradleProjectImporter

use of com.android.tools.idea.gradle.project.importing.GradleProjectImporter in project flutter-intellij by flutter.

the class OpenAndroidModule method openOrImportProject.

private static void openOrImportProject(@NotNull VirtualFile file, @Nullable Project project, boolean forceOpenInNewFrame) {
    // This is very similar to AndroidOpenFileAction.openOrImportProject().
    if (canImportAsGradleProject(file)) {
        VirtualFile target = findImportTarget(file);
        if (target != null) {
            Project[] openProjects = ProjectManager.getInstance().getOpenProjects();
            if (openProjects.length > 0) {
                int exitCode = forceOpenInNewFrame ? GeneralSettings.OPEN_PROJECT_NEW_WINDOW : confirmOpenNewProject(false);
                if (exitCode == GeneralSettings.OPEN_PROJECT_SAME_WINDOW) {
                    Project toClose = ((project != null) && !project.isDefault()) ? project : openProjects[openProjects.length - 1];
                    if (!closeAndDispose(toClose)) {
                        return;
                    }
                } else if (exitCode != GeneralSettings.OPEN_PROJECT_NEW_WINDOW) {
                    return;
                }
            }
            GradleProjectImporter gradleImporter = GradleProjectImporter.getInstance();
            gradleImporter.importProject(file);
            return;
        }
    }
    openOrImport(file.getPath(), project, false);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter)

Example 4 with GradleProjectImporter

use of com.android.tools.idea.gradle.project.importing.GradleProjectImporter in project android by JetBrains.

the class AndroidImportProjectAction method createImportWizard.

@Nullable
protected AddModuleWizard createImportWizard(@NotNull VirtualFile file) throws IOException, ConfigurationException {
    VirtualFile target = findImportTarget(file);
    if (target == null) {
        return null;
    }
    VirtualFile targetDir = target.isDirectory() ? target : target.getParent();
    File targetDirFile = VfsUtilCore.virtualToIoFile(targetDir);
    if (isAdtProjectLocation(file)) {
        importAdtProject(file);
    } else if (isEclipseProjectDir(targetDirFile) && targetDir.findChild(SdkConstants.FN_BUILD_GRADLE) == null && !ApplicationManager.getApplication().isUnitTestMode()) {
        String message = String.format("%1$s is an Eclipse project, but not an Android Eclipse project.\n\n" + "Please select the directory of an Android Eclipse project" + "(which for example will contain\nan AndroidManifest.xml file) and try again.", file.getPath());
        Messages.showErrorDialog(message, "Import Project");
    } else if (canImportAsGradleProject(target)) {
        GradleProjectImporter gradleImporter = GradleProjectImporter.getInstance();
        gradleImporter.importProject(file);
    } else {
        return importWithExtensions(file);
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GradleProjectImporter

use of com.android.tools.idea.gradle.project.importing.GradleProjectImporter in project android by JetBrains.

the class NewProjectWizardDynamic method runFinish.

private void runFinish() {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return;
    }
    GradleProjectImporter projectImporter = GradleProjectImporter.getInstance();
    String rootPath = getState().get(PROJECT_LOCATION_KEY);
    if (rootPath == null) {
        LOG.error("No root path specified for project");
        return;
    }
    File rootLocation = new File(rootPath);
    File wrapperPropertiesFilePath = GradleWrapper.getDefaultPropertiesFilePath(rootLocation);
    try {
        GradleWrapper.get(wrapperPropertiesFilePath).updateDistributionUrl(SdkConstants.GRADLE_LATEST_VERSION);
    } catch (IOException e) {
        // Unlikely to happen. Continue with import, the worst-case scenario is that sync fails and the error message has a "quick fix".
        LOG.warn("Failed to update Gradle wrapper file", e);
    }
    String projectName = getState().get(APPLICATION_NAME_KEY);
    if (projectName == null) {
        projectName = "Unnamed Project";
    }
    // Pick the highest language level of all the modules/form factors.
    // We have to pick the language level up front while creating the project rather than
    // just reacting to it during sync, because otherwise the user gets prompted with
    // a changing-language-level-requires-reopening modal dialog box and have to reload
    // the project
    LanguageLevel initialLanguageLevel = null;
    for (FormFactor factor : FormFactor.values()) {
        Object version = getState().get(FormFactorUtils.getLanguageLevelKey(factor));
        if (version != null) {
            LanguageLevel level = LanguageLevel.parse(version.toString());
            if (level != null && (initialLanguageLevel == null || level.isAtLeast(initialLanguageLevel))) {
                initialLanguageLevel = level;
            }
        }
    }
    // This is required for Android plugin in IDEA
    if (!IdeInfo.getInstance().isAndroidStudio()) {
        final Sdk jdk = IdeSdks.getInstance().getJdk();
        if (jdk != null) {
            ApplicationManager.getApplication().runWriteAction(() -> ProjectRootManager.getInstance(myProject).setProjectSdk(jdk));
        }
    }
    try {
        GradleSyncListener listener = new PostStartupGradleSyncListener(() -> {
            Iterable<File> targetFiles = myState.get(TARGET_FILES_KEY);
            assert targetFiles != null;
            TemplateUtils.reformatAndRearrange(myProject, targetFiles);
            Collection<File> filesToOpen = myState.get(FILES_TO_OPEN_KEY);
            assert filesToOpen != null;
            TemplateUtils.openEditors(myProject, filesToOpen, true);
        });
        GradleProjectImporter.Request request = new GradleProjectImporter.Request();
        request.setLanguageLevel(initialLanguageLevel).setProject(myProject);
        projectImporter.importProject(projectName, rootLocation, request, listener);
    } catch (IOException | ConfigurationException e) {
        Messages.showErrorDialog(e.getMessage(), ERROR_MSG_TITLE);
        LOG.error(e);
    }
}
Also used : GradleProjectImporter(com.android.tools.idea.gradle.project.importing.GradleProjectImporter) IOException(java.io.IOException) GradleSyncListener(com.android.tools.idea.gradle.project.sync.GradleSyncListener) LanguageLevel(com.intellij.pom.java.LanguageLevel) ConfigurationException(com.intellij.openapi.options.ConfigurationException) Sdk(com.intellij.openapi.projectRoots.Sdk) File(java.io.File)

Aggregations

GradleProjectImporter (com.android.tools.idea.gradle.project.importing.GradleProjectImporter)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Project (com.intellij.openapi.project.Project)3 File (java.io.File)3 ConfigurationException (com.intellij.openapi.options.ConfigurationException)2 IOException (java.io.IOException)2 Nullable (org.jetbrains.annotations.Nullable)2 NewProjectImportGradleSyncListener (com.android.tools.idea.gradle.project.importing.NewProjectImportGradleSyncListener)1 GradleSyncListener (com.android.tools.idea.gradle.project.sync.GradleSyncListener)1 Projects.canImportAsGradleProject (com.android.tools.idea.gradle.util.Projects.canImportAsGradleProject)1 Sdk (com.intellij.openapi.projectRoots.Sdk)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 NotNull (org.jetbrains.annotations.NotNull)1