From a7b24d64844bb51b987f0884cfa1306c4d17f0a8 Mon Sep 17 00:00:00 2001 From: Simon Stewart Date: Tue, 7 Jun 2022 18:47:03 +0300 Subject: [PATCH] Expose freeze-deps as a buildable target (#31) --- README.md | 2 +- tools/BUILD.bazel | 8 ++++++++ tools/freeze-deps.py | 12 +++++++++--- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 285137e8..d6a593e7 100644 --- a/README.md +++ b/README.md @@ -522,7 +522,7 @@ file you need to do the following: pinned_maven_install() ``` -2. Run `./tools/freeze-deps.py --repo --zip +2. Run `bazel run //tools:freeze-deps -- --repo --zip `. The `` matches the name used for the `maven_install()` rule above. This will pin the dependencies then collect them into the zip file. diff --git a/tools/BUILD.bazel b/tools/BUILD.bazel index 6aeece61..f1dadcf5 100644 --- a/tools/BUILD.bazel +++ b/tools/BUILD.bazel @@ -14,3 +14,11 @@ java_binary( main_class = "com.google.googlejavaformat.java.Main", runtime_deps = ["@maven//:com_google_googlejavaformat_google_java_format"], ) + +py_binary( + name = "freeze-deps", + srcs = [ + "freeze-deps.py", + ], + visibility = ["//visibility:public"], +) diff --git a/tools/freeze-deps.py b/tools/freeze-deps.py index d8102136..15cf18b3 100755 --- a/tools/freeze-deps.py +++ b/tools/freeze-deps.py @@ -32,6 +32,11 @@ help="Name of the zip repository used to import the zip file. Used only if compat_repositories are enabled" ) +# When run under `bazel run` this directory will be set. If not, +# then we should assume our current directory is the right one +# to use. +cwd = os.environ.get("BUILD_WORKSPACE_DIRECTORY", None) + args = parser.parse_args() pin_env = {"REPIN": "1"} @@ -39,15 +44,16 @@ # Repin our dependencies cmd = ["bazel", "run", "@unpinned_%s//:pin" % args.repo] -subprocess.check_call(cmd, env=pin_env) +subprocess.check_call(cmd, env=pin_env, cwd = cwd) # Now grab the files we need from their output locations cmd = ["bazel", "info", "output_base"] -output_base = subprocess.check_output(cmd).rstrip() +output_base = subprocess.check_output(cmd, cwd = cwd).rstrip() base = output_base.decode(encoding=sys.stdin.encoding) # Generate a stable-ish zip file -output = zipfile.ZipFile(args.zip, "w", zipfile.ZIP_DEFLATED) +zip_path = path.join(cwd, args.zip) if cwd else args.zip +output = zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) for f in UNCHANGED_FILES: p = path.join(base, "external", args.repo, f)