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+
110import sys
211import pathlib
312import subprocess
716from python .runfiles import runfiles
817
918def 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
1524opts = options ()
@@ -23,7 +32,7 @@ def options():
2332go_extractor_dir = workspace_dir / "go" / "extractor"
2433go_dbscheme = workspace_dir / "go" / "ql" / "lib" / "go.dbscheme"
2534r = 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
2938if opts .force :
@@ -43,13 +52,16 @@ def options():
4352print ("running gazelle" )
4453subprocess .check_call ([gazelle ])
4554
55+ # we want to stamp all newly generated `BUILD.bazel` files with a header
4656build_files_to_update = set (go_extractor_dir .glob ("*/**/BUILD.bazel" ))
57+ # if --force, all files are new
4758if 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" )
5365for 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