Search in sources :

Example 1 with HttpArchiveDep

use of com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDep in project startup-os by google.

the class BuildFileGenerator method getDeps.

// TODO: Add support java_grpc libraries
private List<String> getDeps(JavaClass javaClass, List<ThirdPartyDep> thirdPartyDeps, HttpArchiveDepsList httpArchiveDepsList, List<ProtoFile> protoFiles, List<ProtoFile> wholeProjectProtoFiles, Map<String, String> internalProjectDeps) {
    Set<String> result = new HashSet<>();
    List<String> internalPackageDeps = getInternalPackageDeps(javaClass, protoFiles);
    result.addAll(internalPackageDeps);
    for (Import importProto : javaClass.getImportList()) {
        if (importProto.getPackage().startsWith("java.") || importProto.getPackage().startsWith("com.sun.net.") || importProto.getPackage().startsWith("javax.imageio") || importProto.getPackage().startsWith("javax.swing")) {
            continue;
        }
        List<ThirdPartyDep> thirdPartyTargets = new ArrayList<>();
        for (ThirdPartyDep thirdPartyDep : thirdPartyDeps) {
            if (thirdPartyDep.getJavaClassList().contains(importProto.getPackage().replace(".", "/") + "/" + importProto.getRootClass() + ".class")) {
                thirdPartyTargets.add(thirdPartyDep);
            }
        }
        String httpArchiveTarget = "";
        if (httpArchiveTarget.isEmpty()) {
            for (HttpArchiveDeps httpArchiveDeps : httpArchiveDepsList.getHttpArchiveDepsList()) {
                for (HttpArchiveDep httpArchiveDep : httpArchiveDeps.getHttpArchiveDepList()) {
                    if (httpArchiveDep.getJavaClass().contains(importProto.getPackage() + "." + importProto.getRootClass())) {
                        httpArchiveTarget = '@' + httpArchiveDeps.getName() + httpArchiveDep.getTarget();
                        break;
                    }
                }
                if (!httpArchiveTarget.isEmpty()) {
                    break;
                }
            }
        }
        String target;
        if (thirdPartyTargets.isEmpty()) {
            // It isn't third party dep.
            if (httpArchiveTarget.isEmpty()) {
                String classNameWithPackage = importProto.getPackage() + "." + importProto.getRootClass();
                if (internalProjectDeps.containsKey(classNameWithPackage)) {
                    target = internalProjectDeps.get(classNameWithPackage);
                } else {
                    target = getInternalProjectDep(importProto, wholeProjectProtoFiles, javaClass.getPackage());
                }
            } else {
                target = httpArchiveTarget;
            }
        } else if (thirdPartyTargets.size() == 1) {
            target = thirdPartyTargets.get(0).getTarget();
        } else {
            // If the class exists in several third party deps we choose the smallest one
            // since the other ones probably contain it
            target = thirdPartyTargets.stream().sorted(Comparator.comparing(ThirdPartyDep::getJavaClassCount)).collect(Collectors.toList()).get(0).getTarget();
        }
        if (target.endsWith("//third_party/maven/com/google/dagger:dagger") || target.endsWith("//third_party/maven/com/google/dagger") || target.endsWith("//common:dagger_common_component")) {
            if (projectName.equals(STARTUP_OS_PROJECT_NAME)) {
                target = "//common:dagger_with_annotation_processor";
            } else {
                target = "@startup_os//common:dagger_with_annotation_processor";
            }
        }
        // `//common/repo:git_repo` exists then we could generalize to remove `_factory`.
        if (target.equals("//common/repo:git_repo_factory")) {
            if (projectName.equals(STARTUP_OS_PROJECT_NAME)) {
                target = "//common/repo:repo";
            } else {
                target = "@startup_os//common/repo:repo";
            }
        }
        if (!target.isEmpty()) {
            result.add(target);
        }
    }
    return result.stream().sorted().collect(Collectors.toList());
}
Also used : Import(com.google.startupos.tools.build_file_generator.Protos.Import) HttpArchiveDeps(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps) ThirdPartyDep(com.google.startupos.tools.build_file_generator.Protos.ThirdPartyDep) HttpArchiveDep(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDep) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Aggregations

HttpArchiveDep (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDep)1 HttpArchiveDeps (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps)1 Import (com.google.startupos.tools.build_file_generator.Protos.Import)1 ThirdPartyDep (com.google.startupos.tools.build_file_generator.Protos.ThirdPartyDep)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1