| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Sweet::File; |
|
2
|
1
|
|
|
1
|
|
175840
|
use Moose; |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
use namespace::autoclean; |
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Try::Tiny; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use File::Basename; |
|
8
|
|
|
|
|
|
|
use File::Copy; |
|
9
|
|
|
|
|
|
|
use File::Remove 'remove'; |
|
10
|
|
|
|
|
|
|
use File::Slurp; |
|
11
|
|
|
|
|
|
|
use File::Spec; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use MooseX::Types::Path::Class; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has _read => ( |
|
16
|
|
|
|
|
|
|
builder => '_read_file', |
|
17
|
|
|
|
|
|
|
handles => { |
|
18
|
|
|
|
|
|
|
lines => 'elements', |
|
19
|
|
|
|
|
|
|
line => 'get', |
|
20
|
|
|
|
|
|
|
num_lines => 'count', |
|
21
|
|
|
|
|
|
|
}, |
|
22
|
|
|
|
|
|
|
is => 'ro', |
|
23
|
|
|
|
|
|
|
isa => 'ArrayRef[Str]', |
|
24
|
|
|
|
|
|
|
lazy => 1, |
|
25
|
|
|
|
|
|
|
traits => ['Array'], |
|
26
|
|
|
|
|
|
|
); |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _read_file { read_file( shift->path, array_ref => 1 ) } |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
has dir => ( |
|
31
|
|
|
|
|
|
|
builder => '_build_dir', |
|
32
|
|
|
|
|
|
|
is => 'ro', |
|
33
|
|
|
|
|
|
|
isa => 'Sweet::Dir', |
|
34
|
|
|
|
|
|
|
lazy => 1, |
|
35
|
|
|
|
|
|
|
predicate => 'has_dir', |
|
36
|
|
|
|
|
|
|
); |
|
37
|
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
has name => ( |
|
39
|
|
|
|
|
|
|
builder => '_build_name', |
|
40
|
|
|
|
|
|
|
is => 'ro', |
|
41
|
|
|
|
|
|
|
isa => 'Str', |
|
42
|
|
|
|
|
|
|
lazy => 1, |
|
43
|
|
|
|
|
|
|
predicate => 'has_name', |
|
44
|
|
|
|
|
|
|
); |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
has ext => ( |
|
47
|
|
|
|
|
|
|
default => sub { |
|
48
|
|
|
|
|
|
|
my $path = shift->path; |
|
49
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my ( $filename, $dirname, $suffix ) = fileparse( $path, qr/[^.]*$/ ); |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
return $suffix; |
|
53
|
|
|
|
|
|
|
}, |
|
54
|
|
|
|
|
|
|
is => 'ro', |
|
55
|
|
|
|
|
|
|
isa => 'Str', |
|
56
|
|
|
|
|
|
|
lazy => 1, |
|
57
|
|
|
|
|
|
|
predicate => 'has_ext', |
|
58
|
|
|
|
|
|
|
); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
has path => ( |
|
61
|
|
|
|
|
|
|
builder => '_build_path', |
|
62
|
|
|
|
|
|
|
coerce => 1, |
|
63
|
|
|
|
|
|
|
is => 'rw', |
|
64
|
|
|
|
|
|
|
isa => 'Path::Class::File', |
|
65
|
|
|
|
|
|
|
lazy => 1, |
|
66
|
|
|
|
|
|
|
); |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _build_path { |
|
69
|
|
|
|
|
|
|
my $self = shift; |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
my $name = $self->name; |
|
72
|
|
|
|
|
|
|
my $dir = $self->dir; |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my $dir_path = $dir->path; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $path = File::Spec->catfile( $dir_path, $name ); |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
return $path; |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub copy_to_dir { |
|
82
|
|
|
|
|
|
|
my $self = shift; |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
my $dir = shift; |
|
85
|
|
|
|
|
|
|
my $name = $self->name; |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
my $file_copied = try { |
|
88
|
|
|
|
|
|
|
Sweet::File->new( dir => $dir, name => $name ); |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
catch { |
|
91
|
|
|
|
|
|
|
die $_; |
|
92
|
|
|
|
|
|
|
}; |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
my $source_path = $self->path; |
|
95
|
|
|
|
|
|
|
my $target_path = $file_copied->path; |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
try { |
|
98
|
|
|
|
|
|
|
$dir->is_a_directory or $dir->create; |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
catch { |
|
101
|
|
|
|
|
|
|
die $_; |
|
102
|
|
|
|
|
|
|
}; |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
try { |
|
105
|
|
|
|
|
|
|
copy( $source_path, $target_path ); |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
catch { |
|
108
|
|
|
|
|
|
|
die $_; |
|
109
|
|
|
|
|
|
|
}; |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
return $file_copied; |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub does_not_exists { !-e shift->path } |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub erase { remove( shift->path ) } |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub has_zero_size { -z shift->path } |
|
119
|
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub is_a_plain_file { -f shift->path } |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub is_executable { -x shift->path } |
|
123
|
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub is_writable { -w shift->path } |
|
125
|
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
use overload q("") => sub { shift->path }; |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
1; |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
__END__ |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=encoding utf8 |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 NAME |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Sweet::File |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
use Sweet::File; |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
my $file = Sweet::File->new( |
|
145
|
|
|
|
|
|
|
dir => '/path/to/dir', |
|
146
|
|
|
|
|
|
|
name => 'foo', |
|
147
|
|
|
|
|
|
|
); |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head2 dir |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 ext |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head2 name |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head2 path |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 METHODS |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 copy_to_dir |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head2 does_not_exists |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 erase |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head2 has_zero_size |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 is_a_plain_file |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head2 is_executable |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 is_writable |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 line |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
my $line1 = $file->line(0); |
|
178
|
|
|
|
|
|
|
my $line2 = $file->line(1); |
|
179
|
|
|
|
|
|
|
my $line3 = $file->line(2); |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 lines |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
for my $line ( $file->lines ) { |
|
184
|
|
|
|
|
|
|
chomp $line; |
|
185
|
|
|
|
|
|
|
$line =~ s/foo/bar/; |
|
186
|
|
|
|
|
|
|
say $line; |
|
187
|
|
|
|
|
|
|
} |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 num_lines |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
say $file->num_lines if $file->is_a_plain_file; |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 _read_file |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Reads the file contents using L<File::Slurp> C<read_file> function. |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Defaults to |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub { read_file( shift->path, array_ref => 1 ) } |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
and must return an array_ref of strings containing file lines. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=cut |
|
204
|
|
|
|
|
|
|
|