aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-05-06 15:58:09 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-05-06 15:58:09 +0200
commit65f946b63b4786170702097288aa1c84bd8e8fa1 (patch)
tree44b37213172dc91576abd3d95558c2ad2c46a4bf
parentd514585bbb54482ee62070bc86a7447a657afaee (diff)
downloadperlisdead-65f946b63b4786170702097288aa1c84bd8e8fa1.tar.gz
Copy only if modified
-rw-r--r--gen.pl84
1 files 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 <<HELP;
+usage: $0 [-d]
+ -d enable debug messages
+HELP
+}
+sub main::VERSION_MESSAGE {
+ print "App::PerlIsDead version $VERSION\n";
+}