Skip to content

Instantly share code, notes, and snippets.

@PatOConnor43
Created July 21, 2021 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatOConnor43/60bd5104847e38357c5f0a4185887978 to your computer and use it in GitHub Desktop.
Save PatOConnor43/60bd5104847e38357c5f0a4185887978 to your computer and use it in GitHub Desktop.
This is a small script that tries to intelligently download plantuml and keep it up-to-date. It also exposes some functions to generate svgs and pngs.
#!/bin/bash
tool_dir=$(dirname $0)
scriptpath="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
plant="$tool_dir/plantuml.jar"
function log {
if [ "$DEBUG" == "true" ]; then
echo "$@"
fi
}
function getplant {
# First check the modified date to see if the file is older than 10 days.
# Doing time calculations here which are a bit of a cluster. The point of the calculation
# is to find out if this file is 10 days old. I'm accomplishing that by getting the
# modified_time of the plant file (seconds) and subtracting that from "now".
# Next, I compare that diff to to see if it is less than the amount of seconds
# in 10 days. "time_diff" will get bigger as time increases.
modified_time=$(stat -c '%Y' "$plant")
log "modified_time: $modified_time"
time_diff=$((`date +%s`-$modified_time))
log "time_diff: $time_diff"
if [ "$time_diff" -lt $((60 * 60 * 24 * 10)) ]; then
log "Recent version, no need to check"
return 0
fi
# If the jar is older than 10 days, check the md5 locally against sourceforge.
md5=''
if [ -f "$plant" ]; then
md5=$(md5sum "$plant" | cut -d ' ' -f1)
fi
log $md5
json=$(curl -s 'https://sourceforge.net/projects/plantuml/best_release.json')
# Grabbing the linux release but it should work for macos too
newmd5=$(jq -r '.platform_releases.linux.md5sum' <<< $json)
log $newmd5
if [ "$md5" == "$newmd5" ] || [ -z "$newmd5" ]; then
# Bail if the md5 matches or we don't get an md5 from sourceforge
log "same file, bailing"
return 0
fi
url=$(jq '.platform_releases.linux.url' <<< $json)
log "download url: $url"
curl -L -s -o "$plant" "$url"
}
function find_git {
echo "$(git rev-parse --show-toplevel)"
}
function make_svgs {
toplevel=$(find_git)
find $toplevel -type f -name '*.puml' -exec java -jar "$plant" {} -tsvg \;
}
function make_pngs {
toplevel=$(find_git)
find $toplevel -type f -name '*.puml' -exec java -jar "$plant" {} -tpng \;
}
case "$1" in
"") ;;
getplant) getplant; exit;;
make_pngs) make_pngs; exit;;
make_svgs) make_svgs; exit;;
*) echo "Unkown function: $1"; exit 2;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment