...

Text file src/github.com/bazelbuild/bazel-gazelle/internal/gazelle.bash.in

Documentation: github.com/bazelbuild/bazel-gazelle/internal

     1#!/usr/bin/env bash
     2
     3# Copyright 2017 The Bazel Authors. All rights reserved.
     4
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8
     9#    http://www.apache.org/licenses/LICENSE-2.0
    10
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License.
    16
    17# --- begin runfiles.bash initialization v3 ---
    18# Copy-pasted from the Bazel Bash runfiles library v3.
    19set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
    20source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
    21  source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
    22  source "$0.runfiles/$f" 2>/dev/null || \
    23  source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
    24  source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
    25  { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
    26# --- end runfiles.bash initialization v3 ---
    27
    28@@GENERATED_MESSAGE@@
    29
    30set -euo pipefail
    31
    32GAZELLE_PATH=@@GAZELLE_PATH@@
    33ARGS=@@ARGS@@
    34GOTOOL=@@GOTOOL@@
    35REPO_CONFIG_PATH=@@REPO_CONFIG_PATH@@
    36
    37@@ENV@@
    38
    39# set_goroot attempts to set GOROOT to the SDK used by rules_go. gazelle
    40# invokes tools inside the Go SDK for dependency management. It's good to
    41# use the SDK used by the workspace in case the Go SDK is not installed
    42# on the host system or is a different version.
    43function set_goroot {
    44  local gotool
    45  gotool=$(rlocation "$GOTOOL")
    46  if [ -z "$gotool" ]; then
    47    echo "$0: warning: could not locate GOROOT used by rules_go" >&2
    48    return
    49  fi
    50  GOROOT=$(cd "$(dirname "$gotool")/.."; pwd)
    51  export GOROOT
    52  if type cygpath >/dev/null 2>&1; then
    53    # On Windows, convert the path to something usable outside of bash.
    54    GOROOT=$(cygpath -w "$GOROOT")
    55  fi
    56}
    57
    58# If arguments were provided on the command line, either replace or augment
    59# the generated args.
    60case "${1-}" in
    61  "fix" | "update" | "help" | "update-repos")
    62    ARGS=("$@")
    63    ;;
    64  *)
    65    ARGS+=("$@")
    66    ;;
    67esac
    68
    69# Invoke Gazelle.
    70# Note that we don't change directories first; if we did, Gazelle wouldn't be
    71# able to find runfiles, and some extensions rely on that. Gazelle can use
    72# BUILD_WORKSPACE_DIRECTORY to interpret relative paths on the command line.
    73set_goroot
    74gazelle_path=$(rlocation "$GAZELLE_PATH")
    75if [ -z "$gazelle_path" ]; then
    76  echo "error: could not locate gazelle binary" >&2
    77  exit 1
    78fi
    79if [ -z "${BUILD_WORKSPACE_DIRECTORY-}" ]; then
    80  echo "error: BUILD_WORKSPACE_DIRECTORY not set" >&2
    81  exit 1
    82fi
    83
    84# Determine if we are running the fix/update command
    85if [[ ${#ARGS[@]} -gt 0 ]]; then
    86  case "${ARGS[0]}" in
    87    "fix" | "update")
    88      is_fix_or_update="true"
    89      ;;
    90    *)
    91      is_fix_or_update="false"
    92      ;;
    93  esac
    94fi
    95
    96# When running with Bzlmod, there is no WORKSPACE file for Gazelle to read
    97# the definitions of go_repository rules from. Instead, we pass the path to
    98# the repo config file as a flag.
    99if [[ "${is_fix_or_update:-}" == "true"  ]] && [[ -n $REPO_CONFIG_PATH ]]; then
   100  ARGS=("${ARGS[0]}" "-repo_config" "$(rlocation "$REPO_CONFIG_PATH")" "${ARGS[@]:1}")
   101fi
   102
   103runfiles_export_envvars
   104"$gazelle_path" "${ARGS[@]}"

View as plain text