| 1 | package Sweet::File; | ||||||||||
| 2 | 6 | ▶ | use latest; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | ▶ | use Sweet::Types; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use File::Copy; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 6 | ▶ | use File::Remove 'remove'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 6 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 6 | ▶ | use Moose::Util::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use MooseX::AttributeShortcuts; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 6 | ▶ | use MooseX::Types::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | ▶ | use Storable qw(dclone); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 6 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 6 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | sub BUILDARGS { | ||||||||
|
Subroutine
BUILDARGS: not called CC=2 SCAR=17.9
| |||||||||||
| 21 | 0 | my ($class, %attribute) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 0 | my $lines_arrayref = $attribute{lines}; | |||||||||
| 24 | |||||||||||
| 25 | # Needed 'cause init_arg does not work with Array trait. | ||||||||||
| 26 | 0 | ▶ | if (defined $lines_arrayref) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | # Avoid use an external reference to store an attribute. | ||||||||||
| 28 | 0 | $attribute{_lines} = dclone($lines_arrayref); | |||||||||
| 29 | |||||||||||
| 30 | 0 | delete $attribute{lines}; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 0 | return \%attribute; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | my $output = sub { | ||||||||||
| 37 | my ($mode, $layer, $path, $lines_arrayref) = @_; | ||||||||||
| 38 | |||||||||||
| 39 | open my $fh, $mode.$layer, $path or croak "Couldn't open $path: $!"; | ||||||||||
| 40 | $fh->autoflush(1); | ||||||||||
| 41 | say $fh $_ for @{$lines_arrayref}; | ||||||||||
| 42 | close $fh or croak "Couldn't close $path: $!"; | ||||||||||
| 43 | }; | ||||||||||
| 44 | |||||||||||
| 45 | my $input = sub { | ||||||||||
| 46 | my ($layer, $path) = @_; | ||||||||||
| 47 | |||||||||||
| 48 | open my $fh, "<:$layer", $path or croak "Couldn't open $path: $!"; | ||||||||||
| 49 | my @lines =<$fh>; | ||||||||||
| 50 | close $fh or croak "Couldn't close $path: $!"; | ||||||||||
| 51 | |||||||||||
| 52 | chomp @lines; | ||||||||||
| 53 | |||||||||||
| 54 | return \@lines; | ||||||||||
| 55 | }; | ||||||||||
| 56 | |||||||||||
| 57 | # TODO lines should not be in generic Sweet::File, but a C<content> attribute | ||||||||||
| 58 | has _lines => ( | ||||||||||
| 59 | builder => '_build_lines', | ||||||||||
| 60 | handles => { | ||||||||||
| 61 | add_lines => 'push', | ||||||||||
| 62 | lines => 'elements', | ||||||||||
| 63 | line => 'get', | ||||||||||
| 64 | num_lines => 'count', | ||||||||||
| 65 | }, | ||||||||||
| 66 | is => 'lazy', | ||||||||||
| 67 | isa => 'ArrayRef[Str]', | ||||||||||
| 68 | traits => ['Array'], | ||||||||||
| 69 | ); | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub _build_lines { | ||||||||
|
Subroutine
_build_lines: not called CC=1 SCAR=6.9
| |||||||||||
| 72 | 0 | my $self = shift; | |||||||||
| 73 | |||||||||||
| 74 | 0 | my $encoding = $self->encoding; | |||||||||
| 75 | 0 | my $path = $self->path; | |||||||||
| 76 | |||||||||||
| 77 | 0 | my $lines = $input->($encoding, $path); | |||||||||
| 78 | |||||||||||
| 79 | 0 | return $lines; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | has dir => ( | ||||||||||
| 83 | coerce => 1, | ||||||||||
| 84 | is => 'lazy', | ||||||||||
| 85 | isa => 'Sweet::Dir', | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | sub _build_dir { | ||||||||
|
Subroutine
_build_dir: not called CC=1 SCAR=6.9
| |||||||||||
| 89 | 0 | my $self = shift; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $path = $self->path; | |||||||||
| 92 | |||||||||||
| 93 | 0 | my $dirname = dirname($path); | |||||||||
| 94 | |||||||||||
| 95 | 0 | my $dir = Sweet::Dir->new(path => $dirname); | |||||||||
| 96 | |||||||||||
| 97 | 0 | return $dir; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | # TODO see Encode::Supported | ||||||||||
| 101 | my @encodings = qw(utf8); | ||||||||||
| 102 | |||||||||||
| 103 | has encoding => ( | ||||||||||
| 104 | default => sub { 'utf8' }, | ||||||||||
| 105 | is => 'ro', | ||||||||||
| 106 | isa => enum(\@encodings), | ||||||||||
| 107 | required => 1, | ||||||||||
| 108 | ); | ||||||||||
| 109 | |||||||||||
| 110 | has name => ( | ||||||||||
| 111 | is => 'lazy', | ||||||||||
| 112 | isa => 'Str', | ||||||||||
| 113 | ); | ||||||||||
| 114 | |||||||||||
| 115 | 0 | ▶ | sub _build_name { | ||||||||
|
Subroutine
_build_name: not called CC=1 SCAR=6.9
| |||||||||||
| 116 | 0 | my $self = shift; | |||||||||
| 117 | |||||||||||
| 118 | 0 | my $path = $self->path; | |||||||||
| 119 | |||||||||||
| 120 | 0 | my $name = basename($path); | |||||||||
| 121 | |||||||||||
| 122 | 0 | return $name; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | has name_without_extension => ( | ||||||||||
| 126 | default => sub { ( fileparse( shift->path, qr/\.[^.]*$/ ) )[0] }, | ||||||||||
| 127 | init_arg => undef, | ||||||||||
| 128 | is => 'lazy', | ||||||||||
| 129 | isa => 'Str', | ||||||||||
| 130 | ); | ||||||||||
| 131 | |||||||||||
| 132 | has extension => ( | ||||||||||
| 133 | is => 'lazy', | ||||||||||
| 134 | isa => 'Str', | ||||||||||
| 135 | ); | ||||||||||
| 136 | |||||||||||
| 137 | 0 | ▶ | sub _build_extension { | ||||||||
|
Subroutine
_build_extension: not called CC=1 SCAR=6.9
| |||||||||||
| 138 | 0 | my $path = shift->path; | |||||||||
| 139 | |||||||||||
| 140 | 0 | my ($filename, $dirname, $suffix) = fileparse($path, qr/[^.]*$/); | |||||||||
| 141 | |||||||||||
| 142 | 0 | return $suffix; | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | has path => ( | ||||||||||
| 146 | coerce => 1, | ||||||||||
| 147 | is => 'lazy', | ||||||||||
| 148 | isa => 'Path::Class::File', | ||||||||||
| 149 | ); | ||||||||||
| 150 | |||||||||||
| 151 | 0 | ▶ | sub _build_path { | ||||||||
|
Subroutine
_build_path: not called CC=1 SCAR=6.9
| |||||||||||
| 152 | 0 | my $self = shift; | |||||||||
| 153 | |||||||||||
| 154 | 0 | my $name = $self->name; | |||||||||
| 155 | 0 | my $dir = $self->dir; | |||||||||
| 156 | |||||||||||
| 157 | 0 | my $dir_path = $dir->path; | |||||||||
| 158 | |||||||||||
| 159 | 0 | my $path = File::Spec->catfile($dir_path, $name); | |||||||||
| 160 | |||||||||||
| 161 | 0 | return $path; | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | sub append { | ||||||||
|
Subroutine
append: not called CC=1 SCAR=6.9
| |||||||||||
| 165 | 0 | my ($self, $lines_arrayref) = @_; | |||||||||
| 166 | |||||||||||
| 167 | 0 | my @lines = @$lines_arrayref; | |||||||||
| 168 | |||||||||||
| 169 | 0 | $self->add_lines(@lines); | |||||||||
| 170 | |||||||||||
| 171 | 0 | my $encoding = $self->encoding; | |||||||||
| 172 | 0 | my $path = $self->path; | |||||||||
| 173 | |||||||||||
| 174 | 0 | $output->('>>', $encoding, $path, $lines_arrayref); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 0 | ▶ | sub move_to_dir { | ||||||||
|
Subroutine
move_to_dir: not called CC=2 SCAR=17.9
| |||||||||||
| 178 | 0 | my ($self, $dir) = @_; | |||||||||
| 179 | |||||||||||
| 180 | 0 | ▶ | $self->copy_to_dir($dir) && $self->erase; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | } | ||||||||||
| 182 | |||||||||||
| 183 | sub copy_to_dir { | ||||||||||
| 184 | my ($self, $dir) = @_; | ||||||||||
| 185 | |||||||||||
| 186 | my $name = $self->name; | ||||||||||
| 187 | |||||||||||
| 188 | my $class = $self->meta->name; | ||||||||||
| 189 | |||||||||||
| 190 | my $file_copied = try { | ||||||||||
| 191 | $class->new(dir => $dir, name => $name); | ||||||||||
| 192 | } | ||||||||||
| 193 | catch { | ||||||||||
| 194 | croak $_; | ||||||||||
| 195 | }; | ||||||||||
| 196 | |||||||||||
| 197 | my $source_path = $self->path; | ||||||||||
| 198 | my $target_path = $file_copied->path; | ||||||||||
| 199 | $dir = $file_copied->dir; | ||||||||||
| 200 | |||||||||||
| 201 | try { | ||||||||||
| 202 | $dir->is_a_directory or $dir->create; | ||||||||||
| 203 | } | ||||||||||
| 204 | catch { | ||||||||||
| 205 | croak $_; | ||||||||||
| 206 | }; | ||||||||||
| 207 | |||||||||||
| 208 | try { | ||||||||||
| 209 | copy($source_path, $target_path); | ||||||||||
| 210 | } | ||||||||||
| 211 | catch { | ||||||||||
| 212 | croak $_; | ||||||||||
| 213 | }; | ||||||||||
| 214 | |||||||||||
| 215 | return $file_copied; | ||||||||||
| 216 | } | ||||||||||
| 217 | |||||||||||
| 218 | sub does_not_exists { !-e shift->path } | ||||||||||
| 219 | |||||||||||
| 220 | sub erase { remove(shift->path) } | ||||||||||
| 221 | |||||||||||
| 222 | sub has_zero_size { -z shift->path } | ||||||||||
| 223 | |||||||||||
| 224 | sub is_a_plain_file { -f shift->path } | ||||||||||
| 225 | |||||||||||
| 226 | sub is_executable { -x shift->path } | ||||||||||
| 227 | |||||||||||
| 228 | sub is_writable { -w shift->path } | ||||||||||
| 229 | |||||||||||
| 230 | sub split_line { | ||||||||||
| 231 | my $self = shift; | ||||||||||
| 232 | |||||||||||
| 233 | return sub { | ||||||||||
| 234 | my $pattern = shift; | ||||||||||
| 235 | |||||||||||
| 236 | # If pattern is a pipe, escape it. | ||||||||||
| 237 | $pattern = '\|' if ($pattern eq '|'); | ||||||||||
| 238 | |||||||||||
| 239 | return sub { | ||||||||||
| 240 | my $num_line = shift; | ||||||||||
| 241 | |||||||||||
| 242 | my $line = $self->line($num_line); | ||||||||||
| 243 | |||||||||||
| 244 | return split $pattern, $line; | ||||||||||
| 245 | } | ||||||||||
| 246 | } | ||||||||||
| 247 | } | ||||||||||
| 248 | |||||||||||
| 249 | sub write { | ||||||||||
| 250 | my $self = shift; | ||||||||||
| 251 | |||||||||||
| 252 | my $encoding = $self->encoding; | ||||||||||
| 253 | my $lines_arrayref = $self->_lines; | ||||||||||
| 254 | my $path = $self->path; | ||||||||||
| 255 | |||||||||||
| 256 | $output->('>', $encoding, $path, $lines_arrayref); | ||||||||||
| 257 | } | ||||||||||
| 258 | |||||||||||
| 259 | use overload q("") => sub { shift->path }, bool => sub { 1 }, fallback => 1; | ||||||||||
| 260 | |||||||||||
| 261 | __PACKAGE__->meta->make_immutable; | ||||||||||
| 262 | |||||||||||
| 263 | 1; | ||||||||||
| 264 | __END__ | ||||||||||