| 1 | package App::PDRUtils::DistIniCmd::sort_prereqs; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | our $DATE = '2021-05-25'; # DATE | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | our $VERSION = '0.122'; # VERSION | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Log::ger; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use App::PDRUtils::Cmd; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use App::PDRUtils::DistIniCmd; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use Config::IOD::Constants ':ALL'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use POSIX qw(floor); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | our %SPEC; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | $SPEC{handle_cmd} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | v => 1.1, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | summary => 'Sort lines in `[Prereqs/*]` sections', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | description => <<'_', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | This command can sort `[Prereqs/*]` sections in your `dist.ini` according to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | this rule (TODO: allow customized rule): `perl` comes first, then pragmas sorted | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ascibetically and case-insensitively (e.g. `strict`, `utf8`, `warnings`), then | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | other modules sorted ascibetically and case-insensitively. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | Can detect one-spacing or two-spacing. Detects directives and comments. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | _ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | args => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | %App::PDRUtils::DistIniCmd::common_args, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | spacing => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | summary => 'Set spacing explicitly', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | schema => ['int*', min=>1, max=>10], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub handle_cmd { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
handle_cmd: not called CC=26 SCAR=65.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | my %fargs = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my $iod = $fargs{parsed_dist_ini}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $old_content = $iod->as_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | my @sections; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | $iod->each_section( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | my ($self, %cbargs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | return unless $cbargs{section} =~ m!\Aprereqs(?:\s*/\s*(\w+))?\z!ix; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | push @sections, { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | linum_start => $cbargs{linum_start}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | linum_end => $cbargs{linum_end}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | for my $s (reverse @sections) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my @lines; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $num_lines = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $num_blank_lines = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $num_key_lines = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # remove blank lines and calculate spacing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | for my $linum ($s->{linum_start} .. $s->{linum_end}-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | my $l = $iod->{_parsed}[$linum]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $num_lines++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | if ($l->[COL_TYPE] eq 'B') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $num_blank_lines++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $mod; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ($l->[COL_TYPE] eq 'K') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | $num_key_lines++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $mod = $l->[COL_K_KEY]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($l->[COL_TYPE] eq 'C') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # check if this is a commented param | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($l->[COL_C_COMMENT] =~ /\s*([^=]+?)\s*=/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $mod = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | push @lines, { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | mod => $mod, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | linum => $linum, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | parsed => $l, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | my $spacing = $fargs{spacing} // | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | (floor(($num_blank_lines) / ($num_key_lines+1))+1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$fargs{"spacing"} // floor($num_blank_lines / ($num_key_lines + 1)) + 1
MC/DC0%0/1
$fargs{"spacing"} // floor($num_blank_lines / ($num_key_lines + 1)) + 1
$fargs{"spacing"}
Truth table0%0/2
A
A= $fargs{"spacing"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # there's no prereq lines, no need to sort this section | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | next unless $num_key_lines; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # associate comments/directives to the key line directly below it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | my $cur_mod; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | for my $lrec (reverse @lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if (defined $lrec->{mod}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | $cur_mod = $lrec->{mod}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($lrec->{parsed}[COL_TYPE] =~ /[CD]/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $lrec->{mod} = $cur_mod // "Zzzzzzzzzzzzzzzz"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$cur_mod // "Zzzzzzzzzzzzzzzz"
MC/DC0%0/1
$cur_mod // "Zzzzzzzzzzzzzzzz"
$cur_mod
Truth table0%0/2
A
A= $cur_mod
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | # sort it! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | @lines = sort { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | my $mod_a = $a->{mod}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $mod_b = $b->{mod}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | my $mod_a_is_perl = $mod_a eq 'perl' ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $mod_b_is_perl = $mod_b eq 'perl' ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | my $mod_a_is_pragma = $mod_a =~ /\A[a-z]/ ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $mod_b_is_pragma = $mod_b =~ /\A[a-z]/ ? 1:0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ($mod_b_is_perl <=> $mod_a_is_perl) || | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ($mod_b_is_pragma <=> $mod_a_is_pragma) || | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | lc($mod_a) cmp lc($mod_b) || | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $a->{linum} <=> $b->{linum}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
$mod_b_is_perl <=> $mod_a_is_perl || $mod_b_is_pragma <=> $mod_a_is_pragma || lc $mod_a cmp lc $mod_b || $App::PDRUtils::DistIniCmd::sort_prereqs::a->{'linum'} <=> $App::PDRUtils::DistIniCmd::sort_prereqs::b->{'linum'}
$mod_b_is_perl <=> $mod_a_is_perl or $mod_b_is_pragma <=> $mod_a_is_pragma or lc $mod_a cmp lc $mod_b
$mod_b_is_perl <=> $mod_a_is_perl or $mod_b_is_pragma <=> $mod_a_is_pragma
MC/DC0%0/4
$mod_b_is_perl <=> $mod_a_is_perl || $mod_b_is_pragma <=> $mod_a_is_pragma || lc $mod_a cmp lc $mod_b || $App::PDRUtils::DistIniCmd::sort_prereqs::a->{'linum'} <=> $App::PDRUtils::DistIniCmd::sort_prereqs::b->{'linum'}
$mod_b_is_perl <=> $mod_a_is_perl
$mod_b_is_pragma <=> $mod_a_is_pragma
lc $mod_a cmp lc $mod_b
$App::PDRUtils::DistIniCmd::sort_prereqs::a->{'linum'} <=> $App::PDRUtils::DistIniCmd::sort_prereqs::b->{'linum'}
Truth table0%0/5
A or B or C || D
A= $mod_b_is_perl <=> $mod_a_is_perlB= $mod_b_is_pragma <=> $mod_a_is_pragmaC= lc $mod_a cmp lc $mod_bD= $App::PDRUtils::DistIniCmd::sort_prereqs::a->{'linum'} <=> $App::PDRUtils::DistIniCmd::sort_prereqs::b->{'linum'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } @lines; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | # insert spaces if necessary | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | last unless $spacing > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | undef $cur_mod; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | for my $i (reverse (0..$#lines)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my $lrec = $lines[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if (!defined($cur_mod) || $cur_mod ne $lrec->{mod}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $cur_mod or $cur_mod ne $lrec->{"mod"}
MC/DC0%0/2
not defined $cur_mod or $cur_mod ne $lrec->{"mod"}
not defined $cur_mod
$cur_mod ne $lrec->{"mod"}
Truth table0%0/3
not(A) or B
A= not defined $cur_modB= $cur_mod ne $lrec->{"mod"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | for (1..$spacing-1) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | splice @lines, $i+1, 0, {parsed=>['B', "\n"]}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $cur_mod = $lrec->{mod}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | push @lines, {parsed=>['B', "\n"]}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | splice @{ $iod->{_parsed} }, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | $s->{linum_start}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ($s->{linum_end} - $s->{linum_start}), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | map {$_->{parsed}} @lines; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | $iod->_discard_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $new_content = $iod->as_string; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | #say $new_content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $modified = $old_content ne $new_content; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($modified) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | return [200, "Sorted prereqs", $iod]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | return [304, "Not modified"]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # ABSTRACT: Sort lines in `[Prereqs/*]` sections | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||