| 1 | package App::unbelievable::CLI; | ||||||||||||||||||||||||||||
| 2 | # see script/unbelievable for docs. | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use App::unbelievable::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # === Runner for script/unbelievable ====================================== | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 0 | require Getopt::Long::Subcommand; | |||||||||||||||||||||||||||
| 11 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||
| 12 | 0 | ▶ | my $args = shift or die "No args"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 13 | 0 | local @ARGV = @$args; | |||||||||||||||||||||||||||
| 14 | 0 | my %opts = ( # Defaults | |||||||||||||||||||||||||||
| 15 | verbose => 0 | ||||||||||||||||||||||||||||
| 16 | ); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $res = Getopt::Long::Subcommand::GetOptions( | ||||||||||||||||||||||||||||
| 19 | summary => 'Build a static site', | ||||||||||||||||||||||||||||
| 20 | default_subcommand => 'help', | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # common options recognized by all subcommands | ||||||||||||||||||||||||||||
| 23 | options => { | ||||||||||||||||||||||||||||
| 24 | 'help|h|?|usage' => { | ||||||||||||||||||||||||||||
| 25 | summary => 'Display help message', | ||||||||||||||||||||||||||||
| 26 | handler => \$opts{help}, | ||||||||||||||||||||||||||||
| 27 | #handler => sub { | ||||||||||||||||||||||||||||
| 28 | # my ($cb, $val, $res) = @_; | ||||||||||||||||||||||||||||
| 29 | # if ($res->{subcommand}) { | ||||||||||||||||||||||||||||
| 30 | # say "Help message for $res->{subcommand} ..."; | ||||||||||||||||||||||||||||
| 31 | # } else { | ||||||||||||||||||||||||||||
| 32 | # say "General help message ..."; | ||||||||||||||||||||||||||||
| 33 | # } | ||||||||||||||||||||||||||||
| 34 | # exit 0; | ||||||||||||||||||||||||||||
| 35 | #}, | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | 'man' => { | ||||||||||||||||||||||||||||
| 38 | summary => 'Display full docs', | ||||||||||||||||||||||||||||
| 39 | handler => \$opts{man}, | ||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||
| 41 | 'version|V' => { | ||||||||||||||||||||||||||||
| 42 | summary => 'Display program version', | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | handler => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 44 | 0 | say "$0 version $main::VERSION"; | |||||||||||||||||||||||||||
| 45 | 0 | exit 0; | |||||||||||||||||||||||||||
| 46 | }, | ||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||
| 48 | 'verbose|v+' => { | ||||||||||||||||||||||||||||
| 49 | handler => \$opts{verbose}, | ||||||||||||||||||||||||||||
| 50 | }, | ||||||||||||||||||||||||||||
| 51 | }, # options | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # subcommands | ||||||||||||||||||||||||||||
| 54 | subcommands => { | ||||||||||||||||||||||||||||
| 55 | new => { | ||||||||||||||||||||||||||||
| 56 | summary => 'Create a new site', | ||||||||||||||||||||||||||||
| 57 | }, | ||||||||||||||||||||||||||||
| 58 | build => { | ||||||||||||||||||||||||||||
| 59 | summary => 'Build the static html', | ||||||||||||||||||||||||||||
| 60 | options => { | ||||||||||||||||||||||||||||
| 61 | 's|route-style=s' => { | ||||||||||||||||||||||||||||
| 62 | summary => 'Which style of route to request', | ||||||||||||||||||||||||||||
| 63 | handler => \$opts{route_style}, | ||||||||||||||||||||||||||||
| 64 | }, | ||||||||||||||||||||||||||||
| 65 | }, | ||||||||||||||||||||||||||||
| 66 | }, | ||||||||||||||||||||||||||||
| 67 | 0 | serve => { | |||||||||||||||||||||||||||
| 68 | summary => 'Serve the static HTML using a local server', | ||||||||||||||||||||||||||||
| 69 | # TODO | ||||||||||||||||||||||||||||
| 70 | #options => { | ||||||||||||||||||||||||||||
| 71 | # 'server|s=s' => { | ||||||||||||||||||||||||||||
| 72 | # summary => 'Which server to use', | ||||||||||||||||||||||||||||
| 73 | # handler => \$opts{server}, | ||||||||||||||||||||||||||||
| 74 | # }, | ||||||||||||||||||||||||||||
| 75 | #}, | ||||||||||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||||||||||
| 77 | aio => { | ||||||||||||||||||||||||||||
| 78 | summary => 'build + serve (all in one)', | ||||||||||||||||||||||||||||
| 79 | }, | ||||||||||||||||||||||||||||
| 80 | help => { | ||||||||||||||||||||||||||||
| 81 | summary => 'Show help', | ||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | }, # subcommands | ||||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | $VERBOSE = $opts{verbose} if $opts{verbose} > $VERBOSE; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | _diag("Got options:\n", Dumper($res), Dumper(\%opts)); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | Pod::Usage::pod2usage() unless $res->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 0 | ▶ | Pod::Usage::pod2usage(-verbose => 1, -exitval => 0) if $opts{help}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | Pod::Usage::pod2usage(-verbose => 1, -exitval => 0) | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | if $res->{success} && $res->{subcommand}->[0] eq 'help'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->{"success"} and $res->{"subcommand"}[0] eq "help"
MC/DC0%0/2
$res->{"success"} and $res->{"subcommand"}[0] eq "help"
$res->{"success"}
$res->{"subcommand"}[0] eq "help"
Truth table0%0/3
A and B
A= $res->{"success"}B= $res->{"subcommand"}[0] eq "help"
| |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | Pod::Usage::pod2usage(-verbose => 2, -exitval => 0) if $opts{man}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | my $cmdname = 'cmd_' . join '_', @{$res->{subcommand}}; | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | my $fn = __PACKAGE__->can($cmdname) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | or die "I don't know subcommand $cmdname"; | ||||||||||||||||||||||||||||
| 99 | 0 | return $fn->($res, \%opts); | |||||||||||||||||||||||||||
| 100 | } #run() | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # === Subcommands ========================================================= | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub cmd_help { | ||||||||||||||||||||||||||
|
Subroutine
cmd_help: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 105 | 0 | Pod::Usage::pod2usage(-verbose => 1, -exitval => 0); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub cmd_aio { | ||||||||||||||||||||||||||
|
Subroutine
cmd_aio: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my ($res, $opts) = @_; | |||||||||||||||||||||||||||
| 110 | 0 | my $rv = cmd_build($res, $opts); | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | return $rv if $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $rv = cmd_serve($res, $opts); | |||||||||||||||||||||||||||
| 113 | 0 | return $rv; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub cmd_new { | ||||||||||||||||||||||||||
|
Subroutine
cmd_new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 117 | 0 | my ($res, $opts) = @_; | |||||||||||||||||||||||||||
| 118 | 0 | say "New site"; | |||||||||||||||||||||||||||
| 119 | 0 | return 0; | |||||||||||||||||||||||||||
| 120 | } #cmd_new() | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub cmd_build { | ||||||||||||||||||||||||||
|
Subroutine
cmd_build: not called CC=8 SCAR=40.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 0 | my ($res, $opts) = @_; | |||||||||||||||||||||||||||
| 124 | 0 | say "Build site"; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # Defaults | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $opts->{route_style} //= 'htmlfile'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"route_style"} //= "htmlfile"
MC/DC0%0/1
$opts->{"route_style"} //= "htmlfile"
$opts->{"route_style"}
Truth table0%0/2
A
A= $opts->{"route_style"}
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | _diag("unbelivable opts:\n", Dumper($opts)); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | require App::Wallflower; | |||||||||||||||||||||||||||
| 132 | 0 | require Config; | |||||||||||||||||||||||||||
| 133 | 0 | require File::Find::Rule; | |||||||||||||||||||||||||||
| 134 | 0 | require File::Temp; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # TODO get CPU count per | ||||||||||||||||||||||||||||
| 137 | # https://gist.github.com/aras-p/47e2252d6b1fa57d3619fd8e021690ec | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # List the routes | ||||||||||||||||||||||||||||
| 140 | # TODO get all GET routes from app | ||||||||||||||||||||||||||||
| 141 | 0 | my @routes; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Get and filter the routes from /content. | ||||||||||||||||||||||||||||
| 144 | 0 | push @routes, File::Find::Rule->readable->file->not_name('.*')->relative | |||||||||||||||||||||||||||
| 145 | ->in(_here('content')); | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | if($opts->{route_style} eq 'htmlfile') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 147 | 0 | s{\.[^\.]+$}{.html} foreach @routes; | |||||||||||||||||||||||||||
| 148 | } elsif($opts->{route_style} eq 'dir') { | ||||||||||||||||||||||||||||
| 149 | 0 | s{\.[^\.]+$}{/} foreach @routes; | |||||||||||||||||||||||||||
| 150 | } else { | ||||||||||||||||||||||||||||
| 151 | 0 | die "Unknown route style $opts->{route_style}"; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # public/ --- all files appear just as they are, sans leading /public | ||||||||||||||||||||||||||||
| 155 | 0 | push @routes, File::Find::Rule->readable->file->relative | |||||||||||||||||||||||||||
| 156 | ->in(_here('public')); | ||||||||||||||||||||||||||||
| 157 | 0 | s{^([^/])}{/$1} foreach @routes; | |||||||||||||||||||||||||||
| 158 | 0 | push @routes, '/'; | |||||||||||||||||||||||||||
| 159 | 0 | _diag("Routes:\n", join("\n", @routes)); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Export the routes where wallflower can find them | ||||||||||||||||||||||||||||
| 162 | 0 | my $fh = File::Temp->new(); | |||||||||||||||||||||||||||
| 163 | 0 | say {$fh} join("\n", @routes); | |||||||||||||||||||||||||||
| 164 | 0 | close $fh; # TODO does this work? | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | my $destdir = _here('_output'); # TODO make this an option | |||||||||||||||||||||||||||
| 167 | 0 | ▶ | do { no autodie; mkdir $destdir }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
| |||||||||||||||||||||||||||||
| 168 | my $wallflower_opts = [ | ||||||||||||||||||||||||||||
| 169 | ( '--verbose' )x!! $VERBOSE, | ||||||||||||||||||||||||||||
| 170 | '--application' => _here('bin/app.psgi'), | ||||||||||||||||||||||||||||
| 171 | '--destination' => $destdir, | ||||||||||||||||||||||||||||
| 172 | 0 | '--INC' => join($Config::Config{path_sep}, @INC), | |||||||||||||||||||||||||||
| 173 | '--files', # Flag | ||||||||||||||||||||||||||||
| 174 | "$fh", # Stringifies to the filename | ||||||||||||||||||||||||||||
| 175 | ]; | ||||||||||||||||||||||||||||
| 176 | 0 | _diag("Wallflower options:\n", Dumper($wallflower_opts)); | |||||||||||||||||||||||||||
| 177 | 0 | my $builder = App::Wallflower->new_with_options($wallflower_opts); | |||||||||||||||||||||||||||
| 178 | 0 | ▶ | return $builder->run // 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$builder->run // 0
MC/DC0%0/1
$builder->run // 0
$builder->run
Truth table0%0/2
A
A= $builder->run
| |||||||||||||||||||||||||||||
| 179 | } #cmd_build() | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | sub cmd_serve { | ||||||||||||||||||||||||||
|
Subroutine
cmd_serve: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 182 | # Thanks to plackup(1) and to https://github.com/plack/Plack/issues/93 | ||||||||||||||||||||||||||||
| 183 | 0 | require Plack::Runner; | |||||||||||||||||||||||||||
| 184 | 0 | my $runner = Plack::Runner->new; # TODO add options | |||||||||||||||||||||||||||
| 185 | 0 | my $dir = _here('_output'); | |||||||||||||||||||||||||||
| 186 | 0 | $dir =~ s/"/\"/g; # Just in case | |||||||||||||||||||||||||||
| 187 | 0 | $runner->parse_options( | |||||||||||||||||||||||||||
| 188 | qw(-MPlack::App::File -MPlack::Middleware::DirIndex -e), | ||||||||||||||||||||||||||||
| 189 | "enable 'DirIndex'; Plack::App::File->new(root => \"$dir\")->to_app" | ||||||||||||||||||||||||||||
| 190 | ); | ||||||||||||||||||||||||||||
| 191 | 0 | return $runner->run; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # === Helpers ============================================================= | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # Return the path of a directory under cwd | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub _here { | ||||||||||||||||||||||||||
|
Subroutine
_here: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 198 | 0 | return File::Spec->rel2abs(File::Spec->catpath(getcwd, @_)); | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 1 | 1; | |||||||||||||||||||||||||||