lib/CPAN/lib/Locale/Maketext/Extract/Run.pm
changeset 0 414e01d06fd5
equal deleted inserted replaced
-1:000000000000 0:414e01d06fd5
       
     1 package Locale::Maketext::Extract::Run;
       
     2 
       
     3 use strict;
       
     4 use vars qw( @ISA @EXPORT_OK );
       
     5 
       
     6 use Cwd;
       
     7 use File::Find;
       
     8 use Getopt::Long;
       
     9 use Locale::Maketext::Extract;
       
    10 use Exporter;
       
    11 
       
    12 @ISA = 'Exporter';
       
    13 @EXPORT_OK = 'xgettext';
       
    14 
       
    15 sub xgettext { __PACKAGE__->run(@_) }
       
    16 
       
    17 sub run {
       
    18     my $self = shift;
       
    19     local @ARGV = @_;
       
    20 
       
    21     my %opts;
       
    22     Getopt::Long::Configure("no_ignore_case");
       
    23     Getopt::Long::GetOptions( \%opts,
       
    24         'f|files-from:s@',
       
    25         'D|directory:s@',
       
    26         'u|unescaped',
       
    27         'g|gnu-gettext',
       
    28         'o|output:s@',
       
    29         'd|default-domain:s',
       
    30         'p|output-dir:s@',
       
    31         'h|help',
       
    32     ) or help();
       
    33     help() if $opts{h};
       
    34 
       
    35     my @po = @{$opts{o} || [($opts{d}||'messages').'.po']};
       
    36 
       
    37     foreach my $file (@{$opts{f}||[]}) {
       
    38         open FILE, $file or die "Cannot open $file: $!";
       
    39         while (<FILE>) {
       
    40             push @ARGV, $_ if -r and !-d;
       
    41         }
       
    42     }
       
    43 
       
    44     foreach my $dir (@{$opts{D}||[]}) {
       
    45         File::Find::find( {
       
    46             wanted      => sub {
       
    47                 return if
       
    48                     ( -d ) ||
       
    49                     ( $File::Find::dir =~ 'lib/blib|lib/t/autogen|var|m4|local' ) ||
       
    50                     ( /\.po$|\.bak$|~|,D|,B$/i ) ||
       
    51                     ( /^[\.#]/ );
       
    52                 push @ARGV, $File::Find::name;
       
    53             },
       
    54             follow      => 1,
       
    55         }, $dir );
       
    56     }
       
    57 
       
    58     @ARGV = ('-') unless @ARGV;
       
    59     s!^.[/\\]!! for @ARGV;
       
    60 
       
    61     my $cwd = getcwd();
       
    62 
       
    63     foreach my $dir (@{$opts{p}||['.']}) {
       
    64         foreach my $po (@po) {
       
    65             my $Ext = Locale::Maketext::Extract->new;
       
    66             $Ext->read_po($po, $opts{u}) if -r $po;
       
    67             $Ext->extract_file($_) for grep !/\.po$/i, @ARGV;
       
    68             $Ext->compile($opts{u}) or next;
       
    69 
       
    70             chdir $dir;
       
    71             $Ext->write_po($po, $opts{g});
       
    72             chdir $cwd;
       
    73         }
       
    74     }
       
    75 }
       
    76 
       
    77 sub help {
       
    78     local $SIG{__WARN__} = sub {};
       
    79     { exec "perldoc $0"; }
       
    80     { exec "pod2text $0"; }
       
    81 }
       
    82 
       
    83 1;