4 |
use autodie; |
use autodie; |
5 |
use Carp qw/carp confess/; |
use Carp qw/carp confess/; |
6 |
use File::Path qw//; |
use File::Path qw//; |
7 |
|
use Data::Dump qw/dump/; |
8 |
|
|
9 |
|
my $debug = 1; |
10 |
|
|
11 |
sub mkpath { |
sub mkpath { |
12 |
my $file = shift; |
my $file = shift; |
21 |
mkpath $file; |
mkpath $file; |
22 |
write_file $file, $content; |
write_file $file, $content; |
23 |
my $size = -s $file; |
my $size = -s $file; |
24 |
carp "append created $size bytes in $file"; |
carp "## append created $size bytes in $file"; |
25 |
return $size; |
return $size; |
26 |
} |
} |
27 |
|
|
28 |
my $on_disk = read_file $file; |
my $on_disk = read_file $file; |
29 |
|
|
30 |
my $relaxed_content =~ s{\s+}{\\s+}gs; |
my $relaxed_content = $content; |
31 |
|
$relaxed_content =~ s{\s+}{\\s+}gs; |
32 |
|
|
33 |
if ( $on_disk !~ m{$relaxed_content} ) { |
if ( $on_disk !~ m{$relaxed_content} ) { |
34 |
|
|
35 |
# $content =~ s{^[\n\r]+}{\n}s; |
# $content =~ s{^[\n\r]+}{\n}s; |
36 |
# $content =~ s{[\n\r]*$}{\n}s; |
# $content =~ s{[\n\r]*$}{\n}s; |
37 |
|
|
38 |
open($fh, '>>', $file); |
if ( $on_disk =~ s{([\s+]exit[\s\d]*)$}{\n$content\n$1}s ) { |
39 |
print $fh $content; |
# warn "# insert $file\n$on_disk"; |
40 |
close($fh); |
write_file $file, $on_disk; |
41 |
|
} else { |
42 |
my $size = length($content); |
# warn "# append $file\n$content\n"; |
43 |
|
open($fh, '>>', $file); |
44 |
|
print $fh $content; |
45 |
|
close($fh); |
46 |
|
} |
47 |
|
|
48 |
carp "append $size bytes to $file"; |
carp "## append to $file"; |
49 |
warn $content; |
return -s $file; |
|
return $size; |
|
50 |
} |
} |
51 |
} |
} |
52 |
|
|
56 |
my $content = read_file $file; |
my $content = read_file $file; |
57 |
if ( $content =~ s{$from}{$to}s ) { |
if ( $content =~ s{$from}{$to}s ) { |
58 |
write_file $file, $content; |
write_file $file, $content; |
59 |
carp "change $file $from => $to"; |
carp "## change $file $from => $to" if $debug; |
60 |
return 1; |
return 1; |
61 |
} elsif ( $content !~ m{$to}s ) { |
} elsif ( $content !~ m{$to}s ) { |
62 |
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 ); |
63 |
} |
} |
64 |
} |
} |
65 |
|
|
69 |
write_file $file, $content; |
write_file $file, $content; |
70 |
} |
} |
71 |
|
|
72 |
|
sub copy_once { |
73 |
|
my ( $from, $to ) = @_; |
74 |
|
$to ||= $from; |
75 |
|
return if -e $to; |
76 |
|
mkpath $to; |
77 |
|
write_file $to, read_file $from; |
78 |
|
# warn "# copy_once $to ", -s $to, " bytes"; |
79 |
|
} |
80 |
|
|
81 |
1; |
1; |