lib/Riji/CLI/Publish.pm

filter: Statementstmt 93.9% 77 / 82 Branchbran 70.8% 17 / 24 Conditioncond 55.5% 10 / 18 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 100% 11 / 11 Podpod 0.0% 0 / 1 totaltotal 80.2% 118 / 147 CC 20 SCAR 27.9
CC
20
Cov
84%
run
24.0
BEGIN
1.0
BEGIN
1.0
CRAP 16.3
↑ Summary lib/Riji/Model/Blog.pm »
1 package Riji::CLI::Publish;
2 8 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Errno qw(:POSIX);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Path::Tiny qw/path tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use File::Copy::Recursive qw/dircopy/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use Wallflower::Util qw/links_from/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Wallflower;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 8 ▶use Riji;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 8 ▶sub run {
Subroutine run: called CC=20 SCAR=31.8
Pod undocumented
17 8 my ($class, @argv) = @_;
18  
19 8 my $app = Riji->new;
20 7 my $conf = $app->config;
21 7 my $blog = $app->model('Blog');
22 7 my $repo = $blog->repo;
23 7 my $force = grep {$_ eq '--force'} @argv;
24  
25 # `git symbolic-ref --short` is available after git 1.7.10, so care older version.
26 7 my $current_branch = $repo->run(qw/symbolic-ref HEAD/);
27 7 $current_branch =~ s!refs/heads/!!;
28 7 my $publish_branch = $blog->branch;
29 7 ▶ unless ($force){
Branch: 1 branch
BranchTrueFalseTotal
unless ($force) 6 1 7
30 6 my $force_announce = "or you can use --force option\n";
31 6 ▶ if ($publish_branch ne $current_branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($publish_branch ne $current_branch) 0 6 6
32 0 die "You need at publish branch [$publish_branch], so `git checkout $publish_branch` beforehand $force_announce";
33 }
34  
35 6 ▶ if ( my $untracked = $repo->run(qw/ls-files --others --exclude-standard --/, $blog->entry_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $untracked = $repo->run(("ls-files", "--others", "--exclude-standard", "--"), $blog->entry_dir)) 1 5 6
36 1 die "Unknown local files:\n$untracked\n\ngit add them, update .gitignore $force_announce";
37 }
38  
39 5 ▶ if (my $uncommited = $repo->run(qw/diff HEAD --name-only --/, $blog->entry_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $uncommited = $repo->run(("diff", "HEAD", "--name-only", "--"), $blog->entry_dir)) 0 5 5
40 0 die "Found uncommited changes:\n$uncommited\n\ncommit them beforehand $force_announce";
41 }
42 }
43  
44 6 say "start scanning";
45 6 ▶ my $dir = $conf->{publish_dir} // 'blog';
Condition50%1/2
$conf->{"publish_dir"} // "blog"
l!l
06
MC/DC0%0/1
$conf->{"publish_dir"} // "blog"
$conf->{"publish_dir"}
Truth table50%1/2
A
A= $conf->{"publish_dir"}
Aresult
11
00
46 6 ▶ unless (mkdir $dir or $! == EEXIST ){
Branch: 1 branch
BranchTrueFalseTotal
unless (mkdir $dir or $! == 17) 0 6 6
Condition33%1/3
mkdir $dir or $! == 17
l!l&&r!l&&!r
600
MC/DC0%0/2
mkdir $dir or $! == 17
mkdir $dir $! == 17
Truth table33%1/3
A or B
A= mkdir $dirB= $! == 17
ABresult
1X1
011
000
47 0 printf "can't create $dir: $!\n";
48 }
49  
50 6 my $work_dir = tempdir(CLEANUP => 1);
51  
52 6 my $site_url = $blog->site_url;
53 6 my $mount_path = $site_url->path;
54 6 ▶ $mount_path = '' if $mount_path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $mount_path eq "/" 6 0 6
55  
56 6 my $wallflower = Wallflower->new(
57 application => $app->to_psgi,
58 destination => $work_dir . '',
59 url => $site_url,
60 );
61 6 my $host_reg = quotemeta $site_url->host;
62  
63 6 my %seen;
64 6 ▶ my @queue = ($mount_path || '/');
Condition50%1/2
$mount_path || '/'
l!l
06
MC/DC0%0/1
$mount_path || '/'
$mount_path
Truth table50%1/2
A
A= $mount_path
Aresult
11
00
65 6 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 241 6 247
66 241 my $url = URI->new( shift @queue );
67 241 ▶ next if $seen{ $url->path }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$url->path}++ 181 60 241
68 60 ▶ next if $url->scheme && ! eval { $url->host and $url->host =~ /(?:localhost|$host_reg)/ };
Branch: 1 branch
BranchTrueFalseTotal
if $url->scheme and not eval { do { $url->host and $url->host =~ /(?:localhost|$host_reg)/ } } 24 36 60
Condition83%5/6
$url->scheme and not eval { do { $url->host and $url->host =~ /(?:localhost|$host_reg)/ } }
!ll&&!rl&&r
63024
$url->host && $url->host =~ /(?:localhost|$host_reg)/
!ll&&!rl&&r
02430
MC/DC100%2/2
$url->scheme and not eval { do { $url->host and $url->host =~ /(?:localhost|$host_reg)/ } }
$url->scheme not eval { do { $url->host and $url->host =~ /(?:localhost|$host_reg)/ } }
MC/DC50%1/2
$url->host && $url->host =~ /(?:localhost|$host_reg)/
$url->host $url->host =~ /(?:localhost|$host_reg)/
Truth table100%3/3
A and not(B)
A= $url->schemeB= not eval { do { $url->host and $url->host =~ /(?:localhost|$host_reg)/ } }
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= $url->hostB= $url->host =~ /(?:localhost|$host_reg)/
ABresult
0X0
100
111
69  
70 # get the response
71 36 my $response = $wallflower->get($url);
72 36 my ( $status, $headers, $file ) = @$response;
73  
74 # tell the world
75 36 ▶ printf "$status %s %s\n", $url->path, $file && "[${\-s $file}]";
Condition50%1/2
$file && "[${\(-s $file);}]"
!ll
036
MC/DC0%0/1
$file && "[${\(-s $file);}]"
$file
Truth table50%1/2
A
A= $file
Aresult
00
11
76  
77 # obtain links to resources
78 36 ▶ if ( $status eq '200' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq 200) 36 0 36
79 36 push @queue, links_from( $response => $url );
80 }
81  
82 36 ▶ if ($file && $file =~ /\.(?:js|css|html|xml)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file and $file =~ /\.(?:js|css|html|xml)$/) 36 0 36
Condition33%1/3
$file and $file =~ /\.(?:js|css|html|xml)$/
!ll&&!rl&&r
0036
MC/DC0%0/2
$file and $file =~ /\.(?:js|css|html|xml)$/
$file $file =~ /\.(?:js|css|html|xml)$/
Truth table33%1/3
A and B
A= $fileB= $file =~ /\.(?:js|css|html|xml)$/
ABresult
0X0
100
111
83 36 $file = path($file);
84 36 my $content = $file->slurp_utf8;
85 36 $file->spew_utf8($content);
86 }
87 }
88  
89 6 my $copy_from = $work_dir;
90 6 ▶ if ($mount_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mount_path) 0 6 6
91 0 $mount_path =~ s!^/+!!;
92 0 $copy_from = path $work_dir, $mount_path;
93 }
94 6 dircopy $copy_from.'', $dir;
95  
96 6 say "done.";
97 }
98  
99 8 1;
↑ Summary lib/Riji/Model/Blog.pm »