From 0df3d4d15b1522f2ad0e85c2237acea2ac945f61 Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sun, 20 Nov 2022 02:09:32 -0800 Subject: [PATCH] ci: Many fixes and updates --- Makefile | 3 ++ ci/release/build.sh | 10 +++-- ci/release/build_docker.sh | 1 - ci/release/gen_install.sh | 4 ++ ci/release/gen_sh.sh | 11 ++++++ ci/release/gen_template_lib.sh | 4 ++ ci/release/template/scripts/lib.sh | 59 +++++++++++++++++++----------- install.sh | 59 +++++++++++++++++++----------- 8 files changed, 104 insertions(+), 47 deletions(-) create mode 100755 ci/release/gen_sh.sh diff --git a/Makefile b/Makefile index 46577c730..a1c404ed4 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,9 @@ endif .PHONY: fmt fmt: prefix "$@" ./ci/sub/fmt/make.sh +.PHONY: fmt-release +fmt-release_sh: + prefix "$@" ./ci/release/gen_sh.sh .PHONY: lint lint: prefix "$@" go vet --composites=false ./... diff --git a/ci/release/build.sh b/ci/release/build.sh index 04cdc7217..591dbd320 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -167,13 +167,14 @@ build_remote_macos() { trap unlockfile_ssh EXIT sh_c ssh "$REMOTE_HOST" mkdir -p src sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/" - sh_c ssh "$REMOTE_HOST" "DRY_RUN=${DRY_RUN-} \ + sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \ +TERM=${TERM-} \ +DRY_RUN=${DRY_RUN-} \ HW_BUILD_DIR=$HW_BUILD_DIR \ VERSION=$VERSION \ OS=$OS \ ARCH=$ARCH \ ARCHIVE=$ARCHIVE \ -TERM=$TERM \ PATH=\\\"/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin\\\${PATH+:\\\$PATH}\\\" \ ./src/d2/ci/release/_build.sh" sh_c mkdir -p "$HW_BUILD_DIR" @@ -185,13 +186,14 @@ build_remote_linux() { trap unlockfile_ssh EXIT sh_c ssh "$REMOTE_HOST" mkdir -p src sh_c rsync --archive --human-readable --delete ./ "$REMOTE_HOST:src/d2/" - sh_c ssh "$REMOTE_HOST" "DRY_RUN=${DRY_RUN-} \ + sh_c ssh "$REMOTE_HOST" "COLOR=${COLOR-} \ +TERM=${TERM-} \ +DRY_RUN=${DRY_RUN-} \ HW_BUILD_DIR=$HW_BUILD_DIR \ VERSION=$VERSION \ OS=$OS \ ARCH=$ARCH \ ARCHIVE=$ARCHIVE \ -TERM=$TERM \ ./src/d2/ci/release/build_docker.sh" sh_c mkdir -p "$HW_BUILD_DIR" sh_c rsync --archive --human-readable "$REMOTE_HOST:src/d2/$ARCHIVE" "$ARCHIVE" diff --git a/ci/release/build_docker.sh b/ci/release/build_docker.sh index 7c3751af2..2939c5dd0 100755 --- a/ci/release/build_docker.sh +++ b/ci/release/build_docker.sh @@ -13,5 +13,4 @@ docker_run \ -e OS \ -e ARCH \ -e ARCHIVE \ - -e TERM \ "$tag" ./src/d2/ci/release/_build.sh diff --git a/ci/release/gen_install.sh b/ci/release/gen_install.sh index 082bdcc52..ee5249d87 100755 --- a/ci/release/gen_install.sh +++ b/ci/release/gen_install.sh @@ -37,3 +37,7 @@ sh_c cat \ sh_c cat ./ci/release/_install.sh \ \| sed -n "'/cd -- \"\$(dirname/,/cd -/!p'" \>\> install.sh sh_c chmod -w install.sh + +if [ -n "${CI-}" ]; then + git_assert_clean +fi diff --git a/ci/release/gen_sh.sh b/ci/release/gen_sh.sh new file mode 100755 index 000000000..63d9a0d3c --- /dev/null +++ b/ci/release/gen_sh.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -eu +cd -- "$(dirname "$0")/../.." +. ./ci/sub/lib.sh + +./ci/release/gen_install.sh +./ci/release/gen_template_lib.sh + +if [ -n "${CI-}" ]; then + git_assert_clean +fi diff --git a/ci/release/gen_template_lib.sh b/ci/release/gen_template_lib.sh index 1f523767a..b79c8ab61 100755 --- a/ci/release/gen_template_lib.sh +++ b/ci/release/gen_template_lib.sh @@ -27,3 +27,7 @@ sh_c cat \ ./ci/sub/lib/log.sh \ \| sed "-e'/^\. /d'" \>\>./ci/release/template/scripts/lib.sh sh_c chmod -w ./ci/release/template/scripts/lib.sh + +if [ -n "${CI-}" ]; then + git_assert_clean +fi diff --git a/ci/release/template/scripts/lib.sh b/ci/release/template/scripts/lib.sh index 95119c42a..8c6d1ec06 100644 --- a/ci/release/template/scripts/lib.sh +++ b/ci/release/template/scripts/lib.sh @@ -47,23 +47,32 @@ if [ -n "${DEBUG-}" ]; then set -x fi -export COLOR -if ! [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then - if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then - export _COLOR=1 - fi -fi - tput() { - if [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then - return 0 - fi - - if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then + if should_color; then TERM=${TERM:-xterm-256color} command tput "$@" fi } +should_color() { + if [ -n "${COLOR-}" ]; then + if [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then + _COLOR=0 + return 1 + elif [ "${COLOR-}" = 1 -o "${COLOR-}" = true ]; then + _COLOR=1 + return 0 + else + printf '$COLOR must be 0, 1, false or true but got %s' "$COLOR" >&2 + fi + fi + if [ -t 1 ]; then + _COLOR=1 + return 0 + else + return 1 + fi +} + setaf() { tput setaf "$1" shift @@ -100,10 +109,10 @@ printfp() {( if [ -z "${FGCOLOR-}" ]; then FGCOLOR="$(get_rand_color "$prefix")" fi - printf '%s' "$(setaf "$FGCOLOR" "$prefix")" + setaf "$FGCOLOR" "[$prefix]" if [ $# -gt 0 ]; then - printf ': ' + printf ' ' printf "$@" fi )} @@ -112,7 +121,8 @@ catp() { prefix="$1" shift - sed "s/^/$(printfp "$prefix" '')/" + should_color || true + sed "s/^/$(COLOR=${_COLOR-} printfp "$prefix" '')/" } repeat() { @@ -126,27 +136,30 @@ strlen() { } echoerr() { - FGCOLOR=1 echop err "$*" | humanpath>&2 + FGCOLOR=1 logp err "$*" | humanpath>&2 } caterr() { - FGCOLOR=1 catp err "$@" | humanpath >&2 + FGCOLOR=1 logpcat err "$@" | humanpath >&2 } printferr() { - FGCOLOR=1 printfp err "$@" | humanpath >&2 + FGCOLOR=1 logfp err "$@" | humanpath >&2 } logp() { - echop "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} echop "$@" | humanpath >&2 } logfp() { - printfp "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} printfp "$@" | humanpath >&2 } logpcat() { - catp "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} catp "$@" | humanpath >&2 } log() { @@ -169,6 +182,10 @@ warnf() { FGCOLOR=3 logfp warn "$@" } +warncat() { + FGCOLOR=3 logpcat warn "$@" +} + sh_c() { FGCOLOR=3 logp exec "$*" if [ -z "${DRY_RUN-}" ]; then diff --git a/install.sh b/install.sh index a9eb6bec3..a1ac95b45 100755 --- a/install.sh +++ b/install.sh @@ -53,23 +53,32 @@ if [ -n "${DEBUG-}" ]; then set -x fi -export COLOR -if ! [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then - if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then - export _COLOR=1 - fi -fi - tput() { - if [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then - return 0 - fi - - if [ "${COLOR-}" = 1 -o "${COLOR-}" = true -o -t 1 ]; then + if should_color; then TERM=${TERM:-xterm-256color} command tput "$@" fi } +should_color() { + if [ -n "${COLOR-}" ]; then + if [ "${COLOR-}" = 0 -o "${COLOR-}" = false ]; then + _COLOR=0 + return 1 + elif [ "${COLOR-}" = 1 -o "${COLOR-}" = true ]; then + _COLOR=1 + return 0 + else + printf '$COLOR must be 0, 1, false or true but got %s' "$COLOR" >&2 + fi + fi + if [ -t 1 ]; then + _COLOR=1 + return 0 + else + return 1 + fi +} + setaf() { tput setaf "$1" shift @@ -106,10 +115,10 @@ printfp() {( if [ -z "${FGCOLOR-}" ]; then FGCOLOR="$(get_rand_color "$prefix")" fi - printf '%s' "$(setaf "$FGCOLOR" "$prefix")" + setaf "$FGCOLOR" "[$prefix]" if [ $# -gt 0 ]; then - printf ': ' + printf ' ' printf "$@" fi )} @@ -118,7 +127,8 @@ catp() { prefix="$1" shift - sed "s/^/$(printfp "$prefix" '')/" + should_color || true + sed "s/^/$(COLOR=${_COLOR-} printfp "$prefix" '')/" } repeat() { @@ -132,27 +142,30 @@ strlen() { } echoerr() { - FGCOLOR=1 echop err "$*" | humanpath>&2 + FGCOLOR=1 logp err "$*" | humanpath>&2 } caterr() { - FGCOLOR=1 catp err "$@" | humanpath >&2 + FGCOLOR=1 logpcat err "$@" | humanpath >&2 } printferr() { - FGCOLOR=1 printfp err "$@" | humanpath >&2 + FGCOLOR=1 logfp err "$@" | humanpath >&2 } logp() { - echop "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} echop "$@" | humanpath >&2 } logfp() { - printfp "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} printfp "$@" | humanpath >&2 } logpcat() { - catp "$@" | humanpath >&2 + should_color >&2 || true + COLOR=${_COLOR-} catp "$@" | humanpath >&2 } log() { @@ -175,6 +188,10 @@ warnf() { FGCOLOR=3 logfp warn "$@" } +warncat() { + FGCOLOR=3 logpcat warn "$@" +} + sh_c() { FGCOLOR=3 logp exec "$*" if [ -z "${DRY_RUN-}" ]; then