forked from layerssss/freedom-routes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.sh
executable file
·131 lines (114 loc) · 3.26 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
#!/bin/bash
# ./build.sh # build linux/amd64 binary
# ./build.sh -d /usr/share/freedom-routes # build linux binary
# ./build.sh linux/amd64 # build linux/amd64 binary
# ./build.sh release # build $RELEASEs with user complied go and upload the package to s3.
# Build binary for distribution.
#
# Usage:
#
# ./build.sh [options] [platform ...]
# -d <assets_dir>
# -p # packaging the binary
#
EXTRA_FILES="routes/templates freedom-routes.etc README.md"
RELEASE="linux/amd64 linux/386 homebrew/amd64 homebrew/386 windows/amd64 windows/386"
VERSION=$(sed -rn 's/.*const VERSION.*"([0-9.]+)".*/\1/p' main.go)
declare -A OS_MAP=(
[homebrew]="darwin"
)
declare -A DIR_MAP=(
[linux]="/usr/share/freedom-routes"
[homebrew]="/usr/share/freedom-routes"
)
platform=""
os=""
arch=""
assets_dir=""
function cgo_enabled { [ "$1" == "$GOHOSTOS" ] && echo 1 || echo 0; }
function ext { [ $1 == "windows" ] && echo .exe || echo ""; }
# dist{platform, os, arch, assets_dir}
function dist {
output="dist/$platform.$arch"
rm -r $output 2>/dev/null
mkdir -p $output
cp -r $EXTRA_FILES $output
if [[ -d "assets/$platform" ]]; then
cp -r assets/$platform/* $output
fi
build
}
# build{platform, os, arch, assets_dir}
function build {
echo -e "\n>> building $platform/$arch"
output="dist/$platform.$arch/freedom-routes$(ext $os)"
sed -i "/const ASSETS_MODE/s~.*~const ASSETS_MODE = \"$assets_dir\"~" routes/routes.go
CGO_ENABLED=$(cgo_enabled $os) GOOS=$os GOARCH=$arch $GOROOT/bin/go build -o "$output"
sed -i '/const ASSETS_MODE/s/.*/const ASSETS_MODE = "source"/' routes/routes.go
echo ">> created $output"
}
# package{platform, os, arch}
function package {
echo ">> packing $platform/$arch"
case $os in
linux | darwin )
tar zcf dist/freedom-routes.$platform.$arch-$VERSION.tar.gz -C dist $platform.$arch;;
windows )
rm dist/freedom-routes.$platform.$arch-$VERSION.zip 2>/dev/null
cd dist && zip -r freedom-routes.$platform.$arch-$VERSION.zip $platform.$arch && cd ..
;;
esac
}
function upload {
for file in $*; do
s3cmd put --acl-public $file s3://dl.example.com/freedom-routes/
#current=$(echo $file | sed -r 's/(.*)-[0-9.]+(\..*)$/\1\2/')
#s3cmd copy s3://dl.example.com/freedom-routes/$file s3://dl.example.com/freedom-routes/$current
done
}
#
# ¤main
# ----
eval "$(go env)"
o_assets_dir="runtime"
o_package=false
while getopts "d:p" opt; do
case $opt in
d ) o_assets_dir=$OPTARG ;;
p ) o_package=true ;;
esac
done
shift $(( OPTIND - 1 ))
case $1 in
"" )
platform=$GOOS; os=$GOOS; arch=$GOARCH; assets_dir=$o_assets_dir
dist
[ $o_package == true ] && package
;;
dist )
for release in $RELEASE; do
platform=${release%/*}; arch=${release#*/}; os=${OS_MAP[$platform]-$platform}
assets_dir=${DIR_MAP[$platform]-$o_assets_dir}
# fix linux/amd64 build error
[[ $release == "linux/amd64" ]] && GOROOT="" || GOROOT="$HOME/dev/src/go/go"
dist
package
done
;;
release )
rm dist/*.tar.gz dist/*.zip 2>/dev/null
"$0" dist
upload dist/*.tar.gz dist/*.zip
;;
upload )
shift
upload $*
;;
* )
GOROOT="$HOME/dev/src/go/go"
platform=${1%/*}; arch=${1#*/}; os=${OS_MAP[$platform]-$platform}
dist
[ $o_package == true ] && package
;;
esac
exit 0