| 1 | # ABSTRACT: publish site | ||||||||||
| 2 | |||||||||||
| 3 | |||||||||||
| 4 | package HiD::App::Command::publish; | ||||||||||
| 5 | 5 | our $AUTHORITY = 'cpan:GENEHACK'; | |||||||||
| 6 | 5 | $HiD::App::Command::publish::VERSION = '1.96'; | |||||||||
| 7 | 5 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 5 | extends 'HiD::App::Command'; | |||||||||
| 9 | 5 | with 'HiD::Role::PublishesDrafts'; | |||||||||
| 10 | 5 | with 'HiD::Role::DoesLogging'; | |||||||||
| 11 | 5 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 5 | ▶ | use 5.014; # strict, unicode_strings | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 5 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 5 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 5 | ▶ | use warnings qw/ FATAL utf8 /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 5 | ▶ | use open qw/ :std :utf8 /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 5 | ▶ | use charnames qw/ :full /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 5 | ▶ | use Class::Load qw/ try_load_class /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 5 | ▶ | use File::Copy qw/ move /; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 5 | ▶ | use Path::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 5 | has clean => ( | |||||||||
| 26 | is => 'ro' , | ||||||||||
| 27 | isa => 'Bool' , | ||||||||||
| 28 | cmd_aliases => 'C' , | ||||||||||
| 29 | traits => [ 'Getopt' ] , | ||||||||||
| 30 | ); | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | 5 | has limit_posts => ( | |||||||||
| 34 | is => 'ro' , | ||||||||||
| 35 | isa => 'Int' , | ||||||||||
| 36 | cmd_aliases => 'l' , | ||||||||||
| 37 | traits => [ 'Getopt' ] , | ||||||||||
| 38 | ); | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | 5 | has to_github_pages => ( | |||||||||
| 42 | is => 'ro' , | ||||||||||
| 43 | isa => 'Bool' , | ||||||||||
| 44 | cmd_aliases => 'G' , | ||||||||||
| 45 | traits => [ 'Getopt' ] | ||||||||||
| 46 | ); | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 5 | has verbose => ( | |||||||||
| 50 | is => 'ro' , | ||||||||||
| 51 | isa => 'Bool' , | ||||||||||
| 52 | cmd_aliases => 'v' , | ||||||||||
| 53 | traits => [ 'Getopt' ] , | ||||||||||
| 54 | default => 0 , | ||||||||||
| 55 | ); | ||||||||||
| 56 | |||||||||||
| 57 | # internal attributes | ||||||||||
| 58 | |||||||||||
| 59 | 5 | has gw => ( | |||||||||
| 60 | is => 'ro' , | ||||||||||
| 61 | isa => 'Git::Wrapper' , | ||||||||||
| 62 | lazy => 1 , | ||||||||||
| 63 | builder => '_build_gw' , | ||||||||||
| 64 | traits => [ 'NoGetopt' ] , | ||||||||||
| 65 | handles => [ qw/ | ||||||||||
| 66 | add | ||||||||||
| 67 | branch | ||||||||||
| 68 | checkout | ||||||||||
| 69 | commit | ||||||||||
| 70 | push | ||||||||||
| 71 | rev_parse | ||||||||||
| 72 | status | ||||||||||
| 73 | / ] , | ||||||||||
| 74 | ); | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub _build_gw { Git::Wrapper->new('.') } | ||||||||
|
Subroutine
_build_gw: not called CC=1 SCAR=6.9
| |||||||||||
| 77 | |||||||||||
| 78 | 5 | has saved_branch => ( | |||||||||
| 79 | is => 'rw' , | ||||||||||
| 80 | isa => 'Str' , | ||||||||||
| 81 | writer => 'set_saved_branch' , | ||||||||||
| 82 | traits => [ 'NoGetopt' ] , | ||||||||||
| 83 | ); | ||||||||||
| 84 | |||||||||||
| 85 | sub _run { | ||||||||||
| 86 | 15 | ▶ | my( $self , $opts , $args ) = @_; | ||||||||
|
Subroutine
_run: called CC=18 SCAR=51.9
| |||||||||||
| 87 | |||||||||||
| 88 | 15 | my $config = $self->config; | |||||||||
| 89 | 15 | ▶ | if ( $self->clean ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | 0 | $config->{clean_destination} = 1; | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | 15 | ▶ | if ( $self->limit_posts ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | $config->{limit_posts} = $self->limit_posts; | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | 15 | ▶ | if ( $self->publish_drafts ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 98 | 0 | $config->{publish_drafts} = 1; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 15 | ▶ | if ( $self->to_github_pages ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | 0 | ▶ | say( "PUBLISHING TO gh-pages BRANCH" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | |||||||||||
| 104 | 0 | ▶ | $self->FATAL( "Not in the root level of a Git repo" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | unless -e -d '.git'; | ||||||||||
| 106 | |||||||||||
| 107 | 0 | ▶ | $self->FATAL( "Git::Wrapper is required for the '--to-github-pages' option" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | unless try_load_class( 'Git::Wrapper' ); | ||||||||||
| 109 | |||||||||||
| 110 | # publish into a tempdir... | ||||||||||
| 111 | 0 | $config->{destination} = Path::Tiny->tempdir->stringify(); | |||||||||
| 112 | 0 | ▶ | say( "*** destination set to $config->{destination}" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 15 | $self->publish(); | |||||||||
| 116 | |||||||||||
| 117 | 13 | ▶ | if ( $self->to_github_pages ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | |||||||||||
| 119 | 0 | ▶ | say( "Storing current branch" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | $self->set_saved_branch( $self->get_current_branch() ); | |||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | say( "Switching to gh_pages branch (creating if needed)" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | $self->_create_gh_pages_if_needed_and_switch_branch(); | |||||||||
| 124 | |||||||||||
| 125 | # move stuff out of destination (which is a tempdir at this point, | ||||||||||
| 126 | # remember) into the current dir | ||||||||||
| 127 | 0 | ▶ | say( "Moving published files to current directory" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | my $d = path( $self->config->{destination} ); | |||||||||
| 129 | ## use File::Copy::move b/c Path::Tiny::move is mud spelled backwards | ||||||||||
| 130 | 0 | move( $_ , './' ) foreach ( $d->children() ); | |||||||||
| 131 | |||||||||||
| 132 | # add everything, commit, and push | ||||||||||
| 133 | 0 | ▶ | if ( $self->status->is_dirty() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | say( "Committing files and pushing" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | |||||||||||
| 136 | 0 | $self->add( '.' ); | |||||||||
| 137 | ### FIXME include the date or something. | ||||||||||
| 138 | ### or have a '--message/-m' option and some sensible default | ||||||||||
| 139 | 0 | $self->commit( "-m" => "Published to GitHub pages by HiD!" ); | |||||||||
| 140 | 0 | $self->push( '-u' ); | |||||||||
| 141 | } | ||||||||||
| 142 | else { | ||||||||||
| 143 | 0 | ▶ | say( "No changes to commit." ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | # and go back to the starting branch | ||||||||||
| 147 | 0 | ▶ | say( "Restoring previous branch" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | $self->checkout( $self->saved_branch ); | |||||||||
| 149 | } | ||||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | sub _create_gh_pages_if_needed_and_switch_branch { | ||||||||||
| 153 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_create_gh_pages_if_needed_and_switch_branch: not called CC=9 SCAR=45.0
| |||||||||||
| 154 | |||||||||||
| 155 | # do we already have 'gh-pages' ? | ||||||||||
| 156 | ## 'branch' output is either '* NAME' or ' NAME', so strip that | ||||||||||
| 157 | ### FIXME can we require 5.20 and then switch to s///r ? | ||||||||||
| 158 | 0 | ▶ | if ( grep { $_ eq 'gh-pages' } map { s/\*? ?// ; $_ } $self->branch() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 159 | 0 | ▶ | say( "* Checking out existing gh-pages branch" ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 160 | 0 | ▶ | $self->checkout( 'gh-pages' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | unless $self->_get_current_branch eq 'gh-pages'; | ||||||||||
| 162 | |||||||||||
| 163 | 0 | return 1; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | # otherwise, let's set it up | ||||||||||
| 167 | 0 | ▶ | say( '* Creating gh-pages branch' ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | |||||||||||
| 169 | # make the orphan branch | ||||||||||
| 170 | 0 | $self->checkout( '--orphan' => 'gh-pages' ); | |||||||||
| 171 | |||||||||||
| 172 | # clean out all files already there | ||||||||||
| 173 | 0 | ▶ | say( '* Cleaning out existing files' ) if $self->verbose; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 0 | foreach ( path('.')->children() ) { | |||||||||
| 175 | # skip over .git* | ||||||||||
| 176 | 0 | ▶ | next if /^.git/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | |||||||||||
| 178 | 0 | ▶ | $_->is_dir() ? $_->remove_tree() : $_->remove() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | # and we're good to go | ||||||||||
| 182 | 0 | return 1; | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | |||||||||||
| 186 | # this is required by DoesLogging -- but having an empty hash for it works | ||||||||||
| 187 | # just as well. | ||||||||||
| 188 | 0 | ▶ | sub get_config { {} } | ||||||||
|
Subroutine
get_config: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 189 | |||||||||||
| 190 | 0 | ▶ | sub _get_current_branch { shift->rev_parse( '--abbrev-ref' => 'HEAD' ) } | ||||||||
|
Subroutine
_get_current_branch: not called CC=1 SCAR=6.9
| |||||||||||
| 191 | |||||||||||
| 192 | 5 | __PACKAGE__->meta->make_immutable; | |||||||||
| 193 | 5 | 1; | |||||||||
| 194 | |||||||||||
| 195 | __END__ | ||||||||||
| 196 | |||||||||||
| 197 | =pod | ||||||||||
| 198 | |||||||||||
| 199 | =encoding UTF-8 | ||||||||||
| 200 | |||||||||||
| 201 | =head1 NAME | ||||||||||
| 202 | |||||||||||
| 203 | HiD::App::Command::publish - publish site | ||||||||||
| 204 | |||||||||||
| 205 | =head1 SYNOPSIS | ||||||||||
| 206 | |||||||||||
| 207 | $ hid publish | ||||||||||
| 208 | |||||||||||
| 209 | $ hid # 'publish' is the default command... | ||||||||||
| 210 | |||||||||||
| 211 | # publish directly to GitHub Pages | ||||||||||
| 212 | $ hid publish --to_github_pages | ||||||||||
| 213 | $ hid publist -G | ||||||||||
| 214 | |||||||||||
| 215 | =head1 DESCRIPTION | ||||||||||
| 216 | |||||||||||
| 217 | Processes files according to the active configuration and writes output files | ||||||||||
| 218 | accordingly. | ||||||||||
| 219 | |||||||||||
| 220 | =head1 ATTRIBUTES | ||||||||||
| 221 | |||||||||||
| 222 | =head2 clean | ||||||||||
| 223 | |||||||||||
| 224 | Remove any existing site directory prior to the publication run | ||||||||||
| 225 | |||||||||||
| 226 | =head2 limit_posts | ||||||||||
| 227 | |||||||||||
| 228 | Limit the number of blog posts that will be written out. If you have a large | ||||||||||
| 229 | number of blog posts that haven't changed, setting this can significantly | ||||||||||
| 230 | speed up the publication process. | ||||||||||
| 231 | |||||||||||
| 232 | =head2 to_github_pages | ||||||||||
| 233 | |||||||||||
| 234 | If this option is set, the publishing process will switch to a 'gh-pages' | ||||||||||
| 235 | branch in the current repository. If such a branch does not exist, a new | ||||||||||
| 236 | "orphan" branch of that name will be created. Publication will happen in the | ||||||||||
| 237 | normal destination directory, and then files will be moved into the root level | ||||||||||
| 238 | of the repo and the destination directory removed. At the end of publication, | ||||||||||
| 239 | all pending changes will be committed and a push (specifically, 'git push -u') | ||||||||||
| 240 | will be done. Finally, the repository will be set back to whatever branch had | ||||||||||
| 241 | been checked out before. | ||||||||||
| 242 | |||||||||||
| 243 | If this option is given and the current working directory is not the root | ||||||||||
| 244 | level of a Git repository, an error will be thrown. | ||||||||||
| 245 | |||||||||||
| 246 | =head2 verbose | ||||||||||
| 247 | |||||||||||
| 248 | Be noisy. Primarily useful for debugging issues publishing to GitHub. | ||||||||||
| 249 | |||||||||||
| 250 | =head1 METHODS | ||||||||||
| 251 | |||||||||||
| 252 | =head2 get_config | ||||||||||
| 253 | |||||||||||
| 254 | Required for logging output, can be ignored by end users. | ||||||||||
| 255 | |||||||||||
| 256 | =head1 SEE ALSO | ||||||||||
| 257 | |||||||||||
| 258 | See L<HiD::App::Command> for additional command line options supported by all | ||||||||||
| 259 | sub commands. | ||||||||||
| 260 | |||||||||||
| 261 | =head1 VERSION | ||||||||||
| 262 | |||||||||||
| 263 | version 1.96 | ||||||||||
| 264 | |||||||||||
| 265 | =head1 AUTHOR | ||||||||||
| 266 | |||||||||||
| 267 | John SJ Anderson <genehack@genehack.org> | ||||||||||
| 268 | |||||||||||
| 269 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 270 | |||||||||||
| 271 | This software is copyright (c) 2015 by John SJ Anderson. | ||||||||||
| 272 | |||||||||||
| 273 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 274 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 275 | |||||||||||
| 276 | =cut | ||||||||||