Search in sources :

Example 1 with ProtoFile

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

the class BuildFileGenerator method getInternalPackageDeps.

private List<String> getInternalPackageDeps(JavaClass javaClass, List<ProtoFile> protoFiles) {
    List<String> result = new ArrayList<>();
    Map<String, List<String>> protoFilenameToMessage = new HashMap<>();
    Map<String, List<String>> protoFilenameToService = new HashMap<>();
    for (ProtoFile protoFile : protoFiles) {
        List<String> protoMessages = new ArrayList<>();
        protoMessages.addAll(protoFile.getMessagesList());
        protoMessages.add(protoFile.getJavaOuterClassname());
        protoFilenameToMessage.put(protoFile.getFileName(), protoMessages);
        protoFilenameToService.put(protoFile.getFileName(), protoFile.getServicesList());
    }
    for (String classname : javaClass.getUsedClassesFromTheSamePackageList()) {
        String dep = "";
        for (Map.Entry<String, List<String>> entry : protoFilenameToService.entrySet()) {
            for (String service : entry.getValue()) {
                if ((service + "Grpc").equals(classname)) {
                    dep = ":" + entry.getKey() + "_java_grpc";
                }
            }
        }
        for (Map.Entry<String, List<String>> entry : protoFilenameToMessage.entrySet()) {
            if (entry.getValue().contains(classname)) {
                dep = ":" + entry.getKey() + "_java_proto";
                break;
            }
        }
        if (dep.isEmpty()) {
            dep = ":" + convertUpperCamelToLowerUnderscore(classname);
        }
        result.add(dep);
    }
    return result.stream().distinct().collect(Collectors.toList());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) HttpArchiveDepsList(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDepsList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ProtoFile

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

the class BuildFileGenerator method generateBuildFiles.

/* Returns Map<String, BuildFile> where
  String key is an absolute path to a folder where BUILD file should be created,
  BuildFile is the proto message. */
Map<String, BuildFile> generateBuildFiles(List<String> buildFileGenerationBlacklist) throws IOException {
    Map<String, BuildFile> result = new HashMap<>();
    List<ProtoFile> wholeProjectProtoFiles = getWholeProjectProtoFiles();
    List<ThirdPartyDep> thirdPartyDeps = getThirdPartyDeps().getThirdPartyDepList();
    HttpArchiveDepsList httpArchiveDepsList = getHttpArchiveDepsList();
    Map<String, String> internalProjectDeps = getInternalProjectJavaClassToTargetNameMap();
    for (String packagePath : getPathsToCreateBuildFiles(buildFileGenerationBlacklist)) {
        result.put(packagePath, generateBuildFile(packagePath, wholeProjectProtoFiles, thirdPartyDeps, httpArchiveDepsList, internalProjectDeps));
    }
    return result;
}
Also used : BuildFile(com.google.startupos.tools.build_file_generator.Protos.BuildFile) ThirdPartyDep(com.google.startupos.tools.build_file_generator.Protos.ThirdPartyDep) HashMap(java.util.HashMap) ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) HttpArchiveDepsList(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDepsList)

Example 3 with ProtoFile

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

the class BuildFileGenerator method generateBuildFile.

private BuildFile generateBuildFile(String packagePath, List<ProtoFile> wholeProjectProtoFiles, List<ThirdPartyDep> thirdPartyDeps, HttpArchiveDepsList httpArchiveDepsList, Map<String, String> internalProjectDeps) throws IOException {
    BuildFile.Builder buildFile = BuildFile.newBuilder();
    List<String> protoFilenames = getFilesByExtension(packagePath, ".proto");
    List<ProtoFile> protoFiles = new ArrayList<>();
    for (String protoFileName : protoFilenames) {
        ProtoFile protoFile = protoFileAnalyzer.getProtoFile(fileUtils.joinPaths(packagePath, protoFileName));
        protoFiles.add(protoFile);
        ProtoLibrary protoLibrary = getProtoLibrary(protoFile);
        buildFile.addProtoLibrary(protoLibrary);
        buildFile.addJavaProtoLibrary(getJavaProtoLibrary(protoLibrary.getName()));
        if (!protoFile.getServicesList().isEmpty()) {
            buildFile.addJavaGrpcLibrary(getJavaGrpcLibrary(protoFile));
            LoadExtensionStatement javaGrpcLibrary = getLoadExtensionStatement(JAVA_GRPC_LIBRARY_BZL_FILE_PATH, JAVA_GRPC_LIBRARY_SYMBOL);
            if (!buildFile.getExtensionList().contains(javaGrpcLibrary)) {
                buildFile.addExtension(javaGrpcLibrary);
            }
        }
    }
    List<String> javaClasses = getFilesByExtension(packagePath, ".java");
    if (!javaClasses.isEmpty()) {
        buildFile.addExtension(getLoadExtensionStatement(CHECKSTYLE_BZL_FILE_PATH, CHECKSTYLE_SYMBOL));
        for (String javaClassName : javaClasses) {
            JavaClass javaClass = javaClassAnalyzer.getJavaClass(fileUtils.joinPaths(packagePath, javaClassName));
            String targetName;
            if (javaClass.getHasMainMethod()) {
                JavaBinary javaBinary = getJavaBinary(javaClass, thirdPartyDeps, httpArchiveDepsList, protoFiles, wholeProjectProtoFiles, internalProjectDeps);
                targetName = javaBinary.getName();
                buildFile.addJavaBinary(javaBinary);
            } else if (javaClass.getIsTestClass()) {
                JavaTest javaTest = getJavaTest(javaClass, packagePath, thirdPartyDeps, httpArchiveDepsList, protoFiles, wholeProjectProtoFiles, internalProjectDeps);
                targetName = javaTest.getName();
                buildFile.addJavaTest(javaTest);
            } else {
                JavaLibrary javaLibrary = getJavaLibrary(javaClass, thirdPartyDeps, httpArchiveDepsList, protoFiles, wholeProjectProtoFiles, internalProjectDeps);
                targetName = javaLibrary.getName();
                buildFile.addJavaLibrary(javaLibrary);
            }
            buildFile.addCheckstyleTest(getCheckstyleTest(targetName));
        }
    }
    return buildFile.build();
}
Also used : BuildFile(com.google.startupos.tools.build_file_generator.Protos.BuildFile) ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) ArrayList(java.util.ArrayList) JavaBinary(com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaBinary) JavaProtoLibrary(com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaProtoLibrary) ProtoLibrary(com.google.startupos.tools.build_file_generator.Protos.BuildFile.ProtoLibrary) JavaClass(com.google.startupos.tools.build_file_generator.Protos.JavaClass) JavaLibrary(com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaLibrary) JavaTest(com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaTest) LoadExtensionStatement(com.google.startupos.tools.build_file_generator.Protos.BuildFile.LoadExtensionStatement)

Example 4 with ProtoFile

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

the class HttpArchiveDepsGenerator method getHttpArchiveDeps.

public HttpArchiveDepsList getHttpArchiveDeps(WorkspaceFile workspaceFile, List<String> httpArchiveNames) throws IOException {
    HttpArchiveDepsList.Builder result = HttpArchiveDepsList.newBuilder();
    for (String httpArchiveName : httpArchiveNames) {
        WorkspaceFile.HttpArchive httpArchive = WorkspaceFile.HttpArchive.getDefaultInstance();
        for (WorkspaceFile.HttpArchive currentHttpArchive : workspaceFile.getHttpArchiveList()) {
            if (currentHttpArchive.getName().equals(httpArchiveName)) {
                httpArchive = currentHttpArchive;
            }
        }
        if (areCommitIdsTheSame(httpArchiveName, getCommitId(httpArchive.getStripPrefix()))) {
            log.atInfo().log("Commit id in WORKSPACE file and commit id in \'%s\' file for \'%s\' http_archive " + "are the same. Nothing to update.", HTTP_ARCHIVE_DEPS_FILENAME, httpArchiveName);
            continue;
        }
        if (httpArchive.getName().equals(httpArchiveName)) {
            HttpArchiveDeps.Builder builder = HttpArchiveDeps.newBuilder();
            String url = httpArchive.getUrls(0).split("/archive")[0] + ".git";
            String repoName = url.substring(url.lastIndexOf('/') + 1).replace(".git", "");
            GitRepo gitRepo = createRepo(url, repoName);
            switchToCommit(gitRepo, httpArchive.getStripPrefix());
            String absRepoPath = fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER, repoName);
            ImmutableList<String> buildFilesAbsPaths = getBuildFilesAbsPaths(absRepoPath);
            for (String path : buildFilesAbsPaths) {
                BuildFile buildFile = buildFileParser.getBuildFile(path);
                for (BuildFile.JavaLibrary javaLibrary : buildFile.getJavaLibraryList()) {
                    addDeps(absRepoPath, builder, path, javaLibrary.getSrcsList(), javaLibrary.getName());
                }
                for (BuildFile.JavaBinary javaBinary : buildFile.getJavaBinaryList()) {
                    addDeps(absRepoPath, builder, path, javaBinary.getSrcsList(), javaBinary.getName());
                }
                for (BuildFile.ProtoLibrary protoLibrary : buildFile.getProtoLibraryList()) {
                    String absProtoFilePath = path.replace("BUILD", protoLibrary.getSrcs(0));
                    for (BuildFile.JavaProtoLibrary javaProtoLibrary : buildFile.getJavaProtoLibraryList()) {
                        if (javaProtoLibrary.getDepsList().contains(":" + protoLibrary.getName())) {
                            ProtoFile protoFile = protoFileAnalyzer.getProtoFile(absProtoFilePath);
                            if ((!protoFile.getJavaPackage().isEmpty()) && (!protoFile.getJavaOuterClassname().isEmpty())) {
                                String fullJavaClassName = protoFile.getJavaPackage() + "." + protoFile.getJavaOuterClassname();
                                String target = path.replace(absRepoPath, "/").replace("/BUILD", ":") + javaProtoLibrary.getName();
                                builder.addHttpArchiveDep(HttpArchiveDep.newBuilder().setTarget(target).setJavaClass(fullJavaClassName).build());
                            }
                        }
                    }
                }
            }
            builder.setName(getCommitId(httpArchive.getName())).setCommitId(getCommitId(httpArchive.getStripPrefix()));
            result.addHttpArchiveDeps(builder.build());
            fileUtils.clearDirectoryUnchecked(fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER));
        } else {
            log.atWarning().log("Can't find http_archive with name: %s", httpArchiveName);
        }
    }
    fileUtils.deleteFileOrDirectoryIfExists(fileUtils.joinPaths(fileUtils.getCurrentWorkingDirectory(), BUILD_GENERATOR_TEMP_FOLDER));
    return result.build();
}
Also used : BuildFile(com.google.startupos.tools.build_file_generator.Protos.BuildFile) HttpArchiveDeps(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps) WorkspaceFile(com.google.startupos.tools.build_file_generator.Protos.WorkspaceFile) ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) GitRepo(com.google.startupos.common.repo.GitRepo) HttpArchiveDepsList(com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDepsList)

Example 5 with ProtoFile

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

the class ProtoFileAnalyzerTest method getProtoFileTest.

@Test
public void getProtoFileTest() throws IOException {
    String fileContent = fileUtils.readFileFromResourcesUnchecked("tools/build_file_generator/tests/resources/test_proto.proto");
    String filePath = fileUtils.joinToAbsolutePath(testFolder, "test_proto.proto");
    fileUtils.writeStringUnchecked(fileContent, filePath);
    ProtoFile expectedProtoFile = ProtoFile.newBuilder().setPackage("com.test.package").setFileName("test_proto").setJavaPackage("com.test.javapackage").setJavaOuterClassname("Protos").addAllMessages(Arrays.asList("FileRequest", "FileResponse")).addAllServices(Arrays.asList("FileService")).addAllEnums(Arrays.asList("BooleanEnum")).addAllImports(Arrays.asList("tools/build_file_generator/tests/resources/another_proto.proto")).build();
    assertEquals(expectedProtoFile, protoFileAnalyzer.getProtoFile(filePath));
}
Also used : ProtoFile(com.google.startupos.tools.build_file_generator.Protos.ProtoFile) Test(org.junit.Test)

Aggregations

ProtoFile (com.google.startupos.tools.build_file_generator.Protos.ProtoFile)6 BuildFile (com.google.startupos.tools.build_file_generator.Protos.BuildFile)3 HttpArchiveDepsList (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDepsList)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImmutableList (com.google.common.collect.ImmutableList)1 GitRepo (com.google.startupos.common.repo.GitRepo)1 JavaBinary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaBinary)1 JavaLibrary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaLibrary)1 JavaProtoLibrary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaProtoLibrary)1 JavaTest (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaTest)1 LoadExtensionStatement (com.google.startupos.tools.build_file_generator.Protos.BuildFile.LoadExtensionStatement)1 ProtoLibrary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.ProtoLibrary)1 HttpArchiveDeps (com.google.startupos.tools.build_file_generator.Protos.HttpArchiveDeps)1 JavaClass (com.google.startupos.tools.build_file_generator.Protos.JavaClass)1 ThirdPartyDep (com.google.startupos.tools.build_file_generator.Protos.ThirdPartyDep)1 WorkspaceFile (com.google.startupos.tools.build_file_generator.Protos.WorkspaceFile)1 List (java.util.List)1 Map (java.util.Map)1 Test (org.junit.Test)1