ob1
ob1

Reputation: 1792

Graphviz doesn't refresh

Until recently, when I saved a change to a .dot file in TextMate, Graphviz would detect the change and redraw. Now it doesn't. I've tried moving the file to different locations to no avail. This is now true of all files.

Upvotes: 0

Views: 383

Answers (1)

SSteve
SSteve

Reputation: 10728

I don't use TextMate so I can't provide a TextMate-specific answer (although I did find this answer on SuperUser which might be helpful). I thought maybe Folder Actions but they only seem to work when adding a file to a folder, not when an existing file is changed. So I decided to look for a bash-specific answer. I came across fswatch. Using this, you can accomplish what you want.

Create the following folder structure:

folder structure

The way I implemented this, fswatch and rundot.sh must be in the same folder as your Graphviz files.

rundot.sh iterates through your Graphviz files and compiles them if necessary:

#!/bin/sh

graphvizExtension=gv #Change "gv" to the extension you use for your Graphviz files
graphicFormat=png #Change "png" to the file format you are using

for gvfile in *.$graphvizExtension
do
    filename=$(basename "$gvfile")
    outfile="../output/${filename%.*}.$graphicFormat" #build output file name
    if [[ ! -f $outfile || $gvfile -nt $outfile ]]; then
        #output file doesn't exist or Graphviz file is newer than output file
        echo "compiling" $gvfile "to" $outfile
        dot -T$graphicFormat "$gvfile" -o"$outfile"
    else
        #This is mainly for testing. You can delete the else clause if you want
        echo "not necessary to compile" $gvfile
    fi
done

Go to the gv folder in Terminal and type this command:

./fswatch . "./rundot.sh"

Now whenever there is a change in the gv folder, any Graphviz file that is newer than its corresponding output file will be compiled and its output stored in the output folder. You could store the output files in the gv folder but when the output file is changed it triggers rundot.sh again. My original version compiled every Graphviz file every time and thus wound up in an infinite loop. This version that checks timestamps will get triggered again but won't change any output files the second time so won't get stuck in an infinite loop.

The code to extract the base filename came from this answer.

Upvotes: 1

Related Questions