Search in sources :

Example 1 with ImportedModule

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

the class ModuleDisposalDataService method importData.

@Override
public void importData(@NotNull Collection<DataNode<ImportedModule>> toImport, @Nullable ProjectData projectData, @NotNull Project project, @NotNull IdeModifiableModelsProvider modelsProvider) {
    // See https://youtrack.jetbrains.com/issue/IDEA-137433
    if (toImport.isEmpty() || !myIdeInfo.isAndroidStudio() || GradleSyncState.getInstance(project).lastSyncFailedOrHasIssues()) {
        return;
    }
    Module[] modules = modelsProvider.getModules();
    if (modules.length != toImport.size()) {
        Map<String, Module> modulesByName = new HashMap<>();
        for (Module module : modules) {
            modulesByName.put(module.getName(), module);
        }
        for (DataNode<ImportedModule> dataNode : toImport) {
            ImportedModule importedModule = dataNode.getData();
            modulesByName.remove(importedModule.getName());
        }
        Collection<Module> modulesToDispose = new ArrayList<>(modulesByName.values());
        myModuleDisposer.disposeModulesAndMarkImlFilesForDeletion(modulesToDispose, project, modelsProvider);
    }
}
Also used : HashMap(java.util.HashMap) ImportedModule(com.android.tools.idea.gradle.ImportedModule) ArrayList(java.util.ArrayList) ImportedModule(com.android.tools.idea.gradle.ImportedModule) Module(com.intellij.openapi.module.Module)

Example 2 with ImportedModule

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

the class AndroidGradleProjectResolver method populateModuleContentRoots.

@Override
public void populateModuleContentRoots(@NotNull IdeaModule gradleModule, @NotNull DataNode<ModuleData> ideModule) {
    ImportedModule importedModule = new ImportedModule(gradleModule);
    ideModule.createChild(IMPORTED_MODULE, importedModule);
    GradleProject gradleProject = gradleModule.getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (UnsupportedOperationException ignore) {
    }
    if (buildScript == null || !isAndroidGradleProject()) {
        nextResolver.populateModuleContentRoots(gradleModule, ideModule);
        return;
    }
    // do not derive module root dir based on *.iml file location
    File moduleRootDirPath = new File(toSystemDependentName(ideModule.getData().getLinkedExternalProjectPath()));
    AndroidProject androidProject = resolverCtx.getExtraProject(gradleModule, AndroidProject.class);
    boolean androidProjectWithoutVariants = false;
    String moduleName = gradleModule.getName();
    if (androidProject != null) {
        Variant selectedVariant = myVariantSelector.findVariantToSelect(androidProject);
        if (selectedVariant == null) {
            // If an Android project does not have variants, it would be impossible to build. This is a possible but invalid use case.
            // For now we are going to treat this case as a Java library module, because everywhere in the IDE (e.g. run configurations,
            // editors, test support, variants tool window, project building, etc.) we have the assumption that there is at least one variant
            // per Android project, and changing that in the code base is too risky, for very little benefit.
            // See https://code.google.com/p/android/issues/detail?id=170722
            androidProjectWithoutVariants = true;
        } else {
            String variantName = selectedVariant.getName();
            AndroidModuleModel model = new AndroidModuleModel(moduleName, moduleRootDirPath, androidProject, variantName);
            ideModule.createChild(ANDROID_MODEL, model);
        }
    }
    NativeAndroidProject nativeAndroidProject = resolverCtx.getExtraProject(gradleModule, NativeAndroidProject.class);
    if (nativeAndroidProject != null) {
        NdkModuleModel ndkModuleModel = new NdkModuleModel(moduleName, moduleRootDirPath, nativeAndroidProject);
        ideModule.createChild(NDK_MODEL, ndkModuleModel);
    }
    File gradleSettingsFile = new File(moduleRootDirPath, FN_SETTINGS_GRADLE);
    if (gradleSettingsFile.isFile() && androidProject == null && nativeAndroidProject == null) {
        // This is just a root folder for a group of Gradle projects. We don't set an IdeaGradleProject so the JPS builder won't try to
        // compile it using Gradle. We still need to create the module to display files inside it.
        createJavaProject(gradleModule, ideModule, false);
        return;
    }
    BuildScriptClasspathModel buildScriptModel = resolverCtx.getExtraProject(BuildScriptClasspathModel.class);
    String gradleVersion = buildScriptModel != null ? buildScriptModel.getGradleVersion() : null;
    File buildFilePath = buildScript.getSourceFile();
    GradleModuleModel gradleModuleModel = new GradleModuleModel(moduleName, gradleProject, buildFilePath, gradleVersion);
    ideModule.createChild(GRADLE_MODULE_MODEL, gradleModuleModel);
    if (nativeAndroidProject == null && (androidProject == null || androidProjectWithoutVariants)) {
        // This is a Java lib module.
        createJavaProject(gradleModule, ideModule, androidProjectWithoutVariants);
    }
}
Also used : BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) ImportedModule(com.android.tools.idea.gradle.ImportedModule) AndroidProject(com.android.builder.model.AndroidProject) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) Variant(com.android.builder.model.Variant) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) NativeAndroidProject(com.android.builder.model.NativeAndroidProject) GradleScript(org.gradle.tooling.model.gradle.GradleScript) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) GradleProject(org.gradle.tooling.model.GradleProject) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) File(java.io.File)

Example 3 with ImportedModule

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

the class ModuleDisposalDataServiceTest method testImportDataWithAndroidStudioAndSuccessfulSync.

public void testImportDataWithAndroidStudioAndSuccessfulSync() {
    when(myIdeInfo.isAndroidStudio()).thenReturn(true);
    when(mySyncState.lastSyncFailedOrHasIssues()).thenReturn(false);
    IdeaModule moduleModel = mock(IdeaModule.class);
    when(moduleModel.getName()).thenReturn(getModule().getName());
    ImportedModule importedModule = new ImportedModule(moduleModel);
    // This module should be disposed.
    Module libModule = createModule("lib");
    Project project = getProject();
    IdeModifiableModelsProvider modelsProvider = new IdeModifiableModelsProviderImpl(project);
    Collection<DataNode<ImportedModule>> nodes = Collections.singleton(new DataNode<>(IMPORTED_MODULE, importedModule, null));
    myDataService.importData(nodes, null, project, modelsProvider);
    ApplicationManager.getApplication().runWriteAction(modelsProvider::dispose);
    Collection<Module> modulesToDispose = Collections.singletonList(libModule);
    verify(myModuleDisposer).disposeModulesAndMarkImlFilesForDeletion(modulesToDispose, getProject(), modelsProvider);
}
Also used : Project(com.intellij.openapi.project.Project) IdeaModule(org.gradle.tooling.model.idea.IdeaModule) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ImportedModule(com.android.tools.idea.gradle.ImportedModule) IdeModifiableModelsProviderImpl(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl) ImportedModule(com.android.tools.idea.gradle.ImportedModule) IdeaModule(org.gradle.tooling.model.idea.IdeaModule) Module(com.intellij.openapi.module.Module) IdeModifiableModelsProvider(com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider)

Aggregations

ImportedModule (com.android.tools.idea.gradle.ImportedModule)3 Module (com.intellij.openapi.module.Module)2 AndroidProject (com.android.builder.model.AndroidProject)1 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 Variant (com.android.builder.model.Variant)1 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)1 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)1 DataNode (com.intellij.openapi.externalSystem.model.DataNode)1 IdeModifiableModelsProvider (com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProvider)1 IdeModifiableModelsProviderImpl (com.intellij.openapi.externalSystem.service.project.IdeModifiableModelsProviderImpl)1 Project (com.intellij.openapi.project.Project)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 GradleProject (org.gradle.tooling.model.GradleProject)1 GradleScript (org.gradle.tooling.model.gradle.GradleScript)1 IdeaModule (org.gradle.tooling.model.idea.IdeaModule)1 BuildScriptClasspathModel (org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel)1