| 1 | # ABSTRACT: yamltidy config module | ||||||||||||||||||||||||||||||||
| 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 | 8 | ▶ | use v5.20; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use experimental qw/ signatures /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | package YAML::Tidy::Config; | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 8 | our $VERSION = '0.007'; # VERSION | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Cwd; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 8 | use YAML::PP::Common qw/ | |||||||||||||||||||||||||||||||
| 12 | YAML_PLAIN_SCALAR_STYLE YAML_SINGLE_QUOTED_SCALAR_STYLE | ||||||||||||||||||||||||||||||||
| 13 | YAML_DOUBLE_QUOTED_SCALAR_STYLE YAML_LITERAL_SCALAR_STYLE | ||||||||||||||||||||||||||||||||
| 14 | YAML_FOLDED_SCALAR_STYLE | ||||||||||||||||||||||||||||||||
| 15 | YAML_FLOW_SEQUENCE_STYLE YAML_FLOW_MAPPING_STYLE | ||||||||||||||||||||||||||||||||
| 16 | 8 | ▶ | /; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | 8 | my %stylemap = ( | |||||||||||||||||||||||||||||||
| 18 | plain => YAML_PLAIN_SCALAR_STYLE, | ||||||||||||||||||||||||||||||||
| 19 | single => YAML_SINGLE_QUOTED_SCALAR_STYLE, | ||||||||||||||||||||||||||||||||
| 20 | double => YAML_DOUBLE_QUOTED_SCALAR_STYLE, | ||||||||||||||||||||||||||||||||
| 21 | # literal => YAML_LITERAL_SCALAR_STYLE, | ||||||||||||||||||||||||||||||||
| 22 | # folded => YAML_FOLDED_SCALAR_STYLE, | ||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | ▶ | sub new($class, %args) { | |||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=20 SCAR=30.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 26 | 24 | my $yaml; | |||||||||||||||||||||||||||||||
| 27 | 24 | my $overridespaces = delete $args{indentspaces}; | |||||||||||||||||||||||||||||||
| 28 | 24 | ▶ | if (defined $args{configdata}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 29 | 0 | $yaml = $args{configdata}; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | else { | ||||||||||||||||||||||||||||||||
| 32 | 24 | my $file = $args{configfile}; | |||||||||||||||||||||||||||||||
| 33 | 24 | ▶ | unless (defined $file) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 34 | 3 | my ($home) = $class->_homedir; | |||||||||||||||||||||||||||||||
| 35 | 3 | my $cwd = $class->_cwd; | |||||||||||||||||||||||||||||||
| 36 | 3 | my @candidates = ( | |||||||||||||||||||||||||||||||
| 37 | "$cwd/.yamltidy", | ||||||||||||||||||||||||||||||||
| 38 | "$home/.config/yamltidy/config.yaml", | ||||||||||||||||||||||||||||||||
| 39 | "$home/.yamltidy", | ||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||
| 41 | 3 | for my $c (@candidates) { | |||||||||||||||||||||||||||||||
| 42 | 9 | ▶ | if (-f $c) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 43 | 0 | $file = $c; | |||||||||||||||||||||||||||||||
| 44 | 0 | last; | |||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | 24 | ▶ | if (defined $file) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 49 | 21 | ▶ | open my $fh, '<', $file or die $!; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 50 | 21 | $yaml = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||
| 51 | 21 | close $fh; | |||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||
| 54 | 24 | ▶ | unless (defined $yaml) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 3 | $yaml = $class->standardcfg('default'); | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | 24 | my $cfg; | |||||||||||||||||||||||||||||||
| 58 | 24 | my $yp = YAML::PP->new( | |||||||||||||||||||||||||||||||
| 59 | schema => [qw/ + Merge /], | ||||||||||||||||||||||||||||||||
| 60 | cyclic_refs => 'fatal', | ||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||
| 62 | 24 | $cfg = $yp->load_string($yaml); | |||||||||||||||||||||||||||||||
| 63 | 24 | my $v = delete $cfg->{v}; | |||||||||||||||||||||||||||||||
| 64 | 24 | ▶ | my $indent = delete $cfg->{indentation} || {}; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $cfg->{'indentation'} || {}
MC/DC0%0/1
delete $cfg->{'indentation'} || {}
delete $cfg->{'indentation'}
Truth table50%1/2
A
A= delete $cfg->{'indentation'}
| |||||||||||||||||||||||||||||||||
| 65 | 24 | ▶ | $indent->{spaces} = $overridespaces if defined $overridespaces; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | 24 | ▶ | $indent->{spaces} //= 2; # TODO support keeping original indent | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$indent->{"spaces"} //= 2
MC/DC0%0/1
$indent->{"spaces"} //= 2
$indent->{"spaces"}
Truth table50%1/2
A
A= $indent->{"spaces"}
| |||||||||||||||||||||||||||||||||
| 67 | 24 | ▶ | $indent->{'block-sequence-in-mapping'} //= 0; | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$indent->{"block-sequence-in-mapping"} //= 0
MC/DC100%1/1
$indent->{"block-sequence-in-mapping"} //= 0
$indent->{"block-sequence-in-mapping"}
Truth table100%2/2
A
A= $indent->{"block-sequence-in-mapping"}
| |||||||||||||||||||||||||||||||||
| 68 | 24 | ▶ | my $trimtrailing = $cfg->{'trailing-spaces'} || ''; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$cfg->{'trailing-spaces'} || ''
MC/DC0%0/1
$cfg->{'trailing-spaces'} || ''
$cfg->{'trailing-spaces'}
Truth table50%1/2
A
A= $cfg->{'trailing-spaces'}
| |||||||||||||||||||||||||||||||||
| 69 | 24 | ▶ | if ($trimtrailing eq 'fix') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 70 | 23 | $trimtrailing = 1; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | else { | ||||||||||||||||||||||||||||||||
| 73 | 1 | $trimtrailing = 0; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | 24 | my $scalarstyle = { default => YAML_PLAIN_SCALAR_STYLE }; | |||||||||||||||||||||||||||||||
| 76 | 24 | ▶ | if (my $scalar = delete $cfg->{'scalar-style'}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 24 | my $default = $scalar->{default}; | |||||||||||||||||||||||||||||||
| 78 | 24 | ▶ | $scalarstyle->{default} = $default ? $stylemap{ $default } : undef; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 24 | delete @args{qw/ configfile configdata /}; | |||||||||||||||||||||||||||||||
| 82 | 24 | ▶ | if (my @unknown = keys %args) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 1 | die "Unknown configuration parameters: @unknown"; | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | my $self = bless { | ||||||||||||||||||||||||||||||||
| 86 | version => $v, | ||||||||||||||||||||||||||||||||
| 87 | indentation => $indent, | ||||||||||||||||||||||||||||||||
| 88 | trimtrailing => $trimtrailing, | ||||||||||||||||||||||||||||||||
| 89 | header => delete $cfg->{header} // 'keep', | ||||||||||||||||||||||||||||||||
| 90 | footer => delete $cfg->{footer} // 'keep', | ||||||||||||||||||||||||||||||||
| 91 | 23 | ▶ | adjacency => delete $cfg->{adjacency} // 'keep', | ||||||||||||||||||||||||||||||
|
Condition100%6/6
delete $cfg->{"header"} // "keep"
delete $cfg->{"footer"} // "keep"
delete $cfg->{"adjacency"} // "keep"
MC/DC100%1/1
delete $cfg->{"header"} // "keep"
delete $cfg->{"header"}
MC/DC100%1/1
delete $cfg->{"footer"} // "keep"
delete $cfg->{"footer"}
MC/DC100%1/1
delete $cfg->{"adjacency"} // "keep"
delete $cfg->{"adjacency"}
Truth table100%2/2
A
A= delete $cfg->{"header"}
Truth table100%2/2
A
A= delete $cfg->{"footer"}
Truth table100%2/2
A
A= delete $cfg->{"adjacency"}
| |||||||||||||||||||||||||||||||||
| 92 | scalar_style => $scalarstyle, | ||||||||||||||||||||||||||||||||
| 93 | }, $class; | ||||||||||||||||||||||||||||||||
| 94 | 23 | return $self; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | sub _cwd { | ||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | return Cwd::cwd(); | ||||||||||||||||||||||||||||||
|
Subroutine
_cwd: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | ▶ | sub _homedir($class) { | |||||||||||||||||||||||||||||||
|
Subroutine
_homedir: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 102 | 0 | return <~>; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | ▶ | sub indent($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
indent: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 106 | 25445 | return $self->{indentation}->{spaces}; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | ▶ | sub indent_seq_in_map($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
indent_seq_in_map: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 109 | 370 | return $self->{indentation}->{'block-sequence-in-mapping'}; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | ▶ | sub trimtrailing($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
trimtrailing: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 113 | 24473 | return $self->{trimtrailing}; | |||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | ▶ | sub addheader($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
addheader: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 117 | 2043 | my $header = $self->{header}; | |||||||||||||||||||||||||||||||
| 118 | 2043 | ▶ | return 0 if $header eq 'keep'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 119 | 678 | ▶ | return $header ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | ▶ | sub addfooter($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
addfooter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 123 | 3200 | my $footer = $self->{footer}; | |||||||||||||||||||||||||||||||
| 124 | 3200 | ▶ | return 0 if $footer eq 'keep'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | 1062 | ▶ | return $footer ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | ▶ | sub adjacency($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
adjacency: called CC=3 SCAR=15.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 129 | 749 | my $adjacency = $self->{adjacency}; | |||||||||||||||||||||||||||||||
| 130 | 749 | ▶ | return undef if $adjacency eq 'keep'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | return $adjacency ? 1 : 0; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | ▶ | sub removeheader($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
removeheader: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 135 | 955 | my $header = $self->{header}; | |||||||||||||||||||||||||||||||
| 136 | 955 | ▶ | return 0 if $header eq 'keep'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 137 | 318 | ▶ | return $header ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||
| 140 | ▶ | sub removefooter($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
removefooter: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 141 | 147 | my $footer = $self->{footer}; | |||||||||||||||||||||||||||||||
| 142 | 147 | ▶ | return 0 if $footer eq 'keep'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 54 | ▶ | return $footer ? 0 : 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||
| 146 | ▶ | sub default_scalar_style($self) { | |||||||||||||||||||||||||||||||
|
Subroutine
default_scalar_style: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 147 | 10419 | return $self->{scalar_style}->{default}; | |||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | sub standardcfg { | ||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | my $yaml = <<'EOM'; | ||||||||||||||||||||||||||||||
|
Subroutine
standardcfg: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 152 | --- | ||||||||||||||||||||||||||||||||
| 153 | v: v0.1 | ||||||||||||||||||||||||||||||||
| 154 | indentation: | ||||||||||||||||||||||||||||||||
| 155 | spaces: 2 | ||||||||||||||||||||||||||||||||
| 156 | block-sequence-in-mapping: 0 | ||||||||||||||||||||||||||||||||
| 157 | trailing-spaces: fix | ||||||||||||||||||||||||||||||||
| 158 | header: true | ||||||||||||||||||||||||||||||||
| 159 | scalar-style: | ||||||||||||||||||||||||||||||||
| 160 | default: plain | ||||||||||||||||||||||||||||||||
| 161 | adjacency: 0 | ||||||||||||||||||||||||||||||||
| 162 | EOM | ||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 8 | 1; | |||||||||||||||||||||||||||||||