github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/internal/common.bzl (about)

     1  # Copyright 2021 The Bazel Authors. All rights reserved.
     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  def env_execute(ctx, arguments, environment = {}, **kwargs):
    16      """Executes a command in for a repository rule.
    17      It prepends "env -i" to "arguments" before calling "ctx.execute".
    18      Variables that aren't explicitly mentioned in "environment"
    19      are removed from the environment. This should be preferred to "ctx.execute"
    20      in most situations.
    21      """
    22      if ctx.os.name.startswith("windows"):
    23          return ctx.execute(arguments, environment = environment, **kwargs)
    24      env_args = ["env", "-i"]
    25      environment = dict(environment)
    26      for var in ["TMP", "TMPDIR"]:
    27          if var in ctx.os.environ and not var in environment:
    28              environment[var] = ctx.os.environ[var]
    29      for k, v in environment.items():
    30          env_args.append("%s=%s" % (k, v))
    31      arguments = env_args + arguments
    32      return ctx.execute(arguments, **kwargs)
    33  
    34  def executable_extension(ctx):
    35      extension = ""
    36      if ctx.os.name.startswith("windows"):
    37          extension = ".exe"
    38      return extension