blib/lib/Statocles/Command/daemon.pm

filter: Statementstmt 31.4% 38 / 121 Branchbran 2.7% 1 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 10 / 15 Podpod 0.0% 0 / 1 totaltotal 26.3% 49 / 186 CC 28 SCAR 40.0
CC
28
Cov
24%
startup
131.9
__ANON__
56.0
__ANON__
42.0
CRAP 54.7
« blib/lib/Statocles/App/Perldoc.pm ↑ Summary blib/lib/Statocles/Test.pm »
1 package Statocles::Command::daemon;
2 3 our $VERSION = '0.099';
3 # ABSTRACT: Run a daemon to navigate the site
4  
5 3 ▶use Statocles::Base 'Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Statocles::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶sub run {
Subroutine run: called CC=3 SCAR=18.5
Pod undocumented
9 1 my ( $self, @argv ) = @_;
10 # Build the site first no matter what. We may end up watching for
11 # future changes, but assume they meant to build first
12 1 my %build_opt;
13 1 GetOptionsFromArray( \@argv, \%build_opt,
14 'port|p=i',
15 'date|d=s',
16 );
17  
18 1 require Mojo::Server::Daemon;
19 1 my $app = Statocles::Command::daemon::_MOJOAPP->new(
20 site => $self->site,
21 options => \%build_opt,
22 );
23 0 our $daemon = Mojo::Server::Daemon->new(
24 silent => 1,
25 app => $app,
26 );
27  
28 0 ▶ if ( $build_opt{port} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($build_opt{"port"}) 0 0 0
29 0 $daemon->listen([ "http://*:$build_opt{port}" ]);
30 }
31  
32 # Using start() instead of run() so we can stop() inside the tests
33 0 $daemon->start;
34  
35 # Find the port we're listening on
36 0 my $id = $daemon->acceptors->[0];
37 0 my $handle = $daemon->ioloop->acceptor( $id )->handle;
38 0 ▶ say "Listening on " . sprintf( 'http://%s:%d', $handle->sockhost || '127.0.0.1', $handle->sockport );
Condition0%0/2
$handle->sockhost || '127.0.0.1'
l!l
00
MC/DC0%0/1
$handle->sockhost || '127.0.0.1'
$handle->sockhost
Truth table0%0/2
A
A= $handle->sockhost
Aresult
11
00
39  
40 # Give control to the IOLoop
41 0 Mojo::IOLoop->start;
42  
43 0 return 0;
44 }
45  
46 {
47 package # Do not index this
48 Statocles::Command::daemon::_MOJOAPP;
49  
50 # Currently, as of Mojolicious 5.12, loading the Mojolicious module here
51 # will load the Mojolicious::Commands module, which calls GetOptions, which
52 # will remove -h, --help, -m, and -s from @ARGV. We fix this by copying
53 # @ARGV in bin/statocles before we call Statocles::Command.
54 #
55 # We could fix this in the future by moving this out into its own module,
56 # that is only loaded after we are done passing @ARGV into main(), above.
57 3 ▶ use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 3 ▶ use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
59 3 ▶ use File::Share qw( dist_dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
60 3 has 'site';
61 1 ▶ has options => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
62 2 ▶ has cleanup => sub { Mojo::Collection->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
63  
64 2 ▶ sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.6
65 2 my ( $self, $in_global_destruction ) = @_;
66 2 ▶ return unless $self->cleanup;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->cleanup 0 2 2
67 0 ▶ $self->cleanup->each( sub { $_->() } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
68 }
69  
70 2 ▶ sub startup {
Subroutine startup: called CC=12 SCAR=48.8
71 2 my ( $self ) = @_;
72 2 $self->log( $self->site->log );
73  
74 # First build the site
75 2 my $path = $self->site->store->path->child( '.statocles', 'build' );
76 2 $path->mkpath;
77 2 my $store = Statocles::Store->new( path => $path );
78 2 $store->write_file( $_->path, $_->render ) for $self->site->pages( %{ $self->options } );
79  
80 0 my $base;
81 0 ▶ if ( $self->site->base_url ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->site->base_url) 0 0 0
82 0 $base = Mojo::URL->new( $self->site->base_url )->path->to_string;
83 0 $base =~ s{/$}{};
84 }
85  
86 0 my $index = "/index.html";
87 0 ▶ if ( $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) 0 0 0
88 0 $index = $base . $index;
89 }
90  
91 # Add the build dir to the list of static paths for mojolicious to
92 # search
93 0 unshift @{ $self->static->paths }, $store->path;
94  
95 # Watch for filesystem events and rebuild the site Right now this only
96 # works on OSX. We should spin this off into Mojo::IOLoop::FSEvents and
97 # make it work cross-platform, including a pure-Perl fallback
98 0 my $can_watch = eval { require Mac::FSEvents; 1 };
99 0 ▶ if ( !$can_watch && $^O =~ /darwin/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $can_watch and $^O =~ /darwin/u) 0 0 0
Condition0%0/3
not $can_watch and $^O =~ /darwin/u
!ll&&!rl&&r
000
MC/DC0%0/2
not $can_watch and $^O =~ /darwin/u
not $can_watch $^O =~ /darwin/u
Truth table0%0/3
not(A) and B
A= not $can_watchB= $^O =~ /darwin/u
ABresult
0X0
100
111
100 0 say "To watch for filesystem changes and automatically rebuild the site, ",
101 "install the Mac::FSEvents module from CPAN";
102 }
103  
104 0 ▶ if ( $can_watch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($can_watch) 0 0 0
105  
106 # Collect the paths to watch
107 0 my %watches = ();
108 0 for my $app ( values %{ $self->site->apps } ) {
109 0 ▶ if ( $app->can( 'store' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app->can("store")) 0 0 0
110 0 push @{ $watches{ $app->store->path } }, $app->store;
111 }
112 }
113  
114 # Watch the theme, but not built-in themes
115 0 my $theme_path = $self->site->theme->path;
116 0 ▶ if ( !Path::Tiny->new( dist_dir( 'Statocles' ) )->subsumes( $theme_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Path::Tiny"->new(dist_dir("Statocles"))->subsumes($theme_path)) 0 0 0
117 0 push @{ $watches{ $theme_path } }, $self->site->theme;
118 }
119  
120 0 require Mojo::IOLoop::Stream;
121 0 my $ioloop = Mojo::IOLoop->singleton;
122 0 my $build_dir = $store->path->realpath;
123  
124 0 weaken $self;
125  
126 0 for my $path ( keys %watches ) {
127 0 $self->log->info( "Watching for changes in '$path'" );
128  
129 0 my $fs = Mac::FSEvents->new( {
130 path => "$path",
131 latency => 1.0,
132 } );
133 0 my $handle = $fs->watch;
134  
135 0 ▶ push @{ $self->cleanup }, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
136 0 ▶ return if !$fs || !$handle;
Branch: 1 branch
BranchTrueFalseTotal
unless $fs and $handle 0 0 0
Condition0%0/3
$fs and $handle
!ll&&!rl&&r
000
MC/DC0%0/2
$fs and $handle
$fs $handle
Truth table0%0/3
A and B
A= $fsB= $handle
ABresult
0X0
100
111
137 0 $fs->stop;
138 0 Mojo::IOLoop->remove( $handle );
139 0 };
140  
141 0 ▶ $ioloop->reactor->io( $handle, sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
142 0 my ( $reactor, $writable ) = @_;
143  
144 0 my $rebuild;
145 REBUILD:
146 0 for my $event ( $fs->read_events ) {
147 0 ▶ if ( $event->path =~ /^\Q$build_dir/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->path =~ /^\Q$build_dir\E/u) 0 0 0
148 0 next;
149 }
150  
151 0 $self->log->info( "Path '" . $event->path . "' changed... Rebuilding" );
152 0 ▶ $_->can('clear') && $_->clear for @{ $watches{ $path } };
Branch: 1 branch
BranchTrueFalseTotal
$_->can("clear") and $_->clear 0 0 0
153 0 $rebuild = 1;
154 }
155  
156 0 ▶ if ( $rebuild ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rebuild) 0 0 0
157 0 $self->site->clear_pages;
158 0 $store->write_file( $_->path, $_->render ) for $self->site->pages( %{ $self->options } );
159 }
160 0 } );
161 0 $ioloop->reactor->watch( $handle, 1, 0 );
162 }
163  
164 0 $self->log->info( "Ignoring changes in '$build_dir'" );
165 }
166  
167 0 ▶ my $serve_static = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
168 0 my ( $c ) = @_;
169 0 my $path = Mojo::Path->new( $c->stash->{slug} );
170  
171 # Taint check the path, just in case someone uses this "dev" tool to
172 # serve real content
173 0 ▶ return $c->render( status => 400, text => "You didn't say the magic word" )
Branch: 1 branch
BranchTrueFalseTotal
if $path->canonicalize->parts->[0] eq ".." 0 0 0
174 if $path->canonicalize->parts->[0] eq '..';
175  
176 0 my $asset = $c->app->static->file( $path );
177 0 ▶ if ( !$asset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asset) 0 0 0
178 0 ▶ if ( $path =~ m{/$} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($path =~ m[/$]u) { } 0 0 0
elsif ($store->path->child($path)->is_dir) { } 0 0 0
179 # Check for index.html
180 0 $path = Mojo::Path->new( $c->stash->{slug} . "/index.html" );
181 0 $asset = $c->app->static->file( $path );
182 }
183 elsif ( $store->path->child( $path )->is_dir ) {
184 0 return $c->redirect_to( "/$path/" );
185 }
186 }
187  
188 0 ▶ if ( !$asset ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($asset) 0 0 0
189 0 return $c->render( status => 404, text => 'Not found' );
190 }
191  
192 # The static helper will choose the right content type and charset
193 0 return $c->reply->static( $path );
194 0 };
195  
196 0 ▶ if ( $base ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base) { } 0 0 0
197 0 ▶ $self->routes->get( '/', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 my ( $c ) = @_;
199 0 $c->redirect_to( $base );
200 0 } );
201 0 $self->routes->get( $base . '/*slug' )->to( slug => 'index.html', cb => $serve_static );
202 }
203 else {
204 0 $self->routes->get( '/*slug' )->to( slug => 'index.html', cb => $serve_static );
205 }
206  
207 }
208  
209 }
210  
211 3 1;
212  
213 __END__
« blib/lib/Statocles/App/Perldoc.pm ↑ Summary blib/lib/Statocles/Test.pm »