Skip to content

Commit

Permalink
build: misc cleanups
Browse files Browse the repository at this point in the history
- Set default build name if missing
- Explicitly set portable build config argument
- Set BASH "unset" flag for variable expansions and update expansions
- Fix various shellcheck issues
- Remove unused vars
  • Loading branch information
bastimeyer committed Oct 12, 2024
1 parent f9140db commit 8ca5d06
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 32 deletions.
34 changes: 20 additions & 14 deletions build-installer.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
set -eo pipefail
set -euo pipefail

BUILDNAME="${1}"
GITREPO="${2}"
GITREF="${3}"
BUILDNAME="${1:-}"
GITREPO="${2:-}"
GITREF="${3:-}"

declare -A DEPS=(
[convert]=Imagemagick
Expand Down Expand Up @@ -38,26 +38,29 @@ DIR_FILES="${ROOT}/files"

SELF=$(basename "$(readlink -f "${0}")")
log() {
echo "[${SELF}] ${@}"
echo "[${SELF}]" "${@}"
}
err() {
log >&2 "${@}"
exit 1
}

[[ "${CI}" ]] || [[ "${VIRTUAL_ENV}" ]] || err "Can only be built in a virtual environment"
[[ "${CI:-}" ]] || [[ "${VIRTUAL_ENV:-}" ]] || err "Can only be built in a virtual environment"

for dep in "${!DEPS[@]}"; do
command -v "${dep}" 2>&1 >/dev/null || err "${DEPS["${dep}"]} is required to build the installer. Aborting."
command -v "${dep}" >/dev/null 2>&1 || err "${DEPS["${dep}"]} is required to build the installer. Aborting."
done

[[ -f "${CONFIG}" ]] \
|| err "Missing config file: ${CONFIG}"
CONFIGJSON=$(cat "${CONFIG}")

[[ -n "${BUILDNAME}" ]] \
&& yq -e ".builds[\"${BUILDNAME}\"]" 2>&1 >/dev/null <<< "${CONFIGJSON}" \
|| err "Invalid build name"
if [[ -n "${BUILDNAME}" ]]; then
yq -e ".builds[\"${BUILDNAME}\"]" >/dev/null 2>&1 <<< "${CONFIGJSON}" \
|| err "Invalid build name"
else
BUILDNAME=$(yq -r '.builds | keys | first' <<< "${CONFIGJSON}")
fi

read -r appname apprel \
< <(yq -r '.app | "\(.name) \(.rel)"' <<< "${CONFIGJSON}")
Expand All @@ -75,6 +78,7 @@ gitref="${GITREF:-${gitref}}"
# ----


# shellcheck disable=SC2064
TEMP=$(mktemp -d) && trap "rm -rf '${TEMP}'" EXIT || exit 255

DIR_REPO="${TEMP}/source.git"
Expand Down Expand Up @@ -102,7 +106,8 @@ get_sources() {
"${DIR_REPO}"

log "Commit information"
GIT_PAGER=cat git \
git \
-c core.pager=cat \
-C "${DIR_REPO}" \
log \
-1 \
Expand Down Expand Up @@ -149,7 +154,7 @@ build_app() {
"${DIR_REPO}"

log "Removing unneeded dist files"
( set -x; rm -r "${DIR_PKGS}/bin" "${DIR_PKGS}"/*.dist-info/direct_url.json; )
( set -x; rm -r "${DIR_PKGS:?}/bin" "${DIR_PKGS}"/*.dist-info/direct_url.json; )
sed -i -E \
-e '/^.+\.dist-info\/direct_url\.json,sha256=/d' \
-e '/^\.\.\/\.\.\//d' \
Expand Down Expand Up @@ -244,9 +249,8 @@ prepare_installer() {
# Custom ref -> tagged release (no build metadata in version string)
# Add abbreviated commit ID to the plain version string to distinguish it from regular releases, set 0 as app release number
elif [[ "${versionstring}" != *+* ]]; then
local _commit="$(cd "${DIR_REPO}" && git -c core.abbrev=7 rev-parse --short HEAD)"
vi_version="${versionplain}.0"
installerversion="${versionplain}-0-g${_commit}"
installerversion="${versionplain}-0-g$(git -c core.abbrev=7 -C "${DIR_REPO}" rev-parse --short HEAD)"

# Custom ref -> arbitrary untagged commit (version string includes build metadata)
# Translate into correct format
Expand All @@ -256,6 +260,7 @@ prepare_installer() {
fi

log "Preparing installer template"
# shellcheck disable=SC2016
env -i \
DIR_BUILD="${DIR_BUILD}" \
VERSION="${installerversion}" \
Expand All @@ -265,6 +270,7 @@ prepare_installer() {
> "${DIR_BUILD}/installer.nsi"

log "Preparing pynsist config"
# shellcheck disable=SC2016
env -i \
DIR_BUILD="${DIR_BUILD}" \
DIR_WHEELS="${DIR_WHEELS}" \
Expand Down
39 changes: 21 additions & 18 deletions build-portable.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env bash
set -eo pipefail
set -euo pipefail

BUILDNAME="${1}"
GITREPO="${2}"
GITREF="${3}"
BUILDNAME="${1:-}"
GITREPO="${2:-}"
GITREF="${3:-}"

declare -A DEPS=(
[curl]=curl
Expand All @@ -26,42 +26,44 @@ CONFIG="${ROOT}/config.yml"
CONFIG_PORTABLE="${ROOT}/portable.yml"
DIR_CACHE="${ROOT}/cache"
DIR_DIST="${ROOT}/dist"
DIR_FILES="${ROOT}/files"


# ----


SELF=$(basename "$(readlink -f "${0}")")
log() {
echo "[${SELF}] ${@}"
echo "[${SELF}]" "${@}"
}
err() {
log >&2 "${@}"
exit 1
}

[[ "${CI}" ]] || [[ "${VIRTUAL_ENV}" ]] || err "Can only be built in a virtual environment"
[[ "${CI:-}" ]] || [[ "${VIRTUAL_ENV:-}" ]] || err "Can only be built in a virtual environment"

for dep in "${!DEPS[@]}"; do
command -v "${dep}" 2>&1 >/dev/null || err "${DEPS["${dep}"]} is required to build the installer. Aborting."
command -v "${dep}" >/dev/null 2>&1 || err "${DEPS["${dep}"]} is required to build the installer. Aborting."
done

[[ -f "${CONFIG}" ]] \
|| err "Missing config file: ${CONFIG}"
CONFIGJSON=$(cat "${CONFIG}")

[[ -n "${BUILDNAME}" ]] \
&& yq -e ".builds[\"${BUILDNAME}\"]" 2>&1 >/dev/null <<< "${CONFIGJSON}" \
|| err "Invalid build name"
if [[ -n "${BUILDNAME}" ]]; then
yq -e ".builds[\"${BUILDNAME}\"]" >/dev/null 2>&1 <<< "${CONFIGJSON}" \
|| err "Invalid build name"
else
BUILDNAME=$(yq -r '.builds | keys | first' <<< "${CONFIGJSON}")
fi

read -r appname apprel \
< <(yq -r '.app | "\(.name) \(.rel)"' <<< "${CONFIGJSON}")
read -r gitrepo gitref \
< <(yq -r '.git | "\(.repo) \(.ref)"' <<< "${CONFIGJSON}")
read -r implementation pythonversion platform \
< <(yq -r ".builds[\"${BUILDNAME}\"] | \"\(.implementation) \(.pythonversion) \(.platform)\"" <<< "${CONFIGJSON}")
read -r pythonversionfull pythonfilename pythonurl pythonsha256 \
read -r _pythonversionfull pythonfilename pythonurl pythonsha256 \
< <(yq -r ".builds[\"${BUILDNAME}\"].pythonembed | \"\(.version) \(.filename) \(.url) \(.sha256)\"" <<< "${CONFIGJSON}")

gitrepo="${GITREPO:-${gitrepo}}"
Expand All @@ -71,6 +73,7 @@ gitref="${GITREF:-${gitref}}"
# ----


# shellcheck disable=SC2064
TEMP=$(mktemp -d) && trap "rm -rf '${TEMP}'" EXIT || exit 255

DIR_REPO="${TEMP}/source.git"
Expand Down Expand Up @@ -99,7 +102,8 @@ get_sources() {
"${DIR_REPO}"

log "Commit information"
GIT_PAGER=cat git \
git \
-c core.pager=cat \
-C "${DIR_REPO}" \
log \
-1 \
Expand Down Expand Up @@ -167,7 +171,7 @@ prepare_files() {

prepare_executables() {
log "Preparing executables"
TZ=UTC python ./build-portable-commands.py --target="${DIR_BIN}"
TZ=UTC python ./build-portable-commands.py --config="${CONFIG_PORTABLE}" --target="${DIR_BIN}"
}

install_pkgs() {
Expand Down Expand Up @@ -199,7 +203,7 @@ install_pkgs() {
"${DIR_REPO}"

log "Removing unneeded dist files"
( set -x; rm -r "${DIR_PKGS}/bin" "${DIR_PKGS}"/*.dist-info/direct_url.json; )
( set -x; rm -r "${DIR_PKGS:?}/bin" "${DIR_PKGS}"/*.dist-info/direct_url.json; )
sed -i -E \
-e '/^.+\.dist-info\/direct_url\.json,sha256=/d' \
-e '/^\.\.\/\.\.\//d' \
Expand All @@ -222,8 +226,7 @@ build_portable() {
# Custom ref -> tagged release (no build metadata in version string)
# Add abbreviated commit ID to the plain version string to distinguish it from regular releases, set 0 as app release number
elif [[ "${versionstring}" != *+* ]]; then
local _commit="$(cd "${DIR_REPO}" && git -c core.abbrev=7 rev-parse --short HEAD)"
version="${versionplain}-0-g${_commit}"
version="${versionplain}-0-g$(git -c core.abbrev=7 -C "${DIR_REPO}" rev-parse --short HEAD)"

# Custom ref -> arbitrary untagged commit (version string includes build metadata)
# Translate into correct format
Expand All @@ -233,7 +236,7 @@ build_portable() {

log "Updating modification times"
local mtime
[[ "${SOURCE_DATE_EPOCH}" ]] && mtime="@${SOURCE_DATE_EPOCH}" || mtime=now
[[ "${SOURCE_DATE_EPOCH:-}" ]] && mtime="@${SOURCE_DATE_EPOCH}" || mtime=now
find "${DIR_BUILD}" -exec touch --no-dereference "--date=${mtime}" '{}' '+'

log "Packaging portable build"
Expand Down

0 comments on commit 8ca5d06

Please sign in to comment.