Merge pull request #121 from nhooyr/installsh

install.sh: Add brew integration and documentation
This commit is contained in:
Anmol Sethi 2022-11-20 23:29:06 -05:00 committed by GitHub
commit 05c1360e1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 455 additions and 173 deletions

View file

@ -58,6 +58,9 @@ A browser window will open with `out.svg` and live-reload on changes to `in.d2`.
## Install ## Install
For more detailed installation docs with examples for if you do not wish to use the
install script see [./doc/INSTALL.md](./doc/INSTALL.md).
### Install script ### Install script
The recommended way to install is to run our install script, which will figure out the The recommended way to install is to run our install script, which will figure out the
@ -92,7 +95,7 @@ curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall
Alternatively, you can install from source: Alternatively, you can install from source:
```sh ```sh
go install oss.terrastruct.com/d2 go install oss.terrastruct.com/d2/cmd/d2@latest
``` ```
## D2 as a library ## D2 as a library

View file

@ -18,8 +18,11 @@ usage: $arg0 [--dry-run] [--version vX.X.X] [--edge] [--method detect] [--prefix
[--tala latest] [--force] [--uninstall] [--tala latest] [--force] [--uninstall]
install.sh automates the installation of D2 onto your system. It currently only supports 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 the installation of standalone releases from GitHub and via Homebrew on macOS. See the
source, build a release and install from it. docs for --detect below for more information
If you pass --edge, it will clone the source, build a release and install from it.
--edge is incompatible with --tala and currently unimplemented.
Flags: Flags:
@ -29,6 +32,8 @@ Flags:
--version vX.X.X --version vX.X.X
Pass to have install.sh install the given version instead of the latest version. Pass to have install.sh install the given version instead of the latest version.
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--edge --edge
Pass to build and install D2 from source. This will still use --method if set to detect Pass to build and install D2 from source. This will still use --method if set to detect
@ -36,14 +41,15 @@ Flags:
if an unsupported package manager is used. To install from source like a dev would, if an unsupported package manager is used. To install from source like a dev would,
use go install oss.terrastruct.com/d2 use go install oss.terrastruct.com/d2
note: currently unimplemented. note: currently unimplemented.
warn: incompatible with --tala as TALA is closed source.
--method [detect | standalone] --method [detect | standalone | homebrew ]
Pass to control the method by which to install. Right now we only support 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. releases from GitHub but later we'll add support for brew, rpm, deb and more.
note: currently unimplemented.
- detect is currently unimplemented but would use your OS's package manager - detect will use your OS's package manager automatically.
automatically. So far it only detects macOS and automatically uses homebrew.
- homebrew uses https://brew.sh/ which is a macOS and Linux package manager.
- standalone installs a standalone release archive into the unix hierarchy path - standalone installs a standalone release archive into the unix hierarchy path
specified by --prefix which defaults to /usr/local specified by --prefix which defaults to /usr/local
Ensure /usr/local/bin is in your \$PATH to use it. Ensure /usr/local/bin is in your \$PATH to use it.
@ -51,16 +57,19 @@ Flags:
--prefix /usr/local --prefix /usr/local
Controls the unix hierarchy path into which standalone releases are installed. Controls the unix hierarchy path into which standalone releases are installed.
Defaults to /usr/local. You may also want to use ~/.local to avoid needing sudo. Defaults to /usr/local. You may also want to use ~/.local to avoid needing sudo.
Remember that whatever you use, you must have the bin directory of your prefix We use ~/.local by default on arm64 macOS machines as SIP now disables access to
path in \$PATH to execute the d2 binary. For example, if my prefix directory is /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. /usr/local then my \$PATH must contain /usr/local/bin.
--tala [latest] --tala [latest]
Install Terrastruct's closed source TALA for improved layouts. Install Terrastruct's closed source TALA for improved layouts.
See https://github.com/terrastruct/TALA See https://github.com/terrastruct/tala
It optionally takes an argument of the TALA version to install. It optionally takes an argument of the TALA version to install.
Installation obeys all other flags, just like the installation of d2. For example, Installation obeys all other flags, just like the installation of d2. For example,
the d2plugin-tala binary will be installed into /usr/local/bin/d2plugin-tala the d2plugin-tala binary will be installed into /usr/local/bin/d2plugin-tala
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--force: --force:
Force installation over the existing version even if they match. It will attempt a Force installation over the existing version even if they match. It will attempt a
@ -85,7 +94,6 @@ EOF
} }
main() { main() {
METHOD=standalone
while flag_parse "$@"; do while flag_parse "$@"; do
case "$FLAG" in case "$FLAG" in
h|help) h|help)
@ -113,8 +121,6 @@ main() {
method) method)
flag_nonemptyarg && shift "$FLAGSHIFT" flag_nonemptyarg && shift "$FLAGSHIFT"
METHOD=$FLAGARG METHOD=$FLAGARG
echoerr "$FLAGRAW is currently unimplemented"
return 1
;; ;;
prefix) prefix)
flag_nonemptyarg && shift "$FLAGSHIFT" flag_nonemptyarg && shift "$FLAGSHIFT"
@ -149,49 +155,86 @@ main() {
PREFIX=${PREFIX:-/usr/local} PREFIX=${PREFIX:-/usr/local}
CACHE_DIR=$(cache_dir) CACHE_DIR=$(cache_dir)
mkdir -p "$CACHE_DIR" mkdir -p "$CACHE_DIR"
METHOD=${METHOD:-detect}
INSTALL_DIR=$PREFIX/lib/d2 INSTALL_DIR=$PREFIX/lib/d2
case $METHOD in
detect)
case "$OS" in
macos)
if command -v brew >/dev/null; then
log "detected macOS with homebrew, using homebrew for (un)installation"
METHOD=homebrew
else
warn "detected macOS without homebrew, falling back to --method=standalone"
METHOD=standalone
fi
;;
*)
warn "unrecognized OS $OS, falling back to --method=standalone"
METHOD=standalone
;;
esac
;;
standalone) ;;
homebrew) ;;
*)
echoerr "unknown (un)installation method $METHOD"
return 1
;;
esac
if [ -n "${UNINSTALL-}" ]; then if [ -n "${UNINSTALL-}" ]; then
uninstall uninstall
return 0 if [ -n "${DRY_RUN-}" ]; then
log "Rerun without --dry-run to execute printed commands and perform uninstall."
fi fi
else
VERSION=${VERSION:-latest}
if [ "$VERSION" = latest ]; then
header "fetching latest release info"
fetch_release_info
fi
install install
if [ -n "${DRY_RUN-}" ]; then
log "Rerun without --dry-run to execute printed commands and perform install."
fi
fi
} }
install() { install() {
install_d2 case $METHOD in
standalone)
install_d2_standalone
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
# Run in subshell to avoid overwriting VERSION. # Run in subshell to avoid overwriting VERSION.
TALA_VERSION="$( install_tala && echo "$VERSION" )" TALA_VERSION="$( RELEASE_INFO= install_tala_standalone && echo "$VERSION" )"
fi fi
;;
homebrew)
install_d2_brew
if [ -n "${TALA-}" ]; then install_tala_brew; fi
;;
esac
FGCOLOR=2 header success FGCOLOR=2 bigheader 'next steps'
case $METHOD in
standalone) install_post_standalone ;;
homebrew) install_post_brew ;;
esac
}
install_post_standalone() {
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
fi fi
log "Rerun this install script with --uninstall to uninstall" log "Rerun this install script with --uninstall to uninstall."
log
if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then
logcat >&2 <<EOF logcat >&2 <<EOF
%%%%%%%%%%%%%%%%%%%%%%%%%
NEXT STEPS
%%%%%%%%%%%%%%%%%%%%%%%%%
Extend your \$PATH to use d2: Extend your \$PATH to use d2:
export PATH=$PREFIX/bin:\$PATH export PATH=$PREFIX/bin:\$PATH
Then run: Then run:
${TALA+D2_LAYOUT=tala }d2 --help ${TALA+D2_LAYOUT=tala }d2 --help
EOF EOF
else else
log " Run ${TALA+D2_LAYOUT=tala }d2 --help for usage." log "Run ${TALA+D2_LAYOUT=tala }d2 --help for usage."
fi fi
if ! manpath | grep -qF "$PREFIX/share/man"; then if ! manpath | grep -qF "$PREFIX/share/man"; then
logcat >&2 <<EOF logcat >&2 <<EOF
@ -204,36 +247,61 @@ EOF
log " man d2plugin-tala" log " man d2plugin-tala"
fi fi
else else
log " Run man d2 for detailed docs." log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log " Run man d2plugin-tala for detailed docs." log "Run man d2plugin-tala for detailed TALA docs."
fi fi
fi fi
logcat >&2 <<EOF logcat >&2 <<EOF
Something not working? Please let us know: Something not working? Please let us know:
https://github.com/terrastruct/d2/issues/new https://github.com/terrastruct/d2/issues
https://github.com/terrastruct/d2/discussions
https://discord.gg/NF6X8K4eDq
EOF EOF
} }
install_d2() { install_post_brew() {
log "d2 has been successfully installed with homebrew."
if [ -n "${TALA-}" ]; then
log "tala has been successfully installed with homebrew."
fi
log "Rerun this install script with --uninstall to uninstall."
log
log "Run ${TALA+D2_LAYOUT=tala }d2 --help for usage."
log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then
log "Run man d2plugin-tala for detailed TALA docs."
fi
logcat >&2 <<EOF
Something not working? Please let us know:
https://github.com/terrastruct/d2/issues
https://github.com/terrastruct/d2/discussions
https://discord.gg/NF6X8K4eDq
EOF
}
install_d2_standalone() {
VERSION=${VERSION:-latest}
header "installing d2-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2 >/dev/null; then if command -v d2 >/dev/null; then
INSTALLED_VERSION="$(d2 version)" INSTALLED_VERSION="$(d2 version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as version $VERSION is already installed." log "skipping installation as d2 $VERSION is already installed."
return 0 return 0
fi fi
log "uninstalling $INSTALLED_VERSION to install $VERSION" log "uninstalling d2 $INSTALLED_VERSION to install $VERSION"
if ! uninstall_d2; then if ! uninstall_d2_standalone; then
warn "failed to uninstall $INSTALLED_VERSION" warn "failed to uninstall d2 $INSTALLED_VERSION"
fi fi
fi fi
header "installing d2-$VERSION"
install_standalone_d2
}
install_standalone_d2() {
ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz" ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github" log "installing standalone release $ARCHIVE from github"
@ -252,19 +320,38 @@ install_standalone_d2() {
"$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'" "$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
} }
install_tala() { install_d2_brew() {
REPO="${REPO_TALA:-terrastruct/TALA}" header "installing d2 with homebrew"
VERSION=$TALA sh_c brew tap terrastruct/d2
RELEASE_INFO= sh_c brew install d2
fetch_release_info
header "installing tala-$VERSION"
install_standalone_tala
} }
install_standalone_tala() { install_tala_standalone() {
REPO="${REPO_TALA:-terrastruct/tala}"
VERSION=$TALA
header "installing tala-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as tala $VERSION is already installed."
return 0
fi
log "uninstalling tala $INSTALLED_VERSION to install $VERSION"
if ! uninstall_tala_standalone; then
warn "failed to uninstall tala $INSTALLED_VERSION"
fi
fi
ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz" ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github" log "installing standalone release $ARCHIVE from github"
fetch_release_info
asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1') asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1')
asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"') asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"')
@ -280,36 +367,42 @@ install_standalone_tala() {
"$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'" "$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
} }
install_tala_brew() {
header "installing tala with homebrew"
sh_c brew tap terrastruct/d2
sh_c brew install tala
}
uninstall() { uninstall() {
# We uninstall tala first as package managers require that it be uninstalled before
# uninstalling d2 as TALA depends on d2.
if [ "${TALA-}" ]; then
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
header "uninstalling tala-$INSTALLED_VERSION"
case $METHOD in
standalone) uninstall_tala_standalone ;;
homebrew) uninstall_tala_brew ;;
esac
else
warn "no version of tala installed"
fi
fi
if ! command -v d2 >/dev/null; then if ! command -v d2 >/dev/null; then
warn "no version of d2 installed" warn "no version of d2 installed"
return 0 return 0
fi fi
INSTALLED_VERSION="$(d2 --version)" INSTALLED_VERSION="$(d2 --version)"
if ! uninstall_d2; then
echoerr "failed to uninstall $INSTALLED_VERSION"
return 1
fi
if [ "${TALA-}" ]; then
if ! command -v d2plugin-tala >/dev/null; then
warn "no version of tala installed"
return 0
fi
INSTALLED_VERSION="$(d2plugin-tala --version)"
if ! uninstall_tala; then
echoerr "failed to uninstall tala $INSTALLED_VERSION"
return 1
fi
fi
return 0
}
uninstall_d2() {
header "uninstalling d2-$INSTALLED_VERSION" header "uninstalling d2-$INSTALLED_VERSION"
uninstall_standalone_d2 case $METHOD in
standalone) uninstall_d2_standalone ;;
homebrew) uninstall_d2_brew ;;
esac
} }
uninstall_standalone_d2() { uninstall_d2_standalone() {
log "uninstalling standalone release of d2-$INSTALLED_VERSION" log "uninstalling standalone release of d2-$INSTALLED_VERSION"
if [ ! -e "$INSTALL_DIR/d2-$INSTALLED_VERSION" ]; then if [ ! -e "$INSTALL_DIR/d2-$INSTALLED_VERSION" ]; then
@ -327,12 +420,11 @@ uninstall_standalone_d2() {
"$sh_c" rm -rf "$INSTALL_DIR/d2-$INSTALLED_VERSION" "$sh_c" rm -rf "$INSTALL_DIR/d2-$INSTALLED_VERSION"
} }
uninstall_tala() { uninstall_d2_brew() {
header "uninstalling tala-$INSTALLED_VERSION" sh_c brew remove d2
uninstall_standalone_tala
} }
uninstall_standalone_tala() { uninstall_tala_standalone() {
log "uninstalling standalone release tala-$INSTALLED_VERSION" log "uninstalling standalone release tala-$INSTALLED_VERSION"
if [ ! -e "$INSTALL_DIR/tala-$INSTALLED_VERSION" ]; then if [ ! -e "$INSTALL_DIR/tala-$INSTALLED_VERSION" ]; then
@ -350,6 +442,10 @@ uninstall_standalone_tala() {
"$sh_c" rm -rf "$INSTALL_DIR/tala-$INSTALLED_VERSION" "$sh_c" rm -rf "$INSTALL_DIR/tala-$INSTALLED_VERSION"
} }
uninstall_tala_brew() {
sh_c brew remove tala
}
is_prefix_writable() { is_prefix_writable() {
sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true
# The reason for checking whether $INSTALL_DIR is writable is that on macOS you have # The reason for checking whether $INSTALL_DIR is writable is that on macOS you have
@ -405,4 +501,9 @@ fetch_gh() {
sh_c mv "$file.inprogress" "$file" sh_c mv "$file.inprogress" "$file"
} }
brew() {
# Makes brew sane.
HOMEBREW_NO_INSTALL_CLEANUP=1 HOMEBREW_NO_AUTO_UPDATE=1 command brew "$@"
}
main "$@" main "$@"

View file

@ -111,12 +111,11 @@ printfp() {(
if [ -z "${FGCOLOR-}" ]; then if [ -z "${FGCOLOR-}" ]; then
FGCOLOR="$(get_rand_color "$prefix")" FGCOLOR="$(get_rand_color "$prefix")"
fi fi
should_color || true
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
should_color || true printf '%s' "$(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")"
printf '%s' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")
else else
should_color || true printf '%s: %s\n' "$(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")" "$(printf "$@")"
printf '%s: %s\n' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix") "$(printf "$@")"
fi fi
)} )}
@ -219,6 +218,12 @@ header() {
logp "/* $1 */" logp "/* $1 */"
} }
bigheader() {
logp "/**
* $1
**/"
}
# humanpath replaces all occurrences of " $HOME" with " ~" # humanpath replaces all occurrences of " $HOME" with " ~"
# and all occurrences of '$HOME' with the literal '$HOME'. # and all occurrences of '$HOME' with the literal '$HOME'.
humanpath() { humanpath() {

2
ci/sub

@ -1 +1 @@
Subproject commit 8f9a24045546e76e39175ed148fdadd770c750ba Subproject commit f1f144c738f3015f3cafc7d566666f83a0916320

View file

@ -65,8 +65,7 @@ language. Sometimes it gives controversial sentences -- don't use those.
Script to generate one line of random text: Script to generate one line of random text:
``` ```
ipsum1() { ipsum1() {
fortune | head -n1 | sed 's/^ *//;s/ *$//' | tr -d '\n' | pbcopy fortune | head -n1 | sed 's/^ *//;s/ *$//' | tr -d '\n' | tee /dev/stderr | pbcopy
echo "$(pbpaste -Prefer txt)"
} }
``` ```

68
docs/INSTALL.md Normal file
View file

@ -0,0 +1,68 @@
# install
This file documents all the ways by which you can install d2.
<!-- toc -->
- [install.sh](#installsh)
- [Standalone](#standalone)
- [macOS (Homebrew)](#macos-homebrew)
<!-- tocstop -->
## install.sh
```sh
# With --dry-run the install script will print the commands it will use
# to install without actually installing so you know what it's going to do.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --dry-run
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s --
```
For help on the terminal run including the supported package managers
and detection methods see:
```sh
curl -fsSL https://d2lang.com/install.sh | sh -s -- --help
```
## Standalone
We publish standalone release archives with every release on github.
Download the `.tar.gz` release for your OS/ARCH combination and then run:
```sh
make install
```
Inside the extracted directory to install.
```sh
make uninstall
```
To uninstall. You will be prompted for sudo/su/doas if root permissions
are required for installation. You can control the unix hierarchy installation
path with `PREFIX=`. For example:
```
# Install under ~/.local.
# Binaries will be at ~/.local/bin
# And manpages will be under ~/.local/share/man
# And supporting data like icons and fonts at ~/.local/share/d2
make install PREFIX=$HOME/.local
```
The install script places the standalone release into `$PREFIX/lib/d2/d2-<version>`
and we recommend doing the same with manually installed releases so that you
know where the release directory is for easy uninstall.
## macOS (Homebrew)
For macOS you may install as so:
```sh
brew tap terrastruct/d2
brew install d2
```

View file

@ -117,12 +117,11 @@ printfp() {(
if [ -z "${FGCOLOR-}" ]; then if [ -z "${FGCOLOR-}" ]; then
FGCOLOR="$(get_rand_color "$prefix")" FGCOLOR="$(get_rand_color "$prefix")"
fi fi
should_color || true
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
should_color || true printf '%s' "$(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")"
printf '%s' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")
else else
should_color || true printf '%s: %s\n' "$(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix")" "$(printf "$@")"
printf '%s: %s\n' $(COLOR=${_COLOR-} setaf "$FGCOLOR" "$prefix") "$(printf "$@")"
fi fi
)} )}
@ -225,6 +224,12 @@ header() {
logp "/* $1 */" logp "/* $1 */"
} }
bigheader() {
logp "/**
* $1
**/"
}
# humanpath replaces all occurrences of " $HOME" with " ~" # humanpath replaces all occurrences of " $HOME" with " ~"
# and all occurrences of '$HOME' with the literal '$HOME'. # and all occurrences of '$HOME' with the literal '$HOME'.
humanpath() { humanpath() {
@ -477,8 +482,11 @@ usage: $arg0 [--dry-run] [--version vX.X.X] [--edge] [--method detect] [--prefix
[--tala latest] [--force] [--uninstall] [--tala latest] [--force] [--uninstall]
install.sh automates the installation of D2 onto your system. It currently only supports 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 the installation of standalone releases from GitHub and via Homebrew on macOS. See the
source, build a release and install from it. docs for --detect below for more information
If you pass --edge, it will clone the source, build a release and install from it.
--edge is incompatible with --tala and currently unimplemented.
Flags: Flags:
@ -488,6 +496,8 @@ Flags:
--version vX.X.X --version vX.X.X
Pass to have install.sh install the given version instead of the latest version. Pass to have install.sh install the given version instead of the latest version.
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--edge --edge
Pass to build and install D2 from source. This will still use --method if set to detect Pass to build and install D2 from source. This will still use --method if set to detect
@ -495,14 +505,15 @@ Flags:
if an unsupported package manager is used. To install from source like a dev would, if an unsupported package manager is used. To install from source like a dev would,
use go install oss.terrastruct.com/d2 use go install oss.terrastruct.com/d2
note: currently unimplemented. note: currently unimplemented.
warn: incompatible with --tala as TALA is closed source.
--method [detect | standalone] --method [detect | standalone | homebrew ]
Pass to control the method by which to install. Right now we only support 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. releases from GitHub but later we'll add support for brew, rpm, deb and more.
note: currently unimplemented.
- detect is currently unimplemented but would use your OS's package manager - detect will use your OS's package manager automatically.
automatically. So far it only detects macOS and automatically uses homebrew.
- homebrew uses https://brew.sh/ which is a macOS and Linux package manager.
- standalone installs a standalone release archive into the unix hierarchy path - standalone installs a standalone release archive into the unix hierarchy path
specified by --prefix which defaults to /usr/local specified by --prefix which defaults to /usr/local
Ensure /usr/local/bin is in your \$PATH to use it. Ensure /usr/local/bin is in your \$PATH to use it.
@ -510,16 +521,19 @@ Flags:
--prefix /usr/local --prefix /usr/local
Controls the unix hierarchy path into which standalone releases are installed. Controls the unix hierarchy path into which standalone releases are installed.
Defaults to /usr/local. You may also want to use ~/.local to avoid needing sudo. Defaults to /usr/local. You may also want to use ~/.local to avoid needing sudo.
Remember that whatever you use, you must have the bin directory of your prefix We use ~/.local by default on arm64 macOS machines as SIP now disables access to
path in \$PATH to execute the d2 binary. For example, if my prefix directory is /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. /usr/local then my \$PATH must contain /usr/local/bin.
--tala [latest] --tala [latest]
Install Terrastruct's closed source TALA for improved layouts. Install Terrastruct's closed source TALA for improved layouts.
See https://github.com/terrastruct/TALA See https://github.com/terrastruct/tala
It optionally takes an argument of the TALA version to install. It optionally takes an argument of the TALA version to install.
Installation obeys all other flags, just like the installation of d2. For example, Installation obeys all other flags, just like the installation of d2. For example,
the d2plugin-tala binary will be installed into /usr/local/bin/d2plugin-tala the d2plugin-tala binary will be installed into /usr/local/bin/d2plugin-tala
warn: The version may not be obeyed with package manager installations. Use
--method=standalone to enforce the version.
--force: --force:
Force installation over the existing version even if they match. It will attempt a Force installation over the existing version even if they match. It will attempt a
@ -544,7 +558,6 @@ EOF
} }
main() { main() {
METHOD=standalone
while flag_parse "$@"; do while flag_parse "$@"; do
case "$FLAG" in case "$FLAG" in
h|help) h|help)
@ -572,8 +585,6 @@ main() {
method) method)
flag_nonemptyarg && shift "$FLAGSHIFT" flag_nonemptyarg && shift "$FLAGSHIFT"
METHOD=$FLAGARG METHOD=$FLAGARG
echoerr "$FLAGRAW is currently unimplemented"
return 1
;; ;;
prefix) prefix)
flag_nonemptyarg && shift "$FLAGSHIFT" flag_nonemptyarg && shift "$FLAGSHIFT"
@ -608,49 +619,86 @@ main() {
PREFIX=${PREFIX:-/usr/local} PREFIX=${PREFIX:-/usr/local}
CACHE_DIR=$(cache_dir) CACHE_DIR=$(cache_dir)
mkdir -p "$CACHE_DIR" mkdir -p "$CACHE_DIR"
METHOD=${METHOD:-detect}
INSTALL_DIR=$PREFIX/lib/d2 INSTALL_DIR=$PREFIX/lib/d2
case $METHOD in
detect)
case "$OS" in
macos)
if command -v brew >/dev/null; then
log "detected macOS with homebrew, using homebrew for (un)installation"
METHOD=homebrew
else
warn "detected macOS without homebrew, falling back to --method=standalone"
METHOD=standalone
fi
;;
*)
warn "unrecognized OS $OS, falling back to --method=standalone"
METHOD=standalone
;;
esac
;;
standalone) ;;
homebrew) ;;
*)
echoerr "unknown (un)installation method $METHOD"
return 1
;;
esac
if [ -n "${UNINSTALL-}" ]; then if [ -n "${UNINSTALL-}" ]; then
uninstall uninstall
return 0 if [ -n "${DRY_RUN-}" ]; then
log "Rerun without --dry-run to execute printed commands and perform uninstall."
fi fi
else
VERSION=${VERSION:-latest}
if [ "$VERSION" = latest ]; then
header "fetching latest release info"
fetch_release_info
fi
install install
if [ -n "${DRY_RUN-}" ]; then
log "Rerun without --dry-run to execute printed commands and perform install."
fi
fi
} }
install() { install() {
install_d2 case $METHOD in
standalone)
install_d2_standalone
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
# Run in subshell to avoid overwriting VERSION. # Run in subshell to avoid overwriting VERSION.
TALA_VERSION="$( install_tala && echo "$VERSION" )" TALA_VERSION="$( RELEASE_INFO= install_tala_standalone && echo "$VERSION" )"
fi fi
;;
homebrew)
install_d2_brew
if [ -n "${TALA-}" ]; then install_tala_brew; fi
;;
esac
FGCOLOR=2 header success FGCOLOR=2 bigheader 'next steps'
case $METHOD in
standalone) install_post_standalone ;;
homebrew) install_post_brew ;;
esac
}
install_post_standalone() {
log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "d2-$VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX" log "tala-$TALA_VERSION-$OS-$ARCH has been successfully installed into $PREFIX"
fi fi
log "Rerun this install script with --uninstall to uninstall" log "Rerun this install script with --uninstall to uninstall."
log
if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then if ! echo "$PATH" | grep -qF "$PREFIX/bin"; then
logcat >&2 <<EOF logcat >&2 <<EOF
%%%%%%%%%%%%%%%%%%%%%%%%%
NEXT STEPS
%%%%%%%%%%%%%%%%%%%%%%%%%
Extend your \$PATH to use d2: Extend your \$PATH to use d2:
export PATH=$PREFIX/bin:\$PATH export PATH=$PREFIX/bin:\$PATH
Then run: Then run:
${TALA+D2_LAYOUT=tala }d2 --help ${TALA+D2_LAYOUT=tala }d2 --help
EOF EOF
else else
log " Run ${TALA+D2_LAYOUT=tala }d2 --help for usage." log "Run ${TALA+D2_LAYOUT=tala }d2 --help for usage."
fi fi
if ! manpath | grep -qF "$PREFIX/share/man"; then if ! manpath | grep -qF "$PREFIX/share/man"; then
logcat >&2 <<EOF logcat >&2 <<EOF
@ -663,36 +711,61 @@ EOF
log " man d2plugin-tala" log " man d2plugin-tala"
fi fi
else else
log " Run man d2 for detailed docs." log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then if [ -n "${TALA-}" ]; then
log " Run man d2plugin-tala for detailed docs." log "Run man d2plugin-tala for detailed TALA docs."
fi fi
fi fi
logcat >&2 <<EOF logcat >&2 <<EOF
Something not working? Please let us know: Something not working? Please let us know:
https://github.com/terrastruct/d2/issues/new https://github.com/terrastruct/d2/issues
https://github.com/terrastruct/d2/discussions
https://discord.gg/NF6X8K4eDq
EOF EOF
} }
install_d2() { install_post_brew() {
log "d2 has been successfully installed with homebrew."
if [ -n "${TALA-}" ]; then
log "tala has been successfully installed with homebrew."
fi
log "Rerun this install script with --uninstall to uninstall."
log
log "Run ${TALA+D2_LAYOUT=tala }d2 --help for usage."
log "Run man d2 for detailed docs."
if [ -n "${TALA-}" ]; then
log "Run man d2plugin-tala for detailed TALA docs."
fi
logcat >&2 <<EOF
Something not working? Please let us know:
https://github.com/terrastruct/d2/issues
https://github.com/terrastruct/d2/discussions
https://discord.gg/NF6X8K4eDq
EOF
}
install_d2_standalone() {
VERSION=${VERSION:-latest}
header "installing d2-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2 >/dev/null; then if command -v d2 >/dev/null; then
INSTALLED_VERSION="$(d2 version)" INSTALLED_VERSION="$(d2 version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as version $VERSION is already installed." log "skipping installation as d2 $VERSION is already installed."
return 0 return 0
fi fi
log "uninstalling $INSTALLED_VERSION to install $VERSION" log "uninstalling d2 $INSTALLED_VERSION to install $VERSION"
if ! uninstall_d2; then if ! uninstall_d2_standalone; then
warn "failed to uninstall $INSTALLED_VERSION" warn "failed to uninstall d2 $INSTALLED_VERSION"
fi fi
fi fi
header "installing d2-$VERSION"
install_standalone_d2
}
install_standalone_d2() {
ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz" ARCHIVE="d2-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github" log "installing standalone release $ARCHIVE from github"
@ -711,19 +784,38 @@ install_standalone_d2() {
"$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'" "$sh_c" sh -c "'cd \"$INSTALL_DIR/d2-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
} }
install_tala() { install_d2_brew() {
REPO="${REPO_TALA:-terrastruct/TALA}" header "installing d2 with homebrew"
VERSION=$TALA sh_c brew tap terrastruct/d2
RELEASE_INFO= sh_c brew install d2
fetch_release_info
header "installing tala-$VERSION"
install_standalone_tala
} }
install_standalone_tala() { install_tala_standalone() {
REPO="${REPO_TALA:-terrastruct/tala}"
VERSION=$TALA
header "installing tala-$VERSION"
if [ "$VERSION" = latest ]; then
fetch_release_info
fi
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
if [ ! "${FORCE-}" -a "$VERSION" = "$INSTALLED_VERSION" ]; then
log "skipping installation as tala $VERSION is already installed."
return 0
fi
log "uninstalling tala $INSTALLED_VERSION to install $VERSION"
if ! uninstall_tala_standalone; then
warn "failed to uninstall tala $INSTALLED_VERSION"
fi
fi
ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz" ARCHIVE="tala-$VERSION-$OS-$ARCH.tar.gz"
log "installing standalone release $ARCHIVE from github" log "installing standalone release $ARCHIVE from github"
fetch_release_info
asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1') asset_line=$(sh_c 'cat "$RELEASE_INFO" | grep -n "$ARCHIVE" | cut -d: -f1 | head -n1')
asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"') asset_url=$(sh_c 'sed -n $((asset_line-3))p "$RELEASE_INFO" | sed "s/^.*: \"\(.*\)\",$/\1/g"')
@ -739,36 +831,42 @@ install_standalone_tala() {
"$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'" "$sh_c" sh -c "'cd \"$INSTALL_DIR/tala-$VERSION\" && make install PREFIX=\"$PREFIX\"'"
} }
install_tala_brew() {
header "installing tala with homebrew"
sh_c brew tap terrastruct/d2
sh_c brew install tala
}
uninstall() { uninstall() {
# We uninstall tala first as package managers require that it be uninstalled before
# uninstalling d2 as TALA depends on d2.
if [ "${TALA-}" ]; then
if command -v d2plugin-tala >/dev/null; then
INSTALLED_VERSION="$(d2plugin-tala --version)"
header "uninstalling tala-$INSTALLED_VERSION"
case $METHOD in
standalone) uninstall_tala_standalone ;;
homebrew) uninstall_tala_brew ;;
esac
else
warn "no version of tala installed"
fi
fi
if ! command -v d2 >/dev/null; then if ! command -v d2 >/dev/null; then
warn "no version of d2 installed" warn "no version of d2 installed"
return 0 return 0
fi fi
INSTALLED_VERSION="$(d2 --version)" INSTALLED_VERSION="$(d2 --version)"
if ! uninstall_d2; then
echoerr "failed to uninstall $INSTALLED_VERSION"
return 1
fi
if [ "${TALA-}" ]; then
if ! command -v d2plugin-tala >/dev/null; then
warn "no version of tala installed"
return 0
fi
INSTALLED_VERSION="$(d2plugin-tala --version)"
if ! uninstall_tala; then
echoerr "failed to uninstall tala $INSTALLED_VERSION"
return 1
fi
fi
return 0
}
uninstall_d2() {
header "uninstalling d2-$INSTALLED_VERSION" header "uninstalling d2-$INSTALLED_VERSION"
uninstall_standalone_d2 case $METHOD in
standalone) uninstall_d2_standalone ;;
homebrew) uninstall_d2_brew ;;
esac
} }
uninstall_standalone_d2() { uninstall_d2_standalone() {
log "uninstalling standalone release of d2-$INSTALLED_VERSION" log "uninstalling standalone release of d2-$INSTALLED_VERSION"
if [ ! -e "$INSTALL_DIR/d2-$INSTALLED_VERSION" ]; then if [ ! -e "$INSTALL_DIR/d2-$INSTALLED_VERSION" ]; then
@ -786,12 +884,11 @@ uninstall_standalone_d2() {
"$sh_c" rm -rf "$INSTALL_DIR/d2-$INSTALLED_VERSION" "$sh_c" rm -rf "$INSTALL_DIR/d2-$INSTALLED_VERSION"
} }
uninstall_tala() { uninstall_d2_brew() {
header "uninstalling tala-$INSTALLED_VERSION" sh_c brew remove d2
uninstall_standalone_tala
} }
uninstall_standalone_tala() { uninstall_tala_standalone() {
log "uninstalling standalone release tala-$INSTALLED_VERSION" log "uninstalling standalone release tala-$INSTALLED_VERSION"
if [ ! -e "$INSTALL_DIR/tala-$INSTALLED_VERSION" ]; then if [ ! -e "$INSTALL_DIR/tala-$INSTALLED_VERSION" ]; then
@ -809,6 +906,10 @@ uninstall_standalone_tala() {
"$sh_c" rm -rf "$INSTALL_DIR/tala-$INSTALLED_VERSION" "$sh_c" rm -rf "$INSTALL_DIR/tala-$INSTALLED_VERSION"
} }
uninstall_tala_brew() {
sh_c brew remove tala
}
is_prefix_writable() { is_prefix_writable() {
sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true sh_c "mkdir -p '$INSTALL_DIR' 2>/dev/null" || true
# The reason for checking whether $INSTALL_DIR is writable is that on macOS you have # The reason for checking whether $INSTALL_DIR is writable is that on macOS you have
@ -864,4 +965,9 @@ fetch_gh() {
sh_c mv "$file.inprogress" "$file" sh_c mv "$file.inprogress" "$file"
} }
brew() {
# Makes brew sane.
HOMEBREW_NO_INSTALL_CLEANUP=1 HOMEBREW_NO_AUTO_UPDATE=1 command brew "$@"
}
main "$@" main "$@"

View file

@ -1,4 +1,4 @@
package version package version
// Pre-built binaries will have version set during build time. // Pre-built binaries will have version set during build time.
var Version = "master (built from source)" var Version = "????"