From 65f946b63b4786170702097288aa1c84bd8e8fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Thu, 6 May 2021 15:58:09 +0200 Subject: Copy only if modified --- gen.pl | 84 ++++++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/gen.pl b/gen.pl index 0b682c2..fe9a786 100644 --- a/gen.pl +++ b/gen.pl @@ -2,61 +2,99 @@ # gen.pl -- generate static files for perlisdead.org +package App::PerlIsDead; +our $VERSION = '0.1'; + use strict; use warnings; use experimental 'switch'; use File::Copy; use FindBin '$Bin'; +use Getopt::Std; + +my %opt; +$Getopt::Std::STANDARD_HELP_VERSION = 1; +getopts('d', \%opt); # Change directory to script path chdir $Bin; # Generate index.html mkdir 'out' if ! -d 'out'; -open my $f, '<', 'src/index.html' or die "Could not open < src/index.html: $!"; -open my $g, '>', 'out/index.html.tmp' or die "Could not open > out/index.html.tmp: $!"; -for (<$f>) { +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: $!"; +for (<$src>) { when (/^\.inc (.*)/) { open my $h, '<', "inc/$1" or do { warn "Could not open inc/$1: $!"; next; }; - print $g $_ while <$h>; + print STDERR "INCLUDE: $_\n" if $opt{d}; + print $out $_ while <$h>; close $h; } when (/^\.eval (.*)/) { - no strict; no warnings; no experimental; - local *STDOUT = $g; - eval $1; - warn $@ if $@; + 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: $!"; next; }; + print STDERR "RUN: $1\n" if $opt{d}; my $c = do { local $/; <$h> }; close $h; - local *STDOUT = $g; - local $0 = "run/$1"; - no strict; no warnings; no experimental; - eval $c; - warn $@ if $@; + { + no strict; no warnings; no experimental; + local $0 = "run/$1"; + select $out; + eval $c; + select STDOUT; + warn $@ if $@; + } # I guess you could just shell out instead... } default { - print $g $_; + print $out $_; } } -close $g; -close $f; - -# Copy static files -opendir my $dh, 'src' or die "Could not open directory src: $!"; -while (readdir $dh) { - copy "src/$_" => "out/$_" unless /^\./ or /^index\.html$/; -} -closedir $dh; +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) { + if ($out{$_} and $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}; + +# Definitions + +sub main::HELP_MESSAGE { + print <