-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
97 lines (80 loc) · 2.44 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
#!/usr/bin/env bash
# Script to build your Golang project for all available platforms
# Written by Julien Briault <dev[at]jbriault.fr>
package=$1
if [[ -z "$package" ]]; then
echo "usage: $0 <package-name>"
echo "example: $0 main.go"
exit 1
fi
function pprint() {
# Function to colorize the output
# levels: 'error', 'success', 'info', 'warning'
local level=$1
local message=$2
# Colors for CLI output
local yellow='\033[1;33m'
local green='\033[0;32m'
local red='\033[0;31m'
local orange='\033[0;33m'
local resetcolor='\033[0m'
case ${level} in
"error")
echo -e "${red}[!] ${message}${resetcolor}"
;;
"warning")
echo -e "${orange}[!] ${message}${resetcolor}"
;;
"success")
echo -e "${green}[+] ${message}${resetcolor}"
;;
"info")
echo -e "${yellow}[*] ${message}${resetcolor}"
;;
*)
echo "[+] ${message}"
;;
esac
}
# Output build directory
build_dir="$(pwd)/build/"
platforms=$(go tool dist list)
# Remove '.go' from package name
package_split=(${package%.*})
package_name=${package_split[-1]}
# Get project version
package_version=$(cat VERSION)
IFS=$'\n\t'
# Clean go-build cache
pprint "" "Clean go-build cache"
rm -rf ~/.cache/go-build
cd src/
for platform in ${platforms[@]}; do
platform_split=(${platform/\// })
os_name=$(echo ${platform_split} | awk '{ print $1 }')
os_arch=$(echo ${platform_split} | awk '{ print $2 }')
output_name=${package}'-'${package_version}'-'${os_name}'-'${os_arch}
# Add '.exe' extension for Windows binary file
if [ "$os_name" = "windows" ]; then
output_name+='.exe'
fi
if [ ! -d "build" ]; then
mkdir -p ${build_dir}
else
mkdir -p ${build_dir}${os_name}/
fi
case ${os_name} in
"android" | "ios" | "js" ) # You can add the OS you want if you don't want it to be built.
pprint "info" "Skip building package ${package} for ${platform}"
;;
*)
env GOOS=${os_name} GOARCH=${os_arch} go build -o ${build_dir}/${output_name} .
if [ $? -ne 0 ]; then
pprint "error" "[!] An error has occurred! Aborting the script execution..."
exit 1
else
pprint "success" "Building ${package} package for ${platform}"
fi
;;
esac
done