PageRenderTime 26ms CodeModel.GetById 22ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/util/get_messages.pl

http://github.com/gshank/html-formhandler
Perl | 72 lines | 41 code | 12 blank | 19 comment | 4 complexity | ae22783518461db058f260173fcd6657 MD5 | raw file
 1#!/usr/bin/env perl
 2
 3#============================================================
 4# util/get_messages.pl
 5#
 6# This is a utility to pull all of the messages out of the
 7# FormHandler fields (and the Types package), for easy
 8# updating of I18N files.
 9#
10# Writes out file util/messages that contains Dumper
11# output for current messages, so that changes to messages
12# can be tracked.
13#
14# Could also be used to construct customized messages in the
15# form.
16#============================================================
17
18use strict;
19use warnings;
20
21use Cwd;
22use File::Find;
23use Class::Load ':all';
24use Data::Dumper;
25use lib ( getcwd() . '/lib');
26
27my @directories = ( getcwd() . "/lib/HTML/FormHandler/Field" );
28my @field_types;
29find(\&wanted, @directories);
30
31# get base Field class messages
32my $base_messages = $HTML::FormHandler::Field::class_messages;
33
34# get messages from Types module
35use HTML::FormHandler::Types;
36my $type_messages = $HTML::FormHandler::Types::class_messages;
37my $all_messages = [ { 'Field' => { %$base_messages } },
38                     { 'Types' => { %$type_messages } }];
39
40# get messages from the Field classes
41foreach my $type (@field_types) {
42    my $class = "HTML::FormHandler::Field::$type";
43    my $messages = eval '$HTML::FormHandler::Field::' . $type . '::class_messages';
44    next unless keys %$messages;
45    push @$all_messages, { $type => $messages };
46}
47
48$Data::Dumper::Terse = 1;
49my $output = Dumper($all_messages);
50
51my $outfile = getcwd() . '/util/messages';
52open( my $fh, '>', $outfile ) or die "Unable to open $outfile";
53print $fh $output;
54close $fh;
55
56# you can pull in the arrayref of hashrefs that's written out
57my $recovered = eval $output;
58
59sub wanted {
60    my $type = $_;
61    return if $type eq '.';
62    $type =~ s/\.pm$//;
63    return if $type eq 'Result';
64    my $field_class = "HTML::FormHandler::Field::$type";
65    if( try_load_class( $field_class ) ) {
66        push @field_types, $type;
67    }
68    else {
69        print "did not load $type\n";
70    }
71
72}