Zucchini/Config.pm

filter: Statementstmt 75.7% 97 / 128 Branchbran 65.9% 29 / 44 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 77.2% 17 / 22 Podpod 8.3% 1 / 12 totaltotal 68.6% 149 / 217 CC 29 SCAR 18.6
CC
29
Cov
73%
BUILD
13.8
_sane_config
11.7
_load_config
5.0
CRAP 6.4
« Zucchini/Template.pm ↑ Summary Zucchini/Types.pm »
1 package Zucchini::Config;
2 9 $Zucchini::Config::VERSION = '0.0.21';
3 {
4 9 $Zucchini::Config::DIST = 'Zucchini';
5 }
6 # ABSTRACT: configuration provider
7 # vim: ts=8 sts=4 et sw=4 sr sta
8 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use strict; # for kwalitee testing
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use MooX::Types::MooseLike::Base qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use Zucchini::Config::Create;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # object attributes
18 9 has data => (
19 reader => 'get_data',
20 writer => 'set_data',
21 isa => HashRef,
22 is => 'ro',
23 );
24 9 has options => (
25 reader => 'get_options',
26 writer => 'set_options',
27 isa => HashRef,
28 is => 'ro',
29 );
30 9 has site => (
31 reader => 'get_site',
32 writer => 'set_site',
33 isa => Str,
34 is => 'ro',
35 );
36 has config_file => (
37 reader => 'get_config_file',
38 writer => 'set_config_file',
39 isa => Str,
40 15 ▶ default => sub {q{}.file($ENV{HOME}, q{.zucchini})},
Subroutine __ANON__: called CC=1 SCAR=0.0
41 9 is => 'ro',
42 );
43  
44  
45 16 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=26.2
Pod undocumented
46 16 my ($self, $arg_ref) = @_;
47  
48 # if we've been asked to create a new config-file, do just that
49 # and exit
50 16 ▶ if ($arg_ref->{'create-config'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"create-config"}) 0 16 16
51 0 my $zucchini_cfg_create = Zucchini::Config::Create->new();
52  
53 0 $zucchini_cfg_create->write_default_config(
54 $self->get_config_file
55 );
56  
57 0 exit;
58 }
59  
60 # store the config/arg_ref for future reference
61 16 $self->set_options($arg_ref);
62  
63 # we'll allow (clever) people to pass a hashref for the config
64 # (mostly useful for testing, but great for abuse too :) )
65 16 ▶ if (ref($arg_ref->{config_data})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg_ref->{'config_data'}) { } 15 1 16
66 15 $self->set_data( $arg_ref->{config_data} );
67 }
68 # load the config file - this is the preferred, default behaviour
69 else {
70 1 $self->_load_config();
71 }
72  
73 # if we don't have a config file - abort
74 16 ▶ if (not defined $self->get_data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_data) 1 15 16
75 1 warn (
76 $self->get_config_file
77 . qq{: configuration file not found, use 'create-config' option to create one\n}
78 );
79 1 exit;
80 }
81  
82 # see if we have any CLI options to inject from the config file
83 15 ▶ if (defined $self->get_data()->{cli_defaults}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_data->{"cli_defaults"}) 1 14 15
84 my %merged_cli_options = (
85 1 %{ $self->get_data->{cli_defaults} },
86 1 %{ $self->get_options }
87 );
88 1 $self->set_options( \%merged_cli_options );
89 }
90  
91 # deal with user options
92 15 ▶ if (defined $self->get_options->{site}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_options->{"site"}) 5 10 15
93 5 ▶ warn "using user-specified site label\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose(2) 0 5 5
94 if ($self->verbose(2));
95 5 $self->set_site( delete $self->get_options->{site} );
96 }
97  
98 # if we don't have a site specified, try to use a default
99 15 ▶ if (not defined $self->get_site()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_site) 10 5 15
100 10 ▶ warn "looking for default site\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose(2) 0 10 10
101 if ($self->verbose(2));
102 # set the default site (if specified in config file)
103 10 ▶ if (defined (my $default = $self->get_data()->{default_site})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $default = $self->get_data->{'default_site'})) { } 10 0 10
104 10 ▶ warn "using default site label\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose(2) 0 10 10
105 if ($self->verbose(2));
106 10 $self->set_site($default);
107 }
108 else {
109 0 warn "no default site specified\n";
110 }
111 }
112  
113 # make sure out config is sane
114 15 ▶ if (not $self->_sane_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_sane_config) 0 15 15
115 0 warn "configuration file is not valid\n";
116 0 exit;
117 }
118  
119 15 return;
120 }
121  
122 340 ▶sub get_siteconfig {
Subroutine get_siteconfig: called CC=2 SCAR=7.5
Pod undocumented
123 340 my $self = shift;
124 340 my ($site, $siteconfig);
125  
126 # get the site
127 340 $site = $self->get_site;
128  
129 # make sure it's defined
130 340 ▶ if (not defined $site) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $site) 0 340 340
131 0 warn
132 q{'}
133 . $site
134 . q{' is not defined}
135 . qq{\n};
136 0 return;
137 }
138  
139 # fetch the config block for the specified site
140 340 $siteconfig = $self->get_data()->{site}{$site};
141  
142 340 return $siteconfig;
143 }
144  
145 2 ▶sub ignored_directories {
Subroutine ignored_directories: called CC=2 SCAR=7.4
Pod undocumented
146 2 my $self = shift;
147  
148 2 my $ignored = $self->get_siteconfig()->{ignore_dirs};
149  
150 2 ▶ if (ref($ignored) eq q{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignored eq 'ARRAY') { } 2 0 2
151 # do nothing - it's already a list-ref
152 }
153 else {
154 0 $ignored = [ $ignored ];
155 }
156  
157 2 return $ignored;
158 }
159  
160 22 ▶sub ignored_files {
Subroutine ignored_files: called CC=2 SCAR=7.4
Pod undocumented
161 22 my $self = shift;
162  
163 22 my $ignored = $self->get_siteconfig()->{ignore_files};
164  
165 22 ▶ if (ref($ignored) eq q{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ignored eq 'ARRAY') { } 22 0 22
166 # do nothing - it's already a list-ref
167 }
168 else {
169 0 $ignored = [ $ignored ];
170 }
171  
172 22 return $ignored;
173 }
174  
175 0 ▶sub is_dry_run {
Subroutine is_dry_run: not called CC=1 SCAR=6.9
Pod undocumented
176 0 my $self = shift;
177 0 return $self->get_options()->{'dry-run'};
178 }
179  
180 0 ▶sub is_fsync {
Subroutine is_fsync: not called CC=1 SCAR=6.9
Pod undocumented
181 0 my $self = shift;
182 0 return $self->get_options()->{'fsync'};
183 }
184  
185 0 ▶sub is_fsync_only {
Subroutine is_fsync_only: not called CC=1 SCAR=6.9
Pod undocumented
186 0 my $self = shift;
187 0 return $self->get_options()->{'fsync-only'};
188 }
189  
190 0 ▶sub is_rsync {
Subroutine is_rsync: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = shift;
192 0 return $self->get_options()->{'rsync'};
193 }
194  
195 0 ▶sub is_rsync_only {
Subroutine is_rsync_only: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 return $self->get_options()->{'rsync-only'};
198 }
199  
200 20 ▶sub templated_files {
Subroutine templated_files: called CC=2 SCAR=6.9
Pod undocumented
201 20 my $self = shift;
202  
203 20 my $templated = $self->get_siteconfig()->{template_files};
204  
205 20 ▶ if (ref($templated) eq q{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $templated eq 'ARRAY') { } 6 14 20
206 # do nothing - it's already a list-ref
207 }
208 else {
209 14 $templated = [ $templated ];
210 }
211  
212 20 return $templated;
213 }
214  
215 24 ▶sub always_process {
Subroutine always_process: called CC=3 SCAR=11.2
Pod documented
216 24 my $self = shift;
217  
218 24 my $always_process = $self->get_siteconfig()->{always_process};
219  
220 return
221 24 ▶ unless defined $always_process;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $always_process 12 12 24
222  
223 12 ▶ if (ref($always_process) eq q{ARRAY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $always_process eq 'ARRAY') { } 12 0 12
224 # do nothing - it's already a list-ref
225 }
226 else {
227 0 $always_process = [ $always_process ];
228 }
229  
230 12 return $always_process;
231 }
232  
233 29 ▶sub verbose {
Subroutine verbose: called CC=3 SCAR=11.1
Pod undocumented
234 29 my $self = shift;
235 29 ▶ my $level = shift || 1;
Condition100%2/2
shift() || 1
l!l
263
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
236 29 ▶ return (($self->get_options()->{'verbose'}||0) >= $level);
Condition50%1/2
$self->get_options->{'verbose'} || 0
l!l
029
MC/DC0%0/1
$self->get_options->{'verbose'} || 0
$self->get_options->{'verbose'}
Truth table50%1/2
A
A= $self->get_options->{'verbose'}
Aresult
11
00
237 }
238  
239 1 ▶sub _load_config {
Subroutine _load_config: called CC=4 SCAR=16.1
240 1 my $self = shift;
241  
242 1 my $config_file = $self->get_config_file;
243  
244 # read/load/parse the config file
245 1 my $cfg = Config::Any->load_files(
246 {
247 files => [ $config_file ],
248 use_ext => 0,
249 }
250 );
251  
252 1 for (@$cfg) {
253 0 my ($filename, $config) = each %$_;
254 # store the config data (to be fetched later with ->get_data()
255 0 $self->set_data($config);
256 0 warn "loaded config from file: $filename" if (0);
257 }
258  
259 1 ▶ if (not defined $self->get_data()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->get_data) 1 0 1
260 1 ▶ warn "$config_file: no configuration data loaded\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
261 if ($self->verbose);
262 1 return;
263 }
264  
265 0 return;
266 }
267  
268 15 ▶sub _sane_config {
Subroutine _sane_config: called CC=7 SCAR=24.6
269 15 my $self = shift;
270 15 my $errors = 0;
271  
272 15 my $site_config = $self->get_siteconfig();
273  
274 15 ▶ if (not defined $site_config) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $site_config) 0 15 15
275 0 warn "site-specific configuration block is missing\n";
276 0 return;
277 }
278  
279 # these entries should all exist (as top-level keys) in the site-config
280 15 foreach my $required_key (qw[
281 source_dir
282 includes_dir
283 output_dir
284 template_files
285 ignore_dirs
286 ignore_files
287 tags
288 ]) {
289 105 ▶ if (not exists $site_config->{$required_key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $site_config->{$required_key}) 0 105 105
290 0 warn qq{** configuration option missing: $required_key\n};
291 0 $errors++;
292 }
293 }
294  
295 # these directories should exist
296 15 foreach my $required_dir (qw[source_dir includes_dir output_dir]) {
297 # dir should exist
298 45 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (exists $site_config->{$required_dir} and not -d $site_config->{$required_dir}) 0 45 45
Condition33%1/3
exists $site_config->{$required_dir} and not -d $site_config->{$required_dir}
!ll&&!rl&&r
0450
MC/DC0%0/2
exists $site_config->{$required_dir} and not -d $site_config->{$required_dir}
exists $site_config->{$required_dir} not -d $site_config->{$required_dir}
Truth table33%1/3
A and not(B)
A= exists $site_config->{$required_dir}B= not -d $site_config->{$required_dir}
ABresult
0X0
100
111
299 exists $site_config->{$required_dir}
300 and
301 not -d $site_config->{$required_dir}
302 ) {
303 0 warn qq{** directory missing: $site_config->{$required_dir}\n};
304 0 $errors++;
305 }
306 }
307  
308 15 return (not $errors);
309 }
310  
311 9 1;
312  
313 __END__
« Zucchini/Template.pm ↑ Summary Zucchini/Types.pm »