App-PDRUtils

 view release on metacpan or  search on metacpan

lib/App/PDRUtils/DistIniCmd/sort_prereqs.pm  view on Meta::CPAN

        }
        my $spacing = $fargs{spacing} //
            (floor(($num_blank_lines) / ($num_key_lines+1))+1);

        # there's no prereq lines, no need to sort this section
        next unless $num_key_lines;

        # associate comments/directives to the key line directly below it
        my $cur_mod;
        for my $lrec (reverse @lines) {
            if (defined $lrec->{mod}) {
                $cur_mod = $lrec->{mod};
                next;
            }
            if ($lrec->{parsed}[COL_TYPE] =~ /[CD]/) {
                $lrec->{mod} = $cur_mod // "Zzzzzzzzzzzzzzzz";
            }
        }

        # sort it!
        @lines = sort {
            my $mod_a = $a->{mod};
            my $mod_b = $b->{mod};

            my $mod_a_is_perl = $mod_a eq 'perl' ? 1:0;
            my $mod_b_is_perl = $mod_b eq 'perl' ? 1:0;

            my $mod_a_is_pragma = $mod_a =~ /\A[a-z]/ ? 1:0;
            my $mod_b_is_pragma = $mod_b =~ /\A[a-z]/ ? 1:0;

            ($mod_b_is_perl <=> $mod_a_is_perl) ||
                ($mod_b_is_pragma <=> $mod_a_is_pragma) ||
                lc($mod_a) cmp lc($mod_b) ||
                $a->{linum} <=> $b->{linum};
        } @lines;

        # insert spaces if necessary
        {
            last unless $spacing > 1;
            undef $cur_mod;
            for my $i (reverse (0..$#lines)) {
                my $lrec = $lines[$i];
                if (!defined($cur_mod) || $cur_mod ne $lrec->{mod}) {
                    for (1..$spacing-1) {
                        splice @lines, $i+1, 0, {parsed=>['B', "\n"]};
                    }
                }
                $cur_mod = $lrec->{mod};
            }
        }
        push @lines, {parsed=>['B', "\n"]};
        splice @{ $iod->{_parsed} },
            $s->{linum_start},
            ($s->{linum_end} - $s->{linum_start}),
            map {$_->{parsed}} @lines;
    }

    $iod->_discard_cache;

    my $new_content = $iod->as_string;
    #say $new_content;
    my $modified = $old_content ne $new_content;

    if ($modified) {
        return [200, "Sorted prereqs", $iod];
    } else {
        return [304, "Not modified"];
    }
}

1;
# ABSTRACT: Sort lines in `[Prereqs/*]` sections

__END__

=pod

=encoding UTF-8

=head1 NAME

App::PDRUtils::DistIniCmd::sort_prereqs - Sort lines in `[Prereqs/*]` sections

=head1 VERSION

This document describes version 0.122 of App::PDRUtils::DistIniCmd::sort_prereqs (from Perl distribution App-PDRUtils), released on 2021-05-25.

=head1 FUNCTIONS


=head2 handle_cmd

Usage:

 handle_cmd(%args) -> [$status_code, $reason, $payload, \%result_meta]

Sort lines in `[PrereqsE<sol>*]` sections.

This command can sort C<[Prereqs/*]> sections in your C<dist.ini> according to
this rule (TODO: allow customized rule): C<perl> comes first, then pragmas sorted
ascibetically and case-insensitively (e.g. C<strict>, C<utf8>, C<warnings>), then
other modules sorted ascibetically and case-insensitively.

Can detect one-spacing or two-spacing. Detects directives and comments.

This function is not exported.

Arguments ('*' denotes required arguments):

=over 4

=item * B<parsed_dist_ini>* => I<obj>

=item * B<spacing> => I<int>

Set spacing explicitly.


=back

Returns an enveloped result (an array).



( run in 1.718 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )