line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: yamltidy config module |
2
|
6
|
|
|
6
|
|
34
|
use strict; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
172
|
|
3
|
6
|
|
|
6
|
|
25
|
use warnings; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
130
|
|
4
|
6
|
|
|
6
|
|
58
|
use v5.20; |
|
6
|
|
|
|
|
18
|
|
5
|
6
|
|
|
6
|
|
26
|
use experimental qw/ signatures /; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
29
|
|
6
|
|
|
|
|
|
|
package YAML::Tidy::Config; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.006'; # VERSION |
9
|
|
|
|
|
|
|
|
10
|
6
|
|
|
6
|
|
768
|
use Cwd; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
399
|
|
11
|
6
|
|
|
|
|
5869
|
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
|
|
36
|
/; |
|
6
|
|
|
|
|
11
|
|
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
|
756152
|
sub new($class, %args) { |
|
24
|
|
|
|
|
39
|
|
|
24
|
|
|
|
|
45
|
|
|
24
|
|
|
|
|
33
|
|
26
|
24
|
|
|
|
|
33
|
my $yaml; |
27
|
24
|
|
|
|
|
40
|
my $overridespaces = delete $args{indentspaces}; |
28
|
24
|
50
|
|
|
|
59
|
if (defined $args{configdata}) { |
29
|
0
|
|
|
|
|
0
|
$yaml = $args{configdata}; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
else { |
32
|
24
|
|
|
|
|
37
|
my $file = $args{configfile}; |
33
|
24
|
100
|
|
|
|
58
|
unless (defined $file) { |
34
|
3
|
|
|
|
|
16
|
my ($home) = $class->_homedir; |
35
|
3
|
|
|
|
|
25
|
my $cwd = $class->_cwd; |
36
|
3
|
|
|
|
|
24
|
my @candidates = ( |
37
|
|
|
|
|
|
|
"$cwd/.yamltidy", |
38
|
|
|
|
|
|
|
"$home/.config/yamltidy/config.yaml", |
39
|
|
|
|
|
|
|
"$home/.yamltidy", |
40
|
|
|
|
|
|
|
); |
41
|
3
|
|
|
|
|
9
|
for my $c (@candidates) { |
42
|
9
|
50
|
|
|
|
165
|
if (-f $c) { |
43
|
0
|
|
|
|
|
0
|
$file = $c; |
44
|
0
|
|
|
|
|
0
|
last; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
} |
48
|
24
|
100
|
|
|
|
50
|
if (defined $file) { |
49
|
21
|
50
|
|
|
|
792
|
open my $fh, '<', $file or die $!; |
50
|
21
|
|
|
|
|
49
|
$yaml = do { local $/; <$fh> }; |
|
21
|
|
|
|
|
81
|
|
|
21
|
|
|
|
|
750
|
|
51
|
21
|
|
|
|
|
233
|
close $fh; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
24
|
100
|
|
|
|
73
|
unless (defined $yaml) { |
55
|
3
|
|
|
|
|
13
|
$yaml = $class->standardcfg('default'); |
56
|
|
|
|
|
|
|
} |
57
|
24
|
|
|
|
|
38
|
my $cfg; |
58
|
24
|
|
|
|
|
170
|
my $yp = YAML::PP->new( |
59
|
|
|
|
|
|
|
schema => [qw/ + Merge /], |
60
|
|
|
|
|
|
|
cyclic_refs => 'fatal', |
61
|
|
|
|
|
|
|
); |
62
|
24
|
|
|
|
|
53161
|
$cfg = $yp->load_string($yaml); |
63
|
24
|
|
|
|
|
133638
|
my $v = delete $cfg->{v}; |
64
|
24
|
|
50
|
|
|
85
|
my $indent = delete $cfg->{indentation} || {}; |
65
|
24
|
50
|
|
|
|
59
|
$indent->{spaces} = $overridespaces if defined $overridespaces; |
66
|
24
|
|
50
|
|
|
66
|
$indent->{spaces} //= 2; # TODO support keeping original indent |
67
|
24
|
|
100
|
|
|
100
|
$indent->{'block-sequence-in-mapping'} //= 0; |
68
|
24
|
|
50
|
|
|
82
|
my $trimtrailing = $cfg->{'trailing-spaces'} || ''; |
69
|
24
|
100
|
|
|
|
59
|
if ($trimtrailing eq 'fix') { |
70
|
23
|
|
|
|
|
37
|
$trimtrailing = 1; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
else { |
73
|
1
|
|
|
|
|
3
|
$trimtrailing = 0; |
74
|
|
|
|
|
|
|
} |
75
|
24
|
|
|
|
|
53
|
my $scalarstyle = { default => YAML_PLAIN_SCALAR_STYLE }; |
76
|
24
|
50
|
|
|
|
61
|
if (my $scalar = delete $cfg->{'scalar-style'}) { |
77
|
24
|
|
|
|
|
50
|
my $default = $scalar->{default}; |
78
|
24
|
100
|
|
|
|
57
|
$scalarstyle->{default} = $default ? $stylemap{ $default } : undef; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
24
|
|
|
|
|
60
|
delete @args{qw/ configfile configdata /}; |
82
|
24
|
100
|
|
|
|
87
|
if (my @unknown = keys %args) { |
83
|
1
|
|
|
|
|
100
|
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
|
23
|
|
100
|
|
|
220
|
footer => delete $cfg->{footer} // 'keep', |
|
|
|
100
|
|
|
|
|
91
|
|
|
|
|
|
|
scalar_style => $scalarstyle, |
92
|
|
|
|
|
|
|
}, $class; |
93
|
23
|
|
|
|
|
1008
|
return $self; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub _cwd { |
97
|
0
|
|
|
0
|
|
0
|
return Cwd::cwd(); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
0
|
|
0
|
sub _homedir($class) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
101
|
0
|
|
|
|
|
0
|
return <~>; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
25445
|
|
|
25445
|
0
|
28698
|
sub indent($self) { |
|
25445
|
|
|
|
|
27732
|
|
|
25445
|
|
|
|
|
27062
|
|
105
|
25445
|
|
|
|
|
50326
|
return $self->{indentation}->{spaces}; |
106
|
|
|
|
|
|
|
} |
107
|
370
|
|
|
370
|
0
|
662
|
sub indent_seq_in_map($self) { |
|
370
|
|
|
|
|
610
|
|
|
370
|
|
|
|
|
616
|
|
108
|
370
|
|
|
|
|
993
|
return $self->{indentation}->{'block-sequence-in-mapping'}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
24473
|
|
|
24473
|
0
|
27326
|
sub trimtrailing($self) { |
|
24473
|
|
|
|
|
30421
|
|
|
24473
|
|
|
|
|
25255
|
|
112
|
24473
|
|
|
|
|
51440
|
return $self->{trimtrailing}; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
2043
|
|
|
2043
|
0
|
3447
|
sub addheader($self) { |
|
2043
|
|
|
|
|
2658
|
|
|
2043
|
|
|
|
|
2501
|
|
116
|
2043
|
|
|
|
|
4232
|
my $header = $self->{header}; |
117
|
2043
|
100
|
|
|
|
14689
|
return 0 if $header eq 'keep'; |
118
|
678
|
100
|
|
|
|
6475
|
return $header ? 1 : 0; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
3200
|
|
|
3200
|
0
|
4093
|
sub addfooter($self) { |
|
3200
|
|
|
|
|
4367
|
|
|
3200
|
|
|
|
|
3782
|
|
122
|
3200
|
|
|
|
|
5810
|
my $footer = $self->{footer}; |
123
|
3200
|
100
|
|
|
|
17049
|
return 0 if $footer eq 'keep'; |
124
|
1062
|
100
|
|
|
|
6312
|
return $footer ? 1 : 0; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
955
|
|
|
955
|
0
|
1662
|
sub removeheader($self) { |
|
955
|
|
|
|
|
1542
|
|
|
955
|
|
|
|
|
1363
|
|
128
|
955
|
|
|
|
|
2312
|
my $header = $self->{header}; |
129
|
955
|
100
|
|
|
|
4804
|
return 0 if $header eq 'keep'; |
130
|
318
|
100
|
|
|
|
1958
|
return $header ? 0 : 1; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
147
|
|
|
147
|
0
|
285
|
sub removefooter($self) { |
|
147
|
|
|
|
|
283
|
|
|
147
|
|
|
|
|
217
|
|
134
|
147
|
|
|
|
|
348
|
my $footer = $self->{footer}; |
135
|
147
|
100
|
|
|
|
733
|
return 0 if $footer eq 'keep'; |
136
|
54
|
100
|
|
|
|
381
|
return $footer ? 0 : 1; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
8944
|
|
|
8944
|
0
|
10439
|
sub default_scalar_style($self) { |
|
8944
|
|
|
|
|
10156
|
|
|
8944
|
|
|
|
|
9206
|
|
140
|
8944
|
|
|
|
|
19173
|
return $self->{scalar_style}->{default}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub standardcfg { |
144
|
3
|
|
|
3
|
0
|
12
|
my $yaml = <<'EOM'; |
145
|
|
|
|
|
|
|
--- |
146
|
|
|
|
|
|
|
v: v0.1 |
147
|
|
|
|
|
|
|
indentation: |
148
|
|
|
|
|
|
|
spaces: 2 |
149
|
|
|
|
|
|
|
block-sequence-in-mapping: 0 |
150
|
|
|
|
|
|
|
trailing-spaces: fix |
151
|
|
|
|
|
|
|
header: true |
152
|
|
|
|
|
|
|
scalar-style: |
153
|
|
|
|
|
|
|
default: plain |
154
|
|
|
|
|
|
|
EOM |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
1; |