BuildFileGenerator { void generateBuildFiles(ApisVisitor visitor) throws IOException { System.out.println("\n\n========== READING INPUT DIRECTORY =========="); Files.walkFileTree(visitor.getSrcDir(), visitor); visitor.setWriterMode(true); System.out.println("\n\n========== WRITING GENERATED FILES =========="); Files.walkFileTree(visitor.getSrcDir(), visitor); System.out.println("\nBUILD.bazel file generation completed successfully\n"); }  static void main(String[] args);  }
@Test public void testGenerateBuildFiles() throws IOException { String buildozerPath = getBuildozerPath(); ArgsParser args = new ArgsParser(new String[] {"--buildozer=" + buildozerPath, "--src=" + SRC_DIR}); FileWriter fw = new FileWriter(); new BuildFileGenerator().generateBuildFiles(args.createApisVisitor(fw, PATH_PREFIX)); Path fileBodyPathPrefix = Paths.get(PATH_PREFIX, SRC_DIR, "google", "example", "library"); String gapicBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "v1", "BUILD.bazel").toString(); String rawBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "BUILD.bazel").toString(); Assert.assertEquals(2, fw.files.size()); Assert.assertEquals( ApisVisitor.readFile(gapicBuildFilePath + ".baseline"), fw.files.get(gapicBuildFilePath)); Assert.assertEquals( ApisVisitor.readFile(rawBuildFilePath + ".baseline"), fw.files.get(rawBuildFilePath)); }
@Test public void testRegeneration() throws IOException, InterruptedException { Path tempDirPath = getTemporaryDirectory(); Path fixturesPath = Paths.get(PATH_PREFIX, SRC_DIR); new ProcessBuilder(new String[] {"cp", "-r", fixturesPath.toString(), tempDirPath.toString()}) .start() .waitFor(); String buildozerPath = getBuildozerPath(); Path copiedGoogleapis = Paths.get(tempDirPath.toString(), "googleapis"); ArgsParser args = new ArgsParser(new String[] {"--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis}); new BuildFileGenerator() .generateBuildFiles(args.createApisVisitor(null, tempDirPath.toString())); Path fileBodyPathPrefix = Paths.get(copiedGoogleapis.toString(), "google", "example", "library"); Path gapicBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "v1", "BUILD.bazel"); String rawBuildFilePath = Paths.get(fileBodyPathPrefix.toString(), "BUILD.bazel").toString(); Assert.assertEquals( ApisVisitor.readFile(gapicBuildFilePath.toString() + ".baseline"), ApisVisitor.readFile(gapicBuildFilePath.toString())); Assert.assertEquals( ApisVisitor.readFile(rawBuildFilePath + ".baseline"), ApisVisitor.readFile(rawBuildFilePath)); Buildozer.setBinaryPath(buildozerPath); Buildozer buildozer = Buildozer.getInstance(); buildozer.batchSetAttribute( gapicBuildFilePath, "library_nodejs_gapic", "package_name", "@google-cloud/library"); buildozer.batchRemoveAttribute( gapicBuildFilePath, "library_nodejs_gapic", "extra_protoc_parameters"); buildozer.batchAddAttribute( gapicBuildFilePath, "library_nodejs_gapic", "extra_protoc_parameters", "param1"); buildozer.batchAddAttribute( gapicBuildFilePath, "library_nodejs_gapic", "extra_protoc_parameters", "param2"); buildozer.batchSetAttribute( gapicBuildFilePath, "google-cloud-example-library-v1-csharp", "name", "renamed_csharp_rule"); buildozer.batchSetAttribute( gapicBuildFilePath, "google-cloud-example-library-v1-java", "name", "renamed_java_rule"); buildozer.batchSetAttribute( gapicBuildFilePath, "library_nodejs_gapic", "grpc_service_config", "fake_grpc_service_config"); buildozer.commit(); new BuildFileGenerator() .generateBuildFiles(args.createApisVisitor(null, tempDirPath.toString())); Assert.assertEquals( "@google-cloud/library", buildozer.getAttribute(gapicBuildFilePath, "library_nodejs_gapic", "package_name")); Assert.assertEquals( "[param1 param2]", buildozer.getAttribute( gapicBuildFilePath, "library_nodejs_gapic", "extra_protoc_parameters")); Assert.assertEquals( "renamed_csharp_rule", buildozer.getAttribute(gapicBuildFilePath, "%csharp_gapic_assembly_pkg", "name")); Assert.assertEquals( "renamed_java_rule", buildozer.getAttribute(gapicBuildFilePath, "%java_gapic_assembly_gradle_pkg", "name")); Assert.assertEquals( "library_example_grpc_service_config.json", buildozer.getAttribute(gapicBuildFilePath, "library_nodejs_gapic", "grpc_service_config")); ArgsParser argsOverwrite = new ArgsParser( new String[] { "--overwrite", "--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis }); new BuildFileGenerator() .generateBuildFiles(argsOverwrite.createApisVisitor(null, tempDirPath.toString())); Assert.assertEquals( ApisVisitor.readFile(gapicBuildFilePath.toString() + ".baseline"), ApisVisitor.readFile(gapicBuildFilePath.toString())); Assert.assertEquals( ApisVisitor.readFile(rawBuildFilePath + ".baseline"), ApisVisitor.readFile(rawBuildFilePath)); }