Mojolicious-Command-generate-lexicon

 view release on metacpan or  search on metacpan

lib/Mojolicious/Command/generate/lexicon.pm  view on Meta::CPAN

package Mojolicious::Command::generate::lexicon;

use strict;
use warnings;
use utf8;

use Mojo::Base 'Mojolicious::Command';
use String::Escape qw(qqbackslash);

our $VERSION = 0.998;

use File::Find;
use Getopt::Long;

use MojoX::I18N::Lexemes;

has "description" => "Generate lexicon file from templates.";

has "usage" => sub{shift->extract_usage};

sub run {
    my $self     = shift;
    my $language = shift;

    my @templates;
    my $app;
    if (ref $self->app eq 'CODE') {
        $app = $self->app->();
    }
    else {
        $app = $self->app;
    }

    my $verbose;

    my $app_klass = ref $app;
    my $app_class = ref $app;
    $app_class =~ s{::}{/}g;

    $language ||= 'Skeleton';

    my $behavior = '';

    local @ARGV = @_ if @_;

    my $result = GetOptions(
        "behavior|b:s{1,1}" => \$behavior,
        'verbose|v:1'       => \$verbose,
    );
    push @templates, $ARGV[0] if (defined $ARGV[0]);

    my $handler = $app->renderer->default_handler;

    # Find all templates of project
    unless (@templates) {
        find(
            sub {
                push @templates, $File::Find::name if (/\.$handler/);
            },
            @{$app->renderer->paths}
        );
    }

    my $lexem_file =
      $app->home->rel_file("lib/$app_class/I18N/$language.pm")->to_abs()
      ->to_string();
    my %oldlex = ();

    if (-e $lexem_file) {
        if ($language ne 'Skeleton') {
            if (lc $behavior eq 'save') {
                %oldlex = eval {
                    local %INC = %INC;
                    require $app->home->rel_file(
                        "lib/$app_class/I18N/$language.pm");
                    no strict 'refs';
                    %{*{"${app_klass}::I18N::${language}::Lexicon"}};
                };
                %oldlex = () if ($@);
            }
            elsif (lc $behavior eq 'reset') {

                # Just proceed
            }
            else {
                print <<USAGE;
Lexemes already exists.
You must set `--behavior' to one of "reset" or "save".
USAGE
                return;
            }
        }

        print "  [delete] $lexem_file\n" unless $self->quiet;
        unlink $lexem_file;
    }

    my $l = MojoX::I18N::Lexemes->new();

    my %lexicon = %oldlex;

    foreach my $file (@templates) {
        open F, $file or die "Unable to open $file: $!";
        my $t = do { local $/; <F> };



( run in 1.713 second using v1.01-cache-2.11-cpan-364913b4093 )