| 1 | package Daje::Workflow::Activities::Files::Save; | ||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use Mojo::Base 'Daje::Workflow::Common::Activity::Base', -base, -signatures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.42; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # NAME | ||||||||||||||||||||||||||||
| 6 | # ==== | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # Daje::Workflow::Activities::Files::Save - It's a tool to save an array of files | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # SYNOPSIS | ||||||||||||||||||||||||||||
| 11 | # ======== | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # use Daje::Workflow::Activities::Files::Save; | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # "activity_data": { | ||||||||||||||||||||||||||||
| 16 | # "file" : { | ||||||||||||||||||||||||||||
| 17 | # "target_dir_tag": "sql_target_dir", | ||||||||||||||||||||||||||||
| 18 | # "filetype": ".sql", | ||||||||||||||||||||||||||||
| 19 | # "file_list_tag": "sql" | ||||||||||||||||||||||||||||
| 20 | # } | ||||||||||||||||||||||||||||
| 21 | # } | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | # Mandatory meta data | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | # - file contains file name or name an path | ||||||||||||||||||||||||||||
| 26 | # | ||||||||||||||||||||||||||||
| 27 | # - data content to write to disk | ||||||||||||||||||||||||||||
| 28 | # | ||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||
| 30 | # Possible meta data | ||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||
| 32 | # - path if set to 1 the file tag contains full path | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # - new_only if set to 1 dont replace existing file | ||||||||||||||||||||||||||||
| 35 | # | ||||||||||||||||||||||||||||
| 36 | # # Producer | ||||||||||||||||||||||||||||
| 37 | # for(Bla; Bla; Ble) { | ||||||||||||||||||||||||||||
| 38 | # if (my $sql = $self->_process_sql(@{$files_list}[$i])) { | ||||||||||||||||||||||||||||
| 39 | # my $data->{data} = $sql; | ||||||||||||||||||||||||||||
| 40 | # $data->{file} = Mojo::File->new(@{$files_list}[$i])->basename; | ||||||||||||||||||||||||||||
| 41 | # $data->{file} =~ s/.json//g; | ||||||||||||||||||||||||||||
| 42 | # push(@data, $data); | ||||||||||||||||||||||||||||
| 43 | # } | ||||||||||||||||||||||||||||
| 44 | # } | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # $self->context->{context}->{sql} = \@data; | ||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 50 | # =========== | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | # Daje::Workflow::Activities::Files::Save is a file saver | ||||||||||||||||||||||||||||
| 53 | # | ||||||||||||||||||||||||||||
| 54 | # LICENSE | ||||||||||||||||||||||||||||
| 55 | # ======= | ||||||||||||||||||||||||||||
| 56 | # | ||||||||||||||||||||||||||||
| 57 | # Copyright (C) janeskil1525. | ||||||||||||||||||||||||||||
| 58 | # | ||||||||||||||||||||||||||||
| 59 | # This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 60 | # it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 61 | # | ||||||||||||||||||||||||||||
| 62 | # AUTHOR | ||||||||||||||||||||||||||||
| 63 | # ====== | ||||||||||||||||||||||||||||
| 64 | # | ||||||||||||||||||||||||||||
| 65 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||||||||||||||||||||||||||||
| 66 | # | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 1 | ▶ | use Mojo::File qw(path);; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | our $VERSION = "1.11"; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | sub save($self) { | ||||||||||||||||||||||||||
|
Subroutine
save: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | $self->model->insert_history( | |||||||||||||||||||||||||||
| 75 | "Save file(s)", | ||||||||||||||||||||||||||||
| 76 | "Daje::Workflow::Activities::Files::Save::save", | ||||||||||||||||||||||||||||
| 77 | 1 | ||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | try { | |||||||||||||||||||||||||||
| 81 | 0 | my $files = $self->context->{context}->{payload}->{$self->activity_data->{file}->{file_list_tag}}; | |||||||||||||||||||||||||||
| 82 | 0 | my $length = scalar @{$files}; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | for (my $i = 0; $i < $length; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my $file = ""; | |||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (exists @{$files}[$i]->{path} and @{$files}[$i]->{path} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists @{$files;}[$i]->{"path"} and @{$files;}[$i]->{"path"} == 1
MC/DC0%0/2
exists @{$files;}[$i]->{"path"} and @{$files;}[$i]->{"path"} == 1
exists @{$files;}[$i]->{"path"}
@{$files;}[$i]->{"path"} == 1
Truth table0%0/3
A and B
A= exists @{$files;}[$i]->{"path"}B= @{$files;}[$i]->{"path"} == 1
| |||||||||||||||||||||||||||||
| 86 | 0 | $file = @{$files}[$i]->{file}; | |||||||||||||||||||||||||||
| 87 | 0 | $self->_make_dir($file); | |||||||||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||||||||
| 89 | 0 | my $filepath = $self->context->{context}->{payload}->{$self->activity_data->{file}->{target_dir_tag}}; | |||||||||||||||||||||||||||
| 90 | 0 | $file = $filepath . @{$files}[$i]->{file} . $self->activity_data->{file}->{filetype}; | |||||||||||||||||||||||||||
| 91 | 0 | $self->_make_dir($file); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if (exists @{$files}[$i]->{new_only} and @{$files}[$i]->{new_only} == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists @{$files;}[$i]->{"new_only"} and @{$files;}[$i]->{"new_only"} == 1
MC/DC0%0/2
exists @{$files;}[$i]->{"new_only"} and @{$files;}[$i]->{"new_only"} == 1
exists @{$files;}[$i]->{"new_only"}
@{$files;}[$i]->{"new_only"} == 1
Truth table0%0/3
A and B
A= exists @{$files;}[$i]->{"new_only"}B= @{$files;}[$i]->{"new_only"} == 1
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if (!-e $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | $self->_save_file($file, @{$files}[$i]->{data}); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||
| 98 | 0 | $self->_save_file($file, @{$files}[$i]->{data}); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } catch($e) { | ||||||||||||||||||||||||||||
| 102 | 0 | $self->error->add_error($e) ; | |||||||||||||||||||||||||||
| 103 | }; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub _make_dir($self, $file) { | ||||||||||||||||||||||||||
|
Subroutine
_make_dir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 0 | my $file_name = path($file)->basename(); | |||||||||||||||||||||||||||
| 110 | 0 | my $path = $file; | |||||||||||||||||||||||||||
| 111 | 0 | $path =~ s/$file_name//; | |||||||||||||||||||||||||||
| 112 | 0 | my $result = path($path)->make_path(); | |||||||||||||||||||||||||||
| 113 | 0 | return $result; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub _save_file($self, $file, $data) { | ||||||||||||||||||||||||||
|
Subroutine
_save_file: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 117 | 0 | open(my $fh, ">", $file); | |||||||||||||||||||||||||||
| 118 | 0 | print $fh $data; | |||||||||||||||||||||||||||
| 119 | 0 | close $fh; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 1 | 1; | |||||||||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||||||||||