| 1 | package Daje::Workflow::Savefile::Activity; | ||
| 2 | 1 | ▶ | use Mojo::Base 'Daje::Workflow::Common::Activity::Base', -base, -signatures; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | |||
| 5 | # NAME | ||
| 6 | # ==== | ||
| 7 | # | ||
| 8 | # Daje::Workflow::Savefile::Activity - It's a tool to save an array of files | ||
| 9 | # | ||
| 10 | # SYNOPSIS | ||
| 11 | # ======== | ||
| 12 | # | ||
| 13 | # use Daje::Workflow::Savefile::Activity; | ||
| 14 | # | ||
| 15 | # "activity_data": { | ||
| 16 | # "file" : { | ||
| 17 | # "target_dir_tag": "sql_target_dir", | ||
| 18 | # "filetype": ".sql", | ||
| 19 | # "file_list_tag": "sql" | ||
| 20 | # } | ||
| 21 | # | ||
| 22 | # Mandatory meta data | ||
| 23 | # | ||
| 24 | # - file contains file name or name an path | ||
| 25 | # | ||
| 26 | # - data content to write to disk | ||
| 27 | # | ||
| 28 | # | ||
| 29 | # Possible meta data | ||
| 30 | # | ||
| 31 | # - path if set to 1 the file tag contains full path | ||
| 32 | # | ||
| 33 | # - new_only if set to 1 dont replace existing file | ||
| 34 | # | ||
| 35 | # | ||
| 36 | # DESCRIPTION | ||
| 37 | # =========== | ||
| 38 | # | ||
| 39 | # Daje::Workflow::Savefile::Activity is a file saver | ||
| 40 | # | ||
| 41 | # LICENSE | ||
| 42 | # ======= | ||
| 43 | # | ||
| 44 | # Copyright (C) janeskil1525. | ||
| 45 | # | ||
| 46 | # This library is free software; you can redistribute it and/or modify | ||
| 47 | # it under the same terms as Perl itself. | ||
| 48 | # | ||
| 49 | # AUTHOR | ||
| 50 | # ====== | ||
| 51 | # | ||
| 52 | # janeskil1525 E<lt>janeskil1525@gmail.comE<gt> | ||
| 53 | # | ||
| 54 | |||
| 55 | use Mojo::File; | ||
| 56 | |||
| 57 | our $VERSION = "1.10"; | ||
| 58 | |||
| 59 | |||
| 60 | |||
| 61 | sub save($self) { | ||
| 62 | |||
| 63 | eval { | ||
| 64 | |||
| 65 | my $files = $self->context->{context}->{$self->activity_data->{file}->{file_list_tag}}; | ||
| 66 | my $length = scalar @{$files}; | ||
| 67 | for (my $i = 0; $i < $length; $i++) { | ||
| 68 | my $file = ""; | ||
| 69 | if (exists @{$files}[$i]->{path} and @{$files}[$i]->{path} == 1) { | ||
| 70 | $file = @{$files}[$i]->{file}; | ||
| 71 | } else { | ||
| 72 | my $filepath = $self->context->{context}->{$self->activity_data->{file}->{target_dir_tag}}; | ||
| 73 | $file = $filepath . @{$files}[$i]->{file} . $self->activity_data->{file}->{filetype}; | ||
| 74 | } | ||
| 75 | if (exists @{$files}[$i]->{new_only} and @{$files}[$i]->{new_only} == 1) { | ||
| 76 | if (!-e $file) { | ||
| 77 | $self->_save_file($file, @{$files}[$i]->{data}); | ||
| 78 | } | ||
| 79 | } else { | ||
| 80 | $self->_save_file($file, @{$files}[$i]->{data}); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | }; | ||
| 84 | $self->error->add_error($@) if defined $@ and length($@) > 0; | ||
| 85 | |||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _save_file($self, $file, $data) { | ||
| 90 | open(my $fh, ">", $file); | ||
| 91 | print $fh $data; | ||
| 92 | close $fh; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | __END__ | ||