Skip to content

Commit 31c427e

Browse files
committed
Bazel/Go: add more explanation in gen.py
1 parent 00baccb commit 31c427e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

go/gen.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
"""
2+
Update generated files related to Go in the repo. Using --force will regenerate all files from scratch.
3+
4+
In particular the script will:
5+
1. update the `vendor` dir with `go work vendor` (using a go toolchain provided by bazel)
6+
2. update `BUILD.bazel` files using gazelle
7+
3. update `ql/lib/go.dbscheme` using a compiled `go-dbschemegen`
8+
"""
9+
110
import sys
211
import pathlib
312
import subprocess
@@ -7,9 +16,9 @@
716
from python.runfiles import runfiles
817

918
def options():
10-
p = argparse.ArgumentParser(description="Update generated checked in files in the Go pack")
19+
p = argparse.ArgumentParser(description="Update generated files related to Go in the repo")
1120
p.add_argument("--force", "-f", action="store_true", help="Regenerate all files from scratch rather than updating them")
12-
p.add_argument("generators", nargs=3)
21+
p.add_argument("executables", nargs=3, help="Internally provided executables")
1322
return p.parse_args()
1423

1524
opts = options()
@@ -23,7 +32,7 @@ def options():
2332
go_extractor_dir = workspace_dir / "go" / "extractor"
2433
go_dbscheme = workspace_dir / "go" / "ql" / "lib" / "go.dbscheme"
2534
r = runfiles.Create()
26-
go, gazelle, go_gen_dbscheme = map(r.Rlocation, opts.generators)
35+
go, gazelle, go_gen_dbscheme = map(r.Rlocation, opts.executables)
2736

2837

2938
if opts.force:
@@ -43,13 +52,16 @@ def options():
4352
print("running gazelle")
4453
subprocess.check_call([gazelle])
4554

55+
# we want to stamp all newly generated `BUILD.bazel` files with a header
4656
build_files_to_update = set(go_extractor_dir.glob("*/**/BUILD.bazel"))
57+
# if --force, all files are new
4758
if not opts.force:
59+
# otherwise, subtract the files that existed at the start
4860
build_files_to_update -= existing_build_files
49-
# these are always refreshed
61+
# but bring back the `vendor` ones, as the vendor update step always clears them
5062
build_files_to_update.update(go_extractor_dir.glob("vendor/**/BUILD.bazel"))
5163

52-
print("adding header to generated BUILD files")
64+
print("adding header to newly generated BUILD files")
5365
for build_file in build_files_to_update:
5466
contents = build_file.read_text()
5567
build_file.write_text(f"# generated running `bazel run //go/gazelle`, do not edit\n\n{contents}")

0 commit comments

Comments
 (0)