Skip to content

Instantly share code, notes, and snippets.

@cardin
Last active February 5, 2022 15:20
Show Gist options
  • Save cardin/d927cca9beefec510c6c545f26ca2687 to your computer and use it in GitHub Desktop.
Save cardin/d927cca9beefec510c6c545f26ca2687 to your computer and use it in GitHub Desktop.
Export PlantUML diagrams using Docker PlantUML Server
#!/usr/bin/env bash
########################
# Exports PlantUML diagrams using a Docker PlantUML Server.
# ARGS:
# $1 : Target directory to recursively search
# CONFIG:
# $port : Port to bind the PlantUML Server to
# $dockerName : Name of the Docker container
# $fileExt : File extensions that belong to PlantUML
# $dryRun : Detect files but not process them
########################
set -e
port=7985
dockerName=plantuml-exporter
fileExt="puml|pu|plantuml"
targetDir=${1:-.}
dryRun=false
function setup {
if ! $dryRun ; then
echo "Starting Docker" > /dev/tty
docker run -d --rm -p $port:8080 --name $dockerName plantuml/plantuml-server:jetty
sleep 5
fi
}
function shutdown {
if ! $dryRun ; then
echo "Stopping Docker" > /dev/tty
docker stop $dockerName
fi
}
function run_file {
hexValue=$(hexdump -e '16/1 "%02x"' "$1")
url="http://localhost:$port/png/~h$hexValue"
filename="$(echo "$1" | sed -r "s/(.+)\..+/\1/").png"
if ! $dryRun ; then
wget $url -O "$filename"
else
echo $url $filename > /dev/tty
fi
}
echo -e "Target Directory: $targetDir\n"
setup
for fp in $(find $targetDir -regextype posix-extended -regex ".*\.($fileExt)" -type f)
do
echo "=================="
echo $fp
echo "=================="
run_file $(realpath $fp)
done
shutdown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment