View | Details | Raw Unified | Return to bug 39325
Collapse All | Expand All

(-)a/misc/devel/codespell.pl (-1 / +68 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
use Modern::Perl;
3
use Test::PerlTidy;
4
use Test::More;
5
use File::Slurp qw(read_file write_file);
6
use Parallel::ForkManager;
7
8
my @files;
9
push @files, qx{git ls-files '*.pl' '*.PL' '*.pm' '*.t'};
10
push @files, qx{git ls-files svc opac/svc};    # Files without extension
11
push @files, qx{git ls-files '*.tt' '*.inc'};
12
push @files, qx{git ls-files '*.js' '*.ts' '*.vue' ':(exclude)koha-tmpl/intranet-tmpl/lib' ':(exclude)koha-tmpl/opac-tmpl/lib'};
13
14
my $nproc ||= qx{nproc};
15
my $pm       = Parallel::ForkManager->new($nproc);
16
17
my $files_edit;
18
$pm->run_on_finish(
19
    sub {
20
        my ( $pid, $exit_code, $ident, $exit_signal, $core_dump, $data_ref ) = @_;
21
        $files_edit->{$data_ref->{file}} = $data_ref->{edits};
22
    }
23
);
24
25
while ( my ( $i, $file ) = each @files ) {
26
    chomp $file;
27
    $pm->start and next;
28
    say sprintf "%s (%s/%s)", $file, $i + 1, scalar @files;
29
    my $output = qx{codespell -d --ignore-words .codespell-ignore $file};
30
    chomp $output;
31
    my @edits;
32
    if ($output) {
33
        my @lines = split "\n", $output;
34
        for my $line (@lines){
35
            # C4/Auth.pm:144: authentification ==> authentication
36
            if ( $line =~ m{(?<file>[^:]*):(?<line>\d+): (?<pattern>\w+) ==> (?<replace>.*)} ) {
37
                my $line_number = $+{line} - 1;
38
                my $pattern = $+{pattern};
39
                my $replace = $+{replace};
40
                if ( $replace =~ m{,}) {
41
                    $replace = "FIXME CODESPELL ($pattern ==> $replace)";
42
                }
43
                push @edits, {
44
                    line_number => $line_number,
45
                    pattern => $pattern,
46
                    replace => $replace,
47
                }
48
            } else {
49
                warn "Unsupported output line: $line";
50
            }
51
        }
52
    }
53
    $pm->finish( 0, { file => $file, edits => \@edits } );
54
}
55
$pm->wait_all_children;
56
57
while ( my ( $file, $edits ) = each %$files_edit ) {
58
    my @content = read_file($file);
59
    for my $edit (@$edits){
60
        my $file = $edit->{file};
61
        my $line_number = $edit->{line_number};
62
        my $pattern = $edit->{pattern};
63
        my $replace = $edit->{replace};
64
        $content[$line_number] =~ s#$pattern#$replace#g;
65
    }
66
    write_file( $file, @content );
67
};
68

Return to bug 39325