Skip to content

Commit

Permalink
move build/deploy code into commands
Browse files Browse the repository at this point in the history
The site object is now responsible for generating / rewriting pages, and
the commands are responsible for writing that content somewhere. We're
trying to reduce the amount of filesystem writing we need to do,
especially in tests.

This means changes to the deploy API to pass in pages to the deploy()
method, not a store object.

The build command also now takes a directory to build to.
  • Loading branch information
preaction committed Mar 30, 2018
1 parent 8890cd9 commit 724eb9d
Show file tree
Hide file tree
Showing 30 changed files with 590 additions and 1,211 deletions.
14 changes: 14 additions & 0 deletions lib/Statocles/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ The documentation for the command-line application.
=cut

use Statocles::Base 'Class';
use YAML;
use Path::Tiny;

=attr site
Expand All @@ -39,4 +41,16 @@ has site => (
isa => InstanceOf['Statocles::Site'],
);

sub _get_status {
my ( $self, $status ) = @_;
my $path = Path::Tiny->new( '.statocles', 'status.yml' );
return {} unless $path->exists;
YAML::Load( $path->slurp_utf8 );
}

sub _write_status {
my ( $self, $status ) = @_;
Path::Tiny->new( '.statocles', 'status.yml' )->touchpath->spew_utf8( YAML::Dump( $status ) );
}

1;
16 changes: 15 additions & 1 deletion lib/Statocles/Command/build.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,21 @@ sub run {
GetOptionsFromArray( \@argv, \%build_opt,
'date|d=s',
);
$self->site->build( %build_opt );

my $path = Path::Tiny->new( $argv[0] // '.statocles/build' );
$path->mkpath;

my $store = StoreType->coercion->( $path );
#; say "Building site at " . $store->path;

# Remove all pages from the build directory first
$_->remove_tree for $store->path->children;

my @pages = $self->site->pages( %build_opt );
for my $page ( @pages ) {
$store->write_file( $page->path, $page->render );
}

return 0;
}

Expand Down
15 changes: 10 additions & 5 deletions lib/Statocles/Command/daemon.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ our $VERSION = '0.092';
# ABSTRACT: Run a daemon to navigate the site

use Statocles::Base 'Command';
use Statocles::Store;

sub run {
my ( $self, @argv ) = @_;
Expand Down Expand Up @@ -71,7 +72,10 @@ sub run {
$self->log( $self->site->log );

# First build the site
$self->site->build( %{ $self->options } );
my $path = Path::Tiny->new( '.statocles/build' );
$path->mkpath;
my $store = Statocles::Store->new( path => $path );
$store->write_file( $_->path, $_->render ) for $self->site->pages( %{ $self->options } );

my $base;
if ( $self->site->base_url ) {
Expand All @@ -86,7 +90,7 @@ sub run {

# Add the build dir to the list of static paths for mojolicious to
# search
unshift @{ $self->static->paths }, $self->site->build_store->path;
unshift @{ $self->static->paths }, $store->path;

# Watch for filesystem events and rebuild the site Right now this only
# works on OSX. We should spin this off into Mojo::IOLoop::FSEvents and
Expand Down Expand Up @@ -115,7 +119,7 @@ sub run {

require Mojo::IOLoop::Stream;
my $ioloop = Mojo::IOLoop->singleton;
my $build_dir = $self->site->build_store->path->realpath;
my $build_dir = $store->path->realpath;

weaken $self;

Expand Down Expand Up @@ -150,7 +154,8 @@ sub run {
}

if ( $rebuild ) {
$self->site->build( %{ $self->options } );
$self->site->clear_pages;
$store->write_file( $_->path, $_->render ) for $self->site->pages( %{ $self->options } );
}
} );
$ioloop->reactor->watch( $handle, 1, 0 );
Expand All @@ -175,7 +180,7 @@ sub run {
$path = Mojo::Path->new( $c->stash->{path} . "/index.html" );
$asset = $c->app->static->file( $path );
}
elsif ( $c->app->site->build_store->path->child( $path )->is_dir ) {
elsif ( $store->path->child( $path )->is_dir ) {
return $c->redirect_to( "/$path/" );
}
}
Expand Down
14 changes: 13 additions & 1 deletion lib/Statocles/Command/deploy.pm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ sub run {
'clean',
'message|m=s',
);
$self->site->deploy( %deploy_opt );

my $deploy = $self->site->deploy;
$deploy->site( $self->site );
my @pages = $self->site->pages( %deploy_opt, base_url => $deploy->base_url );

#; say "Deploying pages: " . join "\n", map { $_->path } @pages;
$deploy->deploy( \@pages, %deploy_opt );

$self->_write_status( {
last_deploy_date => time(),
last_deploy_args => \%deploy_opt,
} );

return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Statocles/Command/status.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use Statocles::Base 'Command';

sub run {
my ( $self, @argv ) = @_;
my $status = $self->site->_get_status;
my $status = $self->_get_status;
if ($status->{last_deploy_date}) {
say "Last deployed on " .
DateTime::Moonpig->from_epoch(
Expand Down
27 changes: 9 additions & 18 deletions lib/Statocles/Deploy/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ our $VERSION = '0.092';

use Statocles::Base 'Class';
with 'Statocles::Deploy';
use Statocles::Store;

=attr path
Expand All @@ -20,10 +21,9 @@ has path => (

=method deploy
my @paths = $deploy->deploy( $from_store, %options );
my @paths = $deploy->deploy( $pages, %options );
Deploy the site, copying from the given L<from_store|Statocles::Store>.
Returns the paths that were deployed.
Deploy the site, rendering the given pages.
Possible options are:
Expand All @@ -39,7 +39,7 @@ new content.
=cut

sub deploy {
my ( $self, $from_store, %options ) = @_;
my ( $self, $pages, %options ) = @_;

die sprintf 'Deploy directory "%s" does not exist (did you forget to make it?)',
$self->path
Expand All @@ -49,23 +49,14 @@ sub deploy {
$_->remove_tree for $self->path->children;
}

$self->site->log->info( "Copying files from build dir to deploy dir" );
my @files;
my $iter = $from_store->path->iterator({ recurse => 1 });
#; say "Store path: " . $from_store->path;
while ( my $path = $iter->() ) {
next if $path->is_dir;
# Git versions before 1.7.4.1 require a relative path to 'git add'
my $store = Statocles::Store->new( path => $self->path );
$self->site->log->info( "Writing pages to deploy dir" );
for my $page ( @$pages ) {
my $path = $page->path;
#; say "Path: " . $path;
my $rel_path = $path->relative( $from_store->path );
push @files, $rel_path;
#; say "From: " . $from_store->path->child( $path );
#; say "To: " . $self->path->child( $path );
$path->copy( $self->path->child( $rel_path )->touchpath );
$store->write_file( $page->path, $page->render );
}

#; say "Deploying files: " . join "; ", @files;
return @files;
}

1;
Expand Down
19 changes: 8 additions & 11 deletions lib/Statocles/Deploy/Git.pm
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ has remote => (

=method deploy
my @paths = $deploy->deploy( $from_store, %options );
my @paths = $deploy->deploy( $pages, %options );
Deploy the site, copying from the given L<from_store|Statocles::Store>.
Returns the paths that were deployed.
Deploy the site, rendering the given pages.
Possible options are:
Expand All @@ -62,7 +61,7 @@ An optional commit message to use. Defaults to a generic message.
=cut

around 'deploy' => sub {
my ( $orig, $self, $from_store, %options ) = @_;
my ( $orig, $self, $pages, %options ) = @_;

my $deploy_dir = $self->path;

Expand Down Expand Up @@ -107,7 +106,7 @@ around 'deploy' => sub {
}

# Copy the files
my @files = $self->$orig( $from_store, %options );
$self->$orig( $pages, %options );

# Check to see which files were changed
# --porcelain was added in 1.7.0
Expand All @@ -125,10 +124,10 @@ around 'deploy' => sub {
#; say Dumper \%in_status;

# Commit the files
#; say "Copied files: " . join "; ", @files;
@files = grep { $in_status{ $_ } }
map { Path::Tiny->new( $rel_path, $_ ) }
@files;
#; say "Copied files: " . join "; ", map { $_->path } @$pages;
my @files = grep { $in_status{ $_ } }
map { Path::Tiny->new( $rel_path, $_->path ) }
@$pages;
#; say "Files in git status: " . join "; ", @files;

#; say "Committing: " . Dumper \@files;
Expand All @@ -152,8 +151,6 @@ around 'deploy' => sub {

# Tidy up
$self->_run( $git, checkout => $current_branch );

return @files;
};

# Run the given git command on the given git repository, logging the
Expand Down
Loading

0 comments on commit 724eb9d

Please sign in to comment.