|
#!/bin/bash |
|
|
|
# Check if at least one argument is provided |
|
if [ $# -lt 1 ]; then |
|
echo "Usage: $0 \"<file.puml or wildcard>\" [additional arguments for plantuml]" |
|
exit 1 |
|
fi |
|
|
|
# Expand wildcard pattern into a list of files |
|
WATCH_FILES=$(ls $1 2>/dev/null) |
|
if [ -z "$WATCH_FILES" ]; then |
|
echo "No files matching pattern: $1" |
|
exit 1 |
|
fi |
|
|
|
shift |
|
PLANTUML_ARGS="$@" |
|
|
|
# Function to process a single file |
|
process_file() { |
|
local file="$1" |
|
local base_name=$(basename "$file" .puml) |
|
local dir_name=$(dirname "$file") |
|
|
|
# Determine the output format and file extension |
|
local output_format="png" # Default output format |
|
local extension="png" # Default extension |
|
|
|
for arg in $PLANTUML_ARGS; do |
|
if [[ "$arg" == "-t"* ]]; then |
|
output_format="${arg#-t}" |
|
extension="${output_format}" |
|
break |
|
fi |
|
done |
|
|
|
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" |
|
fi |
|
|
|
local output_file="${dir_name}/${base_name}.${extension}" |
|
|
|
echo "Generating diagram for $file..." |
|
plantuml "$file" $PLANTUML_ARGS |
|
|
|
if [ -f "$output_file" ]; then |
|
open "$output_file" |
|
else |
|
echo "Output file not found: $output_file" |
|
fi |
|
} |
|
|
|
# Initial run for all matched files |
|
for file in $WATCH_FILES; do |
|
process_file "$file" |
|
done |
|
|
|
# Watch for changes using fswatch |
|
echo "Watching for changes in:" |
|
for file in $WATCH_FILES; do |
|
echo "$file" |
|
done |
|
|
|
# Use fswatch and ensure the output is properly handled |
|
fswatch $WATCH_FILES | while read -r changed_file; do |
|
# Check if the detected file is valid |
|
if [ -f "$changed_file" ]; then |
|
echo "File $changed_file changed, re-running plantuml..." |
|
process_file "$changed_file" |
|
else |
|
echo "Change detected, but no valid file found: $changed_file" |
|
fi |
|
done |