File Coverage

blib/lib/Riji/CLI/Setup.pm
Criterion Covered Total %
statement 60 62 96.7
branch 7 12 58.3
condition 1 3 33.3
subroutine 12 12 100.0
pod 0 1 0.0
total 80 90 88.8


line stmt bran cond sub pod time code
1             package Riji::CLI::Setup;
2 4     4   4898 use feature ':5.10';
  4         8  
  4         440  
3 4     4   27 use strict;
  4         9  
  4         168  
4 4     4   27 use warnings;
  4         19  
  4         181  
5              
6 4     4   26 use Cwd qw/getcwd/;
  4         10  
  4         322  
7 4     4   1657 use File::Copy qw/copy/;
  4         7811  
  4         306  
8 4     4   2486 use File::Copy::Recursive qw/dircopy/;
  4         17301  
  4         377  
9 4     4   50 use File::Spec;
  4         9  
  4         87  
10 4     4   23 use IPC::Cmd ();
  4         9  
  4         65  
11 4     4   22 use Path::Tiny;
  4         8  
  4         208  
12              
13 4     4   25 use Riji;
  4         8  
  4         2174  
14              
15             sub run {
16 4     4 0 46 my ($class, @argv) = @_;
17              
18 4         12 my $force = grep {$_ eq '--force'} @argv;
  0         0  
19              
20 4         24 my $share_dir = Riji->share_dir;
21 4         92 my $setup_dir = getcwd;
22              
23 4 50 33     42 if (!$force && path($setup_dir)->children) {
24 0         0 die "you must run `riji setup` in empty directory or `riji setup --force`.\n";
25             }
26              
27             my $recurse644 = sub {
28 8     8   318 my $dir = shift;
29 8         57 my $itr = $dir->iterator({recurse => 1});
30 8         342 while (my $file = $itr->()) {
31 36 100       4374 next unless -f $file;
32 32         561 chmod 0644, $file;
33             }
34 4         1014 };
35              
36 4         57 my $target_dir = File::Spec->catdir($setup_dir, 'share', 'tmpl');
37 4         37 dircopy(File::Spec->catdir($share_dir, 'tmpl'), $target_dir);
38 4         17744 $recurse644->(path($target_dir));
39              
40 4         308 $target_dir = File::Spec->catdir($setup_dir, 'article');
41 4         38 dircopy(File::Spec->catdir($share_dir, 'article'), $target_dir);
42 4         12035 $recurse644->(path($target_dir));
43              
44 4         316 copy(File::Spec->catfile($share_dir, 'riji.yml'), $setup_dir);
45 4         2650 copy(File::Spec->catfile($share_dir, 'README.md'), $setup_dir);
46              
47 4         1942 my $cpanfile = path($setup_dir, 'cpanfile');
48 4 50       177 unless (-f $cpanfile) {
49 4         265 $cpanfile->spew(qq{requires "Riji", "$Riji::VERSION";\n});
50             }
51              
52 4         2309 my $gitignore = path($setup_dir, '.gitignore');
53 4 50       174 unless (-f $gitignore) {
54 4         110 $gitignore->spew(".*\n!.gitignore\nlocal/\n*~\n*.swp\n");
55             }
56              
57 4 50       1580 my $git = IPC::Cmd::can_run('git') or die "git not found.\n";
58              
59 4 50       406659 unless (-e path($setup_dir)->child('.git')) {
60 4         78079 system($git, qw!init!);
61             }
62 4         57850 system($git, qw!add .!);
63 4         58765 system($git, qw/commit -m/, "initial blog commit");
64             }
65              
66             1;