File Coverage

blib/lib/YAML/Tidy/Run.pm
Criterion Covered Total %
statement 103 135 76.3
branch 30 38 78.9
condition 1 2 50.0
subroutine 15 19 78.9
pod 0 2 0.0
total 149 196 76.0


line stmt bran cond sub pod time code
1             # ABSTRACT: yamltidy runner
2 2     2   359411 use strict;
  2         3  
  2         104  
3 2     2   10 use warnings;
  2         3  
  2         160  
4 2     2   34 use v5.20;
  2         9  
5 2     2   656 use experimental qw/ signatures /;
  2         2281  
  2         14  
6             package YAML::Tidy::Run;
7              
8             our $VERSION = 'v0.11.0'; # VERSION
9              
10 2     2   1797 use YAML::Tidy;
  2         13  
  2         232  
11 2     2   36 use YAML::Tidy::Config;
  2         39  
  2         77  
12 2     2   1989 use YAML::LibYAML::API;
  2         2633  
  2         116  
13 2     2   1869 use Getopt::Long::Descriptive;
  2         180501  
  2         29  
14 2     2   1386 use Encode;
  2         6  
  2         4503  
15              
16             my @options = (
17             'yamltidy %o file',
18             [ 'config-file|c=s' => 'Config file' ],
19             [ 'config-data|d=s' => 'Configuration as a string' ],
20             [ 'inplace|i' => 'Edit file inplace' ],
21             [ 'debug' => 'Debugging output' ],
22             [ 'partial' => 'Input is only a part of a YAML file' ],
23             [ 'indent=i' => 'Override indentation spaces from config' ],
24             [ 'batch|b' => 'Tidy all files - currently requires parameter "-" for filenames passed via STDIN' ],
25             [ 'verbose|v' => 'Output information' ],
26             [],
27             [ 'help|h', "print usage message and exit", { shortcircuit => 1 } ],
28             [ 'version', "Print version information", { shortcircuit => 1 } ],
29             );
30              
31 10     10 0 343516 sub new($class, %args) {
  10         31  
  10         32  
  10         21  
32 10         103 my ($opt, $usage) = describe_options(@options);
33 10         35934 my $cfg = YAML::Tidy::Config->new(
34             configfile => $opt->config_file,
35             configdata => $opt->config_data,
36             indentspaces => $opt->indent,
37             );
38 10         115 my $yt = YAML::Tidy->new(
39             cfg => $cfg,
40             partial => $opt->partial,
41             );
42             my $self = bless {
43             opt => $opt,
44 10   50     1219 stdin => $args{stdin} || \*STDIN,
45             tidy => $yt,
46             usage => $usage,
47             }, $class;
48             }
49              
50 0     0   0 sub _output($self, $str) {
  0         0  
  0         0  
  0         0  
51 0         0 print $str;
52             }
53              
54 10     10 0 110 sub run($self) {
  10         26  
  10         23  
55 10         36 my $opt = $self->{opt};
56 10         26 my $usage = $self->{usage};
57 10 100       58 $self->_output($usage->text), return if $opt->help;
58 9         393 my @versions = (
59             YAML::Tidy->VERSION, YAML::PP->VERSION,
60             YAML::LibYAML::API->VERSION,
61             YAML::LibYAML::API::XS::libyaml_version
62             );
63 9 100       72 if ($opt->version) {
64 1         43 $self->_output(sprintf <<'EOM', @versions);
65             yamltidy: %s
66             YAML::PP: %s
67             YAML::LibYAML::API: %s
68             libyaml: %s
69             EOM
70 1         17 return;
71             }
72              
73 8 100       60 if ($opt->batch) {
74 2         13 my ($path) = @ARGV;
75 2 50       8 unless ($path eq '-') {
76 0         0 die "--batch currently requires '-' to receive filenames via STDIN\n";
77             }
78 2 100       9 unless ($opt->inplace) {
79 1         13 die "--batch currently requires --inplace\n";
80             }
81 1         6 my $in = $self->{stdin};
82 1         9 while (my $file = <$in>) {
83 2         22 chomp $file;
84 2         9 $self->_process_file($file);
85             }
86 1         12 return;
87             }
88 6         37 my ($file) = @ARGV;
89 6 50       23 unless (defined $file) {
90 0         0 $self->_output($usage->text);
91 0         0 return;
92             }
93              
94 6 100       27 if ($file eq '-') {
95 2         12 $self->_process_stdin;
96 2         34 return;
97             }
98              
99 4         24 $self->_process_file($file);
100             }
101              
102 4     4   14 sub _process_file($self, $file) {
  4         14  
  4         13  
  4         16  
103 4         13 my $opt = $self->{opt};
104 4         14 my $yt = $self->{tidy};
105 4         11 my $changed = 0;
106 4 50   1   357 open my $fh, '<:encoding(UTF-8)', $file or die "Could not open '$file': $!";
  1         1276  
  1         23  
  1         11  
107 4         2119 my $yaml = do { local $/; <$fh> };
  4         26  
  4         212  
108 4         141 close $fh;
109              
110 4 100       28 $opt->debug and $self->_before($file, $yaml);
111              
112 4         61 my $out = $yt->tidy($yaml);
113              
114 4 50       25 if ($out ne $yaml) {
115 4         12 $changed = 1;
116             }
117 4 100       23 if ($opt->inplace) {
118 2 50       25 $changed and $self->_write_file($file, $out);
119             }
120             else {
121 2 100       18 $opt->debug or $self->_output(encode_utf8 $out);
122             }
123 4 100       65 $opt->debug and $self->_after($file, $out);
124 4 50       58 $self->_info(sprintf "Processed '%s' (%s)", $file, $changed ? 'changed' : 'unchanged');
125             }
126              
127 4     4   9 sub _info($self, $msg) {
  4         13  
  4         9  
  4         8  
128 4 100       27 $self->{opt}->verbose and $self->_output("[info] $msg\n");
129             }
130              
131 0     0   0 sub _write_file($self, $file, $out) {
  0         0  
  0         0  
  0         0  
  0         0  
132 0 0       0 open my $fh, '>:encoding(UTF-8)', $file or die "Could not open '$file' for writing: $!";
133 0         0 print $fh $out;
134 0         0 close $fh;
135             }
136              
137 2     2   7 sub _process_stdin($self) {
  2         4  
  2         4  
138 2         6 my $opt = $self->{opt};
139 2         6 my $yt = $self->{tidy};
140 2         6 my $in = $self->{stdin};
141 2         5 my $yaml = decode_utf8 do { local $/; <$in> };
  2         15  
  2         174  
142              
143 2 100       35 $opt->debug and $self->_before('-', $yaml);
144              
145 2         38 my $out = $yt->tidy($yaml);
146              
147 2 100       21 if ($opt->debug) {
148 1         15 $self->_after('-', $out);
149             }
150             else {
151 1         21 $self->_output(encode_utf8 $out);
152             }
153             }
154              
155 0     0   0 sub _before($self, $file, $yaml) {
  0         0  
  0         0  
  0         0  
  0         0  
156 0         0 my $yt = $self->{tidy};
157 0         0 $self->_output( "# Before: ($file)\n");
158 0         0 $self->_output(encode_utf8 $yt->highlight($yaml));
159             }
160              
161 0     0   0 sub _after($self, $file, $yaml) {
  0         0  
  0         0  
  0         0  
  0         0  
162 0         0 my $yt = $self->{tidy};
163 0         0 $self->_output("# After: ($file)\n");
164 0         0 $self->_output(encode_utf8 $yt->highlight($yaml));
165             }
166             1;