github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/scripts/auto-gen-helpers.sh (about)

     1  #!/bin/bash
     2  
     3  DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
     4  ROOT="${DIR}/.."
     5  
     6  source "${ROOT}/.ci/variables.sh"
     7  
     8  
     9  set -e
    10  
    11  add_license() {
    12      local FILE="$1"
    13      update-license $FILE
    14  }
    15  
    16  export -f add_license
    17  
    18  autogen_clear() {
    19  	local DIR="$1"
    20      find $DIR -mindepth 2 -type f -name '*.go' -exec rm -f {} \;
    21  }
    22  
    23  remove_matching_files() {
    24      local DIR=$1
    25      local FILE_PATTERN=$2
    26      find $DIR -type f -name "$FILE_PATTERN" -exec rm -f {} \;
    27  }
    28  
    29  revert_copyright_only_change() {
    30      # We don't want to make a change to a file if the only change
    31      # is the copyright year. We can't check this in add_license because a newly-
    32      # generated file will not contain the copyright notice and thus it will
    33      # add in the copyright (with the new year).
    34      local FILE=$0
    35      numDiffLines=$(git --no-pager diff --text -U0 $FILE | # Get file text diffs with no context.
    36          grep -E -v '^\+\+\+|^---'                       | # Exclude file descriptors.
    37          grep -E '^-|^\+'                                | # Get only line diffs.
    38          grep -Evc '^-// Copyright \(c\)|^\+// Copyright \(c\)') # Exclude copyrights and get the number of lines remaining.
    39      if [ $numDiffLines = 0 ]; then
    40          git checkout -- "$FILE" 2> /dev/null # Remove changes, since the only change was the copyright year.
    41      fi
    42  }
    43  
    44  export -f revert_copyright_only_change
    45  
    46  autogen_cleanup() {
    47      local DIR="$1"
    48      find $DIR -type f -name "*.go" -exec /bin/bash -c 'add_license $0; revert_copyright_only_change $0' {} \;
    49  }
    50  
    51  gen_cleanup_helper() {
    52      local FILE=$0
    53      local DIR=$(dirname $FILE)
    54      add_license $FILE
    55  
    56      # NB(xichen): there is an open issue (https://github.com/golang/mock/issues/30)
    57      # with mockgen that causes the generated mock files to have vendored packages
    58      # in the import list. For now we are working around it by removing the vendored
    59      # path. Also sed -i'' does not work with BSD sed shipped with OS X, whereas
    60      # sed -i '' doesn't work with GNU sed, so we work around it by redirecting to a
    61      # temp file first and moving it back later.
    62      sed "s|$VENDOR_PATH/||" $FILE > $FILE.tmp && mv $FILE.tmp $FILE
    63  
    64      # Strip GOPATH from the source file path
    65      sed "s|Source: $GOPATH/src/\(.*\.go\)|Source: \1|" $FILE > $FILE.tmp && mv $FILE.tmp $FILE
    66  
    67      # NB(prateek): running genclean makes mock-gen idempotent.
    68      # NB(xichen): genclean should be run after the vendor path is stripped.
    69      basePkg=$(echo $DIR | sed -e "s@${GOPATH}/src/@@g")
    70      genclean -pkg $basePkg -out $FILE -in $FILE
    71      gofmt -w $FILE
    72      revert_copyright_only_change $FILE
    73  }
    74  
    75  export -f gen_cleanup_helper
    76  
    77  gen_cleanup_dir() {
    78      local PATTERN=$1
    79      local DIRS=$2
    80      for DIR in $DIRS;
    81      do
    82          find $DIR -name "$PATTERN" -type f -exec /bin/bash -c 'gen_cleanup_helper $0' {} \;
    83      done
    84  }
    85  
    86  gen_cleanup() {
    87      gen_cleanup_dir $1 $SRC
    88  }