#!/usr/bin/perl # gen.pl -- generate static files for perlisdead.org use 5.010; use strict; use warnings; use experimental 'switch'; use File::Copy; use FindBin '$Bin'; use Getopt::Std; # Parse command-line options my %opt; $Getopt::Std::STANDARD_HELP_VERSION = 1; getopts('cd', \%opt); sub main::HELP_MESSAGE { print <', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!"; copy 'out/index.html', 'out/index.html.bkp'; select $out; for (<$src>) { when (/^\.inc (.*)/) { open my $inc, '<', "inc/$1" or do { warn "Could not open inc/$1: $!"; next; }; print STDERR "INCLUDE: $_\n" if $opt{d}; print $_ while <$inc>; close $inc; } when (/^\.eval (.*)/) { print STDERR "EVAL: $1\n" if $opt{d}; { no strict; no warnings; no experimental; eval $1; warn $@ if $@; } } when (/^\.run (.*)/) { 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 "GET CACHED RUN: $1\n" if $opt{d}; print for <$cache>; close $cache; } default { print $_; } } select STDOUT; close $out; close $src; # Rename temporary file move 'out/index.html.tmp', 'out/index.html'; # Copy modified static files opendir $src, 'src' or die "Could not open directory src: $!"; opendir $out, 'out' or die "Could not open directory src: $!"; my %src = map { ($_, (stat "src/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $src; my %out = map { ($_, (stat "out/$_")[9]||0) } grep { not /^\./ or /^index\.html$/ } readdir $out; for (keys %src) { no warnings 'uninitialized'; if ($src{$_} > $out{$_}) { print STDERR "COPY: src/$_ => out/$_\n" if $opt{d}; copy "src/$_", "out/$_"; } } closedir $out; closedir $src; print STDERR "DONE\n" if $opt{d};