CatalystX-Crudite
view release on metacpan or search on metacpan
lib/CatalystX/Crudite/Util.pm view on Meta::CPAN
# Don't copy the files directly, just record in %result what would
# be done. This way we can check after the find() whether files
# would be overwritten and, if necessary, abort. Then the files
# are written in a separate loop at the end.
find(
sub {
return unless -f;
my $rel_name = $File::Find::name =~ s!^$template_dir/?!!r;
$rel_name = '.gitignore' if $rel_name eq 'gitignore';
while (my ($k, $v) = each %filename_replace) {
$rel_name =~ s/$k/$v/g;
}
my $result_name = "$args{dist_dir}/$rel_name";
# Some files are copied as-is.
if (/\.png$/) {
$result{$result_name} = { copy => $File::Find::name };
} else {
# Create a new template processor for each file;
# with a shared template processor there seems to
# be some sort of caching issue.
my $template_processor = Template->new(
START_TAG => '<%',
END_TAG => '%>',
);
my $content;
$template_processor->process($_, \%vars, \$content)
|| die $template_processor->error;
$result{$result_name} = { write => $content };
}
},
$template_dir
);
unless ($args{overwrite}) {
if (my @exists = grep { -e } sort keys %result) {
print "$_ exists, won't overwrite\n" for @exists;
print "Aborting.\n";
return;
}
if (my @exists = grep { -d } sort keys %result) {
print "$_ exists as a directory\n" for @exists;
print "Aborting.\n";
return;
}
}
for my $filename (sort keys %result) {
(my $path = $filename) =~ s!^.*\K/.*!!;
unless (-d $path) {
$log->("mkpath $path");
mkpath($path) unless $args{dryrun};
}
if (my $original = $result{$filename}{copy}) {
$log->("copy -> $filename");
unless ($args{dryrun}) {
copy($original, $filename) or die "copy failed: $!";
}
} elsif (my $content = $result{$filename}{write}) {
$log->("template -> $filename");
write_file($filename, $content) unless $args{dryrun};
chmod 0755, $filename if $filename =~ /\.(pl|sh)$/;
}
}
$log->('Finished.');
}
1;
( run in 0.682 second using v1.01-cache-2.11-cpan-98e64b0badf )