| 1 | package Statocles::Command::create; | ||||||||||||||||||||
| 2 | 2 | our $VERSION = '0.099'; | |||||||||||||||||||
| 3 | # ABSTRACT: The command to create new Statocles site | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use Statocles::Base 'Command'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use Statocles::Command::bundle; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use Statocles::Template; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use File::Share qw( dist_dir ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 7 | ▶ | sub run { | ||||||||||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 11 | 7 | my ( $self, @argv ) = @_; | |||||||||||||||||||
| 12 | 7 | $self->create_site( \@argv ); | |||||||||||||||||||
| 13 | } | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 7 | ▶ | sub create_site { | ||||||||||||||||||
|
Subroutine
create_site: called CC=26 SCAR=33.1
Pod
undocumented
| |||||||||||||||||||||
| 16 | 7 | my ( $self, $argv ) = @_; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 7 | my %answer; | |||||||||||||||||||
| 19 | 7 | my $site_root = '.'; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | # Allow the user to set the base URL and the site folder as an argument | ||||||||||||||||||||
| 22 | 7 | ▶ | if ( @$argv ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 23 | 5 | my $base = $argv->[0]; | |||||||||||||||||||
| 24 | 5 | ▶ | if ( $base =~ m{^https?://(.+)} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 25 | 3 | $answer{base_url} = $base; | |||||||||||||||||||
| 26 | 3 | ▶ | $site_root = $argv->[1] || $1; | ||||||||||||||||||
|
Condition66%2/3
$argv->[1] || $1
MC/DC0%0/2
$argv->[1] || $1
$argv->[1]
$1
Truth table66%2/3
A || B
A= $argv->[1]B= $1
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | else { | ||||||||||||||||||||
| 29 | 2 | $answer{base_url} = "http://$base"; | |||||||||||||||||||
| 30 | 2 | ▶ | $site_root = $argv->[1] || $base; | ||||||||||||||||||
|
Condition33%1/3
$argv->[1] || $base
MC/DC0%0/2
$argv->[1] || $base
$argv->[1]
$base
Truth table33%1/3
A || B
A= $argv->[1]B= $base
| |||||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 7 | my $create_dir = Path::Tiny->new( dist_dir( 'Statocles' ) )->child( 'create' ); | |||||||||||||||||||
| 35 | 7 | my $question = YAML::Load( $create_dir->child( 'script.yml' )->slurp_utf8 ); | |||||||||||||||||||
| 36 | 7 | my %prompt = ( | |||||||||||||||||||
| 37 | flavor => 'Which flavor of site would you like? ([1], 2, 0)', | ||||||||||||||||||||
| 38 | bundle_theme => 'Do you want to bundle the theme? ([Y]/n)', | ||||||||||||||||||||
| 39 | base_url => 'What is the URL where the site will be deployed?', | ||||||||||||||||||||
| 40 | deploy_class => 'How would you like to deploy? ([1], 2, 0)', | ||||||||||||||||||||
| 41 | git_branch => 'What branch? [main]', | ||||||||||||||||||||
| 42 | deploy_path => 'Where to deploy the site? (default: current directory)', | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 7 | print "\n", $question->{flavor}; | |||||||||||||||||||
| 46 | 7 | print "\n", "\n", $prompt{flavor}, " "; | |||||||||||||||||||
| 47 | 7 | chomp( $answer{flavor} = <STDIN> ); | |||||||||||||||||||
| 48 | 7 | ▶ | until ( $answer{flavor} =~ /^[120]*$/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | print $prompt{flavor}, " "; | |||||||||||||||||||
| 50 | 0 | chomp( $answer{flavor} = <STDIN> ); | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | 7 | ▶ | $answer{flavor} = 1 if $answer{flavor} eq ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 7 | print "\n", "\n", $question->{bundle_theme}; | |||||||||||||||||||
| 55 | 7 | print "\n", "\n", $prompt{bundle_theme}, " "; | |||||||||||||||||||
| 56 | 7 | chomp( $answer{bundle_theme} = <STDIN> ); | |||||||||||||||||||
| 57 | 7 | ▶ | until ( $answer{bundle_theme} =~ /^[yn]*$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 0 | print $prompt{bundle_theme}, " "; | |||||||||||||||||||
| 59 | 0 | chomp( $answer{bundle_theme} = <STDIN> ); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 7 | ▶ | $answer{bundle_theme} = "y" if $answer{bundle_theme} eq ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 7 | ▶ | if ( !$answer{base_url} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 2 | print "\n", "\n", $question->{base_url}; | |||||||||||||||||||
| 65 | 2 | print "\n", "\n", $prompt{base_url}, " "; | |||||||||||||||||||
| 66 | 2 | chomp( $answer{base_url} = <STDIN> ); | |||||||||||||||||||
| 67 | 2 | ▶ | if ( $answer{base_url} !~ m{^https?://} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 68 | 1 | $answer{base_url} = "http://$answer{base_url}"; | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 7 | print "\n", "\n", $question->{deploy_class}; | |||||||||||||||||||
| 73 | 7 | print "\n", "\n", $prompt{deploy_class}, " "; | |||||||||||||||||||
| 74 | 7 | chomp( $answer{deploy_class} = <STDIN> ); | |||||||||||||||||||
| 75 | 7 | ▶ | until ( $answer{deploy_class} =~ /^[120]*$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | 0 | print $prompt{deploy_class}, " "; | |||||||||||||||||||
| 77 | 0 | chomp( $answer{deploy_class} = <STDIN> ); | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | 7 | ▶ | $answer{deploy_class} = 1 if $answer{deploy_class} eq ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 7 | ▶ | if ( $answer{deploy_class} == 1 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 82 | # Git deploy questions | ||||||||||||||||||||
| 83 | 2 | print "\n", "\n", $question->{git_branch}; | |||||||||||||||||||
| 84 | 2 | print "\n", "\n", $prompt{git_branch}, " "; | |||||||||||||||||||
| 85 | 2 | chomp( $answer{git_branch} = <STDIN> ); | |||||||||||||||||||
| 86 | 2 | ▶ | $answer{git_branch} ||= "main"; | ||||||||||||||||||
|
Condition50%1/2
$answer{"git_branch"} ||= "main"
MC/DC0%0/1
$answer{"git_branch"} ||= "main"
$answer{"git_branch"}
Truth table50%1/2
A
A= $answer{"git_branch"}
| |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||
| 88 | elsif ( $answer{deploy_class} == 2 ) { | ||||||||||||||||||||
| 89 | # File deploy questions | ||||||||||||||||||||
| 90 | 3 | print "\n", "\n", $question->{deploy_path}; | |||||||||||||||||||
| 91 | 3 | print "\n", "\n", $prompt{deploy_path}, " "; | |||||||||||||||||||
| 92 | 3 | chomp( $answer{deploy_path} = <STDIN> ); | |||||||||||||||||||
| 93 | 3 | ▶ | $answer{deploy_path} ||= '.'; | ||||||||||||||||||
|
Condition50%1/2
$answer{"deploy_path"} ||= "."
MC/DC0%0/1
$answer{"deploy_path"} ||= "."
$answer{"deploy_path"}
Truth table50%1/2
A
A= $answer{"deploy_path"}
| |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | ### Build the site | ||||||||||||||||||||
| 97 | 7 | my $cwd = cwd; | |||||||||||||||||||
| 98 | 7 | my $root = Path::Tiny->new( $site_root ); | |||||||||||||||||||
| 99 | 7 | $root->mkpath; | |||||||||||||||||||
| 100 | 7 | my $config_tmpl = Statocles::Template->new( | |||||||||||||||||||
| 101 | path => $create_dir->child( 'site.yml' ), | ||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||
| 103 | 7 | my %vars; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 7 | ▶ | if ( $answer{flavor} == 1 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 106 | 2 | $vars{site}{index} = "/blog"; | |||||||||||||||||||
| 107 | 2 | $vars{site}{nav}{main}[0] = { | |||||||||||||||||||
| 108 | href => "/", | ||||||||||||||||||||
| 109 | text => "Blog", | ||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | elsif ( $answer{flavor} == 2 ) { | ||||||||||||||||||||
| 113 | 3 | $vars{site}{index} = "/"; | |||||||||||||||||||
| 114 | 3 | $vars{site}{nav}{main}[0] = { | |||||||||||||||||||
| 115 | href => "/blog", | ||||||||||||||||||||
| 116 | text => "Blog", | ||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||
| 120 | 2 | $vars{site}{index} = "/blog"; | |||||||||||||||||||
| 121 | 2 | $vars{site}{nav}{main}[0] = { | |||||||||||||||||||
| 122 | href => "/", | ||||||||||||||||||||
| 123 | text => "Blog", | ||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 7 | ▶ | if ( lc $answer{bundle_theme} eq 'y' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 2 | chdir $root; | |||||||||||||||||||
| 129 | 2 | Statocles::Command::bundle->bundle_theme( 'default', 'theme' ); | |||||||||||||||||||
| 130 | 2 | chdir $cwd; | |||||||||||||||||||
| 131 | 2 | $vars{theme}{store} = 'theme'; | |||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | else { | ||||||||||||||||||||
| 134 | 5 | $vars{theme}{store} = '::default'; | |||||||||||||||||||
| 135 | } | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 7 | ▶ | if ( $answer{base_url} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 7 | $vars{site}{base_url} = $answer{base_url}; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 7 | ▶ | if ( $answer{deploy_class} == 1 ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 142 | 2 | $vars{deploy}{'$class'} = 'Statocles::Deploy::Git'; | |||||||||||||||||||
| 143 | 2 | $vars{deploy}{branch} = $answer{git_branch}; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | # Create the git repo | ||||||||||||||||||||
| 146 | 2 | require Git::Repository; | |||||||||||||||||||
| 147 | # Running init more than once is apparently completely safe, so we don't | ||||||||||||||||||||
| 148 | # even have to check before we run it | ||||||||||||||||||||
| 149 | 2 | chdir $root; | |||||||||||||||||||
| 150 | 2 | Git::Repository->run( 'init', '-q' ); | |||||||||||||||||||
| 151 | 2 | chdir $cwd; | |||||||||||||||||||
| 152 | 2 | $root->child( '.gitignore' )->append( "\n.statocles\n" ); | |||||||||||||||||||
| 153 | } | ||||||||||||||||||||
| 154 | elsif ( $answer{deploy_class} == 2 ) { | ||||||||||||||||||||
| 155 | 3 | $vars{deploy}{'$class'} = 'Statocles::Deploy::File'; | |||||||||||||||||||
| 156 | 3 | $vars{deploy}{path} = $answer{deploy_path}; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | else { | ||||||||||||||||||||
| 159 | # We need a deploy in order to create a Site object | ||||||||||||||||||||
| 160 | 2 | $vars{deploy}{'$class'} = 'Statocles::Deploy::File'; | |||||||||||||||||||
| 161 | 2 | $vars{deploy}{path} = '.'; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 7 | $root->child( 'site.yml' )->spew_utf8( $config_tmpl->render( %vars ) ); | |||||||||||||||||||
| 165 | 7 | my ( $site ) = YAML::Load( $root->child( 'site.yml' )->slurp_utf8 ); | |||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | # Make required store directories | ||||||||||||||||||||
| 168 | 7 | for my $app ( map { $_->{'$ref'} } values %{ $site->{site}{apps} } ) { | |||||||||||||||||||
| 169 | 7 | my $path = $site->{$app}{store}; | |||||||||||||||||||
| 170 | 7 | ▶ | next unless $path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 171 | 0 | $root->child( $path )->mkpath; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | ### Copy initial site content | ||||||||||||||||||||
| 175 | # Blog | ||||||||||||||||||||
| 176 | 7 | ▶ | if ( my $ref = $site->{site}{apps}{blog} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 7 | my $path = $site->{blog_app}{url_root}; | |||||||||||||||||||
| 178 | 7 | my ( undef, undef, undef, $day, $mon, $year ) = localtime; | |||||||||||||||||||
| 179 | 7 | $year += 1900; | |||||||||||||||||||
| 180 | 7 | $mon += 1; | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 7 | my @date_parts = ( | |||||||||||||||||||
| 183 | sprintf( '%04i', $year ), | ||||||||||||||||||||
| 184 | sprintf( '%02i', $mon ), | ||||||||||||||||||||
| 185 | sprintf( '%02i', $day ), | ||||||||||||||||||||
| 186 | ); | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 7 | my $post_path = $root->child( $path, @date_parts, 'first-post', 'index.markdown' ); | |||||||||||||||||||
| 189 | 7 | $post_path->parent->mkpath; | |||||||||||||||||||
| 190 | 7 | $create_dir->child( 'blog', 'post.markdown' )->copy( $post_path ); | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | 7 | my $page_path = $root->child( 'index.markdown' ); | |||||||||||||||||||
| 193 | 7 | $page_path->parent->mkpath; | |||||||||||||||||||
| 194 | 7 | $create_dir->child( 'page', 'index.markdown' )->copy( $page_path ); | |||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | ### DONE! | ||||||||||||||||||||
| 197 | 7 | print "\n", "\n", $question->{finish}, "\n", "\n"; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | 7 | return 0; | |||||||||||||||||||
| 200 | } | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | 2 | 1; | |||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | __END__ | ||||||||||||||||||||