use parent qw/Exporter/;
our @EXPORT_OK = qw/generate/;
+use Fcntl qw/:flock/;
use File::Basename qw/fileparse/;
use File::Slurp qw/write_file/;
use IO::Compress::Gzip qw/gzip/;
+use IO::File;
our $VERSION = '0.001';
our @generators;
my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
my $basename = fileparse $path_noext;
- open my $typemap, ">$path_noext.var";
+ IO::File->new(">$path_noext.var")->close unless -f "$path_noext.var";
+ flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX;
+ open my $typemap, ">$path_noext.var.new";
say $typemap "URI: $basename\n";
for my $gen(@generators) {
my ($regex, $generator) = @$gen;
next unless $path_noext =~ $regex;
for my $lang (@{LANGUAGES()}) {
my $page = $generator->($path, $lang);
- write_file "$path_noext.$lang.$ext", $page;
+ write_file "$path_noext.$lang.$ext.new", $page;
say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
- gzip \$page => "$path_noext.$lang.gz.$ext", Minimal => 1;
+ gzip \$page => "$path_noext.$lang.gz.$ext.new", Minimal => 1;
say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
}
}
+
+ for my $lang(@{LANGUAGES()}) {
+ rename "$path_noext.$lang.$ext.new", "$path_noext.$lang.$ext";
+ rename "$path_noext.$lang.gz.$ext.new", "$path_noext.$lang.gz.$ext";
+ }
+ rename "$path_noext.var.new", "$path_noext.var";
close $typemap;
}