bin/thunderhorse

filter: Statementstmt 95.2% 81 / 85 Branchbran 76.9% 20 / 26 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod n/a totaltotal 92.8% 129 / 139 CC 6 SCAR 6.0
CC
6
Cov
91%
get_template_namespace
4.2
_recurse_locations
3.1
BEGIN
1.0
CRAP 1.8
« blib/lib/Thunderhorse/App.pm ↑ Summary blib/lib/Thunderhorse/Request.pm »
1 #!/usr/bin/env perl
2  
3 11 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Gears::Generator;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use constant EX_DIR => path(__FILE__)->parent->parent->child('ex');
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # try loading app from '.' and 'lib' directories. Dot is required for "do
16 # 'app.pl'" to work.
17 11 ▶use lib '.', 'lib';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # autoflush stdout
20 11 $|++;
21  
22 # prettier Data::Dumper output
23 11 $Data::Dumper::Sortkeys = true;
24 11 $Data::Dumper::Indent = 1;
25  
26 11 my $generate;
27 11 my $config = false;
28 11 my $locations = false;
29 11 my $tabs = false;
30 11 my $help = false;
31 11 my $error = false;
32  
33 11 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("g|generate=s", \$generate, "c|show-config", \$config, "l|show-locations", \$locations, "t|tabs", \$tabs, "h|help", \$help) or 11 0 11
34 'g|generate=s' => \$generate,
35 'c|show-config' => \$config,
36 'l|show-locations' => \$locations,
37 't|tabs' => \$tabs,
38 'h|help' => \$help,
39 ) or $error = true;
40  
41 my $target = shift
42 or $help
43 11 ▶ or do { warn "no target specified\n"; $error = true };
Branch: 1 branch
BranchTrueFalseTotal
my $target = shift @ARGV or $help or 10 1 11
Condition100%3/3
my $target = shift @ARGV or $help
l!l&&r!l&&!r
911
MC/DC100%2/2
my $target = shift @ARGV or $help
my $target = shift @ARGV $help
Truth table100%3/3
A or B
A= my $target = shift @ARGVB= $help
ABresult
1X1
011
000
44  
45 # handle actions
46 11 ▶if ($help || $error) {
Branch: 4 branches
BranchTrueFalseTotal
if ($help or $error) { } 2 9 11
elsif (defined $generate) { } 7 2 9
elsif ($config) { } 1 1 2
elsif ($locations) { } 1 0 1
Condition100%3/3
$help or $error
l!l&&r!l&&!r
119
MC/DC100%2/2
$help or $error
$help $error
Truth table100%3/3
A or B
A= $helpB= $error
ABresult
1X1
011
000
47 2 ▶ pod2usage($error ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$error ? : 1 1 2
48 }
49  
50 elsif (defined $generate) {
51 7 print "Generating project from template $generate... ";
52  
53 7 my $generator = Gears::Generator->new(base_dir => EX_DIR);
54 7 my $namespace = get_template_namespace($generator, $generate);
55  
56 7 ▶ if (defined $namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $namespace) 7 0 7
57 7 my $file = File::Spec->join(split /::/, $namespace);
58 7 my $target_file = File::Spec->join(split /::/, $target);
59  
60 44 ▶ push $generator->name_filters->@*, sub ($name) {
Subroutine __ANON__: called CC=1 SCAR=0.0
61 44 return $name =~ s{\b\Q$file\E}{$target_file}r;
62 7 };
63  
64 44 ▶ push $generator->content_filters->@*, sub ($content) {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 44 return $content =~ s{\b\Q$namespace\E}{$target}rg;
66 7 };
67 }
68  
69 7 ▶ if (!$tabs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tabs) 6 1 7
70 36 ▶ push $generator->content_filters->@*, sub ($content) {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 36 return $content =~ s{\t}{ }rg;
72 6 };
73 }
74  
75 7 $generator->generate($generate, '.');
76 7 say 'done.';
77 }
78  
79 elsif ($config) {
80 1 my $app = load_app($target);
81 1 print Data::Dumper->Dump([$app->config->config], ['*config']);
82 }
83  
84 elsif ($locations) {
85 1 my $app = load_app($target);
86 1 print Data::Dumper->Dump([get_readable_locations($app)], ['*locations']);
87 }
88  
89 else {
90 0 warn "No action specified\n";
91 0 pod2usage(1);
92 }
93  
94 2 ▶sub load_app ($target)
Subroutine load_app: called CC=1 SCAR=0.0
95 {
96 2 local $ENV{THUNDERHORSE_SCRIPT} = true;
97 2 local $0 = $target;
98 2 return do $target;
99 }
100  
101 7 ▶sub get_template_namespace ($generator, $template)
Subroutine get_template_namespace: called CC=4 SCAR=14.2
102 {
103 7 my $files = $generator->get_template($template);
104 7 ▶ my $app_file = first { $_ =~ /app\.pl$/ } $files->@*;
Subroutine __ANON__: called CC=1 SCAR=0.0
105  
106 7 ▶ return undef unless defined $app_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $app_file 0 7 7
107 7 my $app_file_content = $app_file->slurp;
108  
109 7 ▶ return $1
Branch: 1 branch
BranchTrueFalseTotal
if $app_file_content =~ /^package ([\w:]+)/mu 1 6 7
110 if $app_file_content =~ /^package ([\w:]+)/m;
111  
112 6 ▶ return $1
Branch: 1 branch
BranchTrueFalseTotal
if $app_file_content =~ /^\s*([\w:]+)->new/mu 6 0 6
113 if $app_file_content =~ /^\s*([\w:]+)->new/m;
114  
115 0 return undef;
116 }
117  
118 1 ▶sub _recurse_locations ($level)
Subroutine _recurse_locations: called CC=3 SCAR=11.2
119 {
120 1 my @out;
121 1 foreach my $loc (sort { $a->order <=> $b->order } $level->@*) {
122 2 my $this_location = {
123 pattern => $loc->pattern,
124 name => $loc->name,
125 controller => ref $loc->controller,
126 handler => $loc->to,
127 action => $loc->action,
128 };
129  
130 2 ▶ if ($loc->is_bridge) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loc->is_bridge) { } 0 2 2
131 0 push @out, [$this_location, _recurse_locations($loc->locations)];
132 }
133 else {
134 2 push @out, $this_location;
135 }
136 }
137  
138 1 return @out;
139 }
140  
141 1 ▶sub get_readable_locations ($app)
Subroutine get_readable_locations: called CC=1 SCAR=0.0
142 {
143 1 return [_recurse_locations $app->router->locations];
144 }
145  
146 __END__
« blib/lib/Thunderhorse/App.pm ↑ Summary blib/lib/Thunderhorse/Request.pm »