HiD/App/Command/publish.pm

filter: Statementstmt 56.3% 53 / 94 Branchbran 10.8% 5 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 12 / 16 Podpod 100% 1 / 1 totaltotal 45.2% 71 / 157 CC 26 SCAR 46.0
CC
26
Cov
41%
_run
180.0
_create_gh_pages_if_needed_and_switch_branch
90.0
_build_gw
2.0
CRAP 99.2
↑ Summary HiD/Server.pm »
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
BranchTrueFalseTotal
if ($self->clean) 0 15 15
90 0 $config->{clean_destination} = 1;
91 }
92  
93 15 ▶ if ( $self->limit_posts ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->limit_posts) 0 15 15
94 0 $config->{limit_posts} = $self->limit_posts;
95 }
96  
97 15 ▶ if ( $self->publish_drafts ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->publish_drafts) 0 15 15
98 0 $config->{publish_drafts} = 1;
99 }
100  
101 15 ▶ if ( $self->to_github_pages ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_github_pages) 0 15 15
102 0 ▶ say( "PUBLISHING TO gh-pages BRANCH" ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
103  
104 0 ▶ $self->FATAL( "Not in the root level of a Git repo" )
Branch: 1 branch
BranchTrueFalseTotal
unless -e -d ".git" 0 0 0
105 unless -e -d '.git';
106  
107 0 ▶ $self->FATAL( "Git::Wrapper is required for the '--to-github-pages' option" )
Branch: 1 branch
BranchTrueFalseTotal
unless try_load_class("Git::Wrapper") 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
113 }
114  
115 15 $self->publish();
116  
117 13 ▶ if ( $self->to_github_pages ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_github_pages) 0 13 13
118  
119 0 ▶ say( "Storing current branch" ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if ($self->status->is_dirty) { } 0 0 0
134 0 ▶ say( "Committing files and pushing" ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
144 }
145  
146 # and go back to the starting branch
147 0 ▶ say( "Restoring previous branch" ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if (grep {$_ eq "gh-pages";} map({s/\*? ?//u; $_;} $self->branch)) 0 0 0
159 0 ▶ say( "* Checking out existing gh-pages branch" ) if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
160 0 ▶ $self->checkout( 'gh-pages' )
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_get_current_branch eq "gh-pages" 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
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
BranchTrueFalseTotal
if $self->verbose 0 0 0
174 0 foreach ( path('.')->children() ) {
175 # skip over .git*
176 0 ▶ next if /^.git/;
Branch: 1 branch
BranchTrueFalseTotal
if /^.git/u 0 0 0
177  
178 0 ▶ $_->is_dir() ? $_->remove_tree() : $_->remove()
Branch: 1 branch
BranchTrueFalseTotal
$_->is_dir ? : 0 0 0
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
↑ Summary HiD/Server.pm »