Search in sources :

Example 1 with GradleModuleModel

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

the class DataNodeCaches method isCacheMissingModels.

private static boolean isCacheMissingModels(@NotNull DataNode<ModuleData> cache, @NotNull Module module) {
    GradleFacet gradleFacet = GradleFacet.getInstance(module);
    if (gradleFacet != null) {
        DataNode<GradleModuleModel> gradleDataNode = find(cache, GRADLE_MODULE_MODEL);
        if (gradleDataNode == null) {
            return true;
        }
        AndroidFacet androidFacet = AndroidFacet.getInstance(module);
        if (androidFacet != null) {
            DataNode<AndroidModuleModel> androidDataNode = find(cache, ANDROID_MODEL);
            if (androidDataNode == null || !isValidProxyObject(androidDataNode.getData().getAndroidProject())) {
                return true;
            }
        } else {
            JavaFacet javaFacet = JavaFacet.getInstance(module);
            if (javaFacet != null) {
                DataNode<JavaModuleModel> javaProjectDataNode = find(cache, JAVA_MODULE_MODEL);
                if (javaProjectDataNode == null) {
                    return true;
                }
            }
        }
    }
    NdkFacet ndkFacet = NdkFacet.getInstance(module);
    if (ndkFacet != null) {
        DataNode<NdkModuleModel> ndkModuleModelDataNode = find(cache, NDK_MODEL);
        if (ndkModuleModelDataNode == null || !isValidProxyObject(ndkModuleModelDataNode.getData().getAndroidProject())) {
            return true;
        }
    }
    return false;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) JavaFacet(com.android.tools.idea.gradle.project.facet.java.JavaFacet) JavaModuleModel(com.android.tools.idea.gradle.project.model.JavaModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) NdkFacet(com.android.tools.idea.gradle.project.facet.ndk.NdkFacet) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 2 with GradleModuleModel

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

the class GradleModuleSetup method createGradleModel.

@NotNull
private static GradleModuleModel createGradleModel(@NotNull Module module, @NotNull SyncAction.ModuleModels models) {
    GradleProject gradleProject = models.getModule().getGradleProject();
    GradleScript buildScript = null;
    try {
        buildScript = gradleProject.getBuildScript();
    } catch (Throwable e) {
    // Ignored. We got here because the project is using Gradle 1.8 or older.
    }
    File buildFilePath = buildScript != null ? buildScript.getSourceFile() : null;
    BuildScriptClasspathModel classpathModel = models.findModel(BuildScriptClasspathModel.class);
    String gradleVersion = classpathModel != null ? classpathModel.getGradleVersion() : null;
    return new GradleModuleModel(module.getName(), gradleProject, buildFilePath, gradleVersion);
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) BuildScriptClasspathModel(org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel) GradleScript(org.gradle.tooling.model.gradle.GradleScript) GradleProject(org.gradle.tooling.model.GradleProject) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with GradleModuleModel

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

the class ProjectSubset method findAndIncludeModules.

public void findAndIncludeModules(@NotNull Collection<String> moduleGradlePaths) {
    DataNode<ProjectData> projectInfo = DataNodeCaches.getInstance(myProject).getCachedProjectData();
    if (projectInfo == null) {
        return;
    }
    Collection<DataNode<ModuleData>> moduleInfos = findAll(projectInfo, MODULE);
    if (!moduleInfos.isEmpty()) {
        Project project = myProject;
        new Task.Modal(project, "Finding Missing Modules", false) {

            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                String[] originalSelection = getSelection();
                Set<String> selection = originalSelection != null ? Sets.newHashSet(originalSelection) : Sets.newHashSet();
                List<DataNode<ModuleData>> selectedModules = Lists.newArrayList();
                boolean found = false;
                int doneCount = 0;
                for (DataNode<ModuleData> moduleInfo : moduleInfos) {
                    indicator.setFraction(++doneCount / moduleInfos.size());
                    String name = getNameOf(moduleInfo);
                    if (selection.contains(name)) {
                        selectedModules.add(moduleInfo);
                        continue;
                    }
                    DataNode<GradleModuleModel> gradleProjectNode = find(moduleInfo, GRADLE_MODULE_MODEL);
                    if (gradleProjectNode != null) {
                        GradleModuleModel gradleModuleModel = gradleProjectNode.getData();
                        if (moduleGradlePaths.contains(gradleModuleModel.getGradlePath())) {
                            selection.add(name);
                            selectedModules.add(moduleInfo);
                            found = true;
                        }
                    }
                }
                if (!selectedModules.isEmpty() && found) {
                    setSelection(selectedModules);
                    populate(project, projectInfo, selectedModules, DEFAULT_REQUEST);
                }
            }
        }.queue();
    }
}
Also used : Task(com.intellij.openapi.progress.Task) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) Project(com.intellij.openapi.project.Project) DataNode(com.intellij.openapi.externalSystem.model.DataNode) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) ModuleData(com.intellij.openapi.externalSystem.model.project.ModuleData) ProjectData(com.intellij.openapi.externalSystem.model.project.ProjectData)

Example 4 with GradleModuleModel

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

the class AndroidGradleModuleUtils method getContainingModule.

/**
   * Given a file and a project, return the Module corresponding to the containing Gradle project for the file.  If the file is not
   * contained by any project then return null
   */
@Nullable
public static Module getContainingModule(File file, Project project) {
    VirtualFile vFile = VfsUtil.findFileByIoFile(file, false);
    if (vFile == null) {
        return null;
    }
    Module bestMatch = null;
    int bestMatchValue = Integer.MAX_VALUE;
    for (Module module : ModuleManager.getInstance(project).getModules()) {
        GradleFacet facet = GradleFacet.getInstance(module);
        if (facet != null) {
            GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
            assert gradleModuleModel != null;
            VirtualFile buildFile = gradleModuleModel.getBuildFile();
            if (buildFile != null) {
                VirtualFile root = buildFile.getParent();
                if (VfsUtilCore.isAncestor(root, vFile, true)) {
                    String relativePath = VfsUtilCore.getRelativePath(vFile, root, '/');
                    if (relativePath != null) {
                        int value = Iterables.size(Splitter.on('/').split(relativePath));
                        if (value < bestMatchValue) {
                            bestMatch = module;
                            bestMatchValue = value;
                        }
                    }
                }
            }
        }
    }
    return bestMatch;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with GradleModuleModel

use of com.android.tools.idea.gradle.project.model.GradleModuleModel 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)

Aggregations

GradleModuleModel (com.android.tools.idea.gradle.project.model.GradleModuleModel)16 GradleFacet (com.android.tools.idea.gradle.project.facet.gradle.GradleFacet)7 DataNode (com.intellij.openapi.externalSystem.model.DataNode)6 ModuleData (com.intellij.openapi.externalSystem.model.project.ModuleData)5 Module (com.intellij.openapi.module.Module)5 NdkModuleModel (com.android.tools.idea.gradle.project.model.NdkModuleModel)4 ProjectData (com.intellij.openapi.externalSystem.model.project.ProjectData)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)3 File (java.io.File)3 GradleProject (org.gradle.tooling.model.GradleProject)3 Nullable (org.jetbrains.annotations.Nullable)3 GradleScript (org.gradle.tooling.model.gradle.GradleScript)2 NotNull (org.jetbrains.annotations.NotNull)2 BuildScriptClasspathModel (org.jetbrains.plugins.gradle.model.BuildScriptClasspathModel)2 VisibleForTesting (com.android.annotations.VisibleForTesting)1 AndroidProject (com.android.builder.model.AndroidProject)1 NativeAndroidProject (com.android.builder.model.NativeAndroidProject)1 Variant (com.android.builder.model.Variant)1 GradleVersion (com.android.ide.common.repository.GradleVersion)1