Qgoda/Migrator.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 23 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 160 CC 7 SCAR 18.3
CC
7
Cov
0%
createOutputDirectory
20.0
createDirectory
12.0
writeConfig
6.0
CRAP 6.2
1 #! /bin/false
2  
3 # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>,
4 # all rights reserved.
5  
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10  
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15  
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18  
19 package Qgoda::Migrator;
20 $Qgoda::Migrator::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw(qgoda);
24 use File::Path qw(remove_tree make_path);
25 use File::Spec;
26 use File::Copy::Recursive qw(rcopy);
27 use YAML::XS;
28 use Hook::LexWrap;
29  
30 use Qgoda;
31 use Qgoda::Util qw(write_file);
32  
33 sub new {
34 my ($class) = @_;
35  
36 bless {}, $class;
37 }
38  
39 sub logger {
40 my ($self) = @_;
41  
42 my $prefix = ref $self;
43 $prefix =~ s{^Qgoda::Migrator::}{Migrator::};
44  
45 return Qgoda->new->logger($prefix);
46 }
47  
48 sub createOutputDirectory {
49 my ($self) = @_;
50  
51 my $logger = $self->logger;
52 my $out_dir = $self->outputDirectory;
53  
54 if (-e $out_dir) {
55 $logger->info(__x("Removing output directory '{directory}'.",
56 directory => $out_dir));
57 if (!$self->dryRun) {
58 remove_tree $out_dir
59 or $logger->fatal(__x("Cannot remove directory:"
60 . " '{directory}': {error}!"));
61 }
62 }
63  
64 $logger->info(__x("Creating output directory '{directory}'.",
65 directory => $out_dir));
66 if (!$self->dryRun) {
67 make_path $out_dir
68 or $logger->fatal(__x("Cannot create directory:"
69 . " '{directory}': {error}!"));
70 }
71  
72 return $self;
73 }
74  
75 sub dryRun {
76 Qgoda->new->getOption('nochange');
77 }
78  
79 sub outputDirectory {
80 shift->{_out_dir};
81 }
82  
83 sub writeConfig {
84 my ($self, $config) = @_;
85  
86 my $logger = $self->logger;
87  
88 my $out_dir = $self->outputDirectory;
89 my $filename = File::Spec->catfile($out_dir, '_config.yaml');
90  
91 $logger->debug(__x("Writing configuration file '{filename}'.",
92 filename => $filename));
93  
94 my $yaml = YAML::XS::Dump($config);
95 if (!$self->dryRun) {
96 write_file $filename, $yaml
97 or die __x("Error writing '{filename}': {error}.",
98 filename => $filename, error => $!);
99 }
100  
101 return $self;
102 }
103  
104 sub logError {
105 my ($self, $msg) = @_;
106  
107 $self->logger->error($msg);
108 ++$self->{_err_count};
109  
110 # This allows the construct $self->logError or return;
111 return;
112 }
113  
114 sub createDirectory {
115 my ($self, $directory) = @_;
116  
117 return $self if -e $directory;
118  
119 my $logger = $self->logger;
120 $logger->debug(__x("Creating directory '{directory}'.",
121 directory => $directory));
122  
123 return $self if $self->dryRun;
124  
125 make_path $directory
126 or return $self->logError(__x("Error creating directory '{directory}': {error}!",
127 directory => $directory, error => $!));
128  
129 return $self;
130 }
131  
132 sub createFile {
133 my ($self, $path, $data) = @_;
134  
135 my ($volume, $directory, $filename) = File::Spec->splitpath($path);
136 $self->createDirectory(File::Spec->catpath($volume, $directory));
137  
138 write_file $path, $data
139 or $self->logError(__x("Error creating file '{filename}': {error}!",
140 file => $path, error => $!));
141  
142 return $self;
143 }
144  
145 sub markFileDone {
146 my ($self, @files) = @_;
147  
148 $self->{_files_done} ||= {};
149  
150 foreach my $file (@files) {
151 $self->{_files_done}->{$file} = 1;
152 }
153  
154 return $self;
155 }
156  
157 sub copyUndone {
158 my ($self, $fcopy) = @_;
159  
160 my $logger = $self->logger;
161 $logger->info(__"Copying all other files as is.");
162  
163 $self->{_files_done}->{$self->{_out_dir}} = 1;
164  
165 opendir my $dh, $self->{_src_dir}
166 or return $self->logError(__x("Error opening directory '{directory}':"
167 . " {error}!\n"));
168 my @files = grep {
169 !$self->{_files_done}->{$_};
170 } grep {
171 $_ ne '.';
172 } grep {
173 $_ ne '..';
174 } readdir $dh;
175  
176 $fcopy ||= sub { return $_[-1] };
177  
178 my $lexical_wrapper = wrap 'File::Copy::Recursive::fcopy', post => $fcopy;
179  
180 foreach my $file (@files) {
181 $logger->debug(__x("Copying '{file}'.",
182 file => $file));
183 my $dest = File::Spec->catfile($self->{_out_dir}, $file);
184 rcopy $file, $dest
185 or $self->logError(__x("Error copying '{file}' to '{dest}':"
186 . " {error}!\n",
187 file => $file,
188 dest => $dest,
189 error => $!));
190 }
191  
192 return $self;
193 }
194  
195 1;