From 4179b83221ced1b594633eccbae8ba5750ac7abc Mon Sep 17 00:00:00 2001 From: Oliver Lee Date: Mon, 16 Jun 2025 18:51:13 -0700 Subject: [PATCH] fix README generation Add `//example:all_files` as a dependency of `//scripts:readme.update` to ensure that the snippets are inserted to the README when updated. This commit also removes the use of repo rule `@local_config_info` as there no longer a need to obtain the local workspace root directory. Change-Id: I798a59231f7f109e6c3e18c1edacae8de897d911 --- WORKSPACE.bazel | 4 ---- example/BUILD.bazel | 9 +++++++ rules/BUILD.bazel | 5 +++- rules/lcov.bzl | 6 ++--- scripts/BUILD.bazel | 35 ++++++++++----------------- scripts/markdown-update.py | 4 ---- tools/local_config_info.bzl | 47 ------------------------------------- 7 files changed, 28 insertions(+), 82 deletions(-) delete mode 100644 tools/local_config_info.bzl diff --git a/WORKSPACE.bazel b/WORKSPACE.bazel index fcaf4f7..4615158 100644 --- a/WORKSPACE.bazel +++ b/WORKSPACE.bazel @@ -244,10 +244,6 @@ py_library( ), ) -load("//tools:local_config_info.bzl", "local_config_info") - -local_config_info(name = "local_config_info") - RULES_BUILD_ERROR_COMMIT = "4ea1a4fa702b16389c7eb3b695ef97a23dfe9330" http_archive( diff --git a/example/BUILD.bazel b/example/BUILD.bazel index e709a99..b033d6e 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -155,3 +155,12 @@ multirun( ":described_predicates_20_log.update", ], ) + +filegroup( + name = "all_files", + srcs = glob([ + "*.cpp", + "*.log", + ]), + visibility = ["//scripts:__pkg__"], +) diff --git a/rules/BUILD.bazel b/rules/BUILD.bazel index 2236759..8d0b039 100644 --- a/rules/BUILD.bazel +++ b/rules/BUILD.bazel @@ -1,4 +1,7 @@ exports_files( ["copy_to_workspace.bash"], - visibility = ["//example:__pkg__"], + visibility = [ + "//example:__pkg__", + "//scripts:__pkg__", + ], ) diff --git a/rules/lcov.bzl b/rules/lcov.bzl index a5a5246..64837c9 100644 --- a/rules/lcov.bzl +++ b/rules/lcov.bzl @@ -3,7 +3,6 @@ Rule for generating a coverage report """ load("@bazel_skylib//rules:write_file.bzl", "write_file") -load("@local_config_info//:defs.bzl", "BAZEL_BIN") def lcov( name, @@ -66,7 +65,7 @@ def lcov( ] coverage_command = " ".join( - ["{bazel} coverage {bazel_common_opts}"] + + ["bazel coverage {bazel_common_opts}"] + coverage_opts + instrumented + test_targets, ) @@ -86,7 +85,7 @@ def lcov( "lcov_tool=\"$(pwd)/${{1}}\"", "", "cd $BUILD_WORKSPACE_DIRECTORY", - "output_path=\"$({bazel} info {bazel_common_opts} output_path)\"", + "output_path=\"$(bazel info {bazel_common_opts} output_path)\"", "coverage_report=\"${{output_path}}/_coverage/_coverage_report.dat\"", "", coverage_command + " || true", @@ -97,7 +96,6 @@ def lcov( content = [ line.format( - bazel = BAZEL_BIN, bazel_common_opts = "", ) for line in content diff --git a/scripts/BUILD.bazel b/scripts/BUILD.bazel index 433cbac..61f3313 100644 --- a/scripts/BUILD.bazel +++ b/scripts/BUILD.bazel @@ -1,6 +1,5 @@ load("@rules_python//python:defs.bzl", "py_binary") load("@bazel_skylib//rules:diff_test.bzl", "diff_test") -load("@local_config_info//:defs.bzl", "BAZEL_WORKSPACE_ROOT") py_binary( name = "markdown-update", @@ -14,35 +13,27 @@ genrule( srcs = ["README.md.tmpl"], outs = ["README.md.generated"], cmd = """ -export BUILD_WORKSPACE_DIRECTORY={workspace_dir} $(execpath :markdown-update) $(rootpath :README.md.tmpl)> $@ -""".format( - workspace_dir = BAZEL_WORKSPACE_ROOT, - ), - tags = ["manual"], - tools = [":markdown-update"], - visibility = ["//visibility:private"], -) - -genrule( - name = "readme.update_sh", - srcs = ["README.md.tmpl"], - outs = ["readme.update.sh"], - cmd = """ -set -euo pipefail -echo "set -euo pipefail" > $@ -echo "" >> $@ -echo "cd \\$$BUILD_WORKSPACE_DIRECTORY" >> $@ -echo "$(execpath :markdown-update) $(rootpath :README.md.tmpl) > README.md" >> $@ """, tags = ["manual"], - tools = [":markdown-update"], + tools = [ + ":markdown-update", + "//example:all_files", + ], visibility = ["//visibility:private"], ) sh_binary( name = "readme.update", - srcs = [":readme.update.sh"], + srcs = ["//rules:copy_to_workspace.bash"], + args = [ + "$(rootpath :gen_readme)", + "$(rootpath //:README.md)", + ], + data = [ + ":gen_readme", + "//:README.md", + ], ) diff_test( diff --git a/scripts/markdown-update.py b/scripts/markdown-update.py index f406eae..c085e2e 100755 --- a/scripts/markdown-update.py +++ b/scripts/markdown-update.py @@ -2,7 +2,6 @@ # -*- coding: utf-8 -*- import argparse -import os from pathlib import Path import sys @@ -13,12 +12,9 @@ parser.add_argument("filename") args = parser.parse_args() - os.chdir(os.environ.get("BUILD_WORKSPACE_DIRECTORY")) - with open(Path(args.filename), "r") as f: indoc = f.read() outdoc = get_code_emb()(indoc) sys.stdout.buffer.write(outdoc.encode()) - diff --git a/tools/local_config_info.bzl b/tools/local_config_info.bzl deleted file mode 100644 index 46d97be..0000000 --- a/tools/local_config_info.bzl +++ /dev/null @@ -1,47 +0,0 @@ -""" -Repository rule for determining Bazel directories. -""" - -def _local_config_info_impl(rctx): - rctx.file("BUILD.bazel") - - external = str(rctx.path(".").realpath).removesuffix("/" + rctx.name) - - env = rctx.execute(["env"]).stdout.splitlines() - - def env_var(pattern): - values = [ - line.removeprefix(pattern) - for line in env - if line.startswith(pattern) - ] - - if len(values) > 1: - fail() - - return values[0] if values else None - - bazel_bin = env_var("_=") - xdg_cache_home = env_var("XDG_CACHE_HOME=") - home = env_var("HOME=") - - rctx.file( - "defs.bzl", - executable = False, - content = """ -BAZEL_BIN = "{}" -BAZEL_EXTERNAL_DIR = "{}" -BAZEL_WORKSPACE_ROOT = "{}" -XDG_CACHE_HOME = "{}" - """.format( - bazel_bin, - external, - rctx.workspace_root, - xdg_cache_home or (home + "/.cache"), - ), - ) - -local_config_info = repository_rule( - implementation = _local_config_info_impl, - doc = "A repository rule for determining Bazel directories", -)