-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·309 lines (253 loc) · 7.79 KB
/
build.sh
File metadata and controls
executable file
·309 lines (253 loc) · 7.79 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail
ARCH="$(uname -m)"
GITREPO=""
GITREF=""
BUNDLES=()
UPDATEINFO=""
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || dirname "$(readlink -f "${0}")")
CONFIG="${ROOT}/config.yml"
DIR_APP="${ROOT}/app"
DIR_CACHE="${ROOT}/cache"
DIR_DIST="${ROOT}/dist"
SCRIPT_DOCKER="${ROOT}/build-docker.sh"
declare -A DEPS=(
[git]=git
[jq]=jq
[yq]=yq
[docker]=docker
)
_OPTS=$(getopt --name "$0" --long 'help,arch:,gitrepo:,gitref:,bundle:,updinfo' --options 'help,a:,b:,u' -- "$@")
eval set -- "${_OPTS}"
unset _OPTS
# ----
SELF=$(basename -- "$(readlink -f -- "${0}")")
log() {
echo "[${SELF}]" "$@"
}
err() {
log >&2 "$@"
exit 1
}
print_help() {
echo "Usage: ${0} [options]"
echo
echo "Options:"
echo " -a, --arch <arch> Target architecture"
echo " --gitrepo <url> Source"
echo " --gitref <ref> Git branch/tag/commit"
echo " -b, --bundle <name> Comma-separated list of bundled software"
echo " -u, --updinfo Embed AppImage update info; requires GITHUB_REPOSITORY env var data"
exit 0
}
in_array() {
local item elem
item="${1}"
shift
for elem in "${@}"; do
[[ "${elem}" == "${item}" ]] && return 0
done
return 1
}
join() {
local IFS="${1}"
shift
echo "${*}"
}
# ----
while true; do
case "${1}" in
-h | --help)
print_help
;;
-a | --arch)
ARCH="${2}"
shift 2
;;
--gitrepo)
GITREPO="${2}"
shift 2
;;
--gitref)
GITREF="${2}"
shift 2
;;
-b | --bundle)
IFS=',' read -r -a BUNDLES <<< "${2}"
shift 2
;;
-u | --updinfo)
UPDATEINFO=1
shift
;;
--)
shift
break
;;
*)
err "Invalid option: ${1}"
;;
esac
done
# ----
for dep in "${!DEPS[@]}"; do
command -v "${dep}" >/dev/null 2>&1 || err "Missing dependency: ${DEPS["${dep}"]}"
done
[[ -f "${CONFIG}" ]] \
|| err "Missing config file: ${CONFIG}"
CONFIGJSON=$(cat "${CONFIG}")
yq -e --arg a "${ARCH}" '.builds[$a]' >/dev/null <<< "${CONFIGJSON}" \
|| err "Unsupported arch"
mapfile -t bundles < \
<(yq -r --arg a "${ARCH}" '.builds[$a].bundles | keys[]' <<< "${CONFIGJSON}")
for bundle in "${BUNDLES[@]}"; do
in_array "${bundle}" "${bundles[@]}" || err "Invalid bundle name: ${bundle}"
done
read -r appname apprel appentry \
< <(yq -r '.app | "\(.name) \(.rel) \(.entry)"' <<< "${CONFIGJSON}")
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r image tag abi \
< <(yq -r --arg a "${ARCH}" '.builds[$a] | "\(.image) \(.tag) \(.abi)"' <<< "${CONFIGJSON}")
gitrepo="${GITREPO:-${gitrepo}}"
gitref="${GITREF:-${gitref}}"
# ----
# shellcheck disable=SC2064
TEMP=$(mktemp -d) && trap "rm -rf -- '${TEMP}'" EXIT || exit 255
cd "${TEMP}"
DIR_APPDIR="${TEMP}/AppDir"
DIR_BUNDLES="${TEMP}/bundles"
mkdir -p \
"${DIR_CACHE}" \
"${DIR_DIST}" \
"${DIR_APPDIR}" \
"${DIR_BUNDLES}"
get_docker_image() {
log "Getting docker image"
[[ -n "$(docker image ls -q "${image}")" ]] \
|| docker image pull "${image}"
}
get_sources() {
log "Getting sources"
mkdir -p "${TEMP}/source.git"
pushd "${TEMP}/source.git"
# TODO: re-investigate and optimize this
git clone --depth 1 "${gitrepo}" .
git fetch origin --depth 300 "${gitref}:branch"
git ls-remote --tags --sort=version:refname 2>&- \
| awk "END{printf \"+%s:%s\\n\",\$2,\$2}" \
| git fetch origin --depth=300
git -c advice.detachedHead=false checkout --force branch
git fetch origin --depth=300 --update-shallow
log "Commit information"
git describe --tags --long --dirty
git --no-pager log -1 --pretty=full
popd
}
get_bundles() {
local bundle
for bundle in "${bundles[@]}"; do
in_array "${bundle}" "${BUNDLES[@]}" || continue
local filename url sha256
read -r filename url sha256 \
< <(yq -r --arg a "${ARCH}" --arg b "${bundle}" '.builds[$a].bundles[$b] | "\(.filename) \(.url) \(.sha256)"' <<< "${CONFIGJSON}")
if ! [[ -f "${DIR_CACHE}/${filename}" ]]; then
log "Downloading bundle: ${bundle}"
curl -SLo "${DIR_CACHE}/${filename}" "${url}"
fi
log "Checking bundle: ${bundle}"
sha256sum -c - <<< "${sha256} ${DIR_CACHE}/${filename}"
done
}
prepare_bundles() {
log "Preparing bundles"
local bundle
for bundle in "${bundles[@]}"; do
in_array "${bundle}" "${BUNDLES[@]}" || continue
log "Preparing bundle: ${bundle}"
local type filename sourcedir
read -r type filename sourcedir \
< <(yq -r --arg a "${ARCH}" --arg b "${bundle}" '.builds[$a].bundles[$b] | "\(.type) \(.filename) \(.sourcedir)"' <<< "${CONFIGJSON}")
case "${type}" in
tar)
mkdir -p "${DIR_BUNDLES}/${bundle}"
tar -C "${DIR_BUNDLES}/${bundle}" -xvf "${DIR_CACHE}/${filename}"
sourcedir="${DIR_BUNDLES}/${bundle}/${sourcedir}"
;;
zip)
mkdir -p "${DIR_BUNDLES}/${bundle}"
unzip "${DIR_CACHE}/${filename}" -d "${DIR_BUNDLES}/${bundle}"
sourcedir="${DIR_BUNDLES}/${bundle}/${sourcedir}"
;;
*)
sourcedir="${DIR_CACHE}"
;;
esac
while read -r from to; do
install -vDT "${sourcedir}/${from}" "${DIR_APPDIR}/${to}"
done < <(yq -r --arg a "${ARCH}" --arg b "${bundle}" '.builds[$a].bundles[$b].files[] | "\(.from) \(.to)"' <<< "${CONFIGJSON}")
done
}
prepare_tempdir() {
log "Copying container build files"
cp -vt "${TEMP}" "${SCRIPT_DOCKER}"
log "Building requirements.txt"
yq -r --arg a "${ARCH}" '.builds[$a].dependencies | to_entries | .[] | "\(.key)==\(.value)"' <<< "${CONFIGJSON}" \
> "${TEMP}/requirements.txt"
log "Installing AppDir files"
install -Dm644 -t "${DIR_APPDIR}/usr/share/applications/" "${DIR_APP}/${appname}.desktop"
install -Dm644 -t "${DIR_APPDIR}/usr/share/icons/hicolor/scalable/apps/" "${DIR_APP}/${appname}.svg"
install -Dm644 -t "${DIR_APPDIR}/usr/share/metainfo/" "${DIR_APP}/${appname}.appdata.xml"
ln -sr "${DIR_APPDIR}/usr/share/applications/${appname}.desktop" "${DIR_APPDIR}/${appname}.desktop"
ln -sr "${DIR_APPDIR}/usr/share/icons/hicolor/scalable/apps/${appname}.svg" "${DIR_APPDIR}/${appname}.svg"
ln -sr "${DIR_APPDIR}/usr/share/icons/hicolor/scalable/apps/${appname}.svg" "${DIR_APPDIR}/.DirIcon"
cat > "${DIR_APPDIR}/AppRun" <<EOF
#!/usr/bin/env bash
HERE=\$(dirname -- "\$(readlink -f -- "\$0")")
PYTHON=\$(readlink -f -- "\${HERE}/usr/bin/python")
export PYTHONPATH=\$(realpath -- "\$(dirname -- "\${PYTHON}")/../lib/\$(basename -- "\${PYTHON}")/site-packages")
export SSL_CERT_FILE=\${SSL_CERT_FILE:-"\${PYTHONPATH}/certifi/cacert.pem"}
ARGS=()
if [[ -f "\${HERE}/usr/bin/ffmpeg" ]]; then
ARGS+=(--ffmpeg-ffmpeg "\${HERE}/usr/bin/ffmpeg")
fi
"\${PYTHON}" -m '${appentry}' "\${ARGS[@]}" "\$@"
EOF
chmod +x "${DIR_APPDIR}/AppRun"
}
build_app() {
log "Building app inside container"
local target=/app
local name filename updateinfo
name="$(join + "${appname}" "${BUNDLES[@]}")"
filename="${name}-%s-${apprel}-${abi}-${tag}.AppImage"
updateinfo="${name}-*_${ARCH}.AppImage.zsync"
docker run \
--interactive \
--rm \
--user "$(id -u):$(id -g)" \
--mount "type=bind,source=${TEMP},target=${target}" \
--workdir="${target}" \
--env SOURCE_DATE_EPOCH \
--env FILENAME="${filename}" \
--env GITREF="${GITREF}" \
--env ABI="${abi}" \
--env ENTRY="${appentry}" \
--env UPDATEINFO="${UPDATEINFO:+${GITHUB_REPOSITORY:+gh-releases-zsync|${GITHUB_REPOSITORY/\//|}|latest|${updateinfo}}}" \
"${image}" \
/usr/bin/bash -e "${target}/$(basename -- "${SCRIPT_DOCKER}")"
install -Dm755 -t "${DIR_DIST}" "${TEMP}"/*.AppImage
if [[ -n "${UPDATEINFO}" && -n "${GITHUB_REPOSITORY}" ]]; then
install -m644 -t "${DIR_DIST}" "${TEMP}"/*.zsync
fi
}
build() {
get_docker_image
get_sources
get_bundles
prepare_bundles
prepare_tempdir
build_app
}
build