File Coverage

blib/lib/YAML/Tidy/Config.pm
Criterion Covered Total %
statement 120 123 97.5
branch 40 46 86.9
condition 14 14 100.0
subroutine 20 20 100.0
pod 0 12 0.0
total 194 215 90.2


line stmt bran cond sub pod time code
1             # ABSTRACT: yamltidy config module
2 13     13   213690 use strict;
  13         26  
  13         721  
3 13     13   73 use warnings;
  13         36  
  13         766  
4 13     13   219 use v5.20;
  13         52  
5 13     13   738 use experimental qw/ signatures /;
  13         2091  
  13         89  
6             package YAML::Tidy::Config;
7              
8             our $VERSION = 'v0.11.0'; # VERSION
9              
10 13     13   2708 use Cwd;
  13         22  
  13         1349  
11 13         23360 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 13     13   789 /;
  13         2490  
17             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 36     36 0 3553612 sub new($class, %args) {
  36         102  
  36         141  
  36         114  
26 36         76 my $yaml;
27 36         3507 my $overridespaces = delete $args{indentspaces};
28 36 50       203 if (defined $args{configdata}) {
29 0         0 $yaml = $args{configdata};
30             }
31             else {
32 36         151 my $file = $args{configfile};
33 36 100       236 unless (defined $file) {
34 5         35 my ($home) = $class->_homedir;
35 5         41 my $cwd = $class->_cwd;
36 5         94 my @candidates = (
37             "$cwd/.yamltidy",
38             "$home/.config/yamltidy/config.yaml",
39             "$home/.yamltidy",
40             );
41 5         38 for my $c (@candidates) {
42 15 50       558 if (-f $c) {
43 0         0 $file = $c;
44 0         0 last;
45             }
46             }
47             }
48 36 100       210 if (defined $file) {
49 31 50       3205 open my $fh, '<', $file or die $!;
50 31         122 $yaml = do { local $/; <$fh> };
  31         227  
  31         3921  
51 31         586 close $fh;
52             }
53             }
54 36 100       165 unless (defined $yaml) {
55 5         53 $yaml = $class->standardcfg('default');
56             }
57 36         75 my $cfg;
58 36         606 my $yp = YAML::PP->new(
59             schema => [qw/ + Merge /],
60             cyclic_refs => 'fatal',
61             );
62 36         138678 $cfg = $yp->load_string($yaml);
63 36         405317 my $v = delete $cfg->{v};
64 36   100     235 my $indent = delete $cfg->{indentation} || {};
65 36 50       171 $indent->{spaces} = $overridespaces if defined $overridespaces;
66 36   100     187 $indent->{spaces} //= 2; # TODO support keeping original indent
67 36   100     218 $indent->{'block-sequence-in-mapping'} //= 0;
68 36   100     154 my $trimtrailing = $cfg->{'trailing-spaces'} || '';
69 36 100       161 if ($trimtrailing eq 'fix') {
70 33         89 $trimtrailing = 1;
71             }
72             else {
73 3         26 $trimtrailing = 0;
74             }
75 36         161 my $scalarstyle = { default => YAML_PLAIN_SCALAR_STYLE };
76 36 100       204 if (my $scalar = delete $cfg->{'scalar-style'}) {
77 34         124 my $default = $scalar->{default};
78 34 100       161 $scalarstyle->{default} = $default ? $stylemap{ $default } : undef;
79             }
80 36         116 my $serialize_aliases = 0;
81 36 100       187 if (my $aliases = $cfg->{aliases}) {
82 1 50       5 if ($aliases->{serialize}) {
83 1         2 $serialize_aliases = 1;
84             }
85             }
86              
87 36         158 delete @args{qw/ configfile configdata /};
88 36 100       175 if (my @unknown = keys %args) {
89 1         127 die "Unknown configuration parameters: @unknown";
90             }
91             my $self = bless {
92             version => $v,
93             indentation => $indent,
94             trimtrailing => $trimtrailing,
95             header => delete $cfg->{header} // 'keep',
96             footer => delete $cfg->{footer} // 'keep',
97 35   100     954 adjacency => delete $cfg->{adjacency} // 'keep',
      100        
      100        
98             scalar_style => $scalarstyle,
99             serialize_aliases => $serialize_aliases,
100             }, $class;
101 35         4246 return $self;
102             }
103              
104             sub _cwd {
105 2     2   22030 return Cwd::cwd();
106             }
107              
108 2     2   6 sub _homedir($class) {
  2         5  
  2         6  
109 2         196 return <~>;
110             }
111              
112 24271     24271 0 45809 sub indent($self) {
  24271         36149  
  24271         37254  
113 24271         79565 return $self->{indentation}->{spaces};
114             }
115 358     358 0 762 sub indent_seq_in_map($self) {
  358         771  
  358         655  
116 358         1326 return $self->{indentation}->{'block-sequence-in-mapping'};
117             }
118              
119 23305     23305 0 40961 sub trimtrailing($self) {
  23305         38170  
  23305         50548  
120 23305         73961 return $self->{trimtrailing};
121             }
122              
123 1940     1940 0 3789 sub addheader($self) {
  1940         3453  
  1940         2976  
124 1940         5814 my $header = $self->{header};
125 1940 100       26477 return 0 if $header eq 'keep';
126 685 100       8080 return $header ? 1 : 0;
127             }
128              
129 3033     3033 0 5612 sub addfooter($self) {
  3033         5345  
  3033         6172  
130 3033         8621 my $footer = $self->{footer};
131 3033 100       26128 return 0 if $footer eq 'keep';
132 1062 100       10972 return $footer ? 1 : 0;
133             }
134              
135 727     727 0 1264 sub adjacency($self) {
  727         1314  
  727         1120  
136 727         1681 my $adjacency = $self->{adjacency};
137 727 100       2918 return undef if $adjacency eq 'keep';
138 13 50       45 return $adjacency ? 1 : 0;
139             }
140              
141 902     902 0 2091 sub removeheader($self) {
  902         2298  
  902         1863  
142 902         2898 my $header = $self->{header};
143 902 100       6894 return 0 if $header eq 'keep';
144 318 100       3127 return $header ? 0 : 1;
145             }
146              
147 138     138 0 309 sub removefooter($self) {
  138         341  
  138         288  
148 138         400 my $footer = $self->{footer};
149 138 100       1034 return 0 if $footer eq 'keep';
150 54 100       524 return $footer ? 0 : 1;
151             }
152              
153 9986     9986 0 21901 sub default_scalar_style($self) {
  9986         15432  
  9986         14479  
154 9986         45001 return $self->{scalar_style}->{default};
155             }
156              
157             # Turns
158             # ---
159             # - &color blue
160             # - *color
161             # - &color pink
162             # - *color
163             # ...
164             # into
165             # ---
166             # - &color_1 blue
167             # - *blue_1
168             # - &color_2 blue
169             # - *blue_2
170             # ...
171 3171     3171 0 6086 sub serialize_aliases($self) { $self->{serialize_aliases} }
  3171         6868  
  3171         5836  
  3171         11689  
172              
173             sub standardcfg {
174 5     5 0 45 my $yaml = <<'EOM';
175             ---
176             v: v0.1
177             indentation:
178             spaces: 2
179             block-sequence-in-mapping: 0
180             trailing-spaces: fix
181             header: true
182             scalar-style:
183             default: plain
184             adjacency: 0
185             EOM
186             }
187              
188             1;