-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-go-framework.sh
executable file
·180 lines (148 loc) · 4.65 KB
/
update-go-framework.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
178
#!/bin/bash
#
# Manually updates the Go client .aar package. You can automate this process
# via Android Studio Gradle plugin:
#
# https://github.com/golang/go/wiki/Mobile#building-and-deploying-to-android-1
set -e
readonly CURRENTDIR=$(pwd)
readonly GOCODEDIR="${CURRENTDIR}/go-sdk/src/github.com/ninchat/ninchat-go/mobile"
readonly PACKAGE="com.ninchat"
readonly NAME="client"
readonly LIBDIR="${CURRENTDIR}/$(echo ${PACKAGE} | sed 's/\./\//g')/${NAME}"
function checkIsGoInstalled() {
local go=$(which go)
if [ $? -ne "0" ]; then
echo "Go not installed. Aborting."
exit 1
fi
}
function checkIsGOPATHset() {
if [ -z "${GOPATH}" ]; then
echo "GOPATH not set. Aborting."
exit 1
fi
}
function checkIsANDROIDHOMEset() {
if [ -z "${ANDROID_HOME}" ]; then
echo "ANDROID_HOME not set. Aborting."
exit 1
fi
}
function addGOPATHToPATHIfNeeded() {
local hasgopathinpath=$(echo "${PATH}" | grep "${GOPATH}")
if [ -z "${hasgopathinpath}" ]; then
export PATH="${PATH}:${GOPATH}/bin"
fi
}
function checkBeingCalledFromTheRightDirectory() {
if [ ! -e "${GOCODEDIR}" ]; then
echo "Failed to find go code dir. Did you run this from the Android project dir?"
exit 1
fi
}
function getGoRepoDescription() {
cd "${GOCODEDIR}"
local gorepodescription=$(git describe --tags)
local gorepoversion=$(echo "${gorepodescription}" | sed -e 's/^v//' -e 's/-.*//')
local gorepobuildversion=$(echo "${gorepodescription}" | sed -e 's/[^-]*-//' -e 's/-.*//')
if [ ! -z "${gorepobuildversion}" ]; then
gorepoversion="${gorepoversion}-${gorepobuildversion}"
fi
cd "${CURRENTDIR}"
echo "${gorepoversion}"
}
function generateMd5CheckSum() {
local filename="$1"
if which md5 > /dev/null; then
md5 -q "${filename}" > "${filename}.md5"
else
md5sum "${filename}" | sed 's/ .*//' > "${filename}.md5"
fi
}
function generateSha1CheckSum() {
local filename="$1"
shasum -a 1 "${filename}" | sed 's/ .*//' > "${filename}.sha1"
}
function buildGoLibrary() {
local version=$(getGoRepoDescription)
local outdir="${LIBDIR}/${version}"
local filename="${outdir}/${NAME}-${version}.aar"
if [ ! -e "${outdir}" ]; then
mkdir -p "${outdir}"
fi
cd "${GOCODEDIR}"
echo "Running gomobile tool.."
GOPATH="${CURRENTDIR}/go-sdk:${GOPATH}" gomobile bind -target android -javapkg "${PACKAGE}" -o "${filename}"
if [ $? -ne 0 ]; then
echo "gomobile cmd failed, aborting."
exit 1
fi
cd "${CURRENTDIR}"
for file in $(find "${outdir}" -type f -name '*.aar' -o -name '*.jar'); do
generateMd5CheckSum "${file}"
generateSha1CheckSum "${file}"
done
echo "Done."
}
function generateMavenMetadata() {
local releaseversion=$(getGoRepoDescription)
local filename="maven-metadata.xml"
local time=$(date +%Y%m%d%H%M%S)
cat > "${LIBDIR}/${filename}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<metadata>
<groupId>${PACKAGE}</groupId>
<artifactId>${NAME}</artifactId>
<versioning>
<release>${releaseversion}</release>
<versions>
EOF
for dir in $(find "${LIBDIR}" -mindepth 1 -type d); do
local base=$(basename "${dir}")
echo " <version>${base}</version>" >> "${LIBDIR}/${filename}"
done
echo " </versions>" >> "${LIBDIR}/${filename}"
echo " <lastUpdated>${time}</lastUpdated>" >> "${LIBDIR}/${filename}"
cat >> "${LIBDIR}/${filename}" << EOF
</versioning>
</metadata>
EOF
generateMd5CheckSum "${LIBDIR}/${filename}"
generateSha1CheckSum "${LIBDIR}/${filename}"
}
function generatePOMFile() {
local version="$(getGoRepoDescription)"
local outdir="${LIBDIR}/${version}"
local filename="${outdir}/${NAME}-${version}.pom"
cat > "${filename}" << EOF
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>${PACKAGE}</groupId>
<artifactId>${NAME}</artifactId>
<version>${version}</version>
<packaging>aar</packaging>
<dependencies>
</dependencies>
</project>
EOF
generateMd5CheckSum "${filename}"
generateSha1CheckSum "${filename}"
}
function main() {
# Pre-checks
checkIsGoInstalled
checkIsGOPATHset
checkIsANDROIDHOMEset
addGOPATHToPATHIfNeeded
checkBeingCalledFromTheRightDirectory
# Build
echo "Rebuilding Go SDK framework.."
buildGoLibrary
# Post-processing
generateMavenMetadata
generatePOMFile
}
main