forked from m3db/ci-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-gen.sh
executable file
·130 lines (110 loc) · 3.15 KB
/
auto-gen.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
. "$(dirname $0)/variables.sh"
autogen_clear() {
DIR="$1"
FILES=${DIR}/*
for FILE in $(ls $FILES); do
if [ -d $FILE ]; then
autogen_subdir_clear $FILE
fi
done
}
autogen_subdir_clear() {
DIR="$1"
rm -f ${DIR}/*.go
}
remove_matching_files() {
local FILE_PATTERN=$1
for DIR in $SRC;
do
local MATCHING_FILES=${DIR}/$FILE_PATTERN
if ls $MATCHING_FILES &> /dev/null; then
for FILE in $(ls $MATCHING_FILES);
do
rm $FILE
done
fi
done
}
autogen_cleanup() {
DIR="$1"
FILES=${DIR}/*
for FILE in $(ls $FILES);
do
if [ -d $FILE ]; then
autogen_cleanup $FILE
else
add_license $FILE $DIR
fi
done
}
mocks_cleanup() {
local MOCK_PATTERN=$1
for DIR in $SRC;
do
local MOCKS=${DIR}/${MOCK_PATTERN}
if ls $MOCKS &> /dev/null; then
for FILE in $(ls $MOCKS);
do
add_license $FILE $DIR
# NB(xichen): there is an open issue (https://github.com/golang/mock/issues/30)
# with mockgen that causes the generated mock files to have vendored packages
# in the import list. For now we are working around it by removing the vendored
# path. Also sed -i'' does not work with BSD sed shipped with OS X, whereas
# sed -i '' doesn't work with GNU sed, so we work around it by redirecting to a
# temp file first and moving it back later.
sed "s|$VENDOR_PATH/||" $FILE > $FILE.tmp && mv $FILE.tmp $FILE
# Strip GOPATH from the source file path
sed "s|Source: $GOPATH/src/\(.*\.go\)|Source: \1|" $FILE > $FILE.tmp && mv $FILE.tmp $FILE
# NB(prateek): running genclean makes mock-gen idempotent.
# NB(xichen): genclean should be run after the vendor path is stripped.
basePkg=$(echo $DIR | sed -e "s@${GOPATH}/src/@@g")
genclean -pkg $basePkg -out $FILE -in $FILE
gofmt -w $FILE
done
fi
done
}
generics_cleanup() {
local GEN_FILES_PATTERN=$1
for DIR in $SRC;
do
local GEN_FILES=${DIR}/${GEN_FILES_PATTERN}
if ls $GEN_FILES &> /dev/null; then
for FILE in $(ls $GEN_FILES);
do
add_license $FILE $DIR
done
fi
done
}
add_license() {
FILE="$1"
DIR="$2"
# Add uber license
PREV_PWD=$(pwd)
cd $DIR
$LICENSE_BIN --silent --file $(basename $FILE)
cd $PREV_PWD
}
if [ $# -ne 2 ] || [ -z "$1" ] || [ -z "$2" ]; then
echo "usage: auto-gen.sh output_directory file_generation_rules_directory"
exit 1
fi
set -e
. "$(dirname $0)/variables.sh"
if [[ "$2" = *"generated/mocks"* ]]; then
remove_matching_files "*_mock.go"
elif [[ "$2" = *"generated/generics"* ]]; then
remove_matching_files "*.gen.go"
else
autogen_clear $1
fi
go generate $PACKAGE/$2
if [[ "$2" = *"generated/mocks"* ]]; then
mocks_cleanup "*_mock.go"
elif [[ "$2" = *"generated/generics"* ]]; then
generics_cleanup "*.gen.go"
else
autogen_cleanup $1
fi