k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/update-mocks.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2021 The Kubernetes Authors.
     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  # This script generates mock files using mockgen.
    18  # Usage: `hack/update-mocks.sh`.
    19  
    20  set -o errexit
    21  set -o nounset
    22  set -o pipefail
    23  
    24  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    25  source "${KUBE_ROOT}/hack/lib/init.sh"
    26  
    27  kube::golang::setup_env
    28  
    29  echo 'installing mockgen'
    30  go -C "${KUBE_ROOT}/hack/tools" install go.uber.org/mock/mockgen
    31  
    32  function git_grep() {
    33    git grep --untracked --exclude-standard \
    34        "$@" \
    35        ':!:vendor/*'        `# catches vendor/...` \
    36        ':!:*/vendor/*'      `# catches any subdir/vendor/...` \
    37        ':!:third_party/*'   `# catches third_party/...` \
    38        ':!:*/third_party/*' `# catches third_party/...` \
    39        ':!:*/testdata/*'    `# catches any testdata` \
    40        ':(glob)**/*.go'
    41  }
    42  
    43  cd "${KUBE_ROOT}"
    44  
    45  GENERATED_MOCK_FILE_REGEX="^// Code generated by MockGen. DO NOT EDIT.$"
    46  
    47  # pick a tempfile path for writing to
    48  tmp=$(mktemp)
    49  kube::util::trap_add "rm -f ${tmp:?}" EXIT
    50  
    51  git_grep -l -z "${GENERATED_MOCK_FILE_REGEX}" | xargs -0 rm -f
    52  
    53  echo 'executing go generate command on below files'
    54  
    55  git_grep -l -z "//go:generate mockgen" | while read -r -d $'\0' file; do
    56    echo "- ${file}"
    57    temp_file_name="$(kube::realpath "$(mktemp -t "$(basename "$0").XXXXXX")")"
    58  
    59    # search for build tag used in file
    60    build_tag_string=$(grep -o '+build.*$' "$file") || true
    61  
    62    # if the file does not have build string
    63    if [ -n "$build_tag_string" ]; then
    64      # write the build tag in the temp file
    65      echo -n "$build_tag_string" > "$temp_file_name"
    66  
    67      # if +build tag is defined in interface file
    68      BUILD_TAG_FILE=$temp_file_name go generate -v "$file"
    69    else
    70      # if no +build tag is defined in interface file
    71      # NOTE: This works around a bug in `go generate` with workspaces, which
    72      # should be fixed in Go 1.22.1.
    73      go -C "$(dirname "$file")" generate "$(basename "$file")"
    74    fi
    75  done
    76  
    77  # get the changed or new mock files
    78  git ls-files -mo --exclude-standard -z | while read -r -d $'\0' file; do
    79    # only process files that appear to be mocks
    80    test -f "$file" || continue
    81    grep -q "${GENERATED_MOCK_FILE_REGEX}" "$file" || continue
    82  
    83    # search for build tags used in file
    84    # //go:build !providerless
    85    # // +build !providerless
    86    go_build_tag_string=$(grep -o 'go:build.*$' "$file") || true
    87    build_tag_string=$(grep -o '+build.*$' "$file") || true
    88    new_header=''
    89  
    90    # if the file has both headers
    91    if [ -n "$build_tag_string" ] && [ -n "$go_build_tag_string" ]
    92    then
    93      # create a new header with the build string and the copyright text
    94      new_header=$(echo -e "//""$go_build_tag_string""\n""//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
    95  
    96      # ignore the first line (build tag) from the file
    97      tail -n +3 "$file" >"${tmp}"
    98    fi
    99  
   100    # if the file has only // +build !providerless header
   101    if [ -n "$build_tag_string" ] && [ -z "$go_build_tag_string" ]
   102    then
   103      # create a new header with the build string and the copyright text
   104      new_header=$(echo -e "//" "$build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
   105  
   106      # ignore the first line (build tag) from the file
   107      tail -n +2 "$file" >"${tmp}"
   108    fi
   109  
   110    # if the file has only //go:build !providerless header
   111    if [ -z "$build_tag_string" ] && [ -n "$go_build_tag_string" ]
   112    then
   113      # create a new header with the build string and the copyright text
   114      new_header=$(echo -e "//""$go_build_tag_string""\n" | cat - hack/boilerplate/boilerplate.generatego.txt)
   115  
   116      # ignore the first line (build tag) from the file
   117      tail -n +2 "$file" >"${tmp}"
   118    fi
   119  
   120    # if the header is generated
   121    if [ -n "$new_header" ]
   122    then
   123      # write the newly generated header file to the original file
   124      echo -e "$new_header" | cat - "${tmp}" > "$file"
   125    else
   126      # if no build string insert at the top
   127      cat hack/boilerplate/boilerplate.generatego.txt "$file" >"${tmp}" && \
   128      mv "${tmp}" "$file"
   129    fi
   130  done