Skip to content

Commit

Permalink
build: refactor build scripts
Browse files Browse the repository at this point in the history
Use jq/yq --arg parameter for user input data
  • Loading branch information
bastimeyer committed Dec 11, 2024
1 parent a715bda commit dc18e89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
20 changes: 11 additions & 9 deletions build-installer.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# shellcheck disable=SC2016

set -euo pipefail

BUILDNAME="${1:-}"
Expand Down Expand Up @@ -56,7 +58,7 @@ done
CONFIGJSON=$(cat "${CONFIG}")

if [[ -n "${BUILDNAME}" ]]; then
yq -e ".builds[\"${BUILDNAME}\"]" >/dev/null 2>&1 <<< "${CONFIGJSON}" \
yq -e --arg b "${BUILDNAME}" '.builds[$b]' >/dev/null 2>&1 <<< "${CONFIGJSON}" \
|| err "Invalid build name"
else
BUILDNAME=$(yq -r '.builds | keys | first' <<< "${CONFIGJSON}")
Expand All @@ -67,9 +69,9 @@ read -r appname apprel \
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r implementation pythonversion platform \
< <(yq -r ".builds[\"${BUILDNAME}\"] | \"\(.implementation) \(.pythonversion) \(.platform)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b] | "\(.implementation) \(.pythonversion) \(.platform)"' <<< "${CONFIGJSON}")
read -r pythonversionfull pythonfilename pythonurl pythonsha256 \
< <(yq -r ".builds[\"${BUILDNAME}\"].pythonembed | \"\(.version) \(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b].pythonembed | "\(.version) \(.filename) \(.url) \(.sha256)"' <<< "${CONFIGJSON}")

gitrepo="${GITREPO:-${gitrepo}}"
gitref="${GITREF:-${gitref}}"
Expand Down Expand Up @@ -130,14 +132,14 @@ get_assets() {
while read -r assetname; do
local filename url sha256
read -r filename url sha256 \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg a "${assetname}" '.assets[$a] | "\(.filename) \(.url) \(.sha256)"' <<< "${CONFIGJSON}")
if ! [[ -f "${DIR_CACHE}/${filename}" ]]; then
log "Downloading asset: ${assetname}"
curl -SLo "${DIR_CACHE}/${filename}" "${url}"
fi
log "Checking asset: ${assetname}"
sha256sum -c - <<< "${sha256} ${DIR_CACHE}/${filename}"
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
done < <(yq -r --arg b "${BUILDNAME}" '.builds[$b].assets[]' <<< "${CONFIGJSON}")
}

build_app() {
Expand Down Expand Up @@ -188,7 +190,7 @@ download_wheels() {
--implementation="${implementation}" \
--dest="${DIR_WHEELS}" \
--requirement=/dev/stdin \
< <(yq -r ".builds[\"${BUILDNAME}\"].dependencies | to_entries[] | \"\(.key)==\(.value)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b].dependencies | to_entries[] | "\(.key)==\(.value)"' <<< "${CONFIGJSON}")
}

prepare_python() {
Expand All @@ -205,7 +207,7 @@ prepare_assets() {
log "Preparing asset: ${assetname}"
local type filename sourcedir targetdir
read -r type filename sourcedir targetdir \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.type) \(.filename) \(.sourcedir) \(.targetdir)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg a "${assetname}" '.assets[$a] | "\(.type) \(.filename) \(.sourcedir) \(.targetdir)"' <<< "${CONFIGJSON}")
case "${type}" in
zip)
mkdir -p "${DIR_ASSETS}/${assetname}"
Expand All @@ -218,8 +220,8 @@ prepare_assets() {
esac
while read -r from to; do
install -vDT "${sourcedir}/${from}" "${DIR_BUILD}/${targetdir}/${to}"
done < <(yq -r ".assets[\"${assetname}\"].files[] | \"\(.from) \(.to)\"" <<< "${CONFIGJSON}")
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
done < <(yq -r --arg a "${assetname}" '.assets[$a].files[] | "\(.from) \(.to)"' <<< "${CONFIGJSON}")
done < <(yq -r --arg b "${BUILDNAME}" '.builds[$b].assets[]' <<< "${CONFIGJSON}")
}

prepare_files() {
Expand Down
20 changes: 11 additions & 9 deletions build-portable.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/env bash
# shellcheck disable=SC2016

set -euo pipefail

BUILDNAME="${1:-}"
Expand Down Expand Up @@ -51,7 +53,7 @@ done
CONFIGJSON=$(cat "${CONFIG}")

if [[ -n "${BUILDNAME}" ]]; then
yq -e ".builds[\"${BUILDNAME}\"]" >/dev/null 2>&1 <<< "${CONFIGJSON}" \
yq -e --arg b "${BUILDNAME}" '.builds[$b]' >/dev/null 2>&1 <<< "${CONFIGJSON}" \
|| err "Invalid build name"
else
BUILDNAME=$(yq -r '.builds | keys | first' <<< "${CONFIGJSON}")
Expand All @@ -62,9 +64,9 @@ read -r appname apprel \
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r implementation pythonversion platform \
< <(yq -r ".builds[\"${BUILDNAME}\"] | \"\(.implementation) \(.pythonversion) \(.platform)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b] | "\(.implementation) \(.pythonversion) \(.platform)"' <<< "${CONFIGJSON}")
read -r _pythonversionfull pythonfilename pythonurl pythonsha256 \
< <(yq -r ".builds[\"${BUILDNAME}\"].pythonembed | \"\(.version) \(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b].pythonembed | "\(.version) \(.filename) \(.url) \(.sha256)"' <<< "${CONFIGJSON}")

gitrepo="${GITREPO:-${gitrepo}}"
gitref="${GITREF:-${gitref}}"
Expand Down Expand Up @@ -126,14 +128,14 @@ get_assets() {
while read -r assetname; do
local filename url sha256
read -r filename url sha256 \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg a "${assetname}" '.assets[$a] | "\(.filename) \(.url) \(.sha256)"' <<< "${CONFIGJSON}")
if ! [[ -f "${DIR_CACHE}/${filename}" ]]; then
log "Downloading asset: ${assetname}"
curl -SLo "${DIR_CACHE}/${filename}" "${url}"
fi
log "Checking asset: ${assetname}"
sha256sum -c - <<< "${sha256} ${DIR_CACHE}/${filename}"
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
done < <(yq -r --arg b "${BUILDNAME}" '.builds[$b].assets[]' <<< "${CONFIGJSON}")
}

prepare_python() {
Expand All @@ -148,7 +150,7 @@ prepare_assets() {
log "Preparing asset: ${assetname}"
local type filename sourcedir targetdir
read -r type filename sourcedir targetdir \
< <(yq -r ".assets[\"${assetname}\"] | \"\(.type) \(.filename) \(.sourcedir) \(.targetdir)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg a "${assetname}" '.assets[$a] | "\(.type) \(.filename) \(.sourcedir) \(.targetdir)"' <<< "${CONFIGJSON}")
case "${type}" in
zip)
mkdir -p "${DIR_ASSETS}/${assetname}"
Expand All @@ -161,8 +163,8 @@ prepare_assets() {
esac
while read -r from to; do
install -vDT "${sourcedir}/${from}" "${DIR_BUILD}/${targetdir}/${to}"
done < <(yq -r ".assets[\"${assetname}\"].files[] | \"\(.from) \(.to)\"" <<< "${CONFIGJSON}")
done < <(yq -r ".builds[\"${BUILDNAME}\"].assets[]" <<< "${CONFIGJSON}")
done < <(yq -r --arg a "${assetname}" '.assets[$a].files[] | "\(.from) \(.to)"' <<< "${CONFIGJSON}")
done < <(yq -r --arg b "${BUILDNAME}" '.builds[$b].assets[]' <<< "${CONFIGJSON}")
}

prepare_files() {
Expand All @@ -188,7 +190,7 @@ install_pkgs() {
--target="${DIR_PKGS}" \
--no-compile \
--requirement=/dev/stdin \
< <(yq -r ".builds[\"${BUILDNAME}\"].dependencies | to_entries[] | \"\(.key)==\(.value)\"" <<< "${CONFIGJSON}")
< <(yq -r --arg b "${BUILDNAME}" '.builds[$b].dependencies | to_entries[] | "\(.key)==\(.value)"' <<< "${CONFIGJSON}")

log "Installing app"
pip install \
Expand Down

0 comments on commit dc18e89

Please sign in to comment.