From 9c12d821f6912deb848b1593b059c8490f1f30a1 Mon Sep 17 00:00:00 2001 From: Loo Rong Jie Date: Thu, 4 Jan 2018 11:08:03 +0800 Subject: [Bazel/Windows] Wrap rm -rf in Bash for Windows (and some refactoring) --- third_party/repo.bzl | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'third_party/repo.bzl') diff --git a/third_party/repo.bzl b/third_party/repo.bzl index c29fef9629..11e9c842d2 100644 --- a/third_party/repo.bzl +++ b/third_party/repo.bzl @@ -22,6 +22,14 @@ _SINGLE_URL_WHITELIST = depset([ def _is_windows(ctx): return ctx.os.name.lower().find("windows") != -1 +def _wrap_bash_cmd(ctx, cmd): + if _is_windows(ctx): + bazel_sh = _get_env_var(ctx, "BAZEL_SH") + if not bazel_sh: + fail("BAZEL_SH environment variable is not set") + cmd = [bazel_sh, "-c", " ".join(cmd)] + return cmd + def _get_env_var(ctx, name): if name in ctx.os.environ: return ctx.os.environ[name] @@ -46,12 +54,8 @@ def _apply_patch(ctx, patch_file): # Don't check patch on Windows, because patch is only available under bash. if not _is_windows(ctx) and not ctx.which("patch"): fail("patch command is not found, please install it") - cmd = ["patch", "-p1", "-d", ctx.path("."), "-i", ctx.path(patch_file)] - if _is_windows(ctx): - bazel_sh = _get_env_var(ctx, "BAZEL_SH") - if not bazel_sh: - fail("BAZEL_SH environment variable is not set") - cmd = [bazel_sh, "-c", " ".join(cmd)] + cmd = _wrap_bash_cmd( + ctx, ["patch", "-p1", "-d", ctx.path("."), "-i", ctx.path(patch_file)]) _execute_and_check_ret_code(ctx, cmd) def _apply_delete(ctx, paths): @@ -60,8 +64,8 @@ def _apply_delete(ctx, paths): fail("refusing to rm -rf path starting with '/': " + path) if ".." in path: fail("refusing to rm -rf path containing '..': " + path) - _execute_and_check_ret_code( - ctx, ["rm", "-rf"] + [ctx.path(path) for path in paths]) + cmd = _wrap_bash_cmd(ctx, ["rm", "-rf"] + [ctx.path(path) for path in paths]) + _execute_and_check_ret_code(ctx, cmd) def _tf_http_archive(ctx): if ("mirror.bazel.build" not in ctx.attr.urls[0] or -- cgit v1.2.3