From 02c561ce0ecdb6443723d269983b13f1eb49a982 Mon Sep 17 00:00:00 2001 From: Ricardo Pardini Date: Tue, 28 Mar 2023 14:34:27 +0200 Subject: [PATCH] bat-cat: introduce tooling support for `bat` (colorized/smart `cat`), including DEBIAN/xxx syntaxes; cached in Docker image --- lib/functions/cli/cli-requirements.sh | 1 + .../compilation/packages/utils-dpkgdeb.sh | 8 + lib/functions/general/bat-cat.sh | 137 ++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 lib/functions/general/bat-cat.sh diff --git a/lib/functions/cli/cli-requirements.sh b/lib/functions/cli/cli-requirements.sh index 4a40101841..0d41f9c0d3 100644 --- a/lib/functions/cli/cli-requirements.sh +++ b/lib/functions/cli/cli-requirements.sh @@ -41,6 +41,7 @@ function cli_requirements_run() { # We need to change the deployment directory to something not in ./cache, so it's baked into the image. deploy_to_non_cache_dir="yes" run_tool_oras # download-only, to non-cache dir. deploy_to_non_cache_dir="yes" run_tool_shellcheck # download-only, to non-cache dir. + deploy_to_non_cache_dir="yes" run_tool_batcat # download-only, to non-cache dir. # @TODO: shfmt fi diff --git a/lib/functions/compilation/packages/utils-dpkgdeb.sh b/lib/functions/compilation/packages/utils-dpkgdeb.sh index d5ea52a2cc..db24b7449d 100644 --- a/lib/functions/compilation/packages/utils-dpkgdeb.sh +++ b/lib/functions/compilation/packages/utils-dpkgdeb.sh @@ -24,12 +24,20 @@ function fakeroot_dpkg_deb_build() { exit_with_error "fakeroot_dpkg_deb_build: can't find source package directory: ${first_arg}" fi + # Get the basename of the dir + declare pkg_name + pkg_name=$(basename "${first_arg}") + # Show the total human size of the source package directory. display_alert "Source package size" "${first_arg}: $(du -sh "${first_arg}" | cut -f1)" "debug" # find the DEBIAN scripts (postinst, prerm, etc) and run shellcheck on them. dpkg_deb_run_shellcheck_on_scripts "${first_arg}" + # Dump the CONTROL file to the log. + display_alert "Showing DEBIAN/control file" "${pkg_name}/DEBIAN/control" "info" + run_tool_batcat --file-name "${pkg_name}/DEBIAN/control" "${first_arg}/DEBIAN/control" + run_host_command_logged_raw fakeroot dpkg-deb -b "-Z${DEB_COMPRESS}" "${orig_args[@]}" } diff --git a/lib/functions/general/bat-cat.sh b/lib/functions/general/bat-cat.sh new file mode 100644 index 0000000000..ca6a7558a0 --- /dev/null +++ b/lib/functions/general/bat-cat.sh @@ -0,0 +1,137 @@ +#!/usr/bin/env bash +# +# SPDX-License-Identifier: GPL-2.0 +# +# Copyright (c) 2013-2023 Igor Pecovnik, igor@armbian.com +# +# This file is a part of the Armbian Build Framework +# https://github.com/armbian/build/ + +function run_tool_batcat() { + # Default version + BATCAT_VERSION=${BATCAT_VERSION:-0.23.0} # https://github.com/sharkdp/bat/releases + + declare non_cache_dir="/armbian-tools/batcat" # To deploy/reuse cached batcat in a Docker image. + + if [[ -z "${DIR_BATCAT}" ]]; then + display_alert "DIR_BATCAT is not set, using default" "batcat" "debug" + + if [[ "${deploy_to_non_cache_dir:-"no"}" == "yes" ]]; then + DIR_BATCAT="${non_cache_dir}" # root directory. + display_alert "Deploying batcat to non-cache dir" "DIR_BATCAT: ${DIR_BATCAT}" "debug" + else + if [[ -n "${SRC}" ]]; then + DIR_BATCAT="${SRC}/cache/tools/batcat" + else + display_alert "Missing DIR_BATCAT, or SRC fallback" "DIR_BATCAT: ${DIR_BATCAT}; SRC: ${SRC}" "batcat" "err" + return 1 + fi + fi + else + display_alert "DIR_BATCAT is set to ${DIR_BATCAT}" "batcat" "debug" + fi + + mkdir -p "${DIR_BATCAT}" + + declare MACHINE="${BASH_VERSINFO[5]}" BATCAT_OS BATCAT_ARCH + display_alert "Running batcat" "batcat version ${BATCAT_VERSION}" "debug" + MACHINE="${BASH_VERSINFO[5]}" + + case "$MACHINE" in + *x86_64-*-linux-gnu*) BATCAT_ARCH_OS="x86_64-unknown-linux-gnu" ;; + *aarch64-*-linux-gnu*) BATCAT_ARCH_OS="aarch64-unknown-linux-gnu" ;; + *x86_64-apple-darwin*) BATCAT_ARCH_OS="x86_64-apple-darwin" ;; + *) + exit_with_error "unknown os/arch for batcat download: '$MACHINE'" + ;; + esac + + # linux amd64 https://github.com/sharkdp/bat/releases/download/v0.23.0/bat-v0.23.0-x86_64-unknown-linux-gnu.tar.gz + # linux aarch64 https://github.com/sharkdp/bat/releases/download/v0.23.0/bat-v0.23.0-aarch64-unknown-linux-gnu.tar.gz + # linux armhf https://github.com/sharkdp/bat/releases/download/v0.23.0/bat-v0.23.0-arm-unknown-linux-gnueabihf.tar.gz + # darwin amd64 https://github.com/sharkdp/bat/releases/download/v0.23.0/bat-v0.23.0-x86_64-apple-darwin.tar.gz + # darwin arm64 + + declare BATCAT_FN="bat-v${BATCAT_VERSION}-${BATCAT_ARCH_OS}" + declare BATCAT_FN_TARXZ="${BATCAT_FN}.tar.gz" + declare DOWN_URL="https://github.com/sharkdp/bat/releases/download/v${BATCAT_VERSION}/${BATCAT_FN_TARXZ}" + declare BATCAT_BIN="${DIR_BATCAT}/${BATCAT_FN}-bin" + declare ACTUAL_VERSION + + # Check if we have a cached version in a Docker image, and copy it over before possibly updating it. + if [[ "${deploy_to_non_cache_dir:-"no"}" != "yes" && -d "${non_cache_dir}" && ! -f "${BATCAT_BIN}" ]]; then + display_alert "Using cached batcat from Docker image" "batcat" "debug" + run_host_command_logged cp -rv "${non_cache_dir}/"* "${DIR_BATCAT}/" + fi + + if [[ ! -f "${BATCAT_BIN}" ]]; then + # @TODO: do_with_retries 5 + try_download_batcat_tooling + fi + + # set pipefail in the subshell, so grep does not hide the actual command's result + ACTUAL_VERSION="$(set -o pipefail && "${BATCAT_BIN}" --version | grep "^bat" | xargs echo -n)" + display_alert "Running batcat ${ACTUAL_VERSION}" "batcat" "debug" + + # If no 'syntaxes.bin' in the cache dir, prepare it... + if [[ ! -f "${DIR_BATCAT}/cache/syntaxes.bin" ]]; then + display_alert "Preparing batcat cache" "batcat" "debug" + # Problem: "${SRC}/cache" might exist, and confuses batcat into thinking it's a file argument, instead of the "cache" command. + # Workaround: use a subshell to cd into "${SRC}/.tmp" and run from there. + run_host_command_logged cd "${SRC}/.tmp" ";" BAT_CONFIG_DIR="${DIR_BATCAT}/config" BAT_CACHE_PATH="${DIR_BATCAT}/cache" "${BATCAT_BIN}" cache --build + fi + + if [[ "${deploy_to_non_cache_dir:-"no"}" == "yes" ]]; then + display_alert "Deployed batcat to non-cache dir" "DIR_BATCAT: ${DIR_BATCAT}" "debug" + return 0 # don't actually execute. + fi + + # If any parameters passed, call ORAS, otherwise exit. We call it this way (sans-parameters) early to prepare ORAS tooling. + if [[ $# -eq 0 ]]; then + display_alert "No parameters passed to batcat" "batcat" "debug" + return 0 + fi + + declare -i bat_cat_columns=$(("${COLUMNS:-"120"}" - 20)) # A bit shorter since might be prefixed by emoji etc + if [[ "${bat_cat_columns}" -lt 60 ]]; then # but lever less than 60 + bat_cat_columns=60 + fi + display_alert "Calling batcat" "COLUMNS: ${bat_cat_columns} | $*" "debug" + BAT_CONFIG_DIR="${DIR_BATCAT}/config" BAT_CACHE_PATH="${DIR_BATCAT}/cache" "${BATCAT_BIN}" --theme "Dracula" --paging=never --force-colorization --wrap auto --terminal-width "${bat_cat_columns}" "$@" + wait_for_disk_sync "after running batcat" +} + +function try_download_batcat_tooling() { + display_alert "MACHINE: ${MACHINE}" "batcat" "debug" + display_alert "Down URL: ${DOWN_URL}" "batcat" "debug" + display_alert "BATCAT_BIN: ${BATCAT_BIN}" "batcat" "debug" + display_alert "BATCAT_FN: ${BATCAT_FN}" "batcat" "debug" + + display_alert "Downloading required" "batcat tooling${RETRY_FMT_MORE_THAN_ONCE}" "info" + run_host_command_logged wget --no-verbose --progress=dot:giga -O "${BATCAT_BIN}.tar.gz.tmp" "${DOWN_URL}" || { + return 1 + } + run_host_command_logged mv "${BATCAT_BIN}.tar.gz.tmp" "${BATCAT_BIN}.tar.gz" + run_host_command_logged tar -xf "${BATCAT_BIN}.tar.gz" -C "${DIR_BATCAT}" "${BATCAT_FN}/bat" + run_host_command_logged rm -rf "${BATCAT_BIN}.tar.gz" + + # EXTRA: get more syntaxes for batcat. We need Debian syntax for CONTROL files, etc. + run_host_command_logged wget --no-verbose --progress=dot:giga -O "${DIR_BATCAT}/sublime-debian.tar.gz.tmp" "https://github.com/barnumbirr/sublime-debian/archive/refs/heads/master.tar.gz" + run_host_command_logged mkdir -p "${DIR_BATCAT}/temp-debian-syntax" + run_host_command_logged tar -xzf "${DIR_BATCAT}/sublime-debian.tar.gz.tmp" -C "${DIR_BATCAT}/temp-debian-syntax" sublime-debian-master/Syntaxes + + # Prepare the config and cache dir... clean it off and begin anew everytime + run_host_command_logged rm -rf "${DIR_BATCAT}/config" "${DIR_BATCAT}/cache" + run_host_command_logged mkdir -p "${DIR_BATCAT}/config" "${DIR_BATCAT}/cache" "${DIR_BATCAT}/config/syntaxes" + + # Move the sublime-debian syntaxes into the final syntaxes dir + run_host_command_logged mv "${DIR_BATCAT}/temp-debian-syntax/sublime-debian-master/Syntaxes"/* "${DIR_BATCAT}/config/syntaxes/" + + # Delete the temps for sublime-debian + run_host_command_logged rm -rf "${DIR_BATCAT}/temp-debian-syntax" "${DIR_BATCAT}/sublime-debian.tar.gz.tmp" + + # Finish up, mark done. + run_host_command_logged mv "${DIR_BATCAT}/${BATCAT_FN}/bat" "${BATCAT_BIN}" + run_host_command_logged rm -rf "${DIR_BATCAT}/${BATCAT_FN}" + run_host_command_logged chmod +x -v "${BATCAT_BIN}" +}