Skip to content

Instantly share code, notes, and snippets.

@ifnull
Created June 19, 2024 00:10
Show Gist options
  • Save ifnull/25c49f3f0428be85fcb1b4ed39d34bee to your computer and use it in GitHub Desktop.
Save ifnull/25c49f3f0428be85fcb1b4ed39d34bee to your computer and use it in GitHub Desktop.
Watcher for PlantUML on MacOS

Install

brew install plantuml fswatch
# Download plantuml-watch.sh
chmod +x ./plantuml-watch.sh
mkdir -p /usr/local/bin
mv ./plantuml-watch.sh /usr/local/bin/plantuml-watch
# Confirm /usr/local/bin is in PATH or mv script to directory in PATH
echo $PATH

Usage

Use plantuml-watch exactly as you would plantuml. Assuming the file foobar.puml is in the current directory, you should see the output below and the default app for the output filetype should open automatically and refresh after every saved change to the the source file.

$ plantuml-watch ./foobar.puml 
Watching ./foobar.puml for changes...
#!/bin/bash
# Check if at least one argument is provided
if [ $# -lt 1 ]; then
echo "Usage: $0 <file.puml> [additional arguments for plantuml]"
exit 1
fi
# Get the file to watch and any additional arguments for plantuml
WATCH_FILE="$1"
shift
PLANTUML_ARGS="$@"
# Extract the base name and directory of the WATCH_FILE
BASE_NAME=$(basename "$WATCH_FILE" .puml)
DIR_NAME=$(dirname "$WATCH_FILE")
# Determine the output format and file extension
OUTPUT_FORMAT="png" # Default output format
EXTENSION="png" # Default extension
# Check for output format in the arguments
for arg in "$@"; do
if [[ "$arg" == "-t"* ]]; then
OUTPUT_FORMAT="${arg#-t}"
EXTENSION="${OUTPUT_FORMAT}"
break
fi
done
# Handle cases where the extension might be special (e.g., "-txmi")
if [[ "$EXTENSION" == "xmi" || "$EXTENSION" == "dot" || "$EXTENSION" == "txt" || "$EXTENSION" == "pdf" ]]; then
EXTENSION="${OUTPUT_FORMAT}"
elif [[ "$EXTENSION" == "svg" ]]; then
EXTENSION="svg"
elif [[ "$EXTENSION" == "eps" ]]; then
EXTENSION="eps"
else
EXTENSION="png" # Default to PNG for unknown types
fi
# Determine the output file path
OUTPUT_FILE="${DIR_NAME}/${BASE_NAME}.${EXTENSION}"
echo "Watching $WATCH_FILE for changes..."
# Initial run of plantuml
plantuml "$WATCH_FILE" $PLANTUML_ARGS
# Open the initial output file
if [ -f "$OUTPUT_FILE" ]; then
open "$OUTPUT_FILE"
fi
# Use fswatch to watch for changes and re-run plantuml
fswatch -o "$WATCH_FILE" | while read f; do
echo "File $WATCH_FILE changed, re-running plantuml..."
plantuml "$WATCH_FILE" $PLANTUML_ARGS
if [ -f "$OUTPUT_FILE" ]; then
open "$OUTPUT_FILE"
else
echo "Output file not found: $OUTPUT_FILE"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment