Search in sources :

Example 1 with Project

use of com.android.tools.lint.detector.api.Project in project android by JetBrains.

the class LintIdeProject method create.

/** Creates a set of projects for the given IntelliJ modules */
@NonNull
public static List<Project> create(@NonNull LintIdeClient client, @Nullable List<VirtualFile> files, @NonNull Module... modules) {
    List<Project> projects = Lists.newArrayList();
    Map<Project, Module> projectMap = Maps.newHashMap();
    Map<Module, Project> moduleMap = Maps.newHashMap();
    Map<AndroidLibrary, Project> libraryMap = Maps.newHashMap();
    if (files != null && !files.isEmpty()) {
        // Wrap list with a mutable list since we'll be removing the files as we see them
        files = Lists.newArrayList(files);
    }
    for (Module module : modules) {
        addProjects(client, module, files, moduleMap, libraryMap, projectMap, projects);
    }
    client.setModuleMap(projectMap);
    if (projects.size() > 1) {
        // Partition the projects up such that we only return projects that aren't
        // included by other projects (e.g. because they are library projects)
        Set<Project> roots = new HashSet<Project>(projects);
        for (Project project : projects) {
            roots.removeAll(project.getAllLibraries());
        }
        return Lists.newArrayList(roots);
    } else {
        return projects;
    }
}
Also used : Project(com.android.tools.lint.detector.api.Project) Module(com.intellij.openapi.module.Module) NonNull(com.android.annotations.NonNull)

Example 2 with Project

use of com.android.tools.lint.detector.api.Project in project android by JetBrains.

the class LintIdeProject method addProjects.

/**
   * Recursively add lint projects for the given module, and any other module or library it depends on, and also
   * populate the reverse maps so we can quickly map from a lint project to a corresponding module/library (used
   * by the lint client
   */
private static void addProjects(@NonNull LintClient client, @NonNull Module module, @Nullable List<VirtualFile> files, @NonNull Map<Module, Project> moduleMap, @NonNull Map<AndroidLibrary, Project> libraryMap, @NonNull Map<Project, Module> projectMap, @NonNull List<Project> projects) {
    if (moduleMap.containsKey(module)) {
        return;
    }
    LintModuleProject project = createModuleProject(client, module);
    if (project == null) {
        // It's possible for the module to *depend* on Android code, e.g. in a Gradle
        // project there will be a top-level non-Android module
        List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, false);
        for (AndroidFacet dependentFacet : dependentFacets) {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, projects);
        }
        return;
    }
    projects.add(project);
    moduleMap.put(module, project);
    projectMap.put(project, module);
    if (processFileFilter(module, files, project)) {
        // No need to process dependencies when doing single file analysis
        return;
    }
    List<Project> dependencies = Lists.newArrayList();
    // No, this shouldn't use getAllAndroidDependencies; we may have non-Android dependencies that this won't include
    // (e.g. Java-only modules)
    List<AndroidFacet> dependentFacets = AndroidUtils.getAllAndroidDependencies(module, true);
    for (AndroidFacet dependentFacet : dependentFacets) {
        Project p = moduleMap.get(dependentFacet.getModule());
        if (p != null) {
            dependencies.add(p);
        } else {
            addProjects(client, dependentFacet.getModule(), files, moduleMap, libraryMap, projectMap, dependencies);
        }
    }
    AndroidFacet facet = AndroidFacet.getInstance(module);
    if (facet != null) {
        AndroidModuleModel androidModuleModel = AndroidModuleModel.get(facet);
        if (androidModuleModel != null) {
            addGradleLibraryProjects(client, files, libraryMap, projects, facet, androidModuleModel, project, projectMap, dependencies);
        }
    }
    project.setDirectLibraries(dependencies);
}
Also used : Project(com.android.tools.lint.detector.api.Project) AndroidModuleModel(com.android.tools.idea.gradle.project.model.AndroidModuleModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 3 with Project

use of com.android.tools.lint.detector.api.Project in project android by JetBrains.

the class LintIdeProject method addGradleLibraryProjects.

/** Adds any gradle library projects to the dependency list */
private static void addGradleLibraryProjects(@NonNull LintClient client, @Nullable List<VirtualFile> files, @NonNull Map<AndroidLibrary, Project> libraryMap, @NonNull List<Project> projects, @NonNull AndroidFacet facet, @NonNull AndroidModuleModel androidModuleModel, @NonNull LintModuleProject project, @NonNull Map<Project, Module> projectMap, @NonNull List<Project> dependencies) {
    Collection<AndroidLibrary> libraries = androidModuleModel.getSelectedMainCompileDependencies().getLibraries();
    for (AndroidLibrary library : libraries) {
        Project p = libraryMap.get(library);
        if (p == null) {
            File dir = library.getFolder();
            p = new LintGradleLibraryProject(client, dir, dir, library);
            libraryMap.put(library, p);
            projectMap.put(p, facet.getModule());
            projects.add(p);
            if (files != null) {
                VirtualFile libraryDir = LocalFileSystem.getInstance().findFileByIoFile(dir);
                if (libraryDir != null) {
                    ListIterator<VirtualFile> iterator = files.listIterator();
                    while (iterator.hasNext()) {
                        VirtualFile file = iterator.next();
                        if (VfsUtilCore.isAncestor(libraryDir, file, false)) {
                            project.addFile(VfsUtilCore.virtualToIoFile(file));
                            iterator.remove();
                        }
                    }
                }
                if (files.isEmpty()) {
                    // No more work in other modules
                    files = null;
                }
            }
        }
        dependencies.add(p);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.android.tools.lint.detector.api.Project) VirtualFile(com.intellij.openapi.vfs.VirtualFile) File(java.io.File)

Example 4 with Project

use of com.android.tools.lint.detector.api.Project in project android by JetBrains.

the class LintIdeProject method findAndroidModule.

/** Find an Android module that depends on this module; prefer app modules over library modules */
@Nullable
private static Module findAndroidModule(@NonNull final Module module) {
    // Search for dependencies of this module
    Graph<Module> graph = ApplicationManager.getApplication().runReadAction(new Computable<Graph<Module>>() {

        @Override
        public Graph<Module> compute() {
            com.intellij.openapi.project.Project project = module.getProject();
            if (project.isDisposed()) {
                return null;
            }
            return ModuleManager.getInstance(project).moduleGraph();
        }
    });
    if (graph == null) {
        return null;
    }
    Set<AndroidFacet> facets = Sets.newHashSet();
    HashSet<Module> seen = Sets.newHashSet();
    seen.add(module);
    addAndroidModules(facets, seen, graph, module);
    // Prefer Android app modules
    for (AndroidFacet facet : facets) {
        if (facet.isAppProject()) {
            return facet.getModule();
        }
    }
    // Resort to library modules if no app module depends directly on it
    if (!facets.isEmpty()) {
        return facets.iterator().next().getModule();
    }
    return null;
}
Also used : Project(com.android.tools.lint.detector.api.Project) Graph(com.intellij.util.graph.Graph) Module(com.intellij.openapi.module.Module) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with Project

use of com.android.tools.lint.detector.api.Project in project android by JetBrains.

the class LintIdeProject method createForSingleFile.

/**
   * Creates a project for a single file. Also optionally creates a main project for the file, if applicable.
   *
   * @param client the lint client
   * @param file the file to create a project for
   * @param module the module to create a project for
   * @return a project for the file, as well as a project (or null) for the main Android module
   */
@NonNull
public static Pair<Project, Project> createForSingleFile(@NonNull LintIdeClient client, @Nullable VirtualFile file, @NonNull Module module) {
    // TODO: Can make this method even more lightweight: we don't need to initialize anything in the project (source paths etc)
    // other than the metadata necessary for this file's type
    LintModuleProject project = createModuleProject(client, module);
    LintModuleProject main = null;
    Map<Project, Module> projectMap = Maps.newHashMap();
    if (project != null) {
        project.setDirectLibraries(Collections.<Project>emptyList());
        if (file != null) {
            project.addFile(VfsUtilCore.virtualToIoFile(file));
        }
        projectMap.put(project, module);
        // using the library, not "1" (the default for a module without a manifest)
        if (!project.isAndroidProject()) {
            Module androidModule = findAndroidModule(module);
            if (androidModule != null) {
                main = createModuleProject(client, androidModule);
                if (main != null) {
                    projectMap.put(main, androidModule);
                    main.setDirectLibraries(Collections.<Project>singletonList(project));
                }
            }
        }
    }
    client.setModuleMap(projectMap);
    //noinspection ConstantConditions
    return Pair.<Project, Project>create(project, main);
}
Also used : Project(com.android.tools.lint.detector.api.Project) Module(com.intellij.openapi.module.Module) NonNull(com.android.annotations.NonNull)

Aggregations

Project (com.android.tools.lint.detector.api.Project)5 Module (com.intellij.openapi.module.Module)3 NonNull (com.android.annotations.NonNull)2 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)2 AndroidModuleModel (com.android.tools.idea.gradle.project.model.AndroidModuleModel)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 Graph (com.intellij.util.graph.Graph)1 File (java.io.File)1 Nullable (org.jetbrains.annotations.Nullable)1