Skip to content

Commit

Permalink
add build event hook to site
Browse files Browse the repository at this point in the history
This hook will allow us to attach listeners that can do things to the
built pages.

Refs #78
  • Loading branch information
preaction committed Mar 1, 2015
1 parent 4e24f06 commit 473566b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions dist.ini
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Text::Markdown = 0
Mojolicious = 6.0
File::Copy::Recursive = 0
Beam::Wire = 1.010
Beam::Emitter = 0.007
Pod::Usage::Return = 0
Git::Repository = 0
File::Share = 0
Expand Down
8 changes: 8 additions & 0 deletions lib/Statocles/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ our %IMPORT_BUNDLES = (
@class_modules,
],

Emitter => [
'Statocles::Event',
sub {
my ( $bundles, $args ) = @_;
Moo::Role->apply_role_to_package( $args->{package}, 'Beam::Emitter' );
return;
},
],
);

1;
Expand Down
28 changes: 28 additions & 0 deletions lib/Statocles/Event.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package Statocles::Event;
# ABSTRACT: Events objects for Statocles

=head1 EVENTS
=head2 Statocles::Event::Pages
An event with L<page objects|Statocles::Page>.
=cut

package Statocles::Event::Pages;
use Statocles::Base 'Class';
extends 'Beam::Event';

=attr pages
An array of L<Statocles::Page> objects
=cut

has pages => (
is => 'ro',
isa => ArrayRef[ConsumerOf['Statocles::Page']],
required => 1,
);

1;
28 changes: 24 additions & 4 deletions lib/Statocles/Site.pm
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package Statocles::Site;
# ABSTRACT: An entire, configured website

use Statocles::Base 'Class';
use Statocles::Base 'Class', 'Emitter';
use Scalar::Util qw( blessed );
use Mojo::URL;
use Mojo::DOM;
use Mojo::Log;
use Statocles::Page::Plain;
use Statocles::Page::File;

=attr title
Expand Down Expand Up @@ -238,7 +240,7 @@ sub nav {

=method build
Build the site in its build location
Build the site in its build location.
=cut

Expand Down Expand Up @@ -306,19 +308,37 @@ sub build {
# html files only
my @indexed_pages = grep { $_->path =~ /[.]html?$/ } @pages;
my $tmpl = $self->theme->template( site => 'sitemap.xml' );
$store->write_file( 'sitemap.xml', $tmpl->render( site => $self, pages => \@indexed_pages ) );
my $sitemap = Statocles::Page::Plain->new(
path => '/sitemap.xml',
content => $tmpl->render( site => $self, pages => \@indexed_pages ),
);
push @pages, $sitemap;
$store->write_file( 'sitemap.xml', $sitemap->render );

# robots.txt is the best way for crawlers to automatically discover sitemap.xml
# We should do more with this later...
my $robots_tmpl = $self->theme->template( site => 'robots.txt' );
$store->write_file( 'robots.txt', $robots_tmpl->render( site => $self ) );
my $robots = Statocles::Page::Plain->new(
path => '/robots.txt',
content => $robots_tmpl->render( site => $self ),
);
push @pages, $robots;
$store->write_file( 'robots.txt', $robots->render );

# Add the theme
my $theme_iter = $self->theme->store->find_files();
while ( my $theme_file = $theme_iter->() ) {
my $fh = $self->theme->store->open_file( $theme_file );
push @pages, Statocles::Page::File->new(
path => join( '/', '', 'theme', $theme_file ),
fh => $fh,
);
$store->write_file( Path::Tiny->new( 'theme', $theme_file ), $fh );
}

$self->emit( build => class => 'Statocles::Event::Pages', pages => \@pages );

return;
}

=method deploy
Expand Down
33 changes: 33 additions & 0 deletions t/site/events.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

use Statocles::Base 'Test';
my $SHARE_DIR = path( __DIR__, '..', 'share' );

subtest 'build events' => sub {
my ( $site, $build_dir, $deploy_dir ) = build_test_site_apps( $SHARE_DIR );
my ( $event );
$site->on( 'build', sub {
pass "Build event fired during build";
( $event ) = @_;
} );

$site->build;

isa_ok $event, 'Statocles::Event::Pages';
ok scalar @{ $event->pages }, 'got some pages';
cmp_deeply $event->pages,
array_each(
methods( path => re( qr{^/} ) )
),
'all pages are absolute';

cmp_deeply $event->pages,
superbagof(
methods( path => re( qr{^\Q/theme/site/layout.html.ep} ) ),
methods( path => re( qr{^\Q/robots.txt} ) ),
methods( path => re( qr{^\Q/sitemap.xml} ) ),
),
'page paths are correct';

};

done_testing;

0 comments on commit 473566b

Please sign in to comment.