1#!/usr/bin/env bash 2# shellcheck disable=SC2048 3# shellcheck disable=SC2086 # we want word splitting 4# shellcheck disable=SC2155 # mktemp usually not failing 5 6shopt -s expand_aliases 7 8function _x_store_state { 9 if [[ "$-" == *"x"* ]]; then 10 previous_state_x=1 11 else 12 previous_state_x=0 13 fi 14} 15_x_store_state 16alias x_store_state='{ _x_store_state; } >/dev/null 2>/dev/null' 17 18function _x_off { 19 x_store_state 20 set +x 21} 22alias x_off='{ _x_off; } >/dev/null 2>/dev/null' 23 24function _x_restore { 25 [ $previous_state_x -eq 0 ] || set -x 26} 27alias x_restore='{ _x_restore; } >/dev/null 2>/dev/null' 28 29export JOB_START_S=$(date -u +"%s" -d "${CI_JOB_STARTED_AT:?}") 30 31function get_current_minsec { 32 DATE_S=$(date -u +"%s") 33 CURR_TIME=$((DATE_S-JOB_START_S)) 34 printf "%02d:%02d" $((CURR_TIME/60)) $((CURR_TIME%60)) 35} 36 37function _build_section_start { 38 local section_params=$1 39 shift 40 local section_name=$1 41 CURRENT_SECTION=$section_name 42 shift 43 CYAN="\e[0;36m" 44 ENDCOLOR="\e[0m" 45 46 CURR_MINSEC=$(get_current_minsec) 47 echo -e "\n\e[0Ksection_start:$(date +%s):$section_name$section_params\r\e[0K${CYAN}[${CURR_MINSEC}] $*${ENDCOLOR}\n" 48 x_restore 49} 50alias build_section_start="x_off; _build_section_start" 51 52function _section_start { 53 build_section_start "[collapsed=true]" $* 54 x_restore 55} 56alias section_start="x_off; _section_start" 57 58function _uncollapsed_section_start { 59 build_section_start "" $* 60 x_restore 61} 62alias uncollapsed_section_start="x_off; _uncollapsed_section_start" 63 64function _build_section_end { 65 echo -e "\e[0Ksection_end:$(date +%s):$1\r\e[0K" 66 CURRENT_SECTION="" 67 x_restore 68} 69alias build_section_end="x_off; _build_section_end" 70 71function _section_end { 72 build_section_end $* 73 x_restore 74} 75alias section_end="x_off; _section_end" 76 77function _section_switch { 78 if [ -n "$CURRENT_SECTION" ] 79 then 80 build_section_end $CURRENT_SECTION 81 x_off 82 fi 83 build_section_start "[collapsed=true]" $* 84 x_restore 85} 86alias section_switch="x_off; _section_switch" 87 88function _uncollapsed_section_switch { 89 if [ -n "$CURRENT_SECTION" ] 90 then 91 build_section_end $CURRENT_SECTION 92 x_off 93 fi 94 build_section_start "" $* 95 x_restore 96} 97alias uncollapsed_section_switch="x_off; _uncollapsed_section_switch" 98 99export -f _x_store_state 100export -f _x_off 101export -f _x_restore 102export -f get_current_minsec 103export -f _build_section_start 104export -f _section_start 105export -f _build_section_end 106export -f _section_end 107export -f _section_switch 108export -f _uncollapsed_section_switch 109 110# Freedesktop requirement (needed for Wayland) 111[ -n "${XDG_RUNTIME_DIR:-}" ] || export XDG_RUNTIME_DIR="$(mktemp -p "$PWD" -d xdg-runtime-XXXXXX)" 112 113if [ -z "${RESULTS_DIR:-}" ]; then 114 export RESULTS_DIR="${PWD%/}/results" 115 if [ -e "${RESULTS_DIR}" ]; then 116 rm -rf "${RESULTS_DIR}" 117 fi 118 mkdir -p "${RESULTS_DIR}" 119fi 120 121function error { 122 RED="\e[0;31m" 123 ENDCOLOR="\e[0m" 124 # we force the following to be not in a section 125 if [ -n "${CURRENT_SECTION:-}" ]; then 126 section_end $CURRENT_SECTION 127 x_off 128 fi 129 130 CURR_MINSEC=$(get_current_minsec) 131 echo -e "\n${RED}[${CURR_MINSEC}] ERROR: $*${ENDCOLOR}\n" 132 x_restore 133} 134 135function trap_err { 136 x_off 137 error ${CURRENT_SECTION:-'unknown-section'}: ret code: $* 138} 139 140export -f error 141export -f trap_err 142 143set -E 144trap 'trap_err $?' ERR 145