blib/lib/File/Edit.pm

filter: Statementstmt 66.2% 59 / 89 Branchbran 36.3% 8 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 73.3% 11 / 15 Podpod 14.2% 1 / 7 totaltotal 57.2% 79 / 138 CC 16 SCAR 27.4
CC
16
Cov
59%
_find_block
56.0
_find_one
6.1
save
6.0
CRAP 15.5
↑ Summary
1 package File::Edit;
2 2 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '0.0.7';
6  
7 2 has 'file';
8 2 has 'found'; # Line numbers of found lines. ArrayRef.
9 2 has '_lines'; # Text in the form of lines. ArrayRef.
10 2 has '_line_re'; # Regex for _find_one
11 2 has 'at'; # Index into _lines for actions such as insert
12  
13 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
14 11 ▶ @_ > 1
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 11 11
15 ? $_[0]->SUPER::new({ file => path($_[1]),
16 _lines => [path($_[1])->lines],
17 found => [] })
18 : $_[0]->SUPER::new
19 }
20 4 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod undocumented
21 4 my ($self, $text) = @_;
22  
23 4 $text =~ s/\n/\nx-x/g;
24 4 $self->{_lines} = [split('x-x',$text)];
25  
26 4 return $self;
27 }
28 1 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod undocumented
29 1 my ($o, $orig, $repstr) = @_;
30  
31 # Replaces one line
32 1 $o->_find_one($orig)
33 ->_replace_found($repstr);
34  
35 1 return $o;
36 }
37 0 ▶sub get_block {
Subroutine get_block: not called CC=1 SCAR=6.9
Pod undocumented
38 0 my ($o, %opt) = @_;
39  
40 return $o->_find_block($opt{from},$opt{to})
41 0 ->_found_lines;
42 }
43 0 ▶sub save {
Subroutine save: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my ($o, $file) = @_;
45  
46 0 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 0 0
47 0 path($file)->spew(join('',@{$o->_lines}));
48 } else {
49 0 $o->file->spew(join('',@{$o->_lines}));
50 }
51  
52 0 return $o;
53 }
54  
55 0 ▶sub _find_block {
Subroutine _find_block: not called CC=7 SCAR=40.3
56 0 my ($o, $begin_re, $end_re) = @_;
57 0 my $in_block = 0; # True if line is in block
58 0 my $line_begin = -1; # First line num of found block. -1 if not found
59 0 my $line_end = -1; # Last line num of found block. -1 if not found
60  
61 0 foreach my $n (0 .. $#{$o->_lines}) {
62 0 ▶ if (!$in_block) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_block) { } 0 0 0
63 0 ▶ if ($o->_lines->[$n] =~ $begin_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->_lines->[$n] =~ /$begin_re/u) 0 0 0
64 0 $line_begin = $n;
65 0 $in_block = 1;
66 }
67 } else {
68 0 ▶ if ($o->_lines->[$n] =~ $end_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($o->_lines->[$n] =~ /$end_re/u) 0 0 0
69 0 $line_end = $n;
70 0 $in_block = 0;
71 0 last;
72 }
73 }
74 }
75  
76 # Error if block not found
77 0 ▶ croak "Block not found." if $line_begin == -1 or $line_end == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $line_begin == -1 or $line_end == -1 0 0 0
Condition0%0/3
$line_begin == -1 or $line_end == -1
l!l&&r!l&&!r
000
MC/DC0%0/2
$line_begin == -1 or $line_end == -1
$line_begin == -1 $line_end == -1
Truth table0%0/3
A or B
A= $line_begin == -1B= $line_end == -1
ABresult
1X1
011
000
78  
79 0 $o->found([$line_begin, $line_end]);
80  
81 0 return $o;
82 }
83 0 ▶sub _found_lines {
Subroutine _found_lines: not called CC=1 SCAR=6.9
84 0 my ($o) = @_;
85  
86 0 return [@{$o->_lines}[$o->found->[0] .. $o->found->[1]]];
87 }
88  
89 10 ▶sub _find_one {
Subroutine _find_one: called CC=6 SCAR=18.0
90 10 my ($o, $line_re) = @_;
91 10 my $n = 0;
92  
93 # Init search result
94 10 $o->found([]);
95 10 ▶ $line_re = ref $line_re eq 'Regexp' ? $line_re : _qre($line_re);
Branch: 1 branch
BranchTrueFalseTotal
ref $line_re eq 'Regexp' ? : 0 10 10
96 10 $o->_line_re($line_re);
97  
98 10 foreach my $l (@{$o->_lines}) {
99 24 ▶ push @{$o->found}, $n if $l =~ $line_re;
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /$line_re/u 9 15 24
100 24 $n++;
101 }
102  
103 # Error if more than one line found
104 0 croak "Multiple lines found: ".join(', ',@{$o->found})
105 10 ▶ if scalar(@{$o->found}) > 1;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$o->found;} > 1 0 10 10
106  
107 # Error if more than one line found
108 croak "Line not found."
109 10 ▶ if scalar(@{$o->found}) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$o->found;} == 0 1 9 10
110  
111 9 return $o;
112 }
113 2 ▶sub _replace_found {
Subroutine _replace_found: called CC=2 SCAR=6.9
114 # Replaces all lines found (line numbers in $o->found)
115 2 my ($o, $repstr) = @_;
116  
117 2 my $line_re = $o->_line_re; # s// does not work with $o-> notation
118  
119 2 foreach my $n (@{$o->found}) {
120 2 $o->_lines->[$n] =~ s/$line_re/$repstr/;
121 }
122  
123 2 return $o;
124 }
125 10 ▶sub _qre { ## ($string) :> regex
Subroutine _qre: called CC=1 SCAR=0.0
126 10 my $quoted = quotemeta(shift);
127 10 return qr/$quoted/;
128 }
129  
130 2 ▶sub swap { ## ($s1 :>STRING, $s2 :>STRING) :> SELF
Subroutine swap: called CC=1 SCAR=0.0
Pod undocumented
131 2 my ($self,$s1,$s2) = @_;
132  
133 # Find the line indexes
134 2 my $idx_1 = $self->_find_one($s1)->found->[0];
135 2 my $idx_2 = $self->_find_one($s2)->found->[0];
136  
137 # Swap the lines
138 2 my $tmp = $self->_lines->[$idx_1];
139 2 $self->_lines->[$idx_1] = $self->_lines->[$idx_2];
140 2 $self->_lines->[$idx_2] = $tmp;
141  
142 2 return $self;
143 }
144 1 ▶sub insert { ## ($line :>STRING) :> SELF
Subroutine insert: called CC=2 SCAR=7.0
Pod undocumented
145 1 my ($self,$line) = @_;
146  
147 1 ▶ croak "Location to insert not defined" unless defined $self->at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->at 0 1 1
148 1 splice @{$self->_lines}, $self->at, 0, $line;
149  
150 1 return $self;
151 }
152  
153 =head1 NAME
154  
155 File::Edit - A naive, probably buggy, file editor.
156  
157 =cut
158 =head1 SYNOPSIS
159  
160 use File::Edit;
161  
162 # Replace string in file
163 File::Edit->new('build.gradle')
164 ->replace('minSdkVersion 16', 'minSdkVersion 21')
165 ->save()
166 ;
167  
168 # Edit text, save to file
169 File::Edit->new()
170 ->text(" minSdkVersion 16\n targetSdkVersion 29")
171 ->replace('minSdkVersion 16', 'minSdkVersion 21')
172 ->save('build.gradle')
173 ;
174  
175 # Swap lines, save to file
176 File::Edit->new()
177 ->text(" Do this first\n Now do that\n Don't do this")
178 ->swap('Do this', 'do that')
179 ->save('todo.txt')
180 ;
181  
182 # Insert at line index, save to file
183 File::Edit->new()
184 ->text(" Line index 0\n Line index 1\n Line index 2")
185 ->at(1)->insert(' Inserted line\n')
186 ->save('todo.txt')
187 ;
188  
189 =cut
190  
191 =head1 METHODS
192  
193 =head2 new
194  
195 my $fe = File::Edit->new("some_file.txt");
196  
197 Reads in a file for editing.
198  
199 =cut
200 =head2 text
201  
202 my $fe = File::Edit->new()->text(some_text);
203  
204 Reads in some text for editing.
205  
206 =cut
207 =head2 replace
208  
209 $fe->replace($old, $new);
210  
211 Replace the $old portion of a single line with $new.
212  
213 =cut
214 =head2 save
215  
216 my $fe = File::Edit->new("some_file.txt");
217 $fe->save(); # Saves to "some_file.txt"
218 $fe->save("other.txt") # Saves to "other.txt"
219  
220 =cut
221 =head2 swap( $text_1, $text_2 )
222  
223 The swap($s1, $s2) method finds the line containing string $s1 and finds
224 the line containg string $s2 and swaps both lines.
225  
226 =cut
227 =head2 at( $idx )
228  
229 The at($idx) sets the index for which actions such as insert($line) will
230 take place.
231  
232 =cut
233 =head2 insert( $line )
234  
235 The insert( $line ) method inserts the $line at the index location
236 specified. The index location is typically by an earlier call to
237 at($idx). For example:
238  
239 $self->at(0)->insert('Start of file')
240  
241 =cut
242  
243 =head1 AUTHOR
244  
245 Hoe Kit CHEW, C<< <hoekit at gmail.com> >>
246  
247 =head1 BUGS
248  
249 Please report any bugs or feature requests to C<bug-file-edit at rt.cpan.org>, or through
250 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=File-Edit>. I will be notified, and then you'll
251 automatically be notified of progress on your bug as I make changes.
252  
253  
254  
255  
256 =head1 SUPPORT
257  
258 You can find documentation for this module with the perldoc command.
259  
260 perldoc File::Edit
261  
262  
263 You can also look for information at:
264  
265 =over 4
266  
267 =item * RT: CPAN's request tracker (report bugs here)
268  
269 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Edit>
270  
271 =item * CPAN Ratings
272  
273 L<https://metacpan.org/pod/File::Edit>
274  
275 =item * Search CPAN
276  
277 L<https://metacpan.org/release/File-Edit>
278  
279 =back
280  
281  
282 =head1 LICENSE AND COPYRIGHT
283  
284 This software is Copyright (c) 2021 by Hoe Kit CHEW.
285  
286 This is free software, licensed under:
287  
288 The Artistic License 2.0 (GPL Compatible)
289  
290  
291 =cut
292  
293 2 1; # End of File::Edit
↑ Summary