From ee86426ef2d95c76322579fdda02a3a7c72c2b7a Mon Sep 17 00:00:00 2001 From: Anmol Sethi Date: Sat, 12 Nov 2022 14:19:09 -0800 Subject: [PATCH] 2022-11-12 02:19:09pm --- ci/release/build.sh | 60 ++++++-- ci/release/template/README.md.sh | 18 ++- ci/release/template/bin/d2 | 0 ci/release/template/lib.sh | 105 -------------- ci/release/template/{ => scripts}/install.sh | 10 +- ci/release/template/scripts/lib.sh | 137 ++++++++++++++++++ .../template/{ => scripts}/uninstall.sh | 7 +- ci/sub | 2 +- 8 files changed, 213 insertions(+), 126 deletions(-) delete mode 100644 ci/release/template/bin/d2 delete mode 100644 ci/release/template/lib.sh rename ci/release/template/{ => scripts}/install.sh (61%) create mode 100644 ci/release/template/scripts/lib.sh rename ci/release/template/{ => scripts}/uninstall.sh (66%) diff --git a/ci/release/build.sh b/ci/release/build.sh index 13bb69d42..68faa6d50 100755 --- a/ci/release/build.sh +++ b/ci/release/build.sh @@ -7,7 +7,10 @@ help() { cat </d2-.tar.gz +$0 builds D2 release archives into ./ci/release/build//d2-.tar.gz + +The version is detected via git describe which will use the git tag for the current +commit if available. Flags: @@ -23,16 +26,22 @@ EOF } build() { - BUILD_DIR="$BUILD_DIR/$OS/$ARCH" + HW_BUILD_DIR="$BUILD_DIR/$OS/$ARCH/d2-$VERSION" - mkdir -p "$BUILD_DIR/bin" - sh_c cp LICENSE.txt "$BUILD_DIR" - sh_c "./ci/release/template/README.md.sh > $BUILD_DIR" + sh_c mkdir -p "$HW_BUILD_DIR" + sh_c rsync --recursive --perms --delete \ + --human-readable --copy-links ./ci/release/template/ "$HW_BUILD_DIR/" + VERSION=$VERSION sh_c eval "'$HW_BUILD_DIR/README.md.sh'" \> "'$HW_BUILD_DIR/README.md'" + sh_c rm -f "$HW_BUILD_DIR/README.md.sh" + sh_c find "$HW_BUILD_DIR" -exec touch {} \; export GOOS=$(goos "$OS") export GOARCH="$ARCH" - sh_c go build -ldflags "-X lib/version.Version=$VERSION" \ - -o "$BUILD_DIR/bin/d2" ./cmd/d2 + sh_c mkdir -p "$HW_BUILD_DIR/bin" + sh_c go build -ldflags "-X oss.terrastruct.com/d2/lib/version.Version=$VERSION" \ + -o "$HW_BUILD_DIR/bin/d2" ./cmd/d2 + + sh_c tar czf "$BUILD_DIR/d2-$OS-$ARCH-$VERSION.tar.gz" "$HW_BUILD_DIR" } main() { @@ -41,11 +50,44 @@ main() { FLAGARG \ FLAGSHIFT \ VERSION \ + BUILD_DIR \ + HW_BUILD_DIR \ REBUILD \ - DEST \ LOCAL \ + DRYRUN VERSION="$(git_describe_ref)" BUILD_DIR="ci/release/build/$VERSION" + while :; do + flag_parse "$@" + case "$FLAG" in + h|help) + help + return 0 + ;; + rebuild) + flag_noarg + REBUILD=1 + shift "$FLAGSHIFT" + ;; + local) + flag_noarg + LOCAL=1 + shift "$FLAGSHIFT" + ;; + dryrun) + flag_noarg + DRYRUN=1 + shift "$FLAGSHIFT" + ;; + '') + shift "$FLAGSHIFT" + break + ;; + *) + flag_errusage "unrecognized flag $FLAGRAW" + ;; + esac + done if [ $# -gt 0 ]; then flag_errusage "no arguments are accepted" @@ -55,7 +97,7 @@ main() { runjob linux-arm64 'OS=linux ARCH=arm64 build' & runjob macos-amd64 'OS=macos ARCH=amd64 build' & runjob macos-arm64 'OS=macos ARCH=arm64 build' & - wait_jobs + waitjobs } main "$@" diff --git a/ci/release/template/README.md.sh b/ci/release/template/README.md.sh index 4f4b2e082..84a0c01c2 100755 --- a/ci/release/template/README.md.sh +++ b/ci/release/template/README.md.sh @@ -4,11 +4,23 @@ set -eu cat <&2 -} - -printferr() { - COLOR=1 printfp err "$@" >&2 -} - -sh_c() { - COLOR=3 echop exec "$*" - "$@" -} - -get_rand_color() { - # 1-6 are regular and 9-14 are bright. - # 1,2 and 9,10 are red and green but we use those for success and failure. - pick "$*" 3 4 5 6 11 12 13 14 -} - -echop() {( - prefix="$1" - shift - - if [ -z "${COLOR:-}" ]; then - COLOR="$(get_rand_color "$prefix")" - fi - printf '%s: %s\n' "$(setaf "$COLOR" "$prefix")" "$*" -)} - -printfp() {( - PREFIX="$1" - shift - echop "$PREFIX" "$(printf "$@")" -)} - -hide() { - out="$(mktemp)" - set +e - "$@" >"$out" 2>&1 - code="$?" - set -e - if [ "$code" -eq 0 ]; then - return - fi - cat "$out" >&2 - exit "$code" -} - -echo_dur() { - local dur=$1 - local h=$((dur/60/60)) - local m=$((dur/60%60)) - local s=$((dur%60)) - printf '%dh%dm%ds' "$h" "$m" "$s" -} - -sponge() { - dst="$1" - tmp="$(mktemp)" - cat > "$tmp" - cat "$tmp" > "$dst" -} - -stripansi() { - # First regex gets rid of standard xterm escape sequences for controlling - # visual attributes. - # The second regex I'm not 100% sure, the reference says it selects the US - # encoding but I'm not sure why that's necessary or why it always occurs - # in tput sgr0 before the standard escape sequence. - # See tput sgr0 | xxd - sed -e $'s/\x1b\[[0-9;]*m//g' -e $'s/\x1b(.//g' -} - -runtty() { - case "$(uname)" in - Darwin) - script -q /dev/null "$@" - ;; - Linux) - script -eqc "$*" - ;; - *) - echoerr "runtty: unsupported OS $(uname)" - esac -} diff --git a/ci/release/template/install.sh b/ci/release/template/scripts/install.sh similarity index 61% rename from ci/release/template/install.sh rename to ci/release/template/scripts/install.sh index 57207357a..eff495049 100755 --- a/ci/release/template/install.sh +++ b/ci/release/template/scripts/install.sh @@ -1,15 +1,15 @@ #!/bin/sh set -eu -. "$(dirname "$0")/lib.sh" +cd -- "$(dirname "$0")/.." +. ./scripts/lib.sh main() { - if [ ! -e "${PREFIX:-}" ]; then + if [ ! -e "${PREFIX-}" ]; then echoerr "\$PREFIX must be set to a unix prefix directory in which to install d2 like /usr/local" - exit 1 + return 1 fi - mkdir -p "$PREFIX" - + sh_c mkdir -p "$PREFIX" sh_c install ./bin/d2 "$PREFIX/bin/d2" } diff --git a/ci/release/template/scripts/lib.sh b/ci/release/template/scripts/lib.sh new file mode 100644 index 000000000..074eb3a91 --- /dev/null +++ b/ci/release/template/scripts/lib.sh @@ -0,0 +1,137 @@ +#!/bin/sh + +rand() { + seed="$1" + range="$2" + + seed_file="$(mktemp)" + _echo "$seed" | md5sum > "$seed_file" + shuf -i "$range" -n 1 --random-source="$seed_file" +} + +pick() { + seed="$1" + shift + i="$(rand "$seed" "1-$#")" + eval "_echo \"\$$i\"" +} + +tput() { + if [ -n "$TERM" ]; then + command tput "$@" + fi +} + +setaf() { + tput setaf "$1" + shift + printf '%s' "$*" + tput sgr0 +} + +_echo() { + printf '%s\n' "$*" +} + +get_rand_color() { + # 1-6 are regular and 9-14 are bright. + # 1,2 and 9,10 are red and green but we use those for success and failure. + pick "$*" 3 4 5 6 11 12 13 14 +} + +echop() { + prefix="$1" + shift + + if [ "$#" -gt 0 ]; then + printfp "$prefix" "%s\n" "$*" + else + printfp "$prefix" + printf '\n' + fi +} + +printfp() {( + prefix="$1" + shift + + if [ -z "${COLOR:-}" ]; then + COLOR="$(get_rand_color "$prefix")" + fi + printf '%s' "$(setaf "$COLOR" "$prefix")" + + if [ $# -gt 0 ]; then + printf ': ' + printf "$@" + fi +)} + +catp() { + prefix="$1" + shift + + printfp "$prefix" + printf ': ' + read -r line + _echo "$line" + + indent=$(repeat ' ' 2) + sed "s/^/$indent/" +} + +repeat() { + char="$1" + times="$2" + seq -s "$char" "$times" | tr -d '[:digit:]' +} + +strlen() { + printf %s "$1" | wc -c +} + +echoerr() { + COLOR=1 echop err "$*" >&2 +} + +caterr() { + COLOR=1 catp err "$@" >&2 +} + +printferr() { + COLOR=1 printfp err "$@" >&2 +} + +logp() { + echop "$@" >&2 +} + +logfp() { + printfp "$@" >&2 +} + +logpcat() { + catp "$@" >&2 +} + +log() { + COLOR=5 logp log "$@" +} + +logf() { + COLOR=5 logfp log "$@" +} + +logcat() { + COLOR=5 catp log "$@" >&2 +} + +sh_c() { + COLOR=3 logp exec "$*" + if [ -z "${DRYRUN-}" ]; then + "$@" + fi +} + +header() { + logp "/* $1 */" +} diff --git a/ci/release/template/uninstall.sh b/ci/release/template/scripts/uninstall.sh similarity index 66% rename from ci/release/template/uninstall.sh rename to ci/release/template/scripts/uninstall.sh index 2ec6a47c1..d6d891dca 100755 --- a/ci/release/template/uninstall.sh +++ b/ci/release/template/scripts/uninstall.sh @@ -1,11 +1,12 @@ #!/bin/sh set -eu -. "$(dirname "$0")/lib.sh" +cd -- "$(dirname "$0")/.." +. ./scripts/lib.sh main() { - if [ ! -e "${PREFIX:-}" ]; then + if [ ! -e "${PREFIX-}" ]; then echoerr "\$PREFIX must be set to a unix prefix directory from which to uninstall d2 like /usr/local" - exit 1 + return 1 fi sh_c rm -f "$PREFIX/bin/d2" diff --git a/ci/sub b/ci/sub index d95383e2e..4760501f6 160000 --- a/ci/sub +++ b/ci/sub @@ -1 +1 @@ -Subproject commit d95383e2ed77d82ffc34b8ecd0a90b5cc7b89d97 +Subproject commit 4760501f65c5abace1790a501f34a7b1e3223b2f