forked from goplus/gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.bash
executable file
·180 lines (144 loc) · 3.98 KB
/
all.bash
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
#! /usr/bin/env bash
set -e
GOP_ROOT=$(pwd)
GOP_HOME_DIR="$HOME/gop"
GOP_CACHE_DIR="$GOP_ROOT/.gop"
ADD_GOPATH_COMMAND="export PATH=\$PATH:\$GOPATH/bin"
ADD_GO_BIN_COMMAND="export PATH=\$PATH:\$HOME/go/bin"
MANUAL_EXPORT_COMMAND=""
GIT_BRANCH=$(git branch --show-current)
GIT_COMMIT_HASH=$(git rev-parse --verify HEAD)
BUILD_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
GO_FLAGS="-X github.com/goplus/gop/build.Date=${BUILD_DATE} \
-X github.com/goplus/gop/build.Commit=${GIT_COMMIT_HASH} \
-X github.com/goplus/gop/build.Branch=${GIT_BRANCH}"
command_exists() {
command -v "$@" >/dev/null 2>&1
}
build_go_plus_tools() {
command_exists go || {
echo "Error: go is not installed but required, please visit https://golang.org/doc/install for help."
exit 1
}
COMMANDS_DIR="$GOP_ROOT/cmd"
if [ ! -e "$COMMANDS_DIR" ]; then
echo "Error: This shell script should be run at root directory of gop repository."
exit 1
fi
echo "Installing Go+ tools..."
cd $COMMANDS_DIR
# will be overwritten by gop build
go install -v -ldflags "${GO_FLAGS}" ./...
echo "Go+ tools installed successfully!"
}
clear_gop_cache() {
echo "Clearing gop cache"
cd $GOP_ROOT
if [ -d "$GOP_CACHE_DIR" ]; then
rm -r $GOP_CACHE_DIR
echo "Gop cache files cleared"
else
echo "No gop cache files found"
fi
}
link_gop_root_dir() {
echo "Linking $GOP_ROOT to $GOP_HOME_DIR"
cd $GOP_ROOT
if [ ! -e "$GOP_HOME_DIR" ] && [ "$GOP_ROOT" != "$GOP_HOME_DIR" ]; then
ln -s $GOP_ROOT $GOP_HOME_DIR
fi
echo "$GOP_ROOT linked to $GOP_HOME_DIR successfully!"
}
build_go_plus_tutorials() {
command_exists gop || {
if [ -n "$GOPATH" ]; then
MANUAL_EXPORT_COMMAND="$ADD_GOPATH_COMMAND"
echo "Execute command: $ADD_GOPATH_COMMAND"
export PATH=$PATH:$GOPATH/bin
else
MANUAL_EXPORT_COMMAND="$ADD_GO_BIN_COMMAND"
echo "Execute command: $ADD_GO_BIN_COMMAND"
export PATH=$PATH:$HOME/go/bin
fi
}
command_exists gop || {
echo "Error: something wrong, you could create a new issue on https://github.com/goplus/gop/issues, we will help you."
exit 1
}
cd $GOP_ROOT
echo "Building all Go+ tutorials."
gop install -ldflags "${GO_FLAGS}" ./...
echo "Go+ tutorials builded successfully!"
}
summary() {
echo "Installation Summary:"
echo "Go+ is now installed."
echo ""
if [ -n "$MANUAL_EXPORT_COMMAND" ]; then
cat <<-EOF
Notice, we just temporarily add gop command and other tools into your PATH.
To make it permanent effect, you should manually add below command:
${MANUAL_EXPORT_COMMAND}
to your shell startup file, such as: ~/.bashrc, ~/.zshrc...
type 'go help install' for more details.
EOF
fi
}
hello_world() {
HELLO_WORLD_COMMAND="gop run tutorial/01-Hello-world/hello.gop"
cd $GOP_ROOT
EXPORT_CMD=""
if [ -n "$MANUAL_EXPORT_COMMAND" ]; then
EXPORT_CMD="${MANUAL_EXPORT_COMMAND} && "
fi
echo '-----------------------------'
cat <<-EOF
Let's have a try now, Copy below command to get classic Hello, world!
${EXPORT_CMD}${HELLO_WORLD_COMMAND}
Besides, there are another more tutorials listed under ./tutorial/ directory.
Have fun!
EOF
}
gop_test() {
echo "Running gop test"
cd $GOP_ROOT
PATH=$PATH:$GOPATH/bin gop test -v -coverprofile=coverage.txt -covermode=atomic ./...
echo "Finished running gop test"
}
default() {
# Build all Go+ tools
build_go_plus_tools
# Clear gop cache files
clear_gop_cache
# Link Gop root directory to home/ dir
link_gop_root_dir
# Build all Go+ tutorials
build_go_plus_tutorials
# Summary
summary
# hello world
hello_world
}
if [ "$#" -eq 0 ]; then
default
exit 0
fi
# To add more options below, juse add another case.
while [ "$#" -gt 0 ]; do
case "$1" in
-c|--compile)
build_go_plus_tools
;;
-t|--test)
gop_test
;;
-*)
echo "Unknown option: $1"
echo "Valid options:"
echo " -t, --test Running testcases with gop test"
echo " -c, --compile Compile gop and related tools"
exit 1
;;
esac
shift
done