From 5f05cb2eb5f50697244f8d1657e02150dd30a05d Mon Sep 17 00:00:00 2001 From: Andreas Hartmann Date: Thu, 24 Feb 2022 08:59:01 +0100 Subject: [PATCH 1/5] Containerfile: Add a container for the docs that includes the doc dependencies. Builds a container of about 145 MB that builds and serves the docs locally. --- Containerfile | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Containerfile diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..823587b --- /dev/null +++ b/Containerfile @@ -0,0 +1,8 @@ +FROM docker.io/peaceiris/mdbook:v0.4.15 + +RUN apk update && apk add hugo + +# We must give it a bind address, otherwise it will listen to connections from +# 127.0.0.1. Since we connect to the server from "outside" the container, this +# doesn't work. +ENTRYPOINT mdbook watch docs/ -d ../static/documentation & hugo server --bind 0.0.0.0; From 41f559422116e6ef3c90ef7549e372b610ed5251 Mon Sep 17 00:00:00 2001 From: Andreas Hartmann Date: Thu, 24 Feb 2022 09:00:14 +0100 Subject: [PATCH 2/5] watch-serve.sh: Add container support for people that have a container runtime available and prefer not to install the dependencies locally. If the dependencies are installed locally, it will call these directly instead of using the container. Otherwise it will check if podman is installed and use that, or docker otherwise, build the container from the `Containerfile` and run it with the project directory mounted. If neither the dependencies nor a suitable container runtime can be found, a messages is printed to stdout that tells the user what is needed to use the script. --- watch-serve.sh | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/watch-serve.sh b/watch-serve.sh index 9310ae5..c1802b4 100755 --- a/watch-serve.sh +++ b/watch-serve.sh @@ -1,3 +1,42 @@ #!/usr/bin/env bash -{ mdbook watch docs/ -d ../static/documentation & hugo server; } +# Return 0 if command $1 exists, 1 otherwise. +function _exists { + command -v "$1" >/dev/null +} + +if [[ $(_exists mdbook) && $(_exists hugo) ]]; then + { mdbook watch docs/ -d ../static/documentation & hugo server; } + exit 0 +fi + +# Some pre-requisites are missing, try to use containers +CRT="" +CONTAINER_NAME="zellij-docs:latest" + +if $(_exists podman); then + CRT="$(which podman)" + echo "Using '$CRT' as container runtime" + + $CRT build --tag "$CONTAINER_NAME" -f Containerfile + $CRT run --userns keep-id --user "$(id -u):$(id -g)" -v "$PWD:$PWD:z" -w "$PWD" -p 1313:1313 $CONTAINER_NAME + +elif $(_exists docker); then + CRT="$(which docker)" + echo "Using '$CRT' as container runtime" + + $CRT build --tag "$CONTAINER_NAME" Date: Mon, 28 Feb 2022 09:54:57 +0100 Subject: [PATCH 3/5] watch-serve: Remove containers after use Add the `--rm` flag to automatically delete the containers after they have been used. The script won't restart existing containers anyways, so there is no point in keeping them. It will only fill up the users disk. --- watch-serve.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/watch-serve.sh b/watch-serve.sh index c1802b4..3e975b4 100755 --- a/watch-serve.sh +++ b/watch-serve.sh @@ -19,14 +19,14 @@ if $(_exists podman); then echo "Using '$CRT' as container runtime" $CRT build --tag "$CONTAINER_NAME" -f Containerfile - $CRT run --userns keep-id --user "$(id -u):$(id -g)" -v "$PWD:$PWD:z" -w "$PWD" -p 1313:1313 $CONTAINER_NAME + $CRT run --rm -it --userns keep-id --user "$(id -u):$(id -g)" -v "$PWD:$PWD:z" -w "$PWD" -p 1313:1313 $CONTAINER_NAME elif $(_exists docker); then CRT="$(which docker)" echo "Using '$CRT' as container runtime" $CRT build --tag "$CONTAINER_NAME" Date: Tue, 1 Mar 2022 08:19:31 +0100 Subject: [PATCH 4/5] watch-serve.sh: Add nix support Calls the script with `nix-shell --command` if it detects that nix is present on the host. --- watch-serve.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/watch-serve.sh b/watch-serve.sh index 3e975b4..3d88971 100755 --- a/watch-serve.sh +++ b/watch-serve.sh @@ -5,7 +5,7 @@ function _exists { command -v "$1" >/dev/null } -if [[ $(_exists mdbook) && $(_exists hugo) ]]; then +if $(_exists mdbook) && $(_exists hugo); then { mdbook watch docs/ -d ../static/documentation & hugo server; } exit 0 fi @@ -14,7 +14,12 @@ fi CRT="" CONTAINER_NAME="zellij-docs:latest" -if $(_exists podman); then +if $(_exists nix-shell); then + echo "Using nix to execute script" + + nix-shell --command "$0" + +elif $(_exists podman); then CRT="$(which podman)" echo "Using '$CRT' as container runtime" @@ -31,6 +36,7 @@ elif $(_exists docker); then else echo "You must have installed either of:" echo "" + echo " - nix" echo " - docker" echo " - podman" echo " - mdbook AND hugo" From df6dc54ae394a6c0eafc70234be139bd8bd1a037 Mon Sep 17 00:00:00 2001 From: Andreas Hartmann Date: Wed, 2 Mar 2022 11:19:35 +0100 Subject: [PATCH 5/5] watch-serve.sh: Prefer 'nix develop' over 'nix-shell' when running the dependencies through nix. This makes for a better experience for people having flakes enabled, while still retaining the regular `nix-shell` as fallback for people that don't. --- watch-serve.sh | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/watch-serve.sh b/watch-serve.sh index 3d88971..1b2cf64 100755 --- a/watch-serve.sh +++ b/watch-serve.sh @@ -14,10 +14,16 @@ fi CRT="" CONTAINER_NAME="zellij-docs:latest" -if $(_exists nix-shell); then - echo "Using nix to execute script" +if $(_exists nix); then + echo "Trying 'nix develop' to execute script" - nix-shell --command "$0" + nix develop --command "$0" + + if [[ $? -ne 0 ]] && $(_exists nix-shell); then + echo "Using 'nix-shell' to execute script instead" + + nix-shell --command "$0" + fi elif $(_exists podman); then CRT="$(which podman)"