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