| 1 | package TaskPipe::FileInstaller; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use File::Path 'make_path'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | has dirs_created => (is => 'rw', isa => 'ArrayRef', default => sub{[]}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | has files_created => (is => 'rw', isa => 'ArrayRef', default => sub{[]}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | has on_rollback => (is => 'rw', isa => 'CodeRef'); | |||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | sub create_dir{ | ||||||||||
| 14 | 0 | ▶ | my ($self,$dir) = @_; | ||||||||
|
Subroutine
create_dir: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 15 | |||||||||||
| 16 | 0 | ▶ | return if -d $dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | |||||||||||
| 18 | try { | ||||||||||
| 19 | |||||||||||
| 20 | 0 | ▶ | make_path( $dir ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 21 | |||||||||||
| 22 | } catch { | ||||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | confess "Could not create directory $dir: $_"; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 25 | |||||||||||
| 26 | 0 | }; | |||||||||
| 27 | |||||||||||
| 28 | 0 | unshift @{$self->dirs_created}, $dir; | |||||||||
| 29 | |||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | sub create_file{ | ||||||||||
| 34 | 0 | ▶ | my ($self,$path,$text) = @_; | ||||||||
|
Subroutine
create_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | return if -f $path; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | |||||||||||
| 38 | 0 | ▶ | open my $fh,">",$path or confess "Could not open path $path for writing: $!\nSetup operations have been rolled back"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | |||||||||||
| 40 | 0 | print $fh $text; | |||||||||
| 41 | 0 | close $fh; | |||||||||
| 42 | 0 | unshift @{$self->files_created}, $path; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | |||||||||||
| 46 | sub rollback{ | ||||||||||
| 47 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
rollback: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||
| 48 | |||||||||||
| 49 | 0 | ▶ | if ( $self->on_rollback ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | $self->on_rollback->(); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 0 | foreach my $filepath (@{$self->files_created}){ | |||||||||
| 54 | 0 | ▶ | unlink $filepath or warn "An error occurred during rollback. I wasn't able to remove the file $filepath: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | foreach my $dir (@{$self->dirs_created}){ | |||||||||
| 58 | 0 | ▶ | next unless -d $dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | 0 | ▶ | rmdir $dir or warn "An error occurred during rollback. I wasn't able to remove the directory $dir: $!" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =head1 NAME | ||||||||||
| 64 | |||||||||||
| 65 | TaskPipe::FileInstaller - handles file installs for TaskPipe | ||||||||||
| 66 | |||||||||||
| 67 | =head1 DESCRIPTION | ||||||||||
| 68 | |||||||||||
| 69 | It is not recommended to use this module directly. See the general manpages for TaskPipe | ||||||||||
| 70 | |||||||||||
| 71 | =head1 AUTHOR | ||||||||||
| 72 | |||||||||||
| 73 | Tom Gracey <tomgracey@gmail.com> | ||||||||||
| 74 | |||||||||||
| 75 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 76 | |||||||||||
| 77 | Copyright (c) Tom Gracey 2018 | ||||||||||
| 78 | |||||||||||
| 79 | TaskPipe is free software, licensed under | ||||||||||
| 80 | |||||||||||
| 81 | The GNU Public License Version 3 | ||||||||||
| 82 | |||||||||||
| 83 | =cut | ||||||||||
| 84 | |||||||||||
| 85 | |||||||||||
| 86 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 87 | 1 | 1; | |||||||||
| 88 | |||||||||||