From 08276e58732272cb244ea54b2abc1c198132f284 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 9 Jul 2021 15:56:28 +0200 Subject: wpdf: Support build(1) syntax in source file --- wpdf | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) (limited to 'wpdf') diff --git a/wpdf b/wpdf index a906c67..94e5029 100755 --- a/wpdf +++ b/wpdf @@ -12,8 +12,9 @@ my $parent = $$; # pid of parent $SIG{USR1} = sub { kill 'HUP', 0 if not --$alive }; $SIG{HUP} = sub { exit 0 }; -# collect dependencies for given pdf files +# collect dependencies for given files for my $target (@ARGV) { + # if pdf, collect from Makefile if ($target =~ /\.pdf$/) { (my $basename = $target) =~ s/\.pdf$//; open my $f, '<', 'Makefile' @@ -32,18 +33,37 @@ for my $target (@ARGV) { } } close $f; - if (fork == 0) { - system 'xpdf', '-remote', "wpdf-$target", $target; - kill 'USR1', $parent; - exit; + } + + # if not pdf, assume target is actually source file and collect from it + # (with build(1) syntax) + else { + my $i = 0; + my $source = $target; + $target =~ s,\.[^./]*$,,; $target .= '.pdf'; + $deptarget{$source} = $target; + + open my $f, '<', $source; + while (<$f>) { + last if ++$i > 20; + if (/\s% (.*)/) { + $deptarget{$_} = $target for split /\s/, $1; + last; + } } - $alive++; - next; - } else { - warn "skipping $_: not a pdf\n"; + close $f; } + + if (fork == 0) { + system 'xpdf', '-remote', "wpdf-$target", $target; + kill 'USR1', $parent; + exit; + } + $alive++; } +die "no dependencies found\n" if not keys %deptarget; + # make pdf on demand open my $p, '-|', 'watch', keys %deptarget or die "could not start watch: $!\n"; -- cgit v1.2.3