-
Notifications
You must be signed in to change notification settings - Fork 129
feat: preserve some values when regenerating BUILD.bazel #3237
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3237 +/- ##
============================================
- Coverage 87.12% 87.12% -0.01%
+ Complexity 6077 6076 -1
============================================
Files 494 494
Lines 24052 24052
Branches 2613 2613
============================================
- Hits 20956 20955 -1
Misses 2234 2234
- Partials 862 863 +1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we should have a flag that disables buildozer preservation? In the event someone wants to wipe out a BUILD.bazel file.
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Outdated
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Show resolved
Hide resolved
@@ -98,9 +98,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO | |||
} else if (fileName.endsWith(".proto")) { | |||
bp.parseProtoFile(fileName, readFile(file)); | |||
} else if (fileName.endsWith(".bazel")) { | |||
// Consider merging BUILD.bazel files if it becomes necessary (i.e. people will be doing many | |||
// valuable manual edits in their BUILD.bazel files). This will complicate the whole logic | |||
// so not doing it for now, hoping it will not be required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One can hope! But alas... 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment was very helpful to find the actual place to make changes! :)
Updates based on feedback from @noahdietz (thank you!):
|
LGTM, but I'll let @vam-google approve. Thanks Alex, this is great. |
a07063f
to
a30ed2f
Compare
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Outdated
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/ApiVersionedDir.java
Outdated
Show resolved
Hide resolved
String name = split[1]; | ||
if (kind.contains("_gapic_assembly_")) { | ||
this.assemblyPkgRulesNames.put(kind, name); | ||
} else if (kind.endsWith("_gapic_library")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will this logic work if there is more than one instance of the same rule type in build file? (i.e. like two java_gapic_library
targets in the same BUIDL.bazel file?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It won't work. Let's just throw an exception in this case and ask the user to use --overwrite
, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks Ok to me.
if (!target.startsWith(":")) { | ||
target = ":" + target; | ||
} | ||
return execute(new String[] {command, String.format("%s%s", bazelBuildFile, target)}, null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subjective, but String.format("%s%s", bazelBuildFile, target)
looks unnecessary, bazelBuildFile + target
is looks shorter and cleaner.Using plus for string concatenation is still very idiomatic in java.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After refactor, it's now "%s:%s"
which looks a little bit better :)
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/Buildozer.java
Outdated
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/Buildozer.java
Outdated
Show resolved
Hide resolved
rules_gapic/bazel/src/main/java/com/google/api/codegen/bazel/Buildozer.java
Outdated
Show resolved
Hide resolved
@@ -40,6 +44,18 @@ | |||
|
|||
private static String CLOUD_AUTH_SCOPE = "https://www.googleapis.com/auth/cloud-platform"; | |||
|
|||
private static final String[] PRESERVED_PROTO_LIBRARY_STRING_ATTRIBUTES = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How hard would it be to make this stuff configurable (probably to the extent of passing it from command line)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer not to do it now until we have the first user who needs it. That's a kind of YAGNI :)
7809945
to
97f2411
Compare
@vam-google Can you please take a look once more? I resolved most of the comments. Let me know if it looks good. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thought I still don't like the help part. Also, did you have a chance to actually test it from googleapis repo?
static void printUsage() { | ||
String helpMessage = | ||
"Usage (when running from googleapis folder):\n" | ||
+ " bazel run //:build_gen -- --src=rules_gapic/bazel/src/test/data/googleapis\n" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now the help became specific to googleapis context... That was kind of the reason why I did nto want to include any helps at all. Notice, the tool in its help speaks about how it must be executed from bazel (the java tool does not have to be bazel specific).
4037a2e
to
7f6dcf9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
ArgsParser args = new ArgsParser(new String[] {"--src=" + copiedGoogleapis.toString()}); | ||
ArgsParser args = | ||
new ArgsParser( | ||
new String[] {"--buildozer=" + buildozerPath, "--src=" + copiedGoogleapis.toString()}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
toString() is called automatically for all objects participating in string cancatenation. Please remove it (here and below)
New generator feature: preserve some values when regenerating BUILD.bazel. More information: googleapis/gapic-generator#3237 PiperOrigin-RevId: 317983262
New generator feature: preserve some values when regenerating BUILD.bazel. More information: googleapis/gapic-generator#3237 PiperOrigin-RevId: 317983262 Source-Author: Google APIs <noreply@google.com> Source-Date: Tue Jun 23 19:03:52 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: 927ff51dcfbdf092d469083dc3964173851f5575 Source-Link: googleapis/googleapis@927ff51
In this (surprisingly large) PR I'm adding some code to preserve values of some manually edited attributes in the generated
BUILD.bazel
files.The initial generation of build files is not changed, but if a file exists, I use
buildozer
(which is an official tool for manipulatingBUILD.bazel
files and has a proper syntax parser inside) and save the values we want to preserve. When the file is regenerated, I usebuildozer
again to set those preserved values.List of preserved values:
name
of all the*_gapic_assembly_pkg
rules (which is useful for non-cloud library owners who want to removecloud
from the rule name; also, this is the only user-facing rules so making it possible to change their names make sense;*_gapic_library
rules:extra_protoc_parameters
extra_protoc_file_parameters
package_name
main_service
bundle_config
iam_service
(the last four make sense for TypeScript; other languages can add their parameters-to-be-preserved to the list)
Now this all looks wonderful, until we realize thatbuildozer
is written in Go and does not have any API other than its CLI, so to make it all bazel friendly, apparently the only way is to include thebuildozer
binary dependency to the.jar
as a resource. This indeed makes.jar
non-portable (because it would buildbuildozer
for the current platform only), but since we only use this build file generator asbazel run
, it should be fine. If youbazel run
on Linux, your.jar
will contain a Linuxbuildozer
binary; if you're on Mac, it will be a Mac binary; if you're on Windows, it's very unlikely it will work at all but who cares.buildozer
, which is a Go dependency, is now adata
dependency of thejava_binary
task.This is my second Java PR in a few years, I have no idea how to write Java code properly, so please forgive me :)