line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: yamltidy config module |
2
|
6
|
|
|
6
|
|
34
|
use strict; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
159
|
|
3
|
6
|
|
|
6
|
|
25
|
use warnings; |
|
6
|
|
|
|
|
8
|
|
|
6
|
|
|
|
|
112
|
|
4
|
6
|
|
|
6
|
|
48
|
use v5.20; |
|
6
|
|
|
|
|
16
|
|
5
|
6
|
|
|
6
|
|
26
|
use experimental qw/ signatures /; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
25
|
|
6
|
|
|
|
|
|
|
package YAML::Tidy::Config; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.006_001'; # TRIAL VERSION |
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
673
|
use Cwd; |
|
6
|
|
|
|
|
16
|
|
|
6
|
|
|
|
|
404
|
|
11
|
6
|
|
|
|
|
5792
|
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
|
6
|
|
|
6
|
|
52
|
/; |
|
6
|
|
|
|
|
13
|
|
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
|
24
|
|
|
24
|
0
|
757808
|
sub new($class, %args) { |
|
24
|
|
|
|
|
43
|
|
|
24
|
|
|
|
|
46
|
|
|
24
|
|
|
|
|
33
|
|
26
|
24
|
|
|
|
|
31
|
my $yaml; |
27
|
24
|
|
|
|
|
46
|
my $overridespaces = delete $args{indentspaces}; |
28
|
24
|
50
|
|
|
|
60
|
if (defined $args{configdata}) { |
29
|
0
|
|
|
|
|
0
|
$yaml = $args{configdata}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
else { |
32
|
24
|
|
|
|
|
38
|
my $file = $args{configfile}; |
33
|
24
|
100
|
|
|
|
55
|
unless (defined $file) { |
34
|
3
|
|
|
|
|
10
|
my ($home) = $class->_homedir; |
35
|
3
|
|
|
|
|
14
|
my $cwd = $class->_cwd; |
36
|
3
|
|
|
|
|
15
|
my @candidates = ( |
37
|
|
|
|
|
|
|
"$cwd/.yamltidy", |
38
|
|
|
|
|
|
|
"$home/.config/yamltidy/config.yaml", |
39
|
|
|
|
|
|
|
"$home/.yamltidy", |
40
|
|
|
|
|
|
|
); |
41
|
3
|
|
|
|
|
7
|
for my $c (@candidates) { |
42
|
9
|
50
|
|
|
|
121
|
if (-f $c) { |
43
|
0
|
|
|
|
|
0
|
$file = $c; |
44
|
0
|
|
|
|
|
0
|
last; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
24
|
100
|
|
|
|
57
|
if (defined $file) { |
49
|
21
|
50
|
|
|
|
902
|
open my $fh, '<', $file or die $!; |
50
|
21
|
|
|
|
|
53
|
$yaml = do { local $/; <$fh> }; |
|
21
|
|
|
|
|
87
|
|
|
21
|
|
|
|
|
816
|
|
51
|
21
|
|
|
|
|
250
|
close $fh; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
24
|
100
|
|
|
|
77
|
unless (defined $yaml) { |
55
|
3
|
|
|
|
|
8
|
$yaml = $class->standardcfg('default'); |
56
|
|
|
|
|
|
|
} |
57
|
24
|
|
|
|
|
35
|
my $cfg; |
58
|
24
|
|
|
|
|
179
|
my $yp = YAML::PP->new( |
59
|
|
|
|
|
|
|
schema => [qw/ + Merge /], |
60
|
|
|
|
|
|
|
cyclic_refs => 'fatal', |
61
|
|
|
|
|
|
|
); |
62
|
24
|
|
|
|
|
53507
|
$cfg = $yp->load_string($yaml); |
63
|
24
|
|
|
|
|
135258
|
my $v = delete $cfg->{v}; |
64
|
24
|
|
50
|
|
|
78
|
my $indent = delete $cfg->{indentation} || {}; |
65
|
24
|
50
|
|
|
|
59
|
$indent->{spaces} = $overridespaces if defined $overridespaces; |
66
|
24
|
|
50
|
|
|
74
|
$indent->{spaces} //= 2; # TODO support keeping original indent |
67
|
24
|
|
100
|
|
|
94
|
$indent->{'block-sequence-in-mapping'} //= 0; |
68
|
24
|
|
50
|
|
|
62
|
my $trimtrailing = $cfg->{'trailing-spaces'} || ''; |
69
|
24
|
100
|
|
|
|
56
|
if ($trimtrailing eq 'fix') { |
70
|
23
|
|
|
|
|
33
|
$trimtrailing = 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
1
|
|
|
|
|
2
|
$trimtrailing = 0; |
74
|
|
|
|
|
|
|
} |
75
|
24
|
|
|
|
|
66
|
my $scalarstyle = { default => YAML_PLAIN_SCALAR_STYLE }; |
76
|
24
|
50
|
|
|
|
67
|
if (my $scalar = delete $cfg->{'scalar-style'}) { |
77
|
24
|
|
|
|
|
35
|
my $default = $scalar->{default}; |
78
|
24
|
100
|
|
|
|
58
|
$scalarstyle->{default} = $default ? $stylemap{ $default } : undef; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
24
|
|
|
|
|
57
|
delete @args{qw/ configfile configdata /}; |
82
|
24
|
100
|
|
|
|
67
|
if (my @unknown = keys %args) { |
83
|
1
|
|
|
|
|
49
|
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
|
|
100
|
|
|
270
|
adjacency => delete $cfg->{adjacency} // 'keep', |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
92
|
|
|
|
|
|
|
scalar_style => $scalarstyle, |
93
|
|
|
|
|
|
|
}, $class; |
94
|
23
|
|
|
|
|
967
|
return $self; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub _cwd { |
98
|
0
|
|
|
0
|
|
0
|
return Cwd::cwd(); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
0
|
|
0
|
sub _homedir($class) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
102
|
0
|
|
|
|
|
0
|
return <~>; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
25445
|
|
|
25445
|
0
|
27958
|
sub indent($self) { |
|
25445
|
|
|
|
|
28462
|
|
|
25445
|
|
|
|
|
25554
|
|
106
|
25445
|
|
|
|
|
50190
|
return $self->{indentation}->{spaces}; |
107
|
|
|
|
|
|
|
} |
108
|
370
|
|
|
370
|
0
|
698
|
sub indent_seq_in_map($self) { |
|
370
|
|
|
|
|
557
|
|
|
370
|
|
|
|
|
595
|
|
109
|
370
|
|
|
|
|
1024
|
return $self->{indentation}->{'block-sequence-in-mapping'}; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
24473
|
|
|
24473
|
0
|
26869
|
sub trimtrailing($self) { |
|
24473
|
|
|
|
|
27219
|
|
|
24473
|
|
|
|
|
25378
|
|
113
|
24473
|
|
|
|
|
45349
|
return $self->{trimtrailing}; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
2043
|
|
|
2043
|
0
|
2768
|
sub addheader($self) { |
|
2043
|
|
|
|
|
2458
|
|
|
2043
|
|
|
|
|
2236
|
|
117
|
2043
|
|
|
|
|
3520
|
my $header = $self->{header}; |
118
|
2043
|
100
|
|
|
|
12074
|
return 0 if $header eq 'keep'; |
119
|
678
|
100
|
|
|
|
4032
|
return $header ? 1 : 0; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
3200
|
|
|
3200
|
0
|
4007
|
sub addfooter($self) { |
|
3200
|
|
|
|
|
4037
|
|
|
3200
|
|
|
|
|
3519
|
|
123
|
3200
|
|
|
|
|
5248
|
my $footer = $self->{footer}; |
124
|
3200
|
100
|
|
|
|
13998
|
return 0 if $footer eq 'keep'; |
125
|
1062
|
100
|
|
|
|
4752
|
return $footer ? 1 : 0; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
749
|
|
|
749
|
0
|
1081
|
sub adjacency($self) { |
|
749
|
|
|
|
|
931
|
|
|
749
|
|
|
|
|
868
|
|
129
|
749
|
|
|
|
|
1223
|
my $adjacency = $self->{adjacency}; |
130
|
749
|
50
|
|
|
|
2012
|
return undef if $adjacency eq 'keep'; |
131
|
0
|
0
|
|
|
|
0
|
return $adjacency ? 1 : 0; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
955
|
|
|
955
|
0
|
1498
|
sub removeheader($self) { |
|
955
|
|
|
|
|
1480
|
|
|
955
|
|
|
|
|
1445
|
|
135
|
955
|
|
|
|
|
2224
|
my $header = $self->{header}; |
136
|
955
|
100
|
|
|
|
4385
|
return 0 if $header eq 'keep'; |
137
|
318
|
100
|
|
|
|
1695
|
return $header ? 0 : 1; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
147
|
|
|
147
|
0
|
283
|
sub removefooter($self) { |
|
147
|
|
|
|
|
264
|
|
|
147
|
|
|
|
|
226
|
|
141
|
147
|
|
|
|
|
334
|
my $footer = $self->{footer}; |
142
|
147
|
100
|
|
|
|
747
|
return 0 if $footer eq 'keep'; |
143
|
54
|
100
|
|
|
|
368
|
return $footer ? 0 : 1; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
10419
|
|
|
10419
|
0
|
11475
|
sub default_scalar_style($self) { |
|
10419
|
|
|
|
|
12369
|
|
|
10419
|
|
|
|
|
11104
|
|
147
|
10419
|
|
|
|
|
21031
|
return $self->{scalar_style}->{default}; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub standardcfg { |
151
|
3
|
|
|
3
|
0
|
15
|
my $yaml = <<'EOM'; |
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
|
|
|
|
|
|
|
1; |