diff --git a/bazel/src/main/java/com/google/api/codegen/bazel/ApisVisitor.java b/bazel/src/main/java/com/google/api/codegen/bazel/ApisVisitor.java index 5f4bca5..0fcdb7c 100644 --- a/bazel/src/main/java/com/google/api/codegen/bazel/ApisVisitor.java +++ b/bazel/src/main/java/com/google/api/codegen/bazel/ApisVisitor.java @@ -135,6 +135,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce String dirStr = dir.toString(); ApiVersionedDir bp = bazelApiVerPackages.get(dirStr); BazelBuildFileTemplate template = null; + boolean preserveExisting = false; String tmplType = ""; if (bp.getProtoPackage() != null) { boolean isGapicLibrary = @@ -150,6 +151,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce } else if (bp.getServiceYamlPath() != null) { template = this.rootApiTempl; tmplType = "API_ROOT"; + preserveExisting = !overwrite; } if (template == null) { @@ -159,6 +161,7 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce String rootDirStr = srcDir.toString(); String outDirPath = destDir.toString() + dirStr.substring(rootDirStr.length()); File outDir = new File(outDirPath); + Path outFilePath = Paths.get(outDir.toString(), "BUILD.bazel"); if (!outDir.exists()) { if (!outDir.mkdirs()) { @@ -167,11 +170,19 @@ public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOExce } } + // Currently we avoid overwriting existing root api build files. This is to + // preserve such files that may contain manually-added rules for generating + // Ruby wrappers, which we cannot generate yet. In the future, we should + // expand this tool to generate those rules. + if (preserveExisting && outFilePath.toFile().exists()) { + return FileVisitResult.CONTINUE; + } + System.out.println( "Write File [" + tmplType + "]: " + outDir.toString() + File.separator + "BUILD.bazel"); try { BazelBuildFileView bpv = new BazelBuildFileView(bp); - fileWriter.write(Paths.get(outDir.toString(), "BUILD.bazel"), template.expand(bpv)); + fileWriter.write(outFilePath, template.expand(bpv)); } catch (RuntimeException ex) { ex.printStackTrace(); } diff --git a/bazel/src/test/java/com/google/api/codegen/bazel/BuildFileGeneratorTest.java b/bazel/src/test/java/com/google/api/codegen/bazel/BuildFileGeneratorTest.java index 6548569..8fe105e 100644 --- a/bazel/src/test/java/com/google/api/codegen/bazel/BuildFileGeneratorTest.java +++ b/bazel/src/test/java/com/google/api/codegen/bazel/BuildFileGeneratorTest.java @@ -15,6 +15,7 @@ package com.google.api.codegen.bazel; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -72,10 +73,13 @@ public void testGenerateBuildFiles_legacyJavaLanguageOverrides() throws IOExcept @Test public void testRegeneration() throws IOException, InterruptedException { - // In this test we run the generator twice, changing the generated - // google/example/library/v1/BUILD.bazel - // after the first run, and verifying that some changed values are preserved - // (and some are not). + // In this test we run the generator multiple times, changing the generated + // google/example/library/v1/BUILD.bazel (i.e. GAPIC_VERSIONED) and + // google/example/library/BUILD.bazel (i.e. API_ROOT) after the first run. + // In the GAPIC_VERSIONED file, we verify that some changed values are + // preserved and others are not. In the API_ROOT file, we verify that all + // modifications are preserved. Finally, we rerun the generator with + // --overwrite to ensure that it properly overwrites all changes. Path tempDirPath = getTemporaryDirectory(); // I'm lazy, so let's just "cp -r" stuff. @@ -134,6 +138,10 @@ public void testRegeneration() throws IOException, InterruptedException { buildozer.commit(); + // Change the content in google/example/library/BUILD.bazel + String changedRootContent = "# Hello\n"; + Files.write(Paths.get(rootBuildFilePath), changedRootContent.getBytes(StandardCharsets.UTF_8)); + // Run the generator again new BuildFileGenerator() .generateBuildFiles(args.createApisVisitor(null, tempDirPath.toString())); @@ -160,6 +168,9 @@ public void testRegeneration() throws IOException, InterruptedException { Assert.assertEquals( "library_example_grpc_service_config.json", buildozer.getAttribute(gapicBuildFilePath, "library_nodejs_gapic", "grpc_service_config")); + + // Check that the changed root file is preserved + Assert.assertEquals(changedRootContent, ApisVisitor.readFile(rootBuildFilePath)); // Now run with overwrite and verify it actually ignores all the changes ArgsParser argsOverwrite =