Search in sources :

Example 1 with JavaClass

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

the class JavaClassAnalyzerTest method isWholePackageImportTest.

@Test
public void isWholePackageImportTest() throws IOException {
    String fileContent = "package com.test.tests;" + System.lineSeparator() + "import java.util.*;" + System.lineSeparator() + "public class SomeClass {" + System.lineSeparator() + "  List<Integer> list = new ArrayList<>();" + System.lineSeparator() + "}";
    String filePath = fileUtils.joinToAbsolutePath(testFolder, "SomeClass.java");
    fileUtils.writeStringUnchecked(fileContent, filePath);
    JavaClass expectedJavaClass = JavaClass.newBuilder().setPackage("com.test.tests").addImport(Import.newBuilder().setPackage("java.util").setWholePackageImport(true).build()).setClassName("SomeClass").build();
    assertEquals(expectedJavaClass, javaClassAnalyzer.getJavaClass(filePath));
}
Also used : JavaClass(com.google.startupos.tools.build_file_generator.Protos.JavaClass) Test(org.junit.Test)

Example 2 with JavaClass

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

the class JavaClassAnalyzerTest method isTestClassTest.

@Test
public void isTestClassTest() throws IOException {
    String fileContent = "package com.test.tests;" + System.lineSeparator() + "import org.junit.Test;" + System.lineSeparator() + "import static org.junit.Assert.assertEquals;" + System.lineSeparator() + "public class TestClass {" + System.lineSeparator() + "  @Test" + System.lineSeparator() + "  public void test() {" + System.lineSeparator() + "    assertEquals(4, 2+2);" + System.lineSeparator() + "  }" + System.lineSeparator() + "}";
    String filePath = fileUtils.joinToAbsolutePath(testFolder, "TestClass.java");
    fileUtils.writeStringUnchecked(fileContent, filePath);
    JavaClass expectedJavaClass = JavaClass.newBuilder().setPackage("com.test.tests").addImport(Import.newBuilder().setPackage("org.junit").setClassName("Test").setRootClass("Test").build()).addImport(Import.newBuilder().setPackage("org.junit").setClassName("Assert").setIsStatic(true).setRootClass("Assert").build()).setClassName("TestClass").setIsTestClass(true).build();
    assertEquals(expectedJavaClass, javaClassAnalyzer.getJavaClass(filePath));
}
Also used : JavaClass(com.google.startupos.tools.build_file_generator.Protos.JavaClass) Test(org.junit.Test)

Example 3 with JavaClass

use of com.google.startupos.tools.build_file_generator.Protos.JavaClass 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 JavaClass

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

the class HttpArchiveDepsGenerator method addDeps.

private void addDeps(String absRepoPath, HttpArchiveDeps.Builder result, String absBuildFilePath, List<String> javaClasses, String targetName) throws IOException {
    for (String javaClassName : javaClasses) {
        String absJavaClassPath = absBuildFilePath.replace("BUILD", javaClassName);
        if (fileUtils.fileExists(absJavaClassPath)) {
            JavaClass javaClass = javaClassAnalyzer.getJavaClass(absJavaClassPath);
            String fullTargetName = absBuildFilePath.replace(absRepoPath, "/").replace("/BUILD", ":") + targetName;
            result.addHttpArchiveDep(HttpArchiveDep.newBuilder().setJavaClass(getProjectPackageSuffix(javaClass.getPackage(), absRepoPath) + absBuildFilePath.replace("/BUILD", ".").replace(absRepoPath, "").replace("/", ".") + javaClassName.replace("/", ".")).setTarget(shortenTarget(fullTargetName)).build());
        }
    }
}
Also used : JavaClass(com.google.startupos.tools.build_file_generator.Protos.JavaClass)

Example 5 with JavaClass

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

the class BuildFileGenerator method getInternalProjectJavaClassToTargetNameMap.

private Map<String, String> getInternalProjectJavaClassToTargetNameMap() {
    Map<String, String> result = new HashMap<>();
    for (String absPath : getPathsContainJavaAndProtoFiles()) {
        final String relPackagePath = "/" + absPath.replace(fileUtils.getCurrentWorkingDirectory(), "");
        final String absBuildFilePath = fileUtils.joinPaths(absPath, "BUILD");
        if (fileUtils.fileExists(absBuildFilePath)) {
            BuildFile buildFile = buildFileParser.getBuildFile(absBuildFilePath);
            for (JavaLibrary javaLibrary : buildFile.getJavaLibraryList()) {
                if (javaLibrary.getSrcsCount() > 0) {
                    String absClassPath = fileUtils.joinPaths(absPath, javaLibrary.getSrcs(0));
                    try {
                        JavaClass javaClass = javaClassAnalyzer.getJavaClass(absClassPath);
                        final String projectPackageSuffix = buildFileGeneratorUtils.getProjectPackageSuffix(projectName, javaClass.getPackage());
                        String targetName = relPackagePath + ":" + javaLibrary.getName();
                        for (String src : javaLibrary.getSrcsList()) {
                            String fullClassName = (relPackagePath.replace("//", "") + "." + src).replace("/", ".");
                            if (!projectPackageSuffix.isEmpty()) {
                                fullClassName = projectPackageSuffix + "." + fullClassName;
                            }
                            result.put(fullClassName.replaceAll(".java$", ""), targetName);
                        }
                    } catch (IOException e) {
                        throw new RuntimeException("Can't find java class by path: " + absClassPath, e);
                    }
                }
            }
        }
    }
    return result;
}
Also used : BuildFile(com.google.startupos.tools.build_file_generator.Protos.BuildFile) JavaLibrary(com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaLibrary) JavaClass(com.google.startupos.tools.build_file_generator.Protos.JavaClass) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

JavaClass (com.google.startupos.tools.build_file_generator.Protos.JavaClass)6 Test (org.junit.Test)3 BuildFile (com.google.startupos.tools.build_file_generator.Protos.BuildFile)2 JavaLibrary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaLibrary)2 JavaBinary (com.google.startupos.tools.build_file_generator.Protos.BuildFile.JavaBinary)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 ProtoFile (com.google.startupos.tools.build_file_generator.Protos.ProtoFile)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1