Skip to content

Instantly share code, notes, and snippets.

@sierisimo
Last active January 31, 2019 06:12
Show Gist options
  • Save sierisimo/7074062e9196bab006923318d0e47b1d to your computer and use it in GitHub Desktop.
Save sierisimo/7074062e9196bab006923318d0e47b1d to your computer and use it in GitHub Desktop.
A small shell script that I wrote to download the plantuml jar and convert iuml files in a directory to svg or png
#! /usr/bin/env bash
PLANTUML_EXECUTABLE="plantuml.jar"
PLANTUML_URL="http://sourceforge.net/projects/plantuml/files/plantuml.jar/download"
function usage {
printf "Usage: $0 [-h] [-f <FORMAT>]\n\\ntWhere <FORMAT> can be:\n\t\tpng,svg" 1>&2
exit 1
}
function is_bin_in_path {
builtin type -P "$1" &> /dev/null
}
function download_bin {
printf "Checking for needed tools…\n"
is_bin_in_path curl
if [[ $? -eq 0 ]]; then
DOWNLOAD_TOOL=curl
DOWNLOAD_FLAGS="-L"
DOWNLOAD_OUTPUT_FLAG="-o"
else
printf "cURL not found searching for wget…\n"
is_bin_in_path wget
if [[ $? -eq 0 ]]; then
DOWNLOAD_TOOL=wget
DOWNLOAD_FLAGS="-q"
DOWNLOAD_OUTPUT_FLAG="-O"
else
printf "Tools for downloading not found in your system, exiting now\n"
exit 1
fi
fi
printf "Downloading…\n"
$DOWNLOAD_TOOL ${DOWNLOAD_FLAGS} ${PLANTUML_URL} ${DOWNLOAD_OUTPUT_FLAG} ${PLANTUML_EXECUTABLE}
}
### Start of actual operation ###
printf "Welcome to Diagrams builder\n"
printf "Checking for java in system…\n"
is_bin_in_path java
if [[ $? -ne 0 ]]; then
printf "Java is necessary for running this script. Please set your JAVA_HOME and add Java to your PATH"
fi
printf "Checking if executable is available in current directory…\n"
if ! [[ -f ${PLANTUML_EXECUTABLE} ]]; then
download_bin
fi
EXTENSION="png"
while getopts ":hf:" option; do
case "${option}" in
h)
usage
;;
f)
EXTENSION=${OPTARG}
;;
:)
printf "Option -${OPTARG} requires an argument. Use -h to see possible arguments" >&2
exit 1
;;
*)
;;
esac
done
shift $((OPTIND-1))
if [[ ${EXTENSION} = "png" ]]; then
FORMAT="-tpng"
#elif [[ ${EXTENSION} = "pdf" ]]; then
# FORMAT="-tpdf"
elif [[ ${EXTENSION} = "svg" ]]; then
FORMAT="-tsvg"
else
printf "Unsupported format: $FORMAT\n"
exit 1
fi
printf "Using format: ${EXTENSION} for files in current directory…\n"
for file_ in *.iuml; do
[ -f "$file_" ] || break
printf "Creating diagram for file ${file_}\n"
java -jar plantuml.jar ${FORMAT} -o "out" ${file_}
printf "Diagram created at out/${file_%.iuml}.${EXTENSION}\n"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment