Skip to content

Commit

Permalink
generate the site config from the template
Browse files Browse the repository at this point in the history
This makes the comments remain when the site is created, and should help
a bit with getting people started.

Fixes #324
  • Loading branch information
preaction committed Aug 22, 2015
1 parent 658da79 commit e0cf8d6
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 27 deletions.
48 changes: 34 additions & 14 deletions lib/Statocles/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -252,31 +252,50 @@ sub create_site {
my $cwd = cwd;
my $root = Path::Tiny->new( $site_root );
$root->mkpath;
my ( $site ) = YAML::Load( $create_dir->child( 'site.yml' )->slurp_utf8 );
my $config_tmpl = Statocles::Template->new(
path => $create_dir->child( 'site.yml' ),
);
my %vars;

if ( $answer{flavor} == 1 ) {
$site->{site}{args}{index} = "/blog";
$site->{site}{args}{nav}{main}[0]{href} = "/";
$vars{site}{index} = "/blog";
$vars{site}{nav}{main}[0] = {
href => "/",
text => "Blog",
};
}
elsif ( $answer{flavor} == 2 ) {
$site->{site}{args}{index} = "/page";
$site->{site}{args}{nav}{main}[0]{href} = "/blog";
$vars{site}{index} = "/page";
$vars{site}{nav}{main}[0] = {
href => "/blog",
text => "Blog",
};
}
else {
$vars{site}{index} = "/blog";
$vars{site}{nav}{main}[0] = {
href => "/",
text => "Blog",
};
}

if ( lc $answer{bundle_theme} eq 'y' ) {
chdir $root;
$self->bundle_theme( 'default', 'theme' );
chdir $cwd;
$site->{theme}{args}{store} = 'theme';
$vars{theme}{args}{store} = 'theme';
}
else {
$vars{theme}{args}{store} = '::default';
}

if ( $answer{base_url} ) {
$site->{site}{args}{base_url} = $answer{base_url};
$vars{site}{base_url} = $answer{base_url};
}

if ( $answer{deploy_class} == 1 ) {
$site->{deploy}{class} = 'Statocles::Deploy::Git';
$site->{deploy}{args}{branch} = $answer{git_branch};
$vars{deploy}{class} = 'Statocles::Deploy::Git';
$vars{deploy}{args}{branch} = $answer{git_branch};

# Create the git repo
require Git::Repository;
Expand All @@ -288,16 +307,17 @@ sub create_site {
$root->child( '.gitignore' )->append( "\n.statocles\n" );
}
elsif ( $answer{deploy_class} == 2 ) {
$site->{deploy}{class} = 'Statocles::Deploy::File';
$site->{deploy}{args}{path} = $answer{deploy_path};
$vars{deploy}{class} = 'Statocles::Deploy::File';
$vars{deploy}{args}{path} = $answer{deploy_path};
}
else {
# We need a deploy in order to create a Site object
$site->{deploy}{class} = 'Statocles::Deploy::File';
$site->{deploy}{args}{path} = '.';
$vars{deploy}{class} = 'Statocles::Deploy::File';
$vars{deploy}{args}{path} = '.';
}

$root->child( 'site.yml' )->spew_utf8( YAML::Dump( $site ) );
$root->child( 'site.yml' )->spew_utf8( $config_tmpl->render( %vars ) );
my ( $site ) = YAML::Load( $root->child( 'site.yml' )->slurp_utf8 );

# Make required store directories
for my $app ( map { $_->{'$ref'} } values %{ $site->{site}{args}{apps} } ) {
Expand Down
28 changes: 22 additions & 6 deletions share/create/site.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

### Generated by Statocles version <%= $Statocles::VERSION %>

# This is the site configuration file. The format is YAML, and the config is
# read by Beam::Wire, a dependency-injection library that builds complete
# objects. For full details on how to edit this file, see
Expand Down Expand Up @@ -37,6 +39,15 @@ site:
# title: This is the title of the site, shown in the <title> tag
title: 'My Statocles Site'

# base_url: This is the base URL the site will be deployed to. It
# should be a full URL, and may contain a path, like:
#
# http://example.com/username
#
# If the base_url contains a path, all internal links will be
# rewritten appropriately.
base_url: '<%= $site->{base_url} %>'

# apps: These are the applications in this site. The name of the app,
# "blog", "page", or "static", is used to refer to it in commands.
apps:
Expand All @@ -59,7 +70,7 @@ site:
$ref: 'theme'

# index: The path to the page that should be used for the site index.
index: '/blog'
index: '<%= $site->{index} %>'

# nav: These are lists of navigation links used by the theme.
# Navigations are used to move between applications, or to go to
Expand All @@ -73,8 +84,10 @@ site:
# "text", the text of the link, and "href", the URL to link to.
nav:
main:
- text: 'Blog'
href: '/'
% for my $link ( @{ $site->{nav}{main} // [] } ) {
- text: '<%= $link->{text} %>'
href: '<%= $link->{href} %>'
% }

# deploy: This is the deploy object, used to deploy this site.
deploy:
Expand All @@ -92,13 +105,16 @@ theme:
#
# Themes have a special syntax to refer to themes that come bundled with
# Statocles. See Statocles::Help::Theme for more information
store: '::default'
store: '<%= $theme->{args}{store} %>'

# deploy: These are the settings for the site's deploy. See Statocles::Deploy::File
# for a full list of attributes and what they do.
deploy:
class: ~
args: ~
class: '<%= $deploy->{class} %>'
args:
% for my $k ( keys %{ $deploy->{args} } ) {
<%= $k %>: '<%= $deploy->{args}{$k} %>'
% }

#----------------------------------------------------------------------------
# Applications
Expand Down
35 changes: 28 additions & 7 deletions t/command/create.t
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ subtest 'create a site' => sub {
$expect_config->{deploy}{args}{path} = ".";

ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'example.com', 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -84,10 +87,13 @@ subtest 'create a site' => sub {
$expect_config->{deploy}{args}{path} = '.';

ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'example.com', 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -128,10 +134,13 @@ subtest 'create a site' => sub {
$expect_config->{deploy}{args}{path} = ".";

ok $tmp->child( 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -171,10 +180,13 @@ subtest 'create a site' => sub {
$expect_config->{deploy}{args}{path} = ".";

ok $tmp->child( 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -212,10 +224,13 @@ subtest 'create a site' => sub {
$expect_config->{deploy}{args}{path} = '.';

ok $tmp->child( 'example.com', 'site.yml' )->is_file, 'site.yml file exists';
my $config = $tmp->child( 'example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'example.com', 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -274,10 +289,13 @@ subtest 'git deploy' => sub {
$expect_config->{deploy}{args}{branch} = 'master';
$expect_config->{theme}{args}{store} = 'theme';

my $config = $tmp->child( 'www.example.com', 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'www.example.com', 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'www.example.com', 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'www.example.com', 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down Expand Up @@ -326,10 +344,13 @@ subtest 'git deploy' => sub {
$expect_config->{deploy}{args}{branch} = 'master';
$expect_config->{theme}{args}{store} = 'theme';

my $config = $tmp->child( 'site.yml' )->slurp_utf8;
cmp_deeply
YAML::Load( $tmp->child( 'site.yml' )->slurp_utf8 ),
YAML::Load( $config ),
$expect_config,
'config is complete and correct';
like $config, qr{Generated by Statocles version $Statocles::VERSION},
'tagged with version to test that comments survive';

ok $tmp->child( 'blog' )->is_dir, 'blog dir exists';
eq_or_diff $tmp->child( 'blog', @date_parts, 'first-post', 'index.markdown' )->slurp,
Expand Down

0 comments on commit e0cf8d6

Please sign in to comment.