d2/install.sh

426 lines
9.2 KiB
Bash
Raw Normal View History

2022-11-14 05:58:55 +00:00
#!/bin/sh
set -eu
# *************
# DO NOT EDIT
#
# install.sh was bundled together from
#
# - ./ci/sub/lib/rand.sh
# - ./ci/sub/lib/log.sh
# - ./ci/sub/lib/flag.sh
# - ./ci/release/_install.sh
#
# The last of which implements the installation logic.
#
# Generated by ./ci/release/gen_install.sh.
# *************
#!/bin/sh
if [ "${LIB_RAND-}" ]; then
return 0
fi
LIB_RAND=1
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\""
}
#!/bin/sh
if [ "${LIB_LOG-}" ]; then
return 0
fi
LIB_LOG=1
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
}
2022-11-14 06:41:58 +00:00
warn() {
COLOR=3 logp warn "$@"
}
warnf() {
COLOR=3 logfp warn "$@"
}
2022-11-14 05:58:55 +00:00
sh_c() {
COLOR=3 logp exec "$*"
if [ -z "${DRYRUN-}" ]; then
eval "$@"
fi
}
header() {
logp "/* $1 */"
}
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)"
return 1
esac
}
#!/bin/sh
if [ "${LIB_FLAG-}" ]; then
return 0
fi
LIB_FLAG=1
# flag_parse implements a robust flag parser.
#
# For a full fledge example see ../examples/date.sh
#
# It differs from getopts(1) in that long form options are supported. Currently the only
# deficiency is that short combined options are not supported like -xyzq. That would be
# interpreted as a single -xyzq flag. The other deficiency is lack of support for short
# flag syntax like -carg where the arg is not separated from the flag. This one is
# unfixable I believe unfortunately but for combined short flags I have opened
# https://github.com/terrastruct/ci/issues/6
#
# flag_parse stores state in $FLAG, $FLAGRAW, $FLAGARG and $FLAGSHIFT.
# FLAG contains the name of the flag without hyphens.
# FLAGRAW contains the name of the flag as passed in with hyphens.
# FLAGARG contains the argument for the flag if there was any.
# If there was none, it will not be set.
# FLAGSHIFT contains the number by which the arguments should be shifted to
# start at the next flag/argument
#
# After each call check $FLAG for the name of the parsed flag.
# If empty, then no more flags are left.
# Still, call shift "$FLAGSHIFT" in case there was a --
#
# If the argument for the flag is optional, then use ${FLAGARG-} to access
# the argument if one was passed. Use ${FLAGARG+x} = x to check if it was set.
# You only need to explicitly check if the flag was set if you care whether the user
# explicitly passed the empty string as the argument.
#
# Otherwise, call one of the flag_*arg functions:
#
# If a flag requires an argument, call flag_reqarg
# - $FLAGARG is guaranteed to be set after.
# If a flag requires a non empty argument, call flag_nonemptyarg
# - $FLAGARG is guaranteed to be set to a non empty string after.
# If a flag should not be passed an argument, call flag_noarg
# - $FLAGARG is guaranteed to be unset after.
#
# And then shift "$FLAGSHIFT"
flag_parse() {
case "${1-}" in
-*=*)
# Remove everything after first equal sign.
FLAG="${1%%=*}"
FLAGRAW="$FLAG"
# Remove leading hyphens.
FLAG="${FLAG#-}"; FLAG="${FLAG#-}"
# Remove everything before first equal sign.
FLAGARG="${1#*=}"
FLAGSHIFT=1
;;
-)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=0
;;
--)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=1
;;
-*)
# Remove leading hyphens.
FLAG="${1#-}"; FLAG="${FLAG#-}"
FLAGRAW=$1
unset FLAGARG
FLAGSHIFT=1
if [ $# -gt 1 ]; then
case "$2" in
-)
FLAGARG="$2"
FLAGSHIFT=2
;;
-*)
;;
*)
FLAGARG="$2"
FLAGSHIFT=2
;;
esac
fi
;;
*)
FLAG=
FLAGRAW=
unset FLAGARG
FLAGSHIFT=0
;;
esac
return 0
}
flag_reqarg() {
if [ "${FLAGARG+x}" != x ]; then
flag_errusage "flag $FLAGRAW requires an argument"
fi
}
flag_nonemptyarg() {
flag_reqarg
if [ -z "$FLAGARG" ]; then
flag_errusage "flag $FLAGRAW requires a non-empty argument"
fi
}
flag_noarg() {
if [ "$FLAGSHIFT" -eq 2 ]; then
unset FLAGARG
FLAGSHIFT=1
elif [ "${FLAGARG+x}" = x ]; then
# Means an argument was passed via equal sign as in -$FLAG=$FLAGARG
flag_errusage "flag $FLAGRAW does not accept an argument"
fi
}
flag_errusage() {
caterr <<EOF
$1
Run with --help for usage.
EOF
return 1
}
#!/bin/sh
set -eu
2022-11-14 06:41:58 +00:00
help() {
arg0="$0"
if [ "$0" = sh ]; then
arg0="curl -fsSL https://d2lang.com/install.sh | sh -s --"
fi
cat <<EOF
usage: $arg0 [--dryrun] [--version vX.X.X] [--edge] [--method detect] [--prefix ~/.local]
[--tala] [--tala-version vX.X.X] [--force] [--uninstall]
install.sh automates the installation of D2 onto your system. It currently only supports
the installation of standalone releases from GitHub. If you pass --edge, it will clone the
source, build a release and install from it.
Flags:
--dryrun
Pass to have install.sh show the install method and flags that will be used to install
without executing them. Very useful to understand what changes it will make to your system.
--version vX.X.X
Pass to have install.sh install the given version instead of the latest version.
--edge
Pass to build and install D2 from source.
--method [detect | standalone]
Pass to control the method by which to install. Right now we only support standalone
releases from GitHub but later we'll add support for brew, rpm, deb and more.
- detect is currently unimplemented but would use your OS's package manager
automatically.
- standalone installs a standalone release archive into ~/.local
Add ~/.local/bin to your \$PATH to use it.
Control the unix hierarchy path with --prefix
--prefix ~/.local
Controls the unix hierarchy path into which standalone releases are installed.
Defaults to ~/.local. You may also want to use /usr/local
Remember that whatever you use, you must have the bin directory of your prefix
path in \$PATH to execute the d2 binary. For example, if my prefix directory is
/usr/local then my \$PATH must contain /usr/local/bin.
--tala
Install Terrastruct's closed source TALA for improved layouts.
See https://github.com/terrastruct/TALA
--tala-version vX.X.X
Install the passed version of tala instead of latest.
--force:
Force installation over the existing version even if they match. It will attempt a clean
uninstall first before installing the new version. The install assets will not be deleted
from ~/.cache/d2/install.
--uninstall:
Uninstall the installed version of d2. The --method flag must be the same as for
installation. i.e if you used --method standalone you must again use --method standalone
for uninstallation. With detect, the install script will try to use the OS package manager
to uninstall instead.
All downloaded assets are cached into ~/.cache/d2/install. use \$XDG_CACHE_HOME to change
path of the cached assets.
You can rerun install.sh to update your version of D2. install.sh will avoid reinstalling
if the installed version is the latest unless --force is passed.
EOF
}