Iller/DocType.pm

filter: Statementstmt 82.4% 108 / 131 Branchbran 41.1% 14 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.3% 26 / 27 Podpod 0.0% 0 / 3 totaltotal 75.9% 148 / 195 CC 21 SCAR 29.3
CC
21
Cov
74%
generate_file
65.2
parse_config
8.0
__ANON__
4.0
CRAP 18.7
↑ Summary Iller/Role/HasPlugins.pm »
1 8 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dist::Iller::DocType;
6  
7 8 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
8 # ABSTRACT: Role for document types that can be used in Dist::Iller configs
9 8 our $VERSION = '0.1411';
10  
11 8 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use MooseX::AttributeShortcuts;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Text::Diff;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Types::Standard qw/ConsumerOf Str HashRef InstanceOf Maybe/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Module::Load qw/load/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 ▶use String::CamelCase qw/decamelize/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use YAML::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use Safe::Isa qw/$_can/;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use Types::Path::Tiny qw/Path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use PerlX::Maybe qw/maybe/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 8 requires qw/
28 filename
29 parse
30 phase
31 to_hash
32 to_string
33 comment_start
34 /;
35  
36 # this is set if we are parsing a ::Config class
37 8 has config_obj => (
38 is => 'ro',
39 isa => ConsumerOf['Dist::Iller::Config'],
40 predicate =>1,
41 );
42 has doctype => (
43 is => 'ro',
44 isa => Str,
45 init_arg => undef,
46 10 ▶ default => sub { decamelize( (split /::/, shift->meta->name)[-1] ); },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 8 );
48 has included_configs => (
49 is => 'ro',
50 isa => HashRef,
51 init_arg => undef,
52 traits => ['Hash'],
53 10 ▶ default => sub { +{ } },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 8 handles => {
55 set_included_config => 'set',
56 all_included_configs => 'kv',
57 has_included_configs => 'count',
58 },
59 );
60 8 has global => (
61 is => 'ro',
62 isa => Maybe[InstanceOf['Dist::Iller::DocType::Global']],
63 predicate => 1,
64 );
65  
66 13 ▶around parse => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 13 my $next = shift;
68 13 my $self = shift;
69 13 my $yaml = shift;
70  
71 13 $self->parse_config($yaml->{'configs'});
72 13 $self->$next($yaml);
73  
74 13 return $self;
75 8 };
76 16 ▶sub parse_config {
Subroutine parse_config: called CC=7 SCAR=20.8
Pod undocumented
77 16 my $self = shift;
78 16 my $yaml = shift;
79  
80 16 ▶ return if !defined $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $yaml 13 3 16
81  
82 3 ▶ if(ref $yaml eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $yaml eq 'ARRAY') { } 0 3 3
83 0 warn 'Multiple configs found';
84 0 for my $doc (@{ $yaml }) {
85 0 $self->parse_config($doc);
86 }
87 }
88 else {
89 3 my $config_name = delete $yaml->{'+config'};
90 3 my $config_class = "Dist::Iller::Config::$config_name";
91  
92 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
93 3 load "$config_class";
94 }
95 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 croak "Can't find $config_class ($_)";
97 3 };
98  
99 my $configobj = $config_class->new(
100 3 ▶ %{ $yaml },
Branch: 2 branches
BranchTrueFalseTotal
$self->$Dist::Iller::DocType::_can('name') ? : 1 2 3
$self->global ? : 3 0 3
101 maybe distribution_name => ($self->$_can('name') ? $self->name : undef),
102 maybe global => ($self->global ? $self->global : undef),
103 );
104 3 my $configdoc = $configobj->get_yaml_for($self->doctype);
105 3 ▶ return if !defined $configdoc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $configdoc 0 3 3
106  
107 3 $self->parse($configdoc);
108 3 $self->set_included_config($config_class, $config_class->VERSION);
109 }
110 }
111  
112 3 ▶sub to_yaml { YAML::Tiny->new(shift->to_hash)->[0] }
Subroutine to_yaml: called CC=1 SCAR=0.0
Pod undocumented
113  
114 6 ▶around to_string => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
115 6 my $next = shift;
116 6 my $self = shift;
117  
118 6 my $string = $self->$next(@_);
119 6 ▶ return $string if !defined $self->comment_start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->comment_start 0 6 6
120  
121 6 my $now = DateTime->now;
122  
123 6 my @intro = ();
124 6 push @intro => $self->comment_start . sprintf (' This file was auto-generated from iller.yaml by Dist::Iller on %s %s %s.', $now->ymd, $now->hms, $now->time_zone->name);
125 6 ▶ if($self->has_included_configs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_included_configs) 3 3 6
126 3 push @intro => $self->comment_start . ' The following configs were used:';
127  
128 0 for my $config (sort { $a->[0] cmp $b->[0] } $self->all_included_configs) {
129 3 push @intro => $self->comment_start . qq{ * $config->[0]: $config->[1]};
130 }
131 }
132 6 push @intro => ('', '');
133  
134 6 return join ("\n", @intro) . $string;
135  
136 8 };
137  
138 6 ▶sub generate_file {
Subroutine generate_file: called CC=12 SCAR=41.8
Pod undocumented
139 6 my $self = shift;
140  
141 6 ▶ return if !$self->filename; # for doctype:global
Branch: 1 branch
BranchTrueFalseTotal
unless $self->filename 0 6 6
142  
143 6 ▶ my $path = Path->check($self->filename) ? $self->filename : Path->coerce($self->filename);
Branch: 1 branch
BranchTrueFalseTotal
&Path()->check($self->filename) ? : 0 6 6
144  
145 6 my $new_document = $self->to_string;
146 6 ▶ my $previous_document = $path->exists ? $path->slurp_utf8 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$path->exists ? : 0 6 6
147  
148 6 ▶ if(!defined $previous_document) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $previous_document) 6 0 6
149 6 $path->spew_utf8($new_document);
150 6 say "[Iller] Creates $path";
151 6 return;
152 }
153  
154 0 my $comment_start = $self->comment_start;
155 0 my $diff = diff \$previous_document, \$new_document, { STYLE => 'Unified' };
156 0 my $diff_count = 0;
157 0 my $skip_first = 1;
158 0 for my $row (split m{\r?\n}, $diff) {
159 0 ▶ next if $skip_first-- == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_first-- == 1 0 0 0
160 0 ▶ next if $row =~ m{^ };
Branch: 1 branch
BranchTrueFalseTotal
if $row =~ /^ /u 0 0 0
161 0 ▶ if($row =~ m{; authordep }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row =~ /; authordep /u) 0 0 0
162 0 ++$diff_count;
163 0 next;
164 }
165 0 ▶ next if $row =~ m{^[-+]\s*?$comment_start};
Branch: 1 branch
BranchTrueFalseTotal
if $row =~ /^[-+]\s*?$comment_start/u 0 0 0
166 0 ▶ next if $row =~ m{^[-+]\s*$};
Branch: 1 branch
BranchTrueFalseTotal
if $row =~ /^[-+]\s*$/u 0 0 0
167 0 ++$diff_count;
168 }
169  
170 0 ▶ if($diff_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff_count) { } 0 0 0
171 0 $path->spew_utf8($new_document);
172 0 say "[Iller] Generates $path";
173 }
174 else {
175 0 say "[Iller] No changes for $path";
176 }
177 }
178  
179 8 1;
180  
181 __END__
↑ Summary Iller/Role/HasPlugins.pm »