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

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

Return to bug 39325