-
Notifications
You must be signed in to change notification settings - Fork 23
/
build.sh
executable file
·257 lines (210 loc) · 6.07 KB
/
build.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
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
#!/usr/bin/env sh
TYPE=""
NO_LINT="0"
NO_TEST="0"
RELEASE=${RELEASE_BUILD:-"0"}
RELEASE_TYPE="unknown"
VERSION=""
COMMIT_HASH=""
BUILD_UNIXTIME=""
PACKAGE_FILENAME=""
DOCKER_TAG=""
echo_red() {
printf '\033[31m%s\033[0m\n' "$1"
}
check_dependency() {
for cmd in $1
do
if ! which "$cmd" > /dev/null; then
echo_red "Error: \"$cmd\" is required."
exit 127
fi
done
}
show_help() {
cat <<-EOF
ezBookkeeping build script
Usage:
build.sh type [options]
Types:
backend Build backend binary file
frontend Build frontend files
package Build package archive
docker Build docker image
Options:
-r, --release Build release (The script will use environment variable "RELEASE_BUILD" to detect whether this is release building by default)
-o, --output <filename> Package file name (For "package" type only)
-t, --tag Docker tag (For "docker" type only)
--no-lint Do not execute lint check before building
--no-test Do not execute unit testing before building
-h, --help Show help
EOF
}
parse_args() {
if [ "$1" = "backend" ] || [ "$1" = "frontend" ] || [ "$1" = "package" ] || [ "$1" = "docker" ]; then
TYPE="$1"
shift 1
fi
while [ ${#} -gt 0 ]; do
case "${1}" in
--release | -r)
RELEASE="1"
;;
--output | -o)
PACKAGE_FILENAME="$2"
shift
;;
--tag | -t)
DOCKER_TAG="$2"
shift
;;
--no-lint)
NO_LINT="1"
;;
--no-test)
NO_TEST="1"
;;
--help | -h)
show_help
exit 0
;;
*)
echo_red "Invalid argument: $1"
show_help
exit 2
;;
esac
shift 1
done
if [ "$RELEASE" = "0" ]; then
RELEASE_TYPE="snapshot"
else
RELEASE_TYPE="release"
fi
}
check_type_dependencies() {
if [ "$TYPE" = "" ]; then
echo_red "Error: No specified type"
show_help
exit 2
fi
check_dependency "git"
if [ "$TYPE" = "backend" ]; then
check_dependency "go gcc"
elif [ "$TYPE" = "frontend" ]; then
check_dependency "node npm"
elif [ "$TYPE" = "package" ]; then
check_dependency "go gcc node npm tar"
elif [ "$TYPE" = "docker" ]; then
check_dependency "docker"
fi
}
set_build_parameters() {
VERSION="$(grep '"version": ' package.json | awk -F ':' '{print $2}' | tr -d ' ' | tr -d ',' | tr -d '"')"
COMMIT_HASH="$(git rev-parse --short HEAD)"
BUILD_UNIXTIME="$(date '+%s')"
}
build_backend() {
echo "Pulling backend dependencies..."
go get .
if [ "$NO_LINT" = "0" ]; then
echo "Executing backend lint checking..."
go vet -v ./...
if [ "$?" != "0" ]; then
echo_red "Error: Failed to pass lint checking"
exit 1
fi
fi
if [ "$NO_TEST" = "0" ]; then
echo "Executing backend unit testing..."
go clean -cache
go test ./... -v
if [ "$?" != "0" ]; then
echo_red "Error: Failed to pass unit testing"
exit 1
fi
fi
backend_build_extra_arguments="-X main.Version=$VERSION"
backend_build_extra_arguments="$backend_build_extra_arguments -X main.CommitHash=$COMMIT_HASH"
if [ "$RELEASE" = "0" ]; then
backend_build_extra_arguments="$backend_build_extra_arguments -X main.BuildUnixTime=$BUILD_UNIXTIME"
fi
echo "Building backend binary file ($RELEASE_TYPE)..."
CGO_ENABLED=1 go build -a -v -trimpath -ldflags "-w -s -linkmode external -extldflags '-static' $backend_build_extra_arguments" -o ezbookkeeping ezbookkeeping.go
chmod +x ezbookkeeping
}
build_frontend() {
echo "Pulling frontend dependencies..."
npm install
if [ "$NO_LINT" = "0" ]; then
echo "Executing frontend lint checking..."
npm run lint
if [ "$?" != "0" ]; then
echo_red "Error: Failed to pass lint checking"
exit 1
fi
fi
echo "Building frontend files ($RELEASE_TYPE)..."
if [ "$RELEASE" = "0" ]; then
buildUnixTime=$BUILD_UNIXTIME npm run build
else
npm run build
fi
}
build_package() {
package_file_name="$VERSION";
if [ "$RELEASE" = "0" ]; then
package_file_name="$package_file_name-$(date '+%Y%m%d')"
fi
package_file_name="ezbookkeeping-$package_file_name-$(arch).tar.gz"
if [ -n "$PACKAGE_FILENAME" ]; then
package_file_name="$PACKAGE_FILENAME"
fi
echo "Building package archive \"$package_file_name\" ($RELEASE_TYPE)..."
build_backend
build_frontend
rm -rf package
mkdir package
mkdir package/data
mkdir package/storage
mkdir package/log
cp ezbookkeeping package/
cp -R dist package/public
cp -R conf package/conf
cp -R templates package/templates
cp LICENSE package/
cd package || { echo_red "Error: Build Failed"; exit 1; }
tar cvzf "../$package_file_name" .
cd - || return
}
build_docker() {
docker_tag="$VERSION"
if [ "$RELEASE" = "0" ]; then
docker_tag="SNAPSHOT-$(date '+%Y%m%d')";
fi
docker_tag="ezbookkeeping:$docker_tag"
if [ -n "$DOCKER_TAG" ]; then
docker_tag="$DOCKER_TAG"
fi
echo "Building docker image \"$docker_tag\" ($RELEASE_TYPE)..."
docker build . -t "$docker_tag" --build-arg RELEASE_BUILD=$RELEASE
}
main() {
if [ -z "$1" ]; then
show_help
exit 0
fi
parse_args "$@"
check_type_dependencies "$TYPE"
set_build_parameters
if [ "$TYPE" = "backend" ]; then
build_backend
elif [ "$TYPE" = "frontend" ]; then
build_frontend
elif [ "$TYPE" = "package" ]; then
build_package
elif [ "$TYPE" = "docker" ]; then
build_docker
fi
}
main "$@"