273
273
$opt_only_count_files ,
274
274
$opt_fmt ,
275
275
$opt_encoding ,
276
+ $opt_txt , # not associated with a command line option
276
277
);
277
278
278
279
my $getopt_success = GetOptions( # {{{1
@@ -382,6 +383,7 @@ my $getopt_success = GetOptions( # {{{1
382
383
"fmt=i" => \$opt_fmt ,
383
384
"encoding=s" => \$opt_encoding , # not production ready #880
384
385
);
386
+ $opt_txt = 0;
385
387
# 1}}}
386
388
$config_file = $opt_config_file if defined $opt_config_file;
387
389
load_from_config_file($config_file, # {{{2
@@ -657,6 +659,33 @@ if ($opt_by_percent and $opt_by_percent !~ m/^(c|cm|cb|cmb)$/i) {
657
659
}
658
660
$opt_by_percent = lc $opt_by_percent;
659
661
662
+ $opt_txt = 1 if $opt_report_file;
663
+ my $N_OUTPUT_FORMATS = 0;
664
+ my %OUTFILE_EXT = (
665
+ "txt" => $opt_txt ,
666
+ "csv" => $opt_csv ,
667
+ "xml" => 0 ,
668
+ "yaml" => 0 ,
669
+ "json" => 0 ,
670
+ "md" => 0 ,
671
+ );
672
+ $OUTFILE_EXT{"xml" } = 1 if defined($opt_xml );
673
+ $OUTFILE_EXT{"yaml"} = 1 if defined($opt_yaml);
674
+ $OUTFILE_EXT{"json"} = 1 if defined($opt_json);
675
+ $OUTFILE_EXT{"md" } = 1 if defined($opt_md );
676
+ foreach my $out_style (sort keys %OUTFILE_EXT) {
677
+ ++$N_OUTPUT_FORMATS if $OUTFILE_EXT{$out_style};
678
+ }
679
+ if ($N_OUTPUT_FORMATS >= 2 and $OUTFILE_EXT{"txt"}) {
680
+ --$N_OUTPUT_FORMATS;
681
+ $OUTFILE_EXT{"txt"} = 0;
682
+ }
683
+ if (!$N_OUTPUT_FORMATS) {
684
+ $N_OUTPUT_FORMATS = 1;
685
+ $OUTFILE_EXT{"txt"} = 1;
686
+ $opt_txt = 1;
687
+ }
688
+
660
689
if (defined $opt_vcs) {
661
690
if ($opt_vcs eq "auto") {
662
691
if (is_dir(".git")) {
@@ -1057,7 +1086,8 @@ if ($opt_sum_reports) {
1057
1086
my $end_time = get_time();
1058
1087
my @results = generate_report($VERSION, $end_time - $start_time,
1059
1088
$type,
1060
- \%{$Results{ $type }}, \%Scale_Factor);
1089
+ \%{$Results{ $type }}, \%Scale_Factor,
1090
+ "txt");
1061
1091
if ($opt_report_file) {
1062
1092
my $ext = ".lang";
1063
1093
$ext = ".file" unless $type eq "by language";
@@ -1249,7 +1279,6 @@ my @Errors = ();
1249
1279
my @file_list = (); # global variable updated in files()
1250
1280
my %Ignored = (); # files that are not counted (language not recognized or
1251
1281
# problems reading the file)
1252
- my @Lines_Out = ();
1253
1282
my %upper_lower_map = (); # global variable (needed only on Windows) to
1254
1283
# track case of original filename, populated in
1255
1284
# make_file_list() if $ON_WINDOWS
@@ -1546,14 +1575,20 @@ generate_diff_sql($end_time - $start_time, \%Delta_by_File) if $opt_sql;
1546
1575
1547
1576
exit if $skip_generate_report;
1548
1577
1549
- if ($opt_by_file) {
1550
- @Lines_Out = diff_report($VERSION, get_time() - $start_time,
1551
- "by file",
1552
- \%Delta_by_File, \%Scale_Factor, \%Renamed);
1553
- } else {
1554
- @Lines_Out = diff_report($VERSION, get_time() - $start_time,
1555
- "by language",
1556
- \%Delta_by_Language, \%Scale_Factor, undef);
1578
+ foreach my $out_style (sort keys %OUTFILE_EXT) {
1579
+ next unless $OUTFILE_EXT{$out_style};
1580
+ my @Lines_Out = ();
1581
+ if ($opt_by_file) {
1582
+ @Lines_Out = diff_report($VERSION, $end_time - $start_time,
1583
+ "by file",
1584
+ \%Delta_by_File, \%Scale_Factor, \%Renamed);
1585
+ } else {
1586
+ @Lines_Out = diff_report($VERSION, $end_time - $start_time,
1587
+ "by language",
1588
+ \%Delta_by_Language, \%Scale_Factor, undef);
1589
+ }
1590
+ my $outfile = $opt_report_file;
1591
+ produce_output(\@Lines_Out, $opt_report_file, $out_style);
1557
1592
}
1558
1593
1559
1594
# 1}}}
@@ -1717,49 +1752,88 @@ if (!%Results_by_Language) {
1717
1752
1718
1753
generate_sql($end_time - $start_time,
1719
1754
\%Results_by_File, \%Scale_Factor) if $opt_sql;
1720
-
1721
1755
exit if $skip_generate_report;
1722
- if ($opt_by_file_by_lang) {
1723
- push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1724
- "by file",
1725
- \%Results_by_File, \%Scale_Factor);
1726
- push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1727
- "by language",
1728
- \%Results_by_Language, \%Scale_Factor);
1729
- } elsif ($opt_by_file) {
1730
- push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1731
- "by file",
1732
- \%Results_by_File, \%Scale_Factor);
1733
- } else {
1734
- push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1735
- "by language",
1736
- \%Results_by_Language, \%Scale_Factor);
1737
- }
1738
- # 1}}}
1739
- }
1740
- if ($opt_fmt) {
1741
- my $json_string = "";
1742
- write_file(\$json_string, {}, @Lines_Out);
1743
- my ($file_len, $lang_len, $header, %contents) = load_json($json_string);
1744
- @Lines_Out = print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents);
1745
- }
1746
- if ($opt_report_file) {
1747
- write_file($opt_report_file, {}, @Lines_Out);
1748
- } else {
1749
- if ($opt_fmt) {
1750
- print "@Lines_Out";
1756
+
1757
+ foreach my $out_style (sort keys %OUTFILE_EXT) {
1758
+ next unless $OUTFILE_EXT{$out_style};
1759
+ my @Lines_Out = ();
1760
+
1761
+ if ($opt_by_file_by_lang) {
1762
+ push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1763
+ "by file",
1764
+ \%Results_by_File, \%Scale_Factor,
1765
+ $out_style);
1766
+ push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1767
+ "by language",
1768
+ \%Results_by_Language, \%Scale_Factor,
1769
+ $out_style);
1770
+ } elsif ($opt_by_file) {
1771
+ push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1772
+ "by file",
1773
+ \%Results_by_File, \%Scale_Factor,
1774
+ $out_style);
1751
1775
} else {
1752
- print "\n" unless $opt_quiet;
1753
- print join("\n", @Lines_Out), "\n";
1776
+ push @Lines_Out, generate_report( $VERSION, $end_time - $start_time,
1777
+ "by language",
1778
+ \%Results_by_Language, \%Scale_Factor,
1779
+ $out_style);
1754
1780
}
1781
+ produce_output(\@Lines_Out, $opt_report_file, $out_style);
1755
1782
}
1783
+ # 1}}}
1784
+ }
1785
+
1786
+ #if ($opt_fmt) {
1787
+ # my $json_string = "";
1788
+ # write_file(\$json_string, {}, @Lines_Out);
1789
+ # my ($file_len, $lang_len, $header, %contents) = load_json($json_string);
1790
+ # @Lines_Out = print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents);
1791
+ #}
1792
+ #if ($opt_report_file) {
1793
+ # write_file($opt_report_file, {}, @Lines_Out);
1794
+ #} else {
1795
+ # if ($opt_fmt) {
1796
+ # print "@Lines_Out";
1797
+ # } else {
1798
+ # print "\n" unless $opt_quiet;
1799
+ # print join("\n", @Lines_Out), "\n";
1800
+ # }
1801
+ #}
1802
+
1756
1803
if ($opt_count_diff) {
1757
1804
++$opt_count_diff;
1758
1805
exit if $opt_count_diff > 3;
1759
1806
goto Top_of_Processing_Loop;
1760
1807
}
1808
+
1761
1809
suggest_remedies_for_errors(\@Errors) if @Errors;
1762
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1810
+ # - - - - - - - - - - - - - - - end of main - - - - - - - - - - - - - - - #
1811
+ sub produce_output { # {{{1
1812
+ my ($ra_Lines_Out, $outfile, $out_style) = @_;
1813
+ if ($opt_fmt) {
1814
+ my $json_string = "";
1815
+ write_file(\$json_string, {}, @{$ra_Lines_Out});
1816
+ my ($file_len, $lang_len, $header, %contents) = load_json($json_string);
1817
+ @{$ra_Lines_Out} = print_format_n(abs($opt_fmt), $file_len, $lang_len, $header, %contents);
1818
+ }
1819
+ if ($outfile) {
1820
+ my $adjusted_outfile = $outfile;
1821
+ if ($N_OUTPUT_FORMATS > 1 and
1822
+ ($opt_report_file !~ /\.${out_style}$/)) {
1823
+ # ensure the output filename has a suitable extension
1824
+ $adjusted_outfile .= "." . $out_style;
1825
+ }
1826
+ write_file($adjusted_outfile, {}, @{$ra_Lines_Out});
1827
+ } else {
1828
+ if ($opt_fmt) {
1829
+ print "@{$ra_Lines_Out}";
1830
+ } else {
1831
+ print "\n" unless $opt_quiet;
1832
+ print join("\n", @{$ra_Lines_Out}), "\n";
1833
+ }
1834
+ }
1835
+ }
1836
+ # 1}}}
1763
1837
sub suggest_remedies_for_errors { # {{{1
1764
1838
my ($raa_errors) = @_; # [ [ numeric error code, filename], .. ]
1765
1839
my $hit_timeout = 0;
@@ -4503,6 +4577,7 @@ sub generate_report { # {{{1
4503
4577
# where TYPE = name of language, source file,
4504
4578
# or report file
4505
4579
$rh_scale , # in
4580
+ $out_style , # in "txt" | "xml" | "yaml" | "json" | "csv" | "md"
4506
4581
) = @_;
4507
4582
4508
4583
my %orig_case = ();
@@ -4519,7 +4594,7 @@ sub generate_report { # {{{1
4519
4594
print "-> generate_report\n" if $opt_v > 2;
4520
4595
my $DELIM = ",";
4521
4596
$DELIM = $opt_csv_delimiter if defined $opt_csv_delimiter;
4522
- $DELIM = "|" if defined $opt_md ;
4597
+ $DELIM = "|" if $out_style eq "md" ;
4523
4598
4524
4599
my @results = ();
4525
4600
@@ -4575,10 +4650,10 @@ sub generate_report { # {{{1
4575
4650
},
4576
4651
);
4577
4652
my $Style = "txt";
4578
- $Style = "xml" if $opt_xml ;
4579
- $Style = "xml" if $opt_yaml ; # not a typo; just set to anything but txt
4580
- $Style = "xml" if $opt_json ; # not a typo; just set to anything but txt
4581
- $Style = "xml" if $opt_csv ; # not a typo; just set to anything but txt
4653
+ $Style = "xml" if $out_style eq "xml" ;
4654
+ $Style = "xml" if $out_style eq "yaml" ; # not a typo; just set to anything but txt
4655
+ $Style = "xml" if $out_style eq "json" ; # not a typo; just set to anything but txt
4656
+ $Style = "xml" if $out_style eq "csv" ; # not a typo; just set to anything but txt
4582
4657
4583
4658
my $hyphen_line = sprintf "%s", '-' x (79 + $column_1_offset);
4584
4659
$hyphen_line = sprintf "%s", '-' x (68 + $column_1_offset)
@@ -4604,9 +4679,9 @@ sub generate_report { # {{{1
4604
4679
$elapsed_sec ,
4605
4680
$sum_files/$elapsed_sec,
4606
4681
$sum_lines/$elapsed_sec) unless $opt_sum_reports or $opt_hide_rate;
4607
- if ($opt_xml or $opt_yaml or $opt_json ) {
4682
+ if ($out_style eq "xml" or $out_style eq "yaml" or $out_style eq "json" ) {
4608
4683
if (!$ALREADY_SHOWED_HEADER) {
4609
- if ($opt_by_file_by_lang and $opt_json ) {
4684
+ if ($opt_by_file_by_lang and $out_style eq "json" ) {
4610
4685
push @results, '{ "by_file" : ';
4611
4686
}
4612
4687
push @results, xml_yaml_or_json_header($URL, $version, $elapsed_sec,
@@ -4615,15 +4690,15 @@ sub generate_report { # {{{1
4615
4690
# --sum-reports yields two xml or yaml files, one by
4616
4691
# language and one by report file, each of which needs a header
4617
4692
}
4618
- if ($opt_xml ) {
4693
+ if ($out_style eq "xml" ) {
4619
4694
if ($BY_FILE or ($report_type eq "by report file")) {
4620
4695
push @results, "<files>";
4621
4696
} else {
4622
4697
push @results, "<languages>";
4623
4698
}
4624
4699
}
4625
4700
} else {
4626
- $header_line =~ s/,// if $opt_csv ;
4701
+ $header_line =~ s/,// if $out_style eq "csv" ;
4627
4702
push @results, output_header($header_line, $hyphen_line, $BY_FILE);
4628
4703
}
4629
4704
@@ -4650,7 +4725,7 @@ sub generate_report { # {{{1
4650
4725
"scale" ,
4651
4726
"3rd gen. equiv"
4652
4727
if $opt_3;
4653
- if ($opt_md ) {
4728
+ if ($out_style eq "md" ) {
4654
4729
my @col_header = ();
4655
4730
if ($data_line =~ m{\s%}) {
4656
4731
$data_line =~ s{\s%}{_%}g;
@@ -4671,7 +4746,7 @@ sub generate_report { # {{{1
4671
4746
}
4672
4747
}
4673
4748
4674
- if ($opt_csv ) {
4749
+ if ($out_style eq "csv" ) {
4675
4750
my $header2;
4676
4751
if ($BY_FILE) {
4677
4752
$header2 = "language${DELIM}filename";
@@ -4727,7 +4802,7 @@ sub generate_report { # {{{1
4727
4802
4728
4803
if ($BY_FILE) {
4729
4804
my $clean_filename = rm_leading_tempdir($lang_or_file, \%TEMP_DIR);
4730
- $clean_filename = xml_metachars($clean_filename) if $opt_xml ;
4805
+ $clean_filename = xml_metachars($clean_filename) if $out_style eq "xml" ;
4731
4806
$data_line = sprintf $Format{'1'}{$Style}, $clean_filename;
4732
4807
} else {
4733
4808
$data_line = sprintf $Format{'2'}{$Style}, $lang_or_file;
@@ -4754,7 +4829,7 @@ sub generate_report { # {{{1
4754
4829
$scaled if $opt_3;
4755
4830
$sum_scaled += $scaled if $opt_3;
4756
4831
4757
- if ($opt_xml ) {
4832
+ if ($out_style eq "xml" ) {
4758
4833
if (defined $rhh_count->{$lang_or_file}{'lang'}) {
4759
4834
my $lang = $rhh_count->{$lang_or_file}{'lang'};
4760
4835
if (!defined $languages->{$lang}) {
@@ -4767,9 +4842,9 @@ sub generate_report { # {{{1
4767
4842
} else {
4768
4843
push @results, " <language " . $data_line . "/>";
4769
4844
}
4770
- } elsif ($opt_yaml or $opt_json ) {
4845
+ } elsif ($out_style eq "yaml" or $out_style eq "json" ) {
4771
4846
my ($Q, $open_B, $close_B, $start, $C) = yaml_to_json_separators();
4772
- if ($opt_yaml ) {
4847
+ if ($out_style eq "yaml" ) {
4773
4848
# YAML: force quoted language or filename in case these
4774
4849
# have embedded funny characters, issue #312
4775
4850
push @results,"'" . rm_leading_tempdir($lang_or_file, \%TEMP_DIR). "' :$open_B";
@@ -4799,16 +4874,16 @@ sub generate_report { # {{{1
4799
4874
push @results, " ${Q}scaled${Q}: " . $scaled . $C;
4800
4875
push @results, " ${Q}factor${Q}: " . $factor . $C;
4801
4876
}
4802
- if ($opt_json ) { # replace the trailing comma with }, on the last line
4877
+ if ($out_style eq "json" ) { # replace the trailing comma with }, on the last line
4803
4878
$results[-1] =~ s/,\s*$/},/;
4804
4879
}
4805
- } elsif ($opt_csv or $opt_md ) {
4880
+ } elsif ($out_style eq "csv" or $out_style eq "md" ) {
4806
4881
my $extra_3 = "";
4807
4882
$extra_3 = "${DELIM}$factor${DELIM}$scaled" if $opt_3;
4808
4883
my $first_column = undef;
4809
4884
my $clean_name = $lang_or_file;
4810
4885
my $str;
4811
- if ($opt_csv ) {
4886
+ if ($out_style eq "csv" ) {
4812
4887
if ($BY_FILE) {
4813
4888
$first_column = $rhh_count->{$lang_or_file}{'lang'};
4814
4889
$clean_name = rm_leading_tempdir($lang_or_file, \%TEMP_DIR);
@@ -4853,7 +4928,7 @@ sub generate_report { # {{{1
4853
4928
$avg_scale = sprintf("%.2f", $sum_scaled / $sum_code)
4854
4929
if $sum_code and $opt_3;
4855
4930
4856
- if ($opt_xml ) {
4931
+ if ($out_style eq "xml" ) {
4857
4932
$data_line = "";
4858
4933
if (!$BY_FILE) {
4859
4934
$data_line .= sprintf "sum_files=\"%d\" ", $sum_files;
@@ -4890,7 +4965,7 @@ sub generate_report { # {{{1
4890
4965
} else {
4891
4966
$ALREADY_SHOWED_XML_SECTION = 1;
4892
4967
}
4893
- } elsif ($opt_yaml or $opt_json ) {
4968
+ } elsif ($out_style eq "yaml" or $out_style eq "json" ) {
4894
4969
my ($Q, $open_B, $close_B, $start, $C) = yaml_to_json_separators();
4895
4970
push @results, "${Q}SUM${Q}: ${open_B}";
4896
4971
if ($opt_by_percent) {
@@ -4909,7 +4984,7 @@ sub generate_report { # {{{1
4909
4984
push @results, " ${Q}scaled${Q}: " . $sum_scaled . $C;
4910
4985
push @results, " ${Q}factor${Q}: " . $avg_scale . $C;
4911
4986
}
4912
- if ($opt_json ) {
4987
+ if ($out_style eq "json" ) {
4913
4988
$results[-1] =~ s/,\s*$/} }/;
4914
4989
if ($opt_by_file_by_lang) {
4915
4990
if ($ALREADY_SHOWED_HEADER) {
@@ -4919,7 +4994,7 @@ sub generate_report { # {{{1
4919
4994
}
4920
4995
}
4921
4996
}
4922
- } elsif ($opt_csv ) {
4997
+ } elsif ($out_style eq "csv" ) {
4923
4998
my @entries = ();
4924
4999
if ($opt_by_file) {
4925
5000
push @entries, "SUM";
@@ -4968,7 +5043,7 @@ sub generate_report { # {{{1
4968
5043
$data_line .= sprintf $Format{'6'}{$Style},
4969
5044
$avg_scale ,
4970
5045
$sum_scaled if $opt_3;
4971
- if ($opt_md ) {
5046
+ if ($out_style eq "md" ) {
4972
5047
my @words = split(' ', $data_line);
4973
5048
my $n_cols = scalar(@words);
4974
5049
# my $n_cols = scalar(split(' ', $data_line)); # deprecated
0 commit comments