--- lib/PXElator/file.pm 2009/08/20 19:00:13 277 +++ lib/PXElator/file.pm 2009/08/26 08:59:50 291 @@ -4,11 +4,17 @@ use autodie; use Carp qw/carp confess/; use File::Path qw//; +use Data::Dump qw/dump/; + +my $debug = 1; sub mkpath { my $file = shift; - my $dir = $1 if $file =~ s{^.+/[^/]+}{}; - File::Path::mkpath $dir unless -e $dir; + my $dir = $1 if $file =~ s{(^.+)/[^/]+}{$1}; + if ( $dir && ! -d $dir ) { + carp "# mkdir $dir"; + File::Path::mkpath $dir; + } } sub append { @@ -18,28 +24,32 @@ mkpath $file; write_file $file, $content; my $size = -s $file; - carp "append created $size bytes in $file"; + carp "## append created $size bytes in $file"; return $size; } my $on_disk = read_file $file; - my $relaxed_content =~ s{\s+}{\\s+}gs; + my $relaxed_content = $content; + $relaxed_content =~ s{\s+}{\\s+}gs; if ( $on_disk !~ m{$relaxed_content} ) { # $content =~ s{^[\n\r]+}{\n}s; # $content =~ s{[\n\r]*$}{\n}s; - open($fh, '>>', $file); - print $fh $content; - close($fh); - - my $size = length($content); + if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) { +# warn "# insert $file\n$on_disk"; + write_file $file, $on_disk; + } else { +# warn "# append $file\n$content\n"; + open($fh, '>>', $file); + print $fh $content; + close($fh); + } - carp "append $size bytes to $file"; - warn $content; - return $size; + carp "## append to $file"; + return -s $file; } } @@ -49,10 +59,10 @@ my $content = read_file $file; if ( $content =~ s{$from}{$to}s ) { write_file $file, $content; - carp "change $file $from => $to"; + carp "## change $file $from => $to" if $debug; return 1; } elsif ( $content !~ m{$to}s ) { - confess "can't find $from to change into $to in $file"; + confess "can't find $from to change into $to in $file in ",dump( $content ); } } @@ -62,4 +72,13 @@ write_file $file, $content; } +sub copy_once { + my ( $from, $to ) = @_; + die "no destination" unless $to; + return if -e $to; + mkpath $to; + carp "# copy_once $from => $to"; + write_file $to, read_file($from); +} + 1;