From 99f02f1e3b8537ed7b72bfc71ac307806ff25688 Mon Sep 17 00:00:00 2001 From: Bo Shi Date: Fri, 23 Apr 2021 00:07:14 -0700 Subject: [PATCH] Rules for //:kustomize //:gcloud //:kubectl use :execpwd. https://github.com/bazelbuild/bazel/issues/3325 Tested: mkdir examples/tmp cd examples/tmp bazel run @com_benchsci_rules_kustomize//:kustomize -- create Confirm a `kustomize.yaml` file is created in the working directory. Previously, the tool would mutate the underlying runfiles directory. --- BUILD | 22 +++++++++++++++++++--- exec.sh | 2 +- execpwd.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100755 execpwd.sh diff --git a/BUILD b/BUILD index ce30535..d07fc12 100644 --- a/BUILD +++ b/BUILD @@ -10,22 +10,38 @@ filegroup( srcs = ["exec.sh"], ) +filegroup( + name = "execpwd", + srcs = ["execpwd.sh"], +) + sh_binary( name = "kustomize", - srcs = ["@kustomize//:file"], + srcs = [":execpwd"], + args = ["$(location @kustomize//:file)"], + data = ["@kustomize//:file"], ) sh_binary( - name = "kubectl", + name = "kubectl_bin", srcs = select({ "@bazel_tools//src/conditions:linux_x86_64": ["@kubectl_linux//file"], "@bazel_tools//src/conditions:darwin": ["@kubectl_darwin//file"], }), ) +sh_binary( + name = "kubectl", + srcs = [":execpwd"], + args = ["$(location :kubectl_bin)"], + data = [":kubectl_bin"], +) + sh_binary( name = "gcloud", - srcs = ["@gcloud"], + srcs = [":execpwd"], + args = ["$(location @gcloud)"], + data = ["@gcloud"], ) # Exported for documentation (see //tools:docs). diff --git a/exec.sh b/exec.sh index 8307af9..122fa83 100755 --- a/exec.sh +++ b/exec.sh @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -exec $@ +exec "$@" diff --git a/execpwd.sh b/execpwd.sh new file mode 100755 index 0000000..127d246 --- /dev/null +++ b/execpwd.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Copyright 2021 Benchsci Analytics Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# See https://github.com/bazelbuild/bazel/issues/3325 +if [ -z "${BUILD_WORKING_DIRECTORY-}" ]; then + echo "error BUILD_WORKING_DIRECTORY not set" + exit 1 +fi + +cmd="$( pwd )/${1}" +cd "${BUILD_WORKING_DIRECTORY}" +shift + +exec "${cmd}" "$@"