Skip to content

Commit

Permalink
fix: Preserve all manual changes to API_ROOT files for now (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma committed Oct 28, 2021
1 parent 9a45266 commit 80fdf50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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) {
Expand All @@ -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()) {
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()));
Expand All @@ -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 =
Expand Down

0 comments on commit 80fdf50

Please sign in to comment.