lib/App/Dapper/Deploy.pm

filter: Statementstmt 22.9% 34 / 148 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 64.7% 11 / 17 Podpod 100% 6 / 6 totaltotal 18.4% 51 / 276 CC 49 SCAR 53.2
CC
49
Cov
14%
github_pages
462.0
s3
156.0
run
132.0
CRAP 205.0
« lib/App/Dapper.pm ↑ Summary lib/App/Dapper/Check.pm »
1 package App::Dapper::Deploy;
2  
3 =head1 NAME
4  
5 App::Dapper::Deploy - Publish a site to Amazon S3, GitHub Pages, or anywhere.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper deploy> builds the site, checks it, and publishes the output
10 directory. Where it goes is set under C<deploy> in C<_config.yml>:
11  
12 deploy:
13 s3:
14 bucket: example.com
15 region: us-east-1 # optional
16 profile: personal # optional: an AWS CLI profile
17 cloudfront: E1A2B3C4D5E6F7 # optional: a distribution to refresh
18 cache_control: max-age=300 # optional
19 github_pages:
20 repo: git@github.com:me/site.git # default: this repository's origin
21 branch: gh-pages # the default
22 cname: www.example.com # optional
23 command: rsync -av --delete _output/ me@example.com:/var/www/site/
24  
25 With one target, C<dapper deploy> uses it; with several, name one:
26 C<dapper deploy s3>. Credentials are never stored in C<_config.yml>: S3 uses
27 the AWS CLI's own configuration (C<aws configure>), and GitHub Pages uses
28 git's.
29  
30 =over 4
31  
32 =item B<s3>
33  
34 Runs C<aws s3 sync --delete>, which uploads only files that changed and
35 removes files that are gone, then (with C<cloudfront>) asks CloudFront to
36 refresh its copies.
37  
38 =item B<github_pages>
39  
40 Commits the output to a branch (adding C<.nojekyll>, and C<CNAME> if set)
41 and pushes it. The branch keeps its history.
42  
43 =item B<command>
44  
45 Runs a command of your own. C<DAPPER_OUTPUT> holds the output directory.
46  
47 =back
48  
49 The site is not deployed if C<dapper check> finds errors, unless
50 C<--force> is given. C<--dry-run> shows what would happen without changing
51 anything. C<--no-build> deploys the output directory as it is.
52  
53 =cut
54  
55 32 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 32 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 32 ▶use File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 32 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 32 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
64 32 ▶use Cwd qw/ abs_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
65  
66 32 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 32 ▶use App::Dapper::Check;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 32 my @TARGETS = qw/ s3 github_pages command /;
70  
71 =head2 run
72  
73 Takes a Dapper object, the target to deploy to (or C<undef> to pick the only
74 one), and options (C<dry_run>, C<force>, C<no_build>). Builds, checks and
75 deploys the site.
76  
77 =cut
78  
79 0 ▶sub run {
Subroutine run: not called CC=11 SCAR=48.8
Pod documented
80 0 my ($dapper, $target, %options) = @_;
81  
82 0 $dapper->read_project();
83 0 my $settings = $dapper->{site}->{deploy};
84 0 ▶ die "error: there is nowhere to deploy to; add deploy settings to $dapper->{config} (see \"Deploying with dapper deploy\" in the README)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and %$settings 0 0 0
Condition0%0/3
ref $settings eq "HASH" and %$settings
!ll&&!rl&&r
000
MC/DC0%0/2
ref $settings eq "HASH" and %$settings
ref $settings eq "HASH" %$settings
Truth table0%0/3
A and B
A= ref $settings eq "HASH"B= %$settings
ABresult
0X0
100
111
85 unless ref $settings eq 'HASH' and %$settings;
86  
87 0 my @configured = grep { exists $settings->{$_} } @TARGETS;
88 0 my @unknown = grep { my $t = $_; !grep { $_ eq $t } @TARGETS } sort keys %$settings;
89 0 ▶ die "error: unknown deploy target \"$unknown[0]\" in $dapper->{config}; use " . join(', ', @TARGETS) . "\n" if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 0 0
90  
91 0 ▶ if (not defined $target) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $target) 0 0 0
92 0 ▶ die "error: $dapper->{config} has several deploy targets (" . join(', ', @configured)
Branch: 1 branch
BranchTrueFalseTotal
if @configured > 1 0 0 0
93 . "); say which: dapper deploy <target>\n" if @configured > 1;
94 0 $target = $configured[0];
95 }
96 0 ▶ die "error: there are no \"$target\" deploy settings in $dapper->{config}\n" unless exists $settings->{$target};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $settings->{$target} 0 0 0
97  
98 0 ▶ if (not $options{no_build}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options{"no_build"}) 0 0 0
99 0 $dapper->build();
100  
101 0 my $report = App::Dapper::Check::run($dapper);
102 0 ▶ if (not $report->{ok}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($report->{"ok"}) 0 0 0
103 0 my $errors = join('', map { " $_->{file}: $_->{message}\n" } @{$report->{errors}});
104 0 die "error: dapper check found " . scalar(@{$report->{errors}}) . " error(s), so the site was not deployed"
105 0 ▶ . " (use --force to deploy anyway):\n$errors" unless $options{force};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"force"} 0 0 0
106 0 warn "warning: deploying even though dapper check found errors:\n$errors";
107 }
108 }
109  
110 0 ▶ die "error: the output directory \"$dapper->{output}\" doesn't exist; build the site first\n" unless -d $dapper->{output};
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dapper->{"output"} 0 0 0
111  
112 0 my %deploy = (s3 => \&s3, github_pages => \&github_pages, command => \&command);
113 0 return $deploy{$target}->($dapper, $settings->{$target}, \%options);
114 }
115  
116 =head2 find_command
117  
118 Takes a command name and returns true if it is on the C<PATH>.
119  
120 =cut
121  
122 0 ▶sub find_command {
Subroutine find_command: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($name) = @_;
124 0 return scalar grep { -x catfile($_, $name) } File::Spec->path();
125 }
126  
127 =head2 run_command
128  
129 Takes a Dapper object and a command (as a list), prints it, and runs it.
130 Dies if it fails.
131  
132 =cut
133  
134 0 ▶sub run_command {
Subroutine run_command: not called CC=3 SCAR=24.8
Pod documented
135 0 my ($dapper, @command) = @_;
136  
137 0 ▶ $dapper->_log('+ ' . join(' ', map { /\s/ ? "'$_'" : $_ } @command));
Branch: 1 branch
BranchTrueFalseTotal
/\s/ ? : 0 0 0
138 0 ▶ system(@command) == 0
Branch: 1 branch
BranchTrueFalseTotal
system(@command) == 0 or die 'error: ' . join(" ", @command[0, 1]) . ' failed (exit status ' . ($? >> 8) . ")\n" 0 0 0
139 or die "error: " . join(' ', @command[0, 1]) . " failed (exit status " . ($? >> 8) . ")\n";
140 }
141  
142 =head2 s3
143  
144 Deploys to Amazon S3 with the AWS CLI.
145  
146 =cut
147  
148 0 ▶sub s3 {
Subroutine s3: not called CC=12 SCAR=50.5
Pod documented
149 0 my ($dapper, $settings, $options) = @_;
150  
151 die "error: deploy.s3 in $dapper->{config} needs a bucket\n"
152 0 ▶ unless ref $settings eq 'HASH' and defined $settings->{bucket} and length $settings->{bucket};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"} 0 0 0
Condition0%0/6
ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"}
!ll&&!rl&&r
000
ref $settings eq "HASH" and defined $settings->{"bucket"}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $settings eq "HASH" and defined $settings->{"bucket"} and length $settings->{"bucket"}
ref $settings eq "HASH" defined $settings->{"bucket"} length $settings->{"bucket"}
Truth table0%0/4
A and B and C
A= ref $settings eq "HASH"B= defined $settings->{"bucket"}C= length $settings->{"bucket"}
ABCresult
0XX0
10X0
1100
1111
153 0 ▶ die "error: deploying to S3 needs the AWS CLI (https://aws.amazon.com/cli/); install it and run \"aws configure\"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless find_command("aws") 0 0 0
154 unless find_command('aws');
155  
156 0 my @common;
157 0 ▶ push @common, '--region', $settings->{region} if defined $settings->{region};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"region"} 0 0 0
158 0 ▶ push @common, '--profile', $settings->{profile} if defined $settings->{profile};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"profile"} 0 0 0
159  
160 0 (my $bucket = $settings->{bucket}) =~ s{^s3://}{};
161 0 my @sync = ('aws', 's3', 'sync', $dapper->{output}, "s3://$bucket", '--delete', '--exclude', '.dapper-reload', @common);
162 0 ▶ push @sync, '--cache-control', $settings->{cache_control} if defined $settings->{cache_control};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"cache_control"} 0 0 0
163 0 ▶ push @sync, '--dryrun' if $options->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"dry_run"} 0 0 0
164 0 run_command($dapper, @sync);
165  
166 0 ▶ if (defined $settings->{cloudfront}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $settings->{"cloudfront"}) 0 0 0
167 my @invalidate = ('aws', 'cloudfront', 'create-invalidation', '--distribution-id', $settings->{cloudfront},
168 0 '--paths', '/*', @common);
169 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'dry_run'}) { } 0 0 0
170 0 $dapper->_log('Would run: ' . join(' ', @invalidate));
171 }
172 else {
173 0 run_command($dapper, @invalidate);
174 }
175 }
176  
177 0 ▶ $dapper->_log($options->{dry_run} ? "Dry run: nothing was uploaded." : "Deployed to s3://$bucket");
Branch: 1 branch
BranchTrueFalseTotal
$options->{'dry_run'} ? : 0 0 0
178 0 return 1;
179 }
180  
181 =head2 github_pages
182  
183 Deploys to a GitHub Pages branch with git.
184  
185 =cut
186  
187 0 ▶sub github_pages {
Subroutine github_pages: not called CC=21 SCAR=61.4
Pod documented
188 0 my ($dapper, $settings, $options) = @_;
189  
190 0 ▶ $settings = {} unless ref $settings eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $settings eq "HASH" 0 0 0
191 0 ▶ die "error: deploying to GitHub Pages needs git\n" unless find_command('git');
Branch: 1 branch
BranchTrueFalseTotal
unless find_command("git") 0 0 0
192  
193 0 my $repo = $settings->{repo};
194 0 ▶ if (not defined $repo) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $repo) 0 0 0
195 0 $repo = `git remote get-url origin 2>/dev/null`;
196 0 chomp $repo;
197 0 ▶ die "error: deploy.github_pages in $dapper->{config} needs a repo (or run dapper in a git repository with an origin)\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $repo 0 0 0
198 unless length $repo;
199 }
200 0 ▶ my $branch = defined $settings->{branch} ? $settings->{branch} : 'gh-pages';
Branch: 1 branch
BranchTrueFalseTotal
defined $settings->{'branch'} ? : 0 0 0
201 0 my $output = abs_path($dapper->{output});
202  
203 0 my $work = tempdir(CLEANUP => 1) . '/site';
204  
205 # Start from the branch as it is, to keep its history; or a new branch
206 0 ▶ open(my $heads, '-|', 'git', 'ls-remote', '--heads', '--', $repo, "refs/heads/$branch")
Branch: 1 branch
BranchTrueFalseTotal
open my $heads, "-|", "git", "ls-remote", "--heads", "--", $repo, "refs/heads/$branch" or die "error: could not run git: $!\n" 0 0 0
207 or die "error: could not run git: $!\n";
208 0 my $exists = do { local $/; <$heads> };
209 0 ▶ close $heads or die "error: could not reach $repo\n";
Branch: 1 branch
BranchTrueFalseTotal
close $heads or die "error: could not reach $repo\n" 0 0 0
210  
211 # A shallow clone of a local path makes git warn, so clone those in full
212 0 ▶ my @depth = -d $repo ? () : ('--depth', '1');
Branch: 1 branch
BranchTrueFalseTotal
-d $repo ? : 0 0 0
213 0 ▶ if (length $exists) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $exists) { } 0 0 0
214 0 run_command($dapper, 'git', 'clone', '--quiet', @depth, '--branch', $branch, '--', $repo, $work);
215 }
216 else {
217 0 $dapper->_log("Starting a new $branch branch");
218 0 run_command($dapper, 'git', 'init', '--quiet', $work);
219 0 run_command($dapper, 'git', '-C', $work, 'checkout', '--quiet', '--orphan', $branch);
220 0 run_command($dapper, 'git', '-C', $work, 'remote', 'add', 'origin', $repo);
221 }
222  
223 # Replace the branch's files with the output
224 0 for my $entry (App::Dapper::Utils::read_dir($work)) {
225 0 ▶ next if $entry eq '.git';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".git" 0 0 0
226 0 my $path = "$work/$entry";
227 0 ▶ if (-d $path) { File::Path::remove_tree($path) } else { unlink $path }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) { } 0 0 0
228 }
229 0 for my $entry (App::Dapper::Utils::read_dir($output)) {
230 0 ▶ next if $entry eq '.dapper-reload';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq ".dapper-reload" 0 0 0
231 0 App::Dapper::Utils::copy_recursive("$output/$entry", "$work/$entry");
232 }
233 0 App::Dapper::Utils::write_file("$work/.nojekyll", '');
234 0 ▶ App::Dapper::Utils::write_file("$work/CNAME", "$settings->{cname}\n") if defined $settings->{cname};
Branch: 1 branch
BranchTrueFalseTotal
if defined $settings->{"cname"} 0 0 0
235  
236 0 ▶ system('git', '-C', $work, 'add', '--all') == 0 or die "error: git add failed\n";
Branch: 1 branch
BranchTrueFalseTotal
system("git", "-C", $work, "add", "--all") == 0 or die "error: git add failed\n" 0 0 0
237 0 my $changes = `git -C '$work' status --porcelain`;
238 0 ▶ if (not length $changes) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $changes) 0 0 0
239 0 $dapper->_log("Nothing changed since the last deploy to $branch.");
240 0 return 1;
241 }
242  
243 0 my $count = () = $changes =~ /\n/g;
244 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"dry_run"}) 0 0 0
245 0 $dapper->_log("Dry run: would commit $count changed file(s) to $branch of $repo:");
246 0 $dapper->_log($changes);
247 0 return 1;
248 }
249  
250 0 ▶ my $name = defined $dapper->{site}->{name} ? $dapper->{site}->{name} : 'the site';
Branch: 1 branch
BranchTrueFalseTotal
defined $dapper->{'site'}{'name'} ? : 0 0 0
251 # Commit as Dapper where git has no name and email set (in a container, say)
252 0 my $email = `git -C '$work' config user.email`;
253 0 ▶ my @identity = length $email ? () : ('-c', 'user.name=Dapper', '-c', 'user.email=dapper@localhost');
Branch: 1 branch
BranchTrueFalseTotal
length $email ? : 0 0 0
254 0 run_command($dapper, 'git', '-C', $work, @identity, 'commit', '--quiet', '-m', "Deploy $name ($count file(s) changed)");
255 0 run_command($dapper, 'git', '-C', $work, 'push', '--quiet', 'origin', "HEAD:$branch");
256  
257 0 $dapper->_log("Deployed $count changed file(s) to $branch of $repo");
258 0 return 1;
259 }
260  
261 =head2 command
262  
263 Deploys by running a command from C<_config.yml>.
264  
265 =cut
266  
267 0 ▶sub command {
Subroutine command: not called CC=6 SCAR=37.4
Pod documented
268 0 my ($dapper, $command, $options) = @_;
269  
270 0 ▶ die "error: deploy.command in $dapper->{config} must be a command to run\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $command and not ref $command and length $command 0 0 0
Condition0%0/6
defined $command and not ref $command and length $command
!ll&&!rl&&r
000
defined $command and not ref $command
!ll&&!rl&&r
000
MC/DC0%0/3
defined $command and not ref $command and length $command
defined $command not ref $command length $command
Truth table0%0/4
A and not(B) and C
A= defined $commandB= not ref $commandC= length $command
ABCresult
0XX0
10X0
1100
1111
271 unless defined $command and !ref $command and length $command;
272  
273 0 ▶ if ($options->{dry_run}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"dry_run"}) 0 0 0
274 0 $dapper->_log("Dry run: would run: $command");
275 0 return 1;
276 }
277  
278 0 local $ENV{DAPPER_OUTPUT} = $dapper->{output};
279 0 $dapper->_log("+ $command");
280 0 ▶ system($command) == 0 or die "error: the deploy command failed (exit status " . ($? >> 8) . ")\n";
Branch: 1 branch
BranchTrueFalseTotal
system($command) == 0 or die 'error: the deploy command failed (exit status ' . ($? >> 8) . ")\n" 0 0 0
281 0 $dapper->_log('Deployed.');
282 0 return 1;
283 }
284  
285 32 1;
« lib/App/Dapper.pm ↑ Summary lib/App/Dapper/Check.pm »