|
2 | 2 | import pathlib
|
3 | 3 | import subprocess
|
4 | 4 | import os
|
| 5 | +import argparse |
| 6 | +import shutil |
5 | 7 | from python.runfiles import runfiles
|
6 | 8 |
|
| 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 | + |
7 | 17 | try:
|
8 | 18 | workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY'])
|
9 | 19 | except KeyError:
|
|
13 | 23 | go_extractor_dir = workspace_dir / "go" / "extractor"
|
14 | 24 | go_dbscheme = workspace_dir / "go" / "ql" / "lib" / "go.dbscheme"
|
15 | 25 | 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 | + |
17 | 28 |
|
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") |
19 | 36 | subprocess.check_call([go, "-C", go_extractor_dir, "work", "vendor"])
|
20 | 37 |
|
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() |
24 | 42 |
|
25 | 43 | print("running gazelle")
|
26 | 44 | subprocess.check_call([gazelle])
|
27 | 45 |
|
| 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 | + |
28 | 52 | 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: |
30 | 54 | contents = build_file.read_text()
|
31 | 55 | build_file.write_text(f"# generated running `bazel run //go/gazelle`, do not edit\n\n{contents}")
|
32 | 56 |
|
|
0 commit comments