Skip to content

Instantly share code, notes, and snippets.

@OnceUponALoop
Created July 21, 2024 01:07
Show Gist options
  • Save OnceUponALoop/fec02e3828853e7e1bf696818a6436a3 to your computer and use it in GitHub Desktop.
Save OnceUponALoop/fec02e3828853e7e1bf696818a6436a3 to your computer and use it in GitHub Desktop.
This script acts as a wrapper for the PlantUML jar file, making it easier to call the jar file with the appropriate arguments. It allows specifying the path to the Java binary and the PlantUML jar file via command-line arguments or environment variables, and it validates the existence of these files before execution.
#!/usr/bin/env bash
################################################################################
# plantuml.sh - A script to wrap PlantUML jar execution
#
# Author: Firas AlShafei
# Created: 2024.07.20
# Last Modified: 2024.07.20
#
# Description:
# This script acts as a wrapper for the PlantUML jar file, making it easier to
# call the jar file with the appropriate arguments. It allows specifying the
# path to the Java binary and the PlantUML jar file via command-line arguments
# or environment variables, and it validates the existence of these files before
# execution.
#
# Usage:
# ./plantuml.sh [--jar <jar_path>] [--java <java_bin>] [PlantUML options]
#
# Options:
# --jar Path to the PlantUML jar file (default: ./plantuml.jar)
# --java Path to the Java binary (default: java)
# --usage Display this help message
#
# Example:
# ./plantuml.sh --jar ./plantuml.jar --java /usr/bin/java -tpng mydiagram.puml
################################################################################
set -euo pipefail
# Default paths
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEFAULT_PLANTUML_JAR="$SCRIPT_DIR/plantuml.jar"
DEFAULT_JAVA_BIN="java"
# Allow user to override defaults via environment variables
PLANTUML_JAR="${PLANTUML_JAR_PATH:-$DEFAULT_PLANTUML_JAR}"
JAVA_BIN="${JAVA_BIN_PATH:-$DEFAULT_JAVA_BIN}"
# Function to display usage information
usage() {
echo "Usage: $0 [--jar <jar_path>] [--java <java_bin>] [PlantUML options]"
echo "Options:"
echo " --jar Path to the PlantUML jar file (default: $DEFAULT_PLANTUML_JAR)"
echo " --java Path to the Java binary (default: $DEFAULT_JAVA_BIN)"
echo " --usage Display this help message"
}
# Function to parse command-line arguments manually
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--jar)
PLANTUML_JAR="$2"
shift 2
;;
--java)
JAVA_BIN="$2"
shift 2
;;
--usage)
usage
exit 0
;;
--)
shift
break
;;
*)
break
;;
esac
done
PLANTUML_OPTS=("$@")
}
# Function to validate paths
validate_paths() {
if ! command -v "$JAVA_BIN" &> /dev/null; then
echo "Error: Java binary not found at '$JAVA_BIN'. Please install Java or specify the correct path." >&2
exit 1
fi
if [[ ! -f "$PLANTUML_JAR" ]]; then
echo "Error: PlantUML jar file not found at '$PLANTUML_JAR'. Please specify the correct path." >&2
exit 1
fi
}
# Main script execution
main() {
parse_args "$@"
validate_paths
"$JAVA_BIN" -jar "$PLANTUML_JAR" "${PLANTUML_OPTS[@]}"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment