File Coverage

/root/.cpan/build/Riji-v1.1.4-0/lib/Riji/CLI/Publish.pm
Criterion Covered Total %
statement 75 80 93.7
branch 16 24 66.6
condition 8 16 50.0
subroutine 11 11 100.0
pod 0 1 0.0
total 110 132 83.3


line stmt bran cond sub pod time code
1             package Riji::CLI::Publish;
2 8     8   4125 use feature ':5.10';
  8         16  
  8         1260  
3 8     8   44 use strict;
  8         13  
  8         189  
4 8     8   32 use warnings;
  8         12  
  8         582  
5              
6 8     8   39 use Errno qw(:POSIX);
  8         13  
  8         4242  
7 8     8   6794 use Path::Tiny qw/path tempdir/;
  8         110722  
  8         746  
8 8     8   4820 use File::Copy::Recursive qw/dircopy/;
  8         65358  
  8         680  
9              
10 8     8   4199 use Wallflower::Util qw/links_from/;
  8         132406  
  8         710  
11 8     8   4592 use Wallflower;
  8         160271  
  8         376  
12 8     8   68 use URI;
  8         15  
  8         166  
13              
14 8     8   3839 use Riji;
  8         31  
  8         7578  
15              
16             sub run {
17 8     8 0 28 my ($class, @argv) = @_;
18              
19 8         84 my $app = Riji->new;
20 7         18916 my $conf = $app->config;
21 7         44 my $blog = $app->model('Blog');
22 7         1123 my $repo = $blog->repo;
23 7         582450 my $force = grep {$_ eq '--force'} @argv;
  1         12  
24              
25             # `git symbolic-ref --short` is available after git 1.7.10, so care older version.
26 7         111 my $current_branch = $repo->run(qw/symbolic-ref HEAD/);
27 7         133824 $current_branch =~ s!refs/heads/!!;
28 7         133 my $publish_branch = $blog->branch;
29 7 100       181 unless ($force){
30 6         60 my $force_announce = "or you can use --force option\n";
31 6 50       66 if ($publish_branch ne $current_branch) {
32 0         0 die "You need at publish branch [$publish_branch], so `git checkout $publish_branch` beforehand $force_announce";
33             }
34              
35 6 100       128 if ( my $untracked = $repo->run(qw/ls-files --others --exclude-standard --/, $blog->entry_dir) ) {
36 1         15616 die "Unknown local files:\n$untracked\n\ngit add them, update .gitignore $force_announce";
37             }
38              
39 5 50       102415 if (my $uncommited = $repo->run(qw/diff HEAD --name-only --/, $blog->entry_dir) ) {
40 0         0 die "Found uncommited changes:\n$uncommited\n\ncommit them beforehand $force_announce";
41             }
42             }
43              
44 6         100535 say "start scanning";
45 6   50     196 my $dir = $conf->{publish_dir} // 'blog';
46 6 50 33     1226 unless (mkdir $dir or $! == EEXIST ){
47 0         0 printf "can't create $dir: $!\n";
48             }
49              
50 6         148 my $work_dir = tempdir(CLEANUP => 1);
51              
52 6         8609 my $site_url = $blog->site_url;
53 6         348 my $mount_path = $site_url->path;
54 6 50       805 $mount_path = '' if $mount_path eq '/';
55              
56 6         156 my $wallflower = Wallflower->new(
57             application => $app->to_psgi,
58             destination => $work_dir . '',
59             url => $site_url,
60             );
61 6         39876 my $host_reg = quotemeta $site_url->host;
62              
63 6         930 my %seen;
64 6   50     82 my @queue = ($mount_path || '/');
65 6         26 while (@queue) {
66 241         44377 my $url = URI->new( shift @queue );
67 241 100       22232 next if $seen{ $url->path }++;
68 60 50 100     2041 next if $url->scheme && ! eval { $url->host and $url->host =~ /(?:localhost|$host_reg)/ };
  54 100       1423  
69              
70             # get the response
71 36         3802 my $response = $wallflower->get($url);
72 36         69663 my ( $status, $headers, $file ) = @$response;
73              
74             # tell the world
75 36   33     512 printf "$status %s %s\n", $url->path, $file && "[${\-s $file}]";
76              
77             # obtain links to resources
78 36 50       1604 if ( $status eq '200' ) {
79 36         527 push @queue, links_from( $response => $url );
80             }
81              
82 36 50 33     107810 if ($file && $file =~ /\.(?:js|css|html|xml)$/) {
83 36         805 $file = path($file);
84 36         1006 my $content = $file->slurp_utf8;
85 36         9764 $file->spew_utf8($content);
86             }
87             }
88              
89 6         170 my $copy_from = $work_dir;
90 6 50       61 if ($mount_path) {
91 0         0 $mount_path =~ s!^/+!!;
92 0         0 $copy_from = path $work_dir, $mount_path;
93             }
94 6         52 dircopy $copy_from.'', $dir;
95              
96 6         34738 say "done.";
97             }
98              
99             1;