Skip to content

Commit 318d954

Browse files
committed
Go: make //go:gen not clear by default, and clean on --force
1 parent ca2d94b commit 318d954

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

go/gen.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@
22
import pathlib
33
import subprocess
44
import os
5+
import argparse
6+
import shutil
57
from python.runfiles import runfiles
68

9+
def options():
10+
p = argparse.ArgumentParser(description="Update generated checked in files in the Go pack")
11+
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)
13+
return p.parse_args()
14+
15+
opts = options()
16+
717
try:
818
workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY'])
919
except KeyError:
@@ -13,20 +23,34 @@
1323
go_extractor_dir = workspace_dir / "go" / "extractor"
1424
go_dbscheme = workspace_dir / "go" / "ql" / "lib" / "go.dbscheme"
1525
r = runfiles.Create()
16-
go, gazelle, go_gen_dbscheme = map(r.Rlocation, sys.argv[1:])
26+
go, gazelle, go_gen_dbscheme = map(r.Rlocation, opts.generators)
27+
1728

18-
print("updating vendor")
29+
if opts.force:
30+
print("clearing vendor directory")
31+
shutil.rmtree(go_extractor_dir / "vendor")
32+
33+
existing_build_files = set(go_extractor_dir.glob("*/**/BUILD.bazel"))
34+
35+
print("updating vendor directory")
1936
subprocess.check_call([go, "-C", go_extractor_dir, "work", "vendor"])
2037

21-
print("clearing generated BUILD files")
22-
for build_file in go_extractor_dir.glob("*/**/BUILD.bazel"):
23-
build_file.unlink()
38+
if opts.force:
39+
print("clearing generated BUILD files")
40+
for build_file in existing_build_files:
41+
build_file.unlink()
2442

2543
print("running gazelle")
2644
subprocess.check_call([gazelle])
2745

46+
build_files_to_update = set(go_extractor_dir.glob("*/**/BUILD.bazel"))
47+
if not opts.force:
48+
build_files_to_update -= existing_build_files
49+
# these are always refreshed
50+
build_files_to_update.update(go_extractor_dir.glob("vendor/**/BUILD.bazel"))
51+
2852
print("adding header to generated BUILD files")
29-
for build_file in go_extractor_dir.glob("*/**/BUILD.bazel"):
53+
for build_file in build_files_to_update:
3054
contents = build_file.read_text()
3155
build_file.write_text(f"# generated running `bazel run //go/gazelle`, do not edit\n\n{contents}")
3256

0 commit comments

Comments
 (0)