Skip to content

Instantly share code, notes, and snippets.

@ifnull
Last active January 16, 2025 19:12
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.

Revisions

  1. ifnull revised this gist Jan 16, 2025. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,8 @@ echo $PATH
    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...
    $ plantuml-watch "*.puml"
    Watching for changes in:
    foo.puml
    bar.puml
    ```
  2. ifnull revised this gist Jan 16, 2025. 1 changed file with 22 additions and 12 deletions.
    34 changes: 22 additions & 12 deletions plantuml-watch.sh
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,13 @@ if [ $# -lt 1 ]; then
    exit 1
    fi

    # Get the files to watch and any additional arguments for plantuml
    WATCH_FILES="$1"
    # 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="$@"

    @@ -21,7 +26,6 @@ process_file() {
    local output_format="png" # Default output format
    local extension="png" # Default extension

    # Check for output format in the arguments
    for arg in $PLANTUML_ARGS; do
    if [[ "$arg" == "-t"* ]]; then
    output_format="${arg#-t}"
    @@ -30,25 +34,21 @@ process_file() {
    fi
    done

    # Handle cases where the extension might be special
    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
    extension="png"
    fi

    # Determine the output file path
    local output_file="${dir_name}/${base_name}.${extension}"

    # Run plantuml for the file
    echo "Generating diagram for $file..."
    plantuml "$file" $PLANTUML_ARGS

    # Open the output file if it exists
    if [ -f "$output_file" ]; then
    open "$output_file"
    else
    @@ -62,8 +62,18 @@ for file in $WATCH_FILES; do
    done

    # Watch for changes using fswatch
    echo "Watching $WATCH_FILES for changes..."
    fswatch -o $WATCH_FILES | while read changed_file; do
    echo "File $changed_file changed, re-running plantuml..."
    process_file "$changed_file"
    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
  3. ifnull revised this gist Jan 16, 2025. 1 changed file with 50 additions and 45 deletions.
    95 changes: 50 additions & 45 deletions plantuml-watch.sh
    Original file line number Diff line number Diff line change
    @@ -2,63 +2,68 @@

    # Check if at least one argument is provided
    if [ $# -lt 1 ]; then
    echo "Usage: $0 <file.puml> [additional arguments for plantuml]"
    echo "Usage: $0 \"<file.puml or wildcard>\" [additional arguments for plantuml]"
    exit 1
    fi

    # Get the file to watch and any additional arguments for plantuml
    WATCH_FILE="$1"
    # Get the files to watch and any additional arguments for plantuml
    WATCH_FILES="$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")
    # 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
    OUTPUT_FORMAT="png" # Default output format
    EXTENSION="png" # Default extension
    # Determine the output format and file extension
    local output_format="png" # Default output format
    local 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}"
    # Check for output format in the arguments
    for arg in $PLANTUML_ARGS; do
    if [[ "$arg" == "-t"* ]]; then
    output_format="${arg#-t}"
    extension="${output_format}"
    break
    fi
    done

    echo "Watching $WATCH_FILE for changes..."
    # Handle cases where the extension might be special
    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

    # Initial run of plantuml
    plantuml "$WATCH_FILE" $PLANTUML_ARGS
    # Determine the output file path
    local output_file="${dir_name}/${base_name}.${extension}"

    # Open the initial output file
    if [ -f "$OUTPUT_FILE" ]; then
    open "$OUTPUT_FILE"
    fi
    # Run plantuml for the file
    echo "Generating diagram for $file..."
    plantuml "$file" $PLANTUML_ARGS

    # 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"
    # Open the output file if it exists
    if [ -f "$output_file" ]; then
    open "$output_file"
    else
    echo "Output file not found: $OUTPUT_FILE"
    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 $WATCH_FILES for changes..."
    fswatch -o $WATCH_FILES | while read changed_file; do
    echo "File $changed_file changed, re-running plantuml..."
    process_file "$changed_file"
    done
  4. ifnull created this gist Jun 19, 2024.
    20 changes: 20 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # 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...
    ```
    64 changes: 64 additions & 0 deletions plantuml-watch.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    #!/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