-
Notifications
You must be signed in to change notification settings - Fork 0
/
gomake
executable file
·379 lines (308 loc) · 11.3 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#!/usr/bin/env bash
set -efo pipefail # TODO u
: ${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:=false}
: ${build_packages:=}
: ${build_binaries:=$(find "${work_path}/pkg/cmd" -maxdepth 1 -mindepth 1 -type d)}
#$(git log -1 --pretty=format:%s | tr " " _)
: ${build_ldflags:="-s -w -X main.buildTime=$(date -u '+%Y-%m-%d_%H:%M:%S_UTC') -X main.buildCommitMessage= -X main.buildVersion=\${version} -X main.buildCommit=$(git rev-parse --short HEAD)"}
: ${errcheck:=true}
: ${build_output:=true}}
: ${cgo:=}
: ${short_test:=}
read -d '' helper <<EOF || true
Usage: [var=val] gomake 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, zip, push to github releases
gomake_update self updating by downloading and replacing with latest version from github.com/n0rad/gomake
-h, --help this helper
default is 'clean build test quality'
var=val you can persist them to gomake.cfg file
work_path root of project to build (=${work_path})
version version to build/release (=${version})
token github token to upload releases (=${token})
release_osarchi versions to release (=${release_osarchi})
app application name (=${app})
func
EOF
go_files=`find ${work_path} -name '*.go' 2> /dev/null | grep -v ${work_path}/build/ | 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}))
}
quickedit() {
echo -e "Release of version ${version}\n\n#Please write down release page text" > /tmp/release$$
echo -e "#Here is commits since last tag :\n" >> /tmp/release$$
git log `git describe --tags --abbrev=0`..HEAD --oneline >> /tmp/release$$
trap 'rm -f /tmp/release$$' EXIT;
vim /tmp/release$$ >/dev/tty;
cat /tmp/release$$ | awk '{$1=$1};1' | grep -v ^#
}
gomake_update() {
echo_green "Downloading gomake"
curl --fail --silent --show-error --location --remote-time --compressed \
-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"
ensure_tool "goimports" "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
ensure_tool "go-bindata" "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
if [ -d ${work_path}/pkg/cmd ]; then
for i in ${build_binaries}; do
current_app=${i##*/}
echo_green "Building '${current_app}' for $e"
binaryPath=$(targetBinaryPath ${e} ${current_app})
build_binary "${ldflags}" "${e}" "-o ${target_name}/${binaryPath}" "$i"
done
else
echo_green "Building $e"
binaryPath=$(targetBinaryPath ${e} ${app})
build_binary "${ldflags}" "${e}" "-o ${target_name}/${binaryPath}" "${build_packages}"
fi
done
echo_purple "Build duration : $((`date +%s`-${start}))s"
}
build_binary() {
local ldflags=$1
local e=$2
local binaryArg=$3
local packages=$4
local install=""
if [ ${e} == "$(go env GOHOSTOS)-$(go env GOHOSTARCH)" ]; then
install="-i"
fi
cgo_enable=""
if [ ! -z ${cgo} ]; then
cgo_enable="CGO_ENABLED=${cgo}"
fi
$(cd ${work_path} && GOOS="${e%-*}" GOARCH="${e#*-}" ${cgo_enable} go build ${install} -ldflags "${ldflags}" ${binaryArg} ${packages})
if [ "$upx" = true ]; then
if [ "${e%-*}" != "darwin" ]; then # no upx on darwin it sux
echo_green "Compressing ${e}" # TODO compress on release only
command -v upx > /dev/null || (echo "upx is required to compress" && exit 1)
upx ${work_path}/${target_name}/${binaryPath} &> /dev/null
fi
fi
if [ "${e%-*}" == "windows" ]; then
mv ${work_path}/${target_name}/${binaryPath} ${work_path}/${target_name}/${binaryPath}.exe
fi
}
targetBinaryPath() {
if [ "${1%-*}" == "windows" ]; then
echo ${app}-v${version}-${1}/${2}.exe
else
echo ${app}-v${version}-${1}/${2}
fi
}
install() {
echo_green "Installing"
cp ${work_path}/${target_name}/$(targetBinaryPath $(go env GOHOSTOS)-$(go env GOHOSTARCH)) ${GOPATH}/bin/
}
quality() {
start=$(date +%s)
cd ${work_path}
if [ "${errcheck}" = true ]; then
echo_green "Err check"
ensure_tool "errcheck" "github.com/kisielk/errcheck"
res=$(set +o pipefail; errcheck ./... | grep -v 'vendor/' | grep -v 'Close(' | grep -v '_test.go')
err_count "${res}"
echo_red "${res}"
fi
echo_green "Lint"
ensure_tool "golint" "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"
ensure_tool "misspell" "github.com/client9/misspell/cmd/misspell"
misspell -source=text ${go_files}
echo_green "Ineffassign"
ensure_tool "ineffassign" "github.com/gordonklaus/ineffassign"
for i in ${go_files}; do
ineffassign -n ${i} || true
done
echo_green "Gocyclo"
ensure_tool "gocyclo" "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 $(find . -type d -name "*-v${version}-*-*" | sed 's|./||') ; do
tar czf ${i}.tar.gz ${i}
done
cd -
comment=$(quickedit | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g')
git tag v${version} -a -m "Version $version"
git push origin v${version}
sleep 5 # give time to github to ingest tag
posturl=$(curl --data "{\"tag_name\": \"v${version}\",\"target_commitish\": \"master\",\"name\": \"v${version}\",\"body\": \"${comment}\",\"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 $(find ${work_path}/${target_name} -type f -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 ${short_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"
}
ensure_tool() {
command -v $1 > /dev/null || {
echo_yellow "Preparing $1"
mkdir -p ${work_path}/build/src
[ -L ${work_path}/build/vendor ] || ln -s src ${work_path}/build/vendor
GOPATH=$(pwd ${work_path})/build go get $2 # todo fetch
GOPATH=$(pwd ${work_path})/build go install $2
}
}
# golang.org/x/tools/cmd/goimports
# github.com/jteeuwen/go-bindata
# github.com/kisielk/errcheck
# github.com/golang/lint/golint
# github.com/client9/misspell/cmd/misspell
# github.com/gordonklaus/ineffassign
# github.com/fzipp/gocyclo
#########################################
#########################################
global_start=$(date +%s)
commands=()
while [ $# -gt 0 ]; do
case "${1}" in
-h|--help) echo "${helper}"; exit 0;;
-short) short_test="-short"; shift;;
--) shift; commands+=("${@}"); break;;
*) commands+=("${1}"); shift;;
esac
done
if [ -f ${work_path}/gomake.cfg ]; then
. ${work_path}/gomake.cfg
fi
PATH=$PATH:${work_path}/build/bin
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