This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
gomake
executable file
·306 lines (248 loc) · 8.97 KB
/
gomake
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash
set -e
: ${target_name:=dist}
: ${work_path:=.}
: ${app:=$(basename $(cd "${work_path}"; pwd))}
: ${repo:=$(git config --get remote.origin.url | sed -n 's/.*@\(.*\)\.git/\1/p' | tr : /)}
: ${osarchi:="$(go env GOHOSTOS)-$(go env GOHOSTARCH)"}
: ${release_osarchi:="linux-amd64,darwin-amd64,windows-amd64"}
: ${version:=0}
: ${token:=}
: ${upx:=}
: ${build_packages:=}
: ${build_ldflags:="-s -w -X main.BuildTime=`date -u '+%Y-%m-%d_%H:%M:%S_UTC'` -X main.BuildVersion=\${version} -X main.BuildCommit=`git rev-parse --short HEAD`"}
: ${errcheck:=1}
read -d '' helper <<EOF || true
Usage: gomake [-v version][-t token] command...
gomake is a script to build go apps
command
clean clean '${target_name}/' directory
build build (current platform only by default)
quality Format, Fix, check error handled, lint, vet, misspell, ineffassign, Gocyclo
test run go tests
install install release to \$GOPATH/bin
release clean, build all platform, test, check git is clean, tag, push tag & build ZIPs
gomake_update self updating by downloading and replacing with latest version
default is 'clean build test quality'
-v, --version=version version of the app
-h, --help this helper
-t, --token=token token to push releases
-W, --work-path=path set working path, default is ./
EOF
go_files=`find . -name '*.go' 2> /dev/null | grep -v ${target_name}/ | grep -v vendor/ | grep -v .git`
err_count=0
#color_red() {
# echo -n -e "\e[0;31m"
#}
#
#color_reset() {
# echo -n -e "\e[0m"
#}
echo_red() {
echo -n -e "\e[0;31m"
echo "${@}"
echo -n -e "\e[0m"
}
echo_purple() {
echo -e "\e[0;35m${@}\e[0m"
}
echo_green() {
echo -e "\e[0;32m${@}\e[0m"
}
echo_yellow() {
echo -e "\e[0;93m${@}\e[0m"
}
err_count() {
c=$(echo -e "${1}" | wc -l)
((err_count+=${c}))
}
gomake_update() {
echo_green "Downloading gomake"
wget -q -O ${work_path}/gomake.tmp https://raw.githubusercontent.com/n0rad/gomake/master/gomake
chmod +x ${work_path}/gomake.tmp
mv ${work_path}/gomake.tmp ${work_path}/$0
}
clean() {
echo_green "Cleaning"
rm -Rf ${work_path}/${target_name}
}
build() {
start=`date +%s`
[ -z "$1" ] || osarchi="$1"
[ ! -z ${version+x} ] || version="0"
mkdir -p ${work_path}/${target_name}/bindata
if [ `type -t pre-build`"" == 'function' ]; then
pre-build
fi
echo_green "Goimports"
[ -f ${GOPATH}/bin/goimports ] || go get -u golang.org/x/tools/cmd/goimports
goimports -w ${go_files}
echo_green "Format"
gofmt -w -s ${go_files}
echo_green "Fix"
go tool fix ${go_files}
if [ "$(ls -A ${work_path}/${target_name}/bindata)" ]; then
[ -f ${GOPATH}/bin/go-bindata ] || go get -u github.com/jteeuwen/go-bindata/...
go-bindata -nomemcopy -pkg dist -prefix dist/bindata -o ${work_path}/${target_name}/bindata.go ${work_path}/${target_name}/bindata/...
fi
ldflags=$(eval echo ${build_ldflags})
IFS=',' read -ra current <<< "$osarchi"
for e in "${current[@]}"; do
echo_green "Building $e"
binaryPath=$(targetBinaryPath ${e})
$(cd ${work_path} && GOOS="${e%-*}" GOARCH="${e#*-}" go build -ldflags "${ldflags}" -o ${target_name}/${binaryPath} ${build_packages})
if [ ${upx} ]; then
echo_green "Compressing ${e}"
[ -f /usr/bin/upx ] || (echo "upx is required to compress" && exit 1)
upx ${work_path}/${target_name}/${binaryPath} &> /dev/null
fi
if [ "${e%-*}" == "windows" ]; then
mv ${work_path}/${target_name}/${binaryPath} ${work_path}/${target_name}/${binaryPath}.exe
fi
done
echo_purple "Build duration : $((`date +%s`-${start}))s"
}
targetBinaryPath() {
echo ${app}-v${version}-${1}/${app}
}
install() {
echo_green "Installing"
cp ${work_path}/${target_name}/${app}-v${version}-$(go env GOHOSTOS)-$(go env GOHOSTARCH)/${app}* ${GOPATH}/bin/
}
quality() {
start=`date +%s`
cd ${work_path}
if [ ${errcheck} == 1 ]; then
echo_green "Err check"
[ -f ${GOPATH}/bin/errcheck ] || go get -u github.com/kisielk/errcheck
res=$(errcheck ./... | grep -v 'vendor/' | grep -v 'Close(' | grep -v '_test.go')
err_count "${res}"
echo_red "${res}"
fi
echo_green "Lint"
[ -f ${GOPATH}/bin/golint ] || go get -u github.com/golang/lint/golint
for i in ${go_files}; do
golint ${i} | grep -v 'should have comment ' || true
done
echo_green "Vet"
go tool vet ${go_files} || true
echo_green "Misspell"
[ -f ${GOPATH}/bin/misspell ] || go get -u github.com/client9/misspell/cmd/misspell
misspell -source=text ${go_files}
echo_green "Ineffassign"
[ -f ${GOPATH}/bin/ineffassign ] || go get -u github.com/gordonklaus/ineffassign
for i in ${go_files}; do
ineffassign -n ${i} || true
done
echo_green "Gocyclo"
[ -f ${GOPATH}/bin/gocyclo ] || go get -u github.com/fzipp/gocyclo
gocyclo -over 15 ${go_files} || true
cd -
echo_purple "Quality duration : $((`date +%s`-${start}))s"
}
require_clean_work_tree() {
# Update the index
git update-index -q --ignore-submodules --refresh
err=0
# Disallow unstaged changes in the working tree
if ! git diff-files --quiet --ignore-submodules --
then
echo_red "cannot release: you have unstaged changes."
git diff-files --name-status -r --ignore-submodules -- >&2
err=1
fi
# Disallow uncommitted changes in the index
if ! git diff-index --cached --quiet HEAD --ignore-submodules --
then
echo_red "cannot release: your index contains uncommitted changes."
git diff-index --cached --name-status -r --ignore-submodules HEAD -- >&2
err=1
fi
if [ ${err} = 1 ]
then
echo_red "Please commit or stash them."
exit 1
fi
}
release() {
start=`date +%s`
if [ "${repo%%/*}" != "github.com" ]; then
echo "Push to '${repo%%/*}' not implemented"
exit 1
fi
if [ -z "${version}" ] || [ "${version}" == "0" ]; then
echo_red "please set version to release"
exit 1
fi
if [ -z "${token}" ]; then
echo_red "please set token to release"
exit 1
fi
github_repo=${repo#*/}
clean
build ${release_osarchi}
test
quality
require_clean_work_tree
echo_green "Compress release"
cd ${work_path}/${target_name}
for i in *-* ; do
if [ -d "$i" ]; then
tar czf ${i}.tar.gz ${i}
fi
done
cd -
git tag v${version} -a -m "Version $version"
git push --tags
sleep 5
posturl=$(curl --data "{\"tag_name\": \"v${version}\",\"target_commitish\": \"master\",\"name\": \"v${version}\",\"body\": \"Release of version ${version}\",\"draft\": false,\"prerelease\": false}" https://api.github.com/repos/${github_repo}/releases?access_token=${token} | grep "\"upload_url\"" | sed -ne 's/.*\(http[^"]*\).*/\1/p')
for i in ${work_path}/${target_name}/*.tar.gz ; do
fullpath=$(ls ${i})
filename=${fullpath##*/}
curl -i -X POST -H "Content-Type: application/x-gzip" --data-binary "@${fullpath}" "${posturl%\{?name,label\}}?name=${filename}&label=${filename}&access_token=${token}"
done
echo_purple "Release duration : $((`date +%s`-${start}))s"
}
test() {
start=`date +%s`
echo_green "Testing"
go test -cover $(go list ${work_path}/... | grep -v vendor/)
if [ `type -t extra-test`"" == 'function' ]; then
extra-test
fi
echo_purple "Test duration : $((`date +%s`-${start}))s"
}
#########################################
#########################################
global_start=`date +%s`
commands=()
while [ $# -gt 0 ]; do
case "${1}" in
-h|--help) echo "${helper}"; exit 0;;
--version=*)version="${1#*=}"; shift;;
--token=*) token="${1#*=}"; shift;;
--work-path=*) work_path="${1#*=}"; shift;;
-v) version="${2}"; [ $# -gt 1 ] || (echo_red "Missing argument for ${1}"; exit 1); shift 2;;
-t) token="${2}"; [ $# -gt 1 ] || (echo_red "Missing argument for ${1}"; exit 1); shift 2;;
-W) work_path="${2}"; [ $# -gt 1 ] || (echo_red "Missing argument for ${1}"; exit 1); shift 2;;
--) shift; commands+=("${@}"); break;;
*) commands+=("${1}"); shift;;
esac
done
if [ -f ${work_path}/gomake.cfg ]; then
. ${work_path}/gomake.cfg
fi
if [ ${#commands[@]} -eq 0 ]; then
commands=(clean build test quality)
fi
command_count=0
for i in "${commands[@]}"; do
case ${i} in
test|build|release|clean|quality|install|gomake_update) ${i}; ((++command_count));;
*) echo_red "Unknown command '${i}'"; echo ${helper}; exit 1;;
esac
done
if [ ${command_count} -gt 1 ]; then
echo_purple "Global duration : $((`date +%s`-global_start))s"
fi
exit 0