1

Here's a minimal makefile for using pandoc to compile md to pdf. The make watch command watches for changed md files (using fswatch) and triggers make if so.

SRCS=$(wildcard *.md)
PDFS=$(SRCS:.md=.pdf)

all:    $(PDFS)

%.pdf: %.md
    @pandoc $< -o $@

watch: $(SRCS)
    @fswatch -o $^ | xargs -n1 -I{} make

Currently, watch isn't very selective: even if just one md file is changed, it builds all possible targets (everything in PDFS). I would like a version of this code that watches all the md files for changes, but only builds a pdf for the changed md files. (I realize this is kind of pointless for the present case, but it's useful in another, more complicated use case.)

SEC
  • 799
  • 4
  • 16

2 Answers2

1

The following seems to work:

@fswatch -0 $^ | xargs -0 -n1 sh -c 'ALT=`basename "$$1"`; make $${ALT/.md/.pdf}' _

$$1 ends up identified with /path/to/changed_file.md as returned by fswatch. A couple string manipulations give changed_file.pdf, which is fed to make.

SEC
  • 799
  • 4
  • 16
0

Why don't you pass the target you want to be recreated as the goal target on the command line?

@fswatch -o $^ | xargs -n1 -I{} $(MAKE) '{}'
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • Thanks, but this doesn't seem to work: make[1]: *** No rule to make target `1'. Stop. – SEC Oct 16 '16 at 15:54
  • I guess you shouldn't really need the `{}`. Well, I don't know how fswatch works. Clearly something is not as expected with it's output (for example, it's not just printing a list of files that have been modified one filename per line). I recommend you add the `-t` or `--verbose` option to `xargs` and look at what the xargs command is actually invoking. Also you should run the `fswatch` command by itself from the shell prompt and look at the output it's generating. – MadScientist Oct 16 '16 at 18:32
  • 1
    I just looked at the online docs: http://emcrisostomo.github.io/fswatch/doc/1.9.3/fswatch.html/Tutorial-Introduction-to-fswatch.html#Detecting-File-System-Changes You don't want the `-o` option, clearly. That just prints the number of changes. You want the main mode which prints the files that actually changed. – MadScientist Oct 16 '16 at 18:33