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

(-)a/remove_unused_import.pl (-1 / +50 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
5
use File::Slurp qw( read_file write_file );
6
7
my @files = @ARGV;
8
my $i;
9
my $total = scalar @files;
10
my $num_width = length $total;
11
for my $file ( @files ) {
12
    print sprintf "|%-25s| %${num_width}s / %s (%.2f%%)\r",
13
        '=' x (24*$i++/$total). '>',
14
        $i, $total, 100*$i/+$total;
15
    flush STDOUT;
16
17
    my $content = read_file( $file );
18
    my ( $no_export_modules, $modules_used, $package_name );
19
    for my $line ( split "\n", $content ) {
20
        if ( $line =~ m{^package C4::(.*);} ) {
21
            $package_name = $1;
22
            next;
23
        } elsif ( $line =~ m{^use C4::([^\s]*);$} ) {
24
            next;
25
        } elsif ( $line =~ m{C4::([^\s\(\)-]*)} ) {
26
            my $module = $1;
27
            my $namespace_module = "C4::${module}::";
28
            if ( $line =~ m[$namespace_module] ) {
29
                my @parts = split '::', $module;
30
                pop @parts if @parts > 1;
31
                $module = join '::', @parts;
32
            }
33
            $modules_used->{$module}++;
34
        }
35
    }
36
37
    my $new_content;
38
    for my $line ( split "\n", $content ) {
39
        if ( $line =~ m{^use C4::([^\s]*);$} ) {
40
            next
41
              if (!$package_name || $package_name ne $1)
42
              && !exists $modules_used->{$1};
43
        }
44
        $new_content .= "$line\n";
45
    }
46
    if ( $content ne $new_content ) {
47
        say "$file -- Modified";
48
        write_file($file, $new_content);
49
    }
50
}

Return to bug 30926