Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 1 | From bf6362aa01dd00c82996dcabfb1cb20d7919c552 Mon Sep 17 00:00:00 2001 |
| 2 | From: Lorenz Brun <lorenz@nexantic.com> |
| 3 | Date: Wed, 22 Jul 2020 09:46:56 +0200 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 4 | Subject: [PATCH] Add support for prepatching |
| 5 | |
| 6 | --- |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 7 | internal/go_repository.bzl | 14 +++++++++++--- |
| 8 | 1 file changed, 11 insertions(+), 3 deletions(-) |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 9 | |
| 10 | diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 11 | index 9928fa8..a3af13d 100644 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 12 | --- a/internal/go_repository.bzl |
| 13 | +++ b/internal/go_repository.bzl |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 14 | @@ -213,6 +213,8 @@ def _go_repository_impl(ctx): |
| 15 | if ctx.attr.debug_mode and result.stderr: |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 16 | print("fetch_repo: " + result.stderr) |
| 17 | |
| 18 | + patch(ctx, True) |
| 19 | + |
| 20 | # Repositories are fetched. Determine if build file generation is needed. |
| 21 | build_file_names = ctx.attr.build_file_name.split(",") |
| 22 | existing_build_file = "" |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 23 | @@ -474,6 +476,10 @@ go_repository = repository_rule( |
| 24 | Gazelle directives.""", |
| 25 | ), |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 26 | |
| 27 | + # Patches to apply before running gazelle. |
| 28 | + "pre_patches": attr.label_list(), |
| 29 | + "pre_patch_cmds": attr.string_list(default = []), |
| 30 | + |
| 31 | # Patches to apply after running gazelle. |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 32 | "patches": attr.label_list( |
| 33 | doc = "A list of patches to apply to the repository after gazelle runs.", |
| 34 | @@ -504,10 +510,11 @@ go_repository = repository_rule( |
| 35 | """See repository.md#go-repository for full documentation.""" |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 36 | |
| 37 | # Copied from @bazel_tools//tools/build_defs/repo:utils.bzl |
| 38 | -def patch(ctx): |
| 39 | +def patch(ctx, pre_mode = False): |
| 40 | """Implementation of patching an already extracted repository""" |
| 41 | bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash" |
| 42 | - for patchfile in ctx.attr.patches: |
| 43 | + patches = ctx.attr.patches if not pre_mode else ctx.attr.pre_patches |
| 44 | + for patchfile in patches: |
| 45 | command = "{patchtool} {patch_args} < {patchfile}".format( |
| 46 | patchtool = ctx.attr.patch_tool, |
| 47 | patchfile = ctx.path(patchfile), |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 48 | @@ -520,7 +527,8 @@ def patch(ctx): |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 49 | if st.return_code: |
| 50 | fail("Error applying patch %s:\n%s%s" % |
| 51 | (str(patchfile), st.stderr, st.stdout)) |
| 52 | - for cmd in ctx.attr.patch_cmds: |
| 53 | + patch_cmds = ctx.attr.patch_cmds if not pre_mode else ctx.attr.pre_patch_cmds |
| 54 | + for cmd in patch_cmds: |
| 55 | st = ctx.execute([bash_exe, "-c", cmd]) |
| 56 | if st.return_code: |
| 57 | fail("Error applying patch command %s:\n%s%s" % |
| 58 | -- |
Lorenz Brun | d13c1c6 | 2022-03-30 19:58:58 +0200 | [diff] [blame] | 59 | 2.25.1 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 60 | |