-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·162 lines (134 loc) · 3.74 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
#!/usr/bin/env bash
#
# Helper build script.
#
# Function defined as do_something are intended to be used as runnable actions when this
# script is executed.
#
# Example:
#
# ./build.sh test
#
# Will dispatch to do_test function as defined in this script.
set -euo pipefail
APP_NAME="ease"
VERSION=$(git describe --tags --abbrev=8 --dirty --always)
OUT_DIR="out"
RELEASE_DIR="${OUT_DIR}/release"
FFPROBE_EXE=$(command -v ffprobe)
FFMPEG_EXE=$(command -v ffmpeg)
GO="${GO:-$(command -v go)}"
# The firs command line argument is the name of action to run. Default action is "help".
ACTION="${1:-help}"
# Helper functions
err() {
printf "ERROR: %s\n" "$*" 1>&2
}
log() {
printf "%s\n" "$*"
}
# Define ACTION functions in form do_<action>. From commanline point of view the "do_"
# part is stripped.
# Help on usage.
do_help() {
log "Supported build actions:"
awk '
/^do_[a-zA-Z0-9_]+\(\)/ {
a=$0;
gsub("[(){]", "", a);
gsub("^do_", "", a);
printf "%-15s %s\n", a, prev;
}
{
prev=$0;
}
' "$0"
}
# Run tests.
do_test() {
if [ -z "$FFPROBE_EXE" ] ; then
err "ffprobe not found in PATH, ffprobe is required for ease tool and tests"
exit 1
fi
if [ -z "$FFMPEG_EXE" ] ; then
err "ffmpeg not found in PATH, ffmpeg is required for ease tool and tests"
exit 1
fi
"$GO" test -cover ./...
}
# Create test coverage report HTML.
do_coverage() {
mkdir -p out
"$GO" test -coverprofile="${OUT_DIR}"/coverage.out ./... \
&& "$GO" tool cover -html="${OUT_DIR}"/coverage.out
}
# Run linters and static code analysis checks.
do_lint() {
golangci-lint run
}
# Create build.
do_build() {
CGO_ENABLED=0 "$GO" build -o "${OUT_DIR}/${APP_NAME}" -trimpath \
-ldflags="-X main.version=$VERSION -buildid=" -v
build_artifact_info "${OUT_DIR}/${APP_NAME}"
}
# Release build includes additional flags.
rel_build() {
CGO_ENABLED=0 "$GO" build -o "${OUT_DIR}/${APP_NAME}" -trimpath \
-ldflags="-s -w -X main.version=$VERSION -buildid=" -v
build_artifact_info "${OUT_DIR}/${APP_NAME}"
}
# Print some relevant info on build artifact.
#
# arg $1: path to artifact/binary
build_artifact_info() {
go version -m "$1"
stat "$1"
# On GitHub CI also set step output
if [ "${GITHUB_ACTIONS:-false}" == "true" ] ; then
echo "artifact_path=$1" >> "${GITHUB_OUTPUT}"
fi
}
# Generate a simple changelog from git commits
git_changelog() {
local to_tag from_tag
declare -a tags
tags=($(git for-each-ref refs/tags/* --sort=-taggerdate --count=2 --format="%(refname:short)"))
to_tag=${tags[0]}
from_tag=${tags[1]}
echo "## Changelog ($to_tag)"
git log --pretty=format:'* %h %s' "${from_tag}".."${to_tag}"
echo
}
# Prepare release artifacts.
do_release() {
log "Removing old artifacts"
do_clean
mkdir -p "$RELEASE_DIR"
# For Linux we create just amd64 arch build
GOOS=linux GOARCH=amd64 rel_build
tar --gzip -cvf "${RELEASE_DIR}/${APP_NAME}-linux-amd64.tar.gz" -C "$OUT_DIR" "$APP_NAME"
# For macOS create both amd64 and arm64 builds
GOOS=darwin GOARCH=amd64 rel_build
zip -j "${RELEASE_DIR}/${APP_NAME}-darwin-amd64.zip" "${OUT_DIR}/${APP_NAME}"
GOOS=darwin GOARCH=arm64 rel_build
zip -j "${RELEASE_DIR}/${APP_NAME}-darwin-arm64.zip" "${OUT_DIR}/${APP_NAME}"
# Create checksums file
pushd "$RELEASE_DIR"
md5sum "$APP_NAME"* >md5sums.txt
git_changelog > changelog.txt
popd
}
# Clean build artifacts.
do_clean() {
"$GO" clean
rm -v -rf "${OUT_DIR:?}"/*
}
# Dispatch to defined ACTION function.
if grep -q "do_${ACTION}()" "$0" ; then
log "== Running $ACTION =="
"do_${ACTION}"
else
err "Action $ACTION unknown"
do_help
fi