Mojolicious/Command/generate/lexicon.pm

filter: Statementstmt 83.3% 70 / 84 Branchbran 46.4% 13 / 28 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 83.3% 10 / 12 Podpod 100% 1 / 1 totaltotal 75.7% 97 / 128 CC 18 SCAR 28.2
CC
18
Cov
75%
run
27.3
__ANON__
6.0
__ANON__
1.1
CRAP 16.8
↑ Summary MojoX/I18N/Lexemes.pm »
1 package Mojolicious::Command::generate::lexicon;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use String::Escape qw(qqbackslash);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = 0.998;
11  
12 3 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use MojoX::I18N::Lexemes;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 3 has "description" => "Generate lexicon file from templates.";
18  
19 0 ▶has "usage" => sub{shift->extract_usage};
Subroutine __ANON__: not called CC=1 SCAR=1.2
20  
21 4 ▶sub run {
Subroutine run: called CC=17 SCAR=33.1
Pod documented
22 4 my $self = shift;
23 4 my $language = shift;
24  
25 4 my @templates;
26 my $app;
27 4 ▶ if (ref $self->app eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->app eq 'CODE') { } 0 4 4
28 0 $app = $self->app->();
29 }
30 else {
31 4 $app = $self->app;
32 }
33  
34 4 my $verbose;
35  
36 4 my $app_klass = ref $app;
37 4 my $app_class = ref $app;
38 4 $app_class =~ s{::}{/}g;
39  
40 4 ▶ $language ||= 'Skeleton';
Condition100%2/2
$language ||= "Skeleton"
l!l
22
MC/DC100%1/1
$language ||= "Skeleton"
$language
Truth table100%2/2
A
A= $language
Aresult
11
00
41  
42 4 my $behavior = '';
43  
44 4 ▶ local @ARGV = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 0 4
45  
46 4 my $result = GetOptions(
47 "behavior|b:s{1,1}" => \$behavior,
48 'verbose|v:1' => \$verbose,
49 );
50 4 ▶ push @templates, $ARGV[0] if (defined $ARGV[0]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ARGV[0] 4 0 4
51  
52 4 my $handler = $app->renderer->default_handler;
53  
54 # Find all templates of project
55 2 ▶ unless (@templates) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@templates) 0 2 2
56 find(
57 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
58 0 ▶ push @templates, $File::Find::name if (/\.$handler/);
Branch: 1 branch
BranchTrueFalseTotal
if /\.$handler/u 0 0 0
59 },
60 0 @{$app->renderer->paths}
61 );
62 }
63  
64 2 my $lexem_file =
65 $app->home->rel_file("lib/$app_class/I18N/$language.pm")->to_abs()
66 ->to_string();
67 2 my %oldlex = ();
68  
69 2 ▶ if (-e $lexem_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $lexem_file) 1 1 2
70 1 ▶ if ($language ne 'Skeleton') {
Branch: 1 branch
BranchTrueFalseTotal
if ($language ne "Skeleton") 1 0 1
71 1 ▶ if (lc $behavior eq 'save') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $behavior eq 'save') { } 0 1 1
elsif (lc $behavior eq 'reset') { } 1 0 1
72 0 %oldlex = eval {
73 0 local %INC = %INC;
74 0 require $app->home->rel_file(
75 "lib/$app_class/I18N/$language.pm");
76 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
77 0 %{*{"${app_klass}::I18N::${language}::Lexicon"}};
78 };
79 0 ▶ %oldlex = () if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
80 }
81 elsif (lc $behavior eq 'reset') {
82  
83 # Just proceed
84 }
85 else {
86 0 print <<USAGE;
87 Lexemes already exists.
88 You must set `--behavior' to one of "reset" or "save".
89 USAGE
90 0 return;
91 }
92 }
93  
94 1 ▶ print " [delete] $lexem_file\n" unless $self->quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->quiet 1 0 1
95 1 unlink $lexem_file;
96 }
97  
98 2 my $l = MojoX::I18N::Lexemes->new();
99  
100 2 my %lexicon = %oldlex;
101  
102 2 foreach my $file (@templates) {
103 2 ▶ open F, $file or die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, $file or die "Unable to open ${file}: $!" 2 0 2
104 2 my $t = do { local $/; <F> };
105 2 close F;
106  
107 # get lexemes
108 2 ▶ print "Parsing $file \n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
109 2 my $parsed_lexemes = $l->parse($t);
110  
111 # add to all lexemes
112 2 foreach (grep { !exists $lexicon{$_} } @{$parsed_lexemes}) {
113 8 $lexicon{$_} = '';
114 8 ▶ print "New lexeme found => $_\n" if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 8 8
115 }
116 }
117  
118 # Output lexem
119 $self->render_to_file(
120 2 'package',
121 $lexem_file, {
122 app_class => $app_klass,
123 language => $language,
124 lexicon => \%lexicon
125 }
126 );
127 }
128  
129 3 1;
130  
131 __DATA__
↑ Summary MojoX/I18N/Lexemes.pm »