line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sweet::File; |
2
|
1
|
|
|
1
|
|
195512
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Try::Tiny; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use File::Basename; |
7
|
|
|
|
|
|
|
use File::Copy; |
8
|
|
|
|
|
|
|
use File::Remove 'remove'; |
9
|
|
|
|
|
|
|
use File::Spec; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use MooseX::Types::Path::Class; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'dir' => ( |
14
|
|
|
|
|
|
|
builder => '_build_dir', |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
isa => 'Sweet::Dir', |
17
|
|
|
|
|
|
|
lazy => 1, |
18
|
|
|
|
|
|
|
predicate => 'has_dir', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'name' => ( |
22
|
|
|
|
|
|
|
builder => '_build_name', |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => 'Str', |
25
|
|
|
|
|
|
|
lazy => 1, |
26
|
|
|
|
|
|
|
predicate => 'has_name', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'ext' => ( |
30
|
|
|
|
|
|
|
default => sub { |
31
|
|
|
|
|
|
|
my $self = shift; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $path = $self->path; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my ( $filename, $dirname, $suffix ) = fileparse( $path, qr/[^.]*$/ ); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
return $suffix; |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
is => 'ro', |
40
|
|
|
|
|
|
|
isa => 'Str', |
41
|
|
|
|
|
|
|
lazy => 1, |
42
|
|
|
|
|
|
|
predicate => 'has_ext', |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has 'path' => ( |
46
|
|
|
|
|
|
|
builder => '_build_path', |
47
|
|
|
|
|
|
|
coerce => 1, |
48
|
|
|
|
|
|
|
is => 'rw', |
49
|
|
|
|
|
|
|
isa => 'Path::Class::File', |
50
|
|
|
|
|
|
|
lazy => 1, |
51
|
|
|
|
|
|
|
); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _build_path { |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $name = $self->name; |
57
|
|
|
|
|
|
|
my $dir = $self->dir; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $dir_path = $dir->path; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $path = File::Spec->catfile( $dir_path, $name ); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
return $path; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub copy_to_dir { |
67
|
|
|
|
|
|
|
my $self = shift; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
my $dir = shift; |
70
|
|
|
|
|
|
|
my $name = $self->name; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $file_copied = try { |
73
|
|
|
|
|
|
|
Sweet::File->new( dir => $dir, name => $name ); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
catch { |
76
|
|
|
|
|
|
|
die $_; |
77
|
|
|
|
|
|
|
}; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my $source_path = $self->path; |
80
|
|
|
|
|
|
|
my $target_path = $file_copied->path; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
try { |
83
|
|
|
|
|
|
|
$dir->is_a_directory or $dir->create; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
catch { |
86
|
|
|
|
|
|
|
die $_; |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
try { |
90
|
|
|
|
|
|
|
copy( $source_path, $target_path ); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
catch { |
93
|
|
|
|
|
|
|
die $_; |
94
|
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
return $file_copied; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub does_not_exists { !-e shift->path } |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub erase { remove( shift->path ) } |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub has_zero_size { -z shift->path } |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub is_a_plain_file { -f shift->path } |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub is_executable { -x shift->path } |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub is_writable { -w shift->path } |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
use overload q("") => sub { shift->path }; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__END__ |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=encoding utf8 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 NAME |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Sweet::File |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 SYNOPSIS |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
use Sweet::File; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $file = Sweet::File->new( |
130
|
|
|
|
|
|
|
dir => '/path/to/dir', |
131
|
|
|
|
|
|
|
name => 'foo', |
132
|
|
|
|
|
|
|
); |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 dir |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head2 ext |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head2 name |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head2 path |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 METHODS |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head2 copy_to_dir |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 does_not_exists |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 erase |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head2 has_zero_size |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 is_a_plain_file |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head2 is_executable |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 is_writable |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=cut |
161
|
|
|
|
|
|
|
|