1#!/bin/bash
2
3DEST=$2
4PACKAGE=$3
5TMPFILE="mockgen_tmp.go"
6# uppercase the name of the interface
7ORIG_INTERFACE_NAME=$4
8INTERFACE_NAME="$(tr '[:lower:]' '[:upper:]' <<< ${ORIG_INTERFACE_NAME:0:1})${ORIG_INTERFACE_NAME:1}"
9
10# Gather all files that contain interface definitions.
11# These interfaces might be used as embedded interfaces,
12# so we need to pass them to mockgen as aux_files.
13AUX=()
14for f in *.go; do
15  if [[ -z ${f##*_test.go} ]]; then
16    # skip test files
17    continue;
18  fi
19  if $(egrep -qe "type (.*) interface" $f); then
20    AUX+=("github.com/lucas-clemente/quic-go=$f")
21  fi
22done
23
24# Find the file that defines the interface we're mocking.
25for f in *.go; do
26  if [[ -z ${f##*_test.go} ]]; then
27    # skip test files
28    continue;
29  fi
30  INTERFACE=$(sed -n "/^type $ORIG_INTERFACE_NAME interface/,/^}/p" $f)
31  if [[ -n "$INTERFACE" ]]; then
32    SRC=$f
33    break
34  fi
35done
36
37if [[ -z "$INTERFACE" ]]; then
38  echo "Interface $ORIG_INTERFACE_NAME not found."
39  exit 1
40fi
41
42AUX_FILES=$(IFS=, ; echo "${AUX[*]}")
43
44## create a public alias for the interface, so that mockgen can process it
45echo -e "package $1\n" > $TMPFILE
46echo "$INTERFACE" | sed "s/$ORIG_INTERFACE_NAME/$INTERFACE_NAME/" >> $TMPFILE
47goimports -w $TMPFILE
48mockgen -package $1 -self_package $3 -destination $DEST -source=$TMPFILE -aux_files $AUX_FILES
49goimports -w $DEST
50sed "s/$TMPFILE/$SRC/" "$DEST" > "$DEST.new" && mv "$DEST.new" "$DEST"
51rm "$TMPFILE"
52