-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
build.sh
executable file
·177 lines (167 loc) · 4.37 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
#! /usr/bin/env bash
set -eo pipefail
trap 'echo -e "\033[33;5mBuild failed on build.sh:$LINENO\033[0m"' ERR
# Process args
RELEASE="0.0"
for arg in "$@"; do
case "$arg" in
--all | -a)
BUILD_GO=1
BUILD_GEN=1
LINT=1
TEST=1
RACE=-race
SOMETHING=1
;;
--go | -g)
BUILD_GO=1
SOMETHING=1
;;
--gen | -G)
BUILD_GEN=1
SOMETHING=1
;;
--lint | -l)
LINT=1
SOMETHING=1
;;
--test | -t)
TEST=1
SOMETHING=1
;;
--race | -r)
TEST=1
RACE=-race
SOMETHING=1
;;
--i18n | -i)
I18N=1
SOMETHING=1
;;
--dist | -d)
EXTRA_BUILD_FLAGS="-a -trimpath"
EXTRA_LD_FLAGS="-s -w"
RELEASE="5.31.0"
DIST=1
BUILD_GO=1
BUILD_GEN=1
SOMETHING=1
;;
--help | -h)
echo "$0 [options]"
echo " -a, --all Equivalent to --gen --go --lint --race"
echo " -d, --dist Create distribution"
echo " -g, --go Build the Go code"
echo " -G, --gen Generate the source"
echo " -i, --i18n Extract the localization template"
echo " -l, --lint Run the linters"
echo " -r, --race Run the tests with race-checking enabled"
echo " -t, --test Run the tests"
echo " -h, --help This help text"
exit 0
;;
*)
echo "Invalid argument: $arg"
exit 1
;;
esac
done
if [ "$SOMETHING"x != "1x" ]; then
BUILD_GEN=1
BUILD_GO=1
fi
case $(uname -s) in
Darwin*)
if [ "$(uname -p)" == "arm" ]; then
export MACOSX_DEPLOYMENT_TARGET=11
else
export MACOSX_DEPLOYMENT_TARGET=10.15
fi
;;
esac
LDFLAGS_ALL="-X github.com/richardwilkes/toolbox/cmdline.AppVersion=$RELEASE $EXTRA_LD_FLAGS"
STD_FLAGS="-v -buildvcs=true $EXTRA_BUILD_FLAGS"
# Generate the source
if [ "$BUILD_GEN"x == "1x" ]; then
echo -e "\033[33mGenerating...\033[0m"
go generate ./gen/srcgen.go
fi
# Generate the translation file
if [ "$I18N"x == "1x" ]; then
i18n $(go list -f "{{.Dir}}" -m github.com/richardwilkes/json) \
$(go list -f "{{.Dir}}" -m github.com/richardwilkes/pdf) \
$(go list -f "{{.Dir}}" -m github.com/richardwilkes/rpgtools) \
$(go list -f "{{.Dir}}" -m github.com/richardwilkes/toolbox) \
$(go list -f "{{.Dir}}" -m github.com/richardwilkes/unison) \
.
fi
# Build our Go code
if [ "$BUILD_GO"x == "1x" ]; then
echo -e "\033[33mBuilding the Go code...\033[0m"
case $(uname -s) in
Darwin*)
go run $STD_FLAGS -ldflags all="$LDFLAGS_ALL" packaging/main.go
go build $STD_FLAGS -ldflags all="$LDFLAGS_ALL" -o "GCS.app/Contents/MacOS/" .
touch GCS.app
;;
Linux*)
go build $STD_FLAGS -ldflags all="$LDFLAGS_ALL" .
;;
MINGW*)
go run $STD_FLAGS -ldflags all="$LDFLAGS_ALL" packaging/main.go
go build $STD_FLAGS -ldflags all="$LDFLAGS_ALL -H windowsgui" .
;;
*)
echo "Unsupported OS"
false
;;
esac
if [ "$LINT"x == "1x" ]; then
GOLANGCI_LINT_VERSION=$(curl --head -s https://github.com/golangci/golangci-lint/releases/latest | grep -i location: | sed 's/^.*v//' | tr -d '\r\n')
TOOLS_DIR=$(go env GOPATH)/bin
if [ ! -e "$TOOLS_DIR/golangci-lint" ] || [ "$("$TOOLS_DIR/golangci-lint" version 2>&1 | awk '{ print $4 }' || true)x" != "${GOLANGCI_LINT_VERSION}x" ]; then
echo -e "\033[33mInstalling version $GOLANGCI_LINT_VERSION of golangci-lint into $TOOLS_DIR...\033[0m"
mkdir -p "$TOOLS_DIR"
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$TOOLS_DIR" v$GOLANGCI_LINT_VERSION
fi
echo -e "\033[33mLinting the Go code...\033[0m"
"$TOOLS_DIR/golangci-lint" run
fi
fi
# Run the tests
if [ "$TEST"x == "1x" ]; then
if [ -n "$RACE" ]; then
echo -e "\033[33mTesting with -race enabled...\033[0m"
else
echo -e "\033[33mTesting...\033[0m"
fi
go test $RACE ./... | grep -v "no test files"
fi
# Package for distribution
if [ "$DIST"x == "1x" ]; then
echo -e "\033[33mPackaging...\033[0m"
case $(uname -s) in
Darwin*)
if [ "$(uname -p)" == "arm" ]; then
HW=apple
else
HW=intel
fi
codesign -s "Richard Wilkes" -f -v --timestamp --options runtime GCS.app
/bin/rm -rf tmp *.dmg
mkdir tmp
# Installation of https://github.com/create-dmg/create-dmg is required
create-dmg --volname "GCS v$RELEASE" --icon-size 128 --window-size 448 280 --add-file GCS.app GCS.app 64 64 \
--app-drop-link 256 64 --codesign "Richard Wilkes" --hdiutil-quiet --no-internet-enable --notarize gcs-notary \
gcs-$RELEASE-macos-$HW.dmg tmp
/bin/rm -rf tmp
;;
Linux*)
/bin/rm -f gcs-${RELEASE}-linux.tgz
tar czf gcs-${RELEASE}-linux.tgz gcs
;;
MINGW*)
go run -ldflags all="$LDFLAGS_ALL" packaging/main.go -z
;;
esac
fi