Search in sources :

Example 6 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 7 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)

Example 8 with GradleModuleModel

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

the class GradleProjectSyncData method createFrom.

@Nullable
@VisibleForTesting
static GradleProjectSyncData createFrom(@NotNull Project project) throws IOException {
    GradleProjectSyncData data = new GradleProjectSyncData();
    File rootDirPath = getBaseDirPath(project);
    Module[] modules = ModuleManager.getInstance(project).getModules();
    for (Module module : modules) {
        GradleFacet gradleFacet = GradleFacet.getInstance(module);
        if (gradleFacet != null) {
            GradleModuleModel gradleModuleModel = gradleFacet.getGradleModuleModel();
            if (gradleModuleModel != null) {
                data.addFileChecksum(rootDirPath, gradleModuleModel.getBuildFile());
            } else {
                LOG.warn(String.format("Trying to create project data from a not initialized project '%1$s'. Abort.", project.getName()));
                return null;
            }
        }
        if (isGradleProjectModule(module)) {
            data.addFileChecksum(rootDirPath, getGradleBuildFile(module));
            data.addFileChecksum(rootDirPath, getGradleSettingsFile(rootDirPath));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_GRADLE_PROPERTIES));
            data.addFileChecksum(rootDirPath, new File(rootDirPath, FN_LOCAL_PROPERTIES));
            data.addFileChecksum(rootDirPath, getGradleUserSettingsFile());
        }
        NdkModuleModel ndkModuleModel = NdkModuleModel.get(module);
        if (ndkModuleModel != null) {
            for (File externalBuildFile : ndkModuleModel.getAndroidProject().getBuildFiles()) {
                data.addFileChecksum(rootDirPath, externalBuildFile);
            }
        }
    }
    GradleSyncState syncState = GradleSyncState.getInstance(project);
    data.myLastGradleSyncTimestamp = syncState.getSummary().getSyncTimestamp();
    return data;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Projects.isGradleProjectModule(com.android.tools.idea.gradle.util.Projects.isGradleProjectModule) Module(com.intellij.openapi.module.Module) NdkModuleModel(com.android.tools.idea.gradle.project.model.NdkModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) VfsUtilCore.virtualToIoFile(com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile) VfsUtil.findFileByIoFile(com.intellij.openapi.vfs.VfsUtil.findFileByIoFile) GradleSyncState(com.android.tools.idea.gradle.project.sync.GradleSyncState) VisibleForTesting(com.android.annotations.VisibleForTesting) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with GradleModuleModel

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

the class MakeBeforeRunTaskProvider method createAvailableTasks.

private List<String> createAvailableTasks() {
    ModuleManager moduleManager = ModuleManager.getInstance(myProject);
    List<String> gradleTasks = Lists.newArrayList();
    for (Module module : moduleManager.getModules()) {
        GradleFacet facet = GradleFacet.getInstance(module);
        if (facet == null) {
            continue;
        }
        GradleModuleModel gradleModuleModel = facet.getGradleModuleModel();
        if (gradleModuleModel == null) {
            continue;
        }
        gradleTasks.addAll(gradleModuleModel.getTaskNames());
    }
    return gradleTasks;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) ModuleManager(com.intellij.openapi.module.ModuleManager) Module(com.intellij.openapi.module.Module)

Example 10 with GradleModuleModel

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

the class WrapArchiveOptionsStep method getContainingModule.

@Nullable
private Module getContainingModule(@NotNull VirtualFile file) {
    Module bestMatch = null;
    if (myProject != null) {
        int bestMatchValue = Integer.MAX_VALUE;
        for (Module module : ModuleManager.getInstance(myProject).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, file, true)) {
                        String relativePath = VfsUtilCore.getRelativePath(file, root, '/');
                        if (relativePath != null) {
                            int value = Iterables.size(Splitter.on('/').split(relativePath));
                            if (value < bestMatchValue) {
                                bestMatch = module;
                            }
                        }
                    }
                }
            }
        }
    }
    return bestMatch;
}
Also used : GradleModuleModel(com.android.tools.idea.gradle.project.model.GradleModuleModel) VirtualFile(com.intellij.openapi.vfs.VirtualFile) GradleFacet(com.android.tools.idea.gradle.project.facet.gradle.GradleFacet) Module(com.intellij.openapi.module.Module) Nullable(org.jetbrains.annotations.Nullable)

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