| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
my $target = shift @ARGV or $help
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
| |||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||
| 45 | # handle actions | ||||||||||||||||||||||||||||||||||||||||
| 46 | 11 | ▶ | if ($help || $error) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition100%3/3
$help or $error
MC/DC100%2/2
$help or $error
$help
$error
Truth table100%3/3
A or B
A= $helpB= $error
| |||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | pod2usage($error ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 107 | 7 | my $app_file_content = $app_file->slurp; | |||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||
| 109 | 7 | ▶ | return $1 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 110 | if $app_file_content =~ /^package ([\w:]+)/m; | ||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||
| 112 | 6 | ▶ | return $1 | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||