Last active
May 18, 2023 06:41
-
-
Save paulojeronimo/95977442a96c0c6571064d10c997d3f2 to your computer and use it in GitHub Desktop.
docker-asciidoctor-builder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
BUILD_DIR=${BUILD_DIR:-build} | |
ADOC=${ADOC:-README.adoc} | |
REMOTE_REPO=${REMOTE_REPO:-`git config --get remote.origin.url`} || : | |
GENERATE_PDF=${GENERATE_PDF:-false} | |
HTML_NAME=${HTML_NAME:-index.html} | |
PDF_NAME=${PDF_NAME:-`basename "$BASE_DIR".pdf`} | |
SERVE_PORT=${SERVE_PORT:-5001} | |
MAIN_BRANCH=${MAIN_BRANCH:-main} | |
RESTART_GH_PAGES=${RESTART_GH_PAGES:-true} | |
USE_DOCKER=${USE_DOCKER:-true} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eou pipefail | |
BASE_DIR=${BASE_DIR:-$PWD} | |
! [[ `uname` = Darwin ]] || { | |
command -v greadlink &> /dev/null || { | |
echo "Install greadlink! ($ brew install coreutils)" | |
exit 1 | |
} | |
readlink() { greadlink "${@:-}"; } | |
} | |
BUILDER_DIR=`dirname "$(readlink -m "$(which "$0")")"` | |
cd "$BASE_DIR" | |
config=.${0##*/} | |
defaults=~/$config | |
source ./$config 2>&- && { | |
echo "File \"$config\" found. Merging it with defaults (~/${defaults##$HOME/})." | |
} || { | |
echo "File \"$config\" not found. Using defaults (~/${defaults##$HOME/})." | |
} | |
[ -f "$BUILDER_DIR" ] || (cd; ln -sf "${BUILDER_DIR##$HOME/}/$config" $defaults) | |
source "$defaults" | |
echo "Base dir is \"$PWD\"." | |
echo "USE_DOCKER is \"$USE_DOCKER\"." | |
# tag::INTERNAL_FUNCTIONS[] | |
__docker-asciidoctor() { | |
local installed_docker=$(which docker || echo -n "uninstalled") | |
case $1 in | |
asciidoctor) echo "Generating HTML version ($HTML_NAME) ...";; | |
asciidoctor-pdf) echo "Generating PDF version ($PDF_NAME) ...";; | |
esac | |
if [ "$installed_docker" = "uninstalled" ] | |
then | |
echo "WARNING: docker isn't installed! fallback to \"$1\"" | |
eval "$@" | |
else | |
if $USE_DOCKER | |
then | |
$installed_docker run -e TZ=America/Sao_Paulo -it --rm \ | |
-v "$PWD":/documents \ | |
asciidoctor/docker-asciidoctor "$@" | |
else | |
eval "$@" | |
fi | |
fi | |
case "`uname`" in | |
Linux) sudo chown -R $USER: "${BUILD_DIR%%/*}";; | |
esac | |
} | |
__generate-html() { | |
__docker-asciidoctor asciidoctor -D "$BUILD_DIR" ${attrs:-} "$ADOC" -o "$HTML_NAME" | |
} | |
__generate-pdf() { | |
__docker-asciidoctor asciidoctor-pdf -D "$BUILD_DIR" ${attrs:-} "$ADOC" -o "$PDF_NAME" | |
} | |
# end::INTERNAL_FUNCTIONS[] | |
html() { | |
local tag | |
local attrs | |
local production=false | |
while [ "${1:-}" ]; do | |
case "$1" in | |
-a) | |
shift | |
[ "${1:-}" ] && attrs="${attrs:-} -a $1" || { | |
echo "Please, specify the a value!" | |
exit 1 | |
} | |
;; | |
-t|--tag) | |
shift | |
[ "${1:-}" ] && tag=$1 || { | |
echo "Please, specify the tag!" | |
exit 1 | |
} | |
git stash &> /dev/null | |
git tag -l | grep -q "^$tag$" || { | |
echo "Git tag is invalid!" | |
git stash pop &> /dev/null | |
exit 1 | |
} | |
git checkout $tag &> /dev/null | |
BUILD_DIR=$BUILD_DIR/$tag | |
;; | |
-p) production=true;; | |
-g|--generate-pdf) GENERATE_PDF=true;; | |
esac | |
shift || break | |
done | |
[ -f "$ADOC" ] || { | |
>&2 echo "File \"$ADOC\" not found!" | |
exit 1 | |
} | |
echo -n "Building $ADOC " | |
$production && echo "for production ..." || echo "for development ..." | |
if ! $production && ! [ "${attrs:-}" ]; then attrs="-a env-localhost"; fi | |
__generate-html | |
! $GENERATE_PDF || __generate-pdf | |
! [ -d images ] || { | |
if [ -L images ] | |
then | |
(cd "$BUILD_DIR"; rm -rf images; ln -s "$OLDPWD"/images) | |
else | |
rsync -a images "$BUILD_DIR"/ | |
fi | |
} | |
if [ "${tag:-}" ]; then | |
git checkout master &> /dev/null | |
git stash pop &> /dev/null || : | |
fi | |
} | |
# TODO | |
pdf() { | |
: | |
} | |
gh-pages() { | |
[ -d $BUILD_DIR ] || { echo "\"$BUILD_DIR\" directory does not exists!"; return 1; } | |
echo "Publish the contents in \"$BUILD_DIR\" to GitHub Pages ..." | |
local msg="Published at `date +"%F %T %z"`" | |
cd "$BUILD_DIR" | |
if $RESTART_GH_PAGES | |
then | |
rm -rf .git | |
git init | |
fi | |
git add -A | |
git commit -m "$msg" | |
git push --force $REMOTE_REPO $MAIN_BRANCH:gh-pages | |
} | |
serve() { | |
local serve_bin | |
serve_bin=$(which serve 2> /dev/null) || { | |
echo "The utility \"serve\" was not found in PATH!" | |
return 1 | |
} | |
$serve_bin -l $SERVE_PORT "$BUILD_DIR" | |
} | |
usage() { | |
echo "Usage: $0 <[html] [-<t|-tag> <tag>]|gh-pages>" | |
exit 0 | |
} | |
if [ "${1:-}" ]; then | |
[[ $1 =~ ^- ]] && { task=html; set -- html "$@"; } || task=$1 | |
else | |
task=html | |
fi | |
shift || : | |
type $task &> /dev/null || usage | |
$task "${@:-}" | |
# vim: et ts=2 sw=2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -eou pipefail | |
SCRIPT=${SCRIPT:-docker-asciidoctor-builder} | |
BASE_DIR=$(cd "`dirname "$0"`"; pwd) | |
# install the script | |
cd /usr/local/bin | |
sudo rm -f $SCRIPT | |
sudo ln -s $BASE_DIR/$SCRIPT | |
# install the default user configuration | |
cd | |
ln -sf $BASE_DIR/.$SCRIPT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment