Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame^] | 1 | Copyright 2020 The Monogon Project Authors. |
| 2 | |
| 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | you may not use this file except in compliance with the License. |
| 5 | You may obtain a copy of the License at |
| 6 | |
| 7 | http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | |
| 9 | Unless required by applicable law or agreed to in writing, software |
| 10 | distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | See the License for the specific language governing permissions and |
| 13 | limitations under the License. |
| 14 | |
| 15 | |
| 16 | From db55421f284253c06561a4df18bca262999962ce Mon Sep 17 00:00:00 2001 |
| 17 | From: Lorenz Brun <lorenz@nexantic.com> |
| 18 | Date: Wed, 22 Jul 2020 09:46:56 +0200 |
| 19 | Subject: [PATCH] Add support for prepatching |
| 20 | |
| 21 | --- |
| 22 | internal/go_repository.bzl | 18 +++++++++++++----- |
| 23 | 1 file changed, 13 insertions(+), 5 deletions(-) |
| 24 | |
| 25 | diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl |
| 26 | index 51a7317..86899bd 100644 |
| 27 | --- a/internal/go_repository.bzl |
| 28 | +++ b/internal/go_repository.bzl |
| 29 | @@ -127,6 +127,8 @@ def _go_repository_impl(ctx): |
| 30 | if result.stderr: |
| 31 | print("fetch_repo: " + result.stderr) |
| 32 | |
| 33 | + patch(ctx, True) |
| 34 | + |
| 35 | # Repositories are fetched. Determine if build file generation is needed. |
| 36 | build_file_names = ctx.attr.build_file_name.split(",") |
| 37 | existing_build_file = "" |
| 38 | @@ -160,7 +162,7 @@ def _go_repository_impl(ctx): |
| 39 | "-repo_root", |
| 40 | ctx.path(""), |
| 41 | "-repo_config", |
| 42 | - ctx.path(ctx.attr.build_config) |
| 43 | + ctx.path(ctx.attr.build_config), |
| 44 | ] |
| 45 | if ctx.attr.version: |
| 46 | cmd.append("-go_repository_module_mode") |
| 47 | @@ -247,9 +249,13 @@ go_repository = repository_rule( |
| 48 | ], |
| 49 | ), |
| 50 | "build_extra_args": attr.string_list(), |
| 51 | - "build_config": attr.label(default= "@bazel_gazelle_go_repository_config//:WORKSPACE"), |
| 52 | + "build_config": attr.label(default = "@bazel_gazelle_go_repository_config//:WORKSPACE"), |
| 53 | "build_directives": attr.string_list(default = []), |
| 54 | |
| 55 | + # Patches to apply before running gazelle. |
| 56 | + "pre_patches": attr.label_list(), |
| 57 | + "pre_patch_cmds": attr.string_list(default = []), |
| 58 | + |
| 59 | # Patches to apply after running gazelle. |
| 60 | "patches": attr.label_list(), |
| 61 | "patch_tool": attr.string(default = "patch"), |
| 62 | @@ -260,10 +266,11 @@ go_repository = repository_rule( |
| 63 | """See repository.rst#go-repository for full documentation.""" |
| 64 | |
| 65 | # Copied from @bazel_tools//tools/build_defs/repo:utils.bzl |
| 66 | -def patch(ctx): |
| 67 | +def patch(ctx, pre_mode = False): |
| 68 | """Implementation of patching an already extracted repository""" |
| 69 | bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash" |
| 70 | - for patchfile in ctx.attr.patches: |
| 71 | + patches = ctx.attr.patches if not pre_mode else ctx.attr.pre_patches |
| 72 | + for patchfile in patches: |
| 73 | command = "{patchtool} {patch_args} < {patchfile}".format( |
| 74 | patchtool = ctx.attr.patch_tool, |
| 75 | patchfile = ctx.path(patchfile), |
| 76 | @@ -276,7 +283,8 @@ def patch(ctx): |
| 77 | if st.return_code: |
| 78 | fail("Error applying patch %s:\n%s%s" % |
| 79 | (str(patchfile), st.stderr, st.stdout)) |
| 80 | - for cmd in ctx.attr.patch_cmds: |
| 81 | + patch_cmds = ctx.attr.patch_cmds if not pre_mode else ctx.attr.pre_patch_cmds |
| 82 | + for cmd in patch_cmds: |
| 83 | st = ctx.execute([bash_exe, "-c", cmd]) |
| 84 | if st.return_code: |
| 85 | fail("Error applying patch command %s:\n%s%s" % |
| 86 | -- |
| 87 | 2.25.1 |
| 88 | |