Qgoda/Init/copy.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 14 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 112 CC 13 SCAR 48.3
CC
13
Cov
0%
run
182.0
new
2.0
BEGIN
2.0
CRAP 125.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::Init::copy;
20 $Qgoda::Init::copy::VERSION = 'v0.9.2';
21 use strict;
22  
23 use Locale::TextDomain qw('qgoda');
24 use File::Spec;
25 use File::Find qw(find);
26 use File::Copy qw(copy);
27  
28 sub new {
29 my ($class, $init) = @_;
30  
31 bless {
32 __init => $init,
33 }, $class;
34 }
35  
36 sub run {
37 my ($self, $config) = @_;
38  
39 my $q = Qgoda->new;
40 my $logger = $q->logger;
41 my $force = $self->{__init}->getOption('force');
42 my $ignore_case = !$q->config->{'case-sensitive'};
43 my @excludes = @{$config->{_exclude} || []};
44 push @excludes, '/_config.yaml', '/_config.yml', '/_init.yaml', '/_init.yml';
45 my $exclude = File::Globstar::ListMatch->new(\@excludes,
46 ignoreCase => $ignore_case);
47 my $precious;
48 if ($force < 2 && $config->{_precious}) {
49 $precious = File::Globstar::ListMatch->new($config->{_precious});
50 }
51  
52 $logger->info(__"copying files");
53 if ($force) {
54 $logger->info(__"option '--force' given, will overwrite files");
55 if ($config->{_precious}) {
56 if ($force > 1) {
57 $logger->info(__"option '--force' given twice, will overwrite precious files");
58 } else {
59 $logger->info(__"but will preserve precious files, give '--force' twice to overwrite");
60 }
61 }
62 } else {
63 $logger->info(__"will preserve files, use '--force' to overwrite");
64 }
65  
66 # Filter out entries that should be ignored. This happens again in the
67 # wanted function but it allows to bypass ignored directories completely.
68 # This may come in handy if the directory contains '/node_modules'.
69 my $preprocess = sub {
70 my (@names) = @_;
71 my $relpath = File::Spec->abs2rel($File::Find::dir, $config->{_srcdir});
72  
73 # Do not use File::Spec->catfile! Ignore patterns uses slashes only.
74 return grep { !$exclude->match("$relpath/$_") } @names;
75 };
76  
77 my $wanted = sub {
78 return if '.' eq $_;
79 return if '..' eq $_;
80  
81 my $relpath = File::Spec->abs2rel($File::Find::name, $config->{_srcdir});
82 next if $exclude->match($relpath);
83  
84 if (-d $File::Find::name) {
85 if (!-e $relpath) {
86 $logger->debug(__x("create directory '{dir}'",
87 dir => $relpath));
88 mkdir $relpath
89 or $logger->fatal(__x("cannot create '{dir}': {error}",
90 dir => $relpath, error => $!));
91 }
92  
93 return; # Done.
94 }
95  
96 if (-e $relpath) {
97 if (!$force) {
98 $logger->info(__x("not overwriting '{file}' without '--force'",
99 file => $relpath));
100 return; # Done.
101 }
102  
103 if ($precious && $precious->matchInclude($relpath)) {
104 $logger->info(__x("not overwriting precious '{file}' without '--force --force'",
105 file => $relpath));
106 return; # Done.
107 }
108 }
109  
110 # All tests passed, copy the file.
111 $logger->debug(__x("copy '{src}' to '{dest}'",
112 src => $File::Find::name, dest => $relpath));
113 copy $File::Find::name, $relpath
114 or $logger->fatal(__x("cannot copy '{src}' to '{dest}': {error}",
115 src => $File::Find::name,
116 dest => $relpath,
117 error => $!));
118 };
119  
120 find { wanted => $wanted, no_chdir => 1 }, $config->{_srcdir};
121  
122 return $self;
123 }
124  
125 1;