diff --git a/lib/tools/gen-library.sh b/lib/tools/gen-library.sh new file mode 100755 index 0000000000..11de9946f7 --- /dev/null +++ b/lib/tools/gen-library.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +set -e + +SRC="$( + cd "$(dirname "$0")/../.." + pwd -P +)" +echo "SRC: ${SRC}" + +TARGET_SH="${SRC}/lib/library-functions.sh" + +echo "Writing ${TARGET_SH}" + +cat <<- AUTOGEN_INCLUDES_HEADER > "${TARGET_SH}" + #!/usr/bin/env bash + # This file is/was autogenerated by ${0}; don't modify manually + +AUTOGEN_INCLUDES_HEADER +cd "${SRC}" + +find lib/functions -type f -name \*.sh | sort -h | while read -r path; do + ref="$(echo -n "${path}")" + cat <<- AUTOGEN_INCLUDES_EACH >> "${TARGET_SH}" + # no errors tolerated. invoked before each sourced file to make sure. + #set -o pipefail # trace ERR through pipes - will be enabled "soon" + #set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled + set -o errtrace # trace ERR through - enabled + set -o errexit ## set -e : exit the script if any statement returns a non-true return value - enabled + ### ${path} + # shellcheck source=${ref} + source "\${SRC}"/${path} + + AUTOGEN_INCLUDES_EACH +done + +cat <<- AUTOGEN_INCLUDES_FOOTER >> "${TARGET_SH}" + + # no errors tolerated. one last time for the win! + #set -o pipefail # trace ERR through pipes - will be enabled "soon" + #set -o nounset ## set -u : exit the script if you try to use an uninitialised variable - one day will be enabled + set -o errtrace # trace ERR through - enabled + set -o errexit ## set -e : exit the script if any statement returns a non-true return value - enabled + # This file is/was autogenerated by ${0}; don't modify manually +AUTOGEN_INCLUDES_FOOTER + +echo "done." diff --git a/lib/tools/shellfmt.sh b/lib/tools/shellfmt.sh new file mode 100755 index 0000000000..348da9dbb5 --- /dev/null +++ b/lib/tools/shellfmt.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash + +SHELLFMT_VERSION=${SHELLFMT_VERSION:-3.5.1} # https://github.com/mvdan/sh/releases/ + +SRC="$( + cd "$(dirname "$0")/../.." + pwd -P +)" +echo "SRC: ${SRC}" + +DIR_SHELLFMT="${SRC}/cache/tools/shellfmt" +mkdir -p "${DIR_SHELLFMT}" + +MACHINE="${BASH_VERSINFO[5]}" +case "$MACHINE" in + *darwin*) SHELLFMT_OS="darwin" ;; + *linux*) SHELLFMT_OS="linux" ;; + *) + echo "unknown os: $MACHINE" + exit 3 + ;; +esac + +case "$MACHINE" in + *aarch64*) SHELLFMT_ARCH="arm64" ;; + *x86_64*) SHELLFMT_ARCH="amd64" ;; + *) + echo "unknown arch: $MACHINE" + exit 2 + ;; +esac + +SHELLFMT_FN="shfmt_v${SHELLFMT_VERSION}_${SHELLFMT_OS}_${SHELLFMT_ARCH}" +DOWN_URL="https://github.com/mvdan/sh/releases/download/v${SHELLFMT_VERSION}/${SHELLFMT_FN}" +SHELLFMT_BIN="${DIR_SHELLFMT}/${SHELLFMT_FN}" + +echo "MACHINE: ${MACHINE}" +echo "Down URL: ${DOWN_URL}" +echo "SHELLFMT_BIN: ${SHELLFMT_BIN}" + +if [[ ! -f "${SHELLFMT_BIN}" ]]; then + echo "Cache miss, downloading..." + wget -O "${SHELLFMT_BIN}" "${DOWN_URL}" + chmod +x "${SHELLFMT_BIN}" +fi + +ACTUAL_VERSION="$("${SHELLFMT_BIN}" -version)" +echo "Running shellfmt ${ACTUAL_VERSION}" + +cd "${SRC}" +#"${SHELLFMT_BIN}" --help +#"${SHELLFMT_BIN}" -f "${SRC}" +#"${SHELLFMT_BIN}" -d "${SRC}" + +# Should match the .editorconfig + +declare -a ALL_BASH_FILES=($(find config/sources -type f | grep -e "\.conf\$" -e "\.inc\$") $(find lib -type f | grep -e "\.sh\$" | grep -v -e "^lib\/tools\/") $(find extensions -type f | grep -e "\.sh\$") compile.sh) + +echo "All files:" "${ALL_BASH_FILES[@]}" + +echo "Shellfmt files differing:" +"${SHELLFMT_BIN}" -l "${ALL_BASH_FILES[@]}" | sort -h # list all formatted files + +#echo "Diff with current:" +# "${SHELLFMT_BIN}" -d "${ALL_BASH_FILES[@]}" # list files that have different formatting than they should + +echo "Doing for real:" +"${SHELLFMT_BIN}" -w "${ALL_BASH_FILES[@]}"