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 | |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 16 | From 2cb763552e2e386928e6c2211c4fb056b9ca7971 Mon Sep 17 00:00:00 2001 |
| 17 | From: Serge Bazanski <serge@nexantic.com> |
| 18 | Date: Thu, 14 Jan 2021 16:30:32 +0100 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 19 | Subject: [PATCH] Add support for prepatching |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 20 | Company: nexantic GmbH |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 21 | |
| 22 | --- |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 23 | internal/go_repository.bzl | 14 +++++++++++--- |
| 24 | 1 file changed, 11 insertions(+), 3 deletions(-) |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 25 | |
| 26 | diff --git a/internal/go_repository.bzl b/internal/go_repository.bzl |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 27 | index 7f87ed1..a0ef36d 100644 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 28 | --- a/internal/go_repository.bzl |
| 29 | +++ b/internal/go_repository.bzl |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 30 | @@ -130,6 +130,8 @@ def _go_repository_impl(ctx): |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 31 | if result.stderr: |
| 32 | print("fetch_repo: " + result.stderr) |
| 33 | |
| 34 | + patch(ctx, True) |
| 35 | + |
| 36 | # Repositories are fetched. Determine if build file generation is needed. |
| 37 | build_file_names = ctx.attr.build_file_name.split(",") |
| 38 | existing_build_file = "" |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 39 | @@ -263,6 +265,10 @@ go_repository = repository_rule( |
| 40 | "build_config": attr.label(default = "@bazel_gazelle_go_repository_config//:WORKSPACE"), |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 41 | "build_directives": attr.string_list(default = []), |
| 42 | |
| 43 | + # Patches to apply before running gazelle. |
| 44 | + "pre_patches": attr.label_list(), |
| 45 | + "pre_patch_cmds": attr.string_list(default = []), |
| 46 | + |
| 47 | # Patches to apply after running gazelle. |
| 48 | "patches": attr.label_list(), |
| 49 | "patch_tool": attr.string(default = "patch"), |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 50 | @@ -273,10 +279,11 @@ go_repository = repository_rule( |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 51 | """See repository.rst#go-repository for full documentation.""" |
| 52 | |
| 53 | # Copied from @bazel_tools//tools/build_defs/repo:utils.bzl |
| 54 | -def patch(ctx): |
| 55 | +def patch(ctx, pre_mode = False): |
| 56 | """Implementation of patching an already extracted repository""" |
| 57 | bash_exe = ctx.os.environ["BAZEL_SH"] if "BAZEL_SH" in ctx.os.environ else "bash" |
| 58 | - for patchfile in ctx.attr.patches: |
| 59 | + patches = ctx.attr.patches if not pre_mode else ctx.attr.pre_patches |
| 60 | + for patchfile in patches: |
| 61 | command = "{patchtool} {patch_args} < {patchfile}".format( |
| 62 | patchtool = ctx.attr.patch_tool, |
| 63 | patchfile = ctx.path(patchfile), |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 64 | @@ -289,7 +296,8 @@ def patch(ctx): |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 65 | if st.return_code: |
| 66 | fail("Error applying patch %s:\n%s%s" % |
| 67 | (str(patchfile), st.stderr, st.stdout)) |
| 68 | - for cmd in ctx.attr.patch_cmds: |
| 69 | + patch_cmds = ctx.attr.patch_cmds if not pre_mode else ctx.attr.pre_patch_cmds |
| 70 | + for cmd in patch_cmds: |
| 71 | st = ctx.execute([bash_exe, "-c", cmd]) |
| 72 | if st.return_code: |
| 73 | fail("Error applying patch command %s:\n%s%s" % |
| 74 | -- |
Serge Bazanski | f12bedf | 2021-01-15 16:58:50 +0100 | [diff] [blame] | 75 | 2.26.2 |
Lorenz Brun | efb028f | 2020-07-28 17:04:49 +0200 | [diff] [blame] | 76 | |