diff options
author | John Ankarström <john@ankarstrom.se> | 2021-05-08 14:57:08 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-05-08 14:57:08 +0200 |
commit | 4298cc2332d0d0bf0ad7bc8cf186324da1d0f9c9 (patch) | |
tree | 73afe43eb64a3ec89dfd3d979be193837adb3d7d | |
parent | fd9efab7d394b0bb40f6b9f000513fc720a888d8 (diff) | |
download | perlisdead-4298cc2332d0d0bf0ad7bc8cf186324da1d0f9c9.tar.gz |
Add caching of runs as a fallback in case of failure
-rwxr-xr-x | gen.pl | 58 |
1 files changed, 37 insertions, 21 deletions
@@ -16,11 +16,12 @@ use Getopt::Std; my %opt; $Getopt::Std::STANDARD_HELP_VERSION = 1; -getopts('d', \%opt); +getopts('cd', \%opt); sub main::HELP_MESSAGE { print <<HELP; usage: $0 [-d] + -c use cached run results -d enable debug messages HELP } @@ -35,48 +36,63 @@ chdir $Bin; mkdir 'out' if ! -d 'out'; open my $src, '<', 'src/index.html' or die "Could not open < src/index.html: $!"; open my $out, '>', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!"; +select $out; for (<$src>) { when (/^\.inc (.*)/) { - open my $h, '<', "inc/$1" or do { + open my $inc, '<', "inc/$1" or do { warn "Could not open inc/$1: $!"; next; }; print STDERR "INCLUDE: $_\n" if $opt{d}; - print $out $_ while <$h>; - close $h; + print $_ while <$inc>; + close $inc; } when (/^\.eval (.*)/) { print STDERR "EVAL: $1\n" if $opt{d}; { no strict; no warnings; no experimental; - select $out; eval $1; - select STDOUT; warn $@ if $@; } } when (/^\.run (.*)/) { - open my $h, '<', "run/$1" or do { - warn "Could not open run/$1: $!"; + if (not $opt{c}) { + open my $run, '-|', "run/$1" or do { + warn "Could not start run/$1: $!"; + next; + }; + open my $cache, '>', "cache/$1.tmp" or do { + warn "Could not open cache/$1.tmp: $!"; + next; + }; + print STDERR "RUN: $1\n" if $opt{d}; + my $i; + for (<$run>) { + $i++; + print; + print $cache $_; + } + close $cache; + close $run; + if ($i) { + move "cache/$1.tmp" => "cache/$1"; + next; + } + } + # Fall back to cached run + open my $cache, '<', "cache/$1" or do { + warn "Could not open cache/$1: $!"; next; }; - print STDERR "RUN: $1\n" if $opt{d}; - my $c = do { local $/; <$h> }; - close $h; - { - no strict; no warnings; no experimental; - local $0 = "run/$1"; - select $out; - eval $c; - select STDOUT; - warn $@ if $@; - } - # I guess you *could* just run the script in a separate process... + print STDERR "GET CACHED RUN: $1\n" if $opt{d}; + print for <$cache>; + close $cache; } default { - print $out $_; + print $_; } } +select STDOUT; close $out; close $src; |