]> git.imager.perl.org - bse.git/blob - site/cgi-bin/modules/BSE/MessageScanner.pm
allow importing custom fields for product option values
[bse.git] / site / cgi-bin / modules / BSE / MessageScanner.pm
1 package BSE::MessageScanner;
2 use strict;
3 use File::Find;
4
5 our $VERSION = "1.001";
6
7 =item BSE::MessageScanner->scan(\@basepaths)
8
9 Scan .tmpl, .pm and .pl files under the given directories for apparent
10 message uses and return each use with the id, file and line number.
11
12 This isn't perfect and attempts to skip generated message ids.
13
14 =cut
15
16 my $base_re = qr(\b(msg:[\w-]+(?:/\$?[\w-]+)*));
17
18 sub scan {
19   my ($class, $bases) = @_;
20
21   my @files;
22   find
23     (
24      sub {
25        -f && /\.(tmpl|pm|pl)$/ && push @files, $File::Find::name;
26      },
27      @$bases
28     );
29   my @ids;
30   for my $file (@files) {
31     open my $fh, "<", $file
32       or die "Cannot open $file: $!\n";
33     my $errors = 0;
34     while (my $line = <$fh>) {
35       next if $line =~ /NOMSGID/;
36       my @msgs = $line =~ m($base_re);
37       # crude
38       $line =~ / _ / && $file =~ /\.tmpl$/
39         and @msgs = ();
40       push @ids, map [ $_, $file, $. ], grep !/\$/, @msgs;
41     }
42     close $fh;
43   }
44
45   return @ids;
46 }
47
48 1;