blib/lib/Statocles.pm

filter: Statementstmt 90.3% 84 / 93 Branchbran 81.8% 36 / 44 Conditioncond 58.6% 17 / 29 MC/DCmcdc 35.2% 6 / 17 Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 78.9% 154 / 195 CC 33 SCAR 26.3
CC
33
Cov
83%
_load_site
20.7
run
14.5
log
4.6
CRAP 13.8
« blib/lib/Statocles/App/Role/Store.pm ↑ Summary blib/lib/Statocles/Page/Document.pm »
1 package Statocles;
2 61 our $VERSION = '0.099';
3 # ABSTRACT: A static site generator
4  
5 61 ▶use Statocles::Base 'Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 61 ▶use Getopt::Long qw( GetOptionsFromArray :config pass_through bundling no_auto_abbrev );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 61 ▶use Pod::Usage::Return qw( pod2usage );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 61 ▶use Beam::Wire;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 61 ▶use Mojo::Loader qw( load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 61 my @VERBOSE = ( "warn", "info", "debug", "trace" );
13  
14 #pod =attr site
15 #pod
16 #pod The L<site|Statocles::Site> we're working with.
17 #pod
18 #pod =cut
19  
20 61 has site => (
21 is => 'ro',
22 isa => InstanceOf['Statocles::Site'],
23 );
24  
25 #pod =method run
26 #pod
27 #pod my $exitval = $cmd->run( @argv );
28 #pod
29 #pod Run the command given in @argv. See L<statocles> for a list of commands and
30 #pod options.
31 #pod
32 #pod =cut
33  
34 40 ▶sub run {
Subroutine run: called CC=14 SCAR=26.8
Pod documented
35 40 my ( $class, @argv ) = @_;
36  
37 40 my %opt = (
38 config => 'site.yml',
39 site => 'site',
40 verbose => 0,
41 );
42 40 GetOptionsFromArray( \@argv, \%opt,
43 'config:s',
44 'site:s',
45 'help|h',
46 'version',
47 'verbose|v+',
48 'include|I:s@',
49 'lib|l',
50 );
51 40 ▶ return pod2usage(0) if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 4 36 40
52  
53 36 ▶ if ( $opt{version} || ( $opt{verbose} && !@argv ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"} or $opt{"verbose"} and not @argv) 2 34 36
Condition100%6/6
$opt{"version"} or $opt{"verbose"} and not @argv
l!l&&r!l&&!r
1134
$opt{"verbose"} and not @argv
!ll&&!rl&&r
3221
MC/DC100%3/3
$opt{"version"} or $opt{"verbose"} and not @argv
$opt{"version"} $opt{"verbose"} not @argv
Truth table100%4/4
A or B and not(C)
A= $opt{"version"}B= $opt{"verbose"}C= not @argv
ABCresult
1XX1
0111
00X0
0100
54 2 say "Statocles version $Statocles::VERSION (Perl $^V)";
55 2 require POSIX;
56 2 say "Locale: " . POSIX::setlocale( POSIX::LC_CTYPE );
57 2 return 0;
58 }
59  
60 34 ▶ if ( $opt{lib} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"lib"}) 0 34 34
61 0 unshift @{ $opt{include} }, 'lib';
62 }
63  
64 34 ▶ if ( $opt{include} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"include"}) 0 34 34
65 0 unshift @INC, @{ $opt{include} };
66 }
67  
68 34 my $method = shift @argv;
69 34 ▶ return pod2usage("ERROR: Missing command") unless $method;
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 33 34
70  
71 # Create site does not require a config file
72 33 ▶ if ( $method eq 'create' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "create") 7 26 33
73 7 require Statocles::Command::create;
74 7 return Statocles::Command::create->new->run( @argv );
75 }
76  
77 26 my ( $exit, $site ) = _load_site( %opt );
78 26 ▶ if ( $exit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit) 10 16 26
79 10 return $exit;
80 }
81  
82 16 ▶ if ( $opt{verbose} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"verbose"}) 0 16 16
83 0 $site->log->handle( \*STDOUT );
84 0 $site->log->level( $VERBOSE[ $opt{verbose} ] );
85 }
86  
87 16 my $cmd_class = 'Statocles::Command::' . $method;
88  
89 16 my $error = load_class( $cmd_class );
90 16 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 4 12 16
91 4 ▶ if ( my $app = $site->apps->{ $method } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $app = $site->apps->{$method}) { } 3 1 4
92 3 ▶ if ( !$app->can( 'command' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($app->can("command")) 1 2 3
93 1 say STDERR sprintf 'ERROR: Application "%s" has no commands', $method;
94 1 return 1;
95 }
96 2 return $app->command( $method, @argv );
97 }
98 else {
99 1 return pod2usage("ERROR: Unknown command or app '$method'");
100 }
101 }
102  
103 12 my $cmd = $cmd_class->new( site => $site );
104 12 return $cmd->run( @argv );
105 }
106  
107 26 ▶sub _load_site {
Subroutine _load_site: called CC=17 SCAR=30.3
108 26 my ( %opt ) = @_;
109 26 ▶ if ( !-e $opt{config} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $opt{"config"}) 2 24 26
110 2 warn sprintf qq{ERROR: Could not find config file "\%s"\n}, $opt{config};
111 2 return 1;
112 }
113  
114 24 my $wire = eval { Beam::Wire->new( file => $opt{config} ) };
115  
116 24 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 20 24
117 4 ▶ if ( blessed $@ && $@->isa( 'Beam::Wire::Exception::Config' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa("Beam::Wire::Exception::Config")) 4 0 4
Condition33%1/3
builtin::blessed($@) and $@->isa("Beam::Wire::Exception::Config")
!ll&&!rl&&r
004
MC/DC0%0/2
builtin::blessed($@) and $@->isa("Beam::Wire::Exception::Config")
builtin::blessed($@) $@->isa("Beam::Wire::Exception::Config")
Truth table33%1/3
A and B
A= builtin::blessed($@)B= $@->isa("Beam::Wire::Exception::Config")
ABresult
0X0
100
111
118 4 my $remedy;
119 4 ▶ if ( $@ =~ /found character that cannot start any token/ || $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($@ =~ /found character that cannot start any token/ or $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/) { } 2 2 4
elsif ($@ =~ /did not find expected key/ or $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/) { } 2 0 2
elsif ($@ =~ /Syck parser/ and $@ =~ /syntax error/) { } 0 0 0
Condition66%2/3
$@ =~ /found character that cannot start any token/ or $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/
l!l&&r!l&&!r
022
MC/DC50%1/2
$@ =~ /found character that cannot start any token/ or $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/
$@ =~ /found character that cannot start any token/ $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/
Truth table66%2/3
A or B
A= $@ =~ /found character that cannot start any token/B= $@ =~ /YAML_PARSE_ERR_NONSPACE_INDENTATION/
ABresult
1X1
011
000
120 2 $remedy = "Check that you are not using tabs for indentation. ";
121 }
122 ▶ elsif ( $@ =~ /did not find expected key/ || $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/ ) {
Condition33%1/3
$@ =~ /did not find expected key/ or $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/
l!l&&r!l&&!r
020
MC/DC0%0/2
$@ =~ /did not find expected key/ or $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/
$@ =~ /did not find expected key/ $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/
Truth table33%1/3
A or B
A= $@ =~ /did not find expected key/B= $@ =~ /YAML_PARSE_ERR_INCONSISTENT_INDENTATION/
ABresult
1X1
011
000
123 2 $remedy = "Check your indentation. ";
124 }
125 ▶ elsif ( $@ =~ /Syck parser/ && $@ =~ /syntax error/ ) {
Condition0%0/3
$@ =~ /Syck parser/ and $@ =~ /syntax error/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ =~ /Syck parser/ and $@ =~ /syntax error/
$@ =~ /Syck parser/ $@ =~ /syntax error/
Truth table0%0/3
A and B
A= $@ =~ /Syck parser/B= $@ =~ /syntax error/
ABresult
0X0
100
111
126 0 $remedy = "Check your indentation. ";
127 }
128  
129 4 ▶ my $more_info = ( !$opt{verbose} ? qq{run with the "--verbose" option or } : "" )
Branch: 1 branch
BranchTrueFalseTotal
!$opt{'verbose'} ? : 2 2 4
130 . "check Statocles::Help::Error";
131  
132 warn sprintf qq{ERROR: Could not load config file "%s". %sFor more information, %s.%s},
133 $opt{config},
134 $remedy,
135 $more_info,
136 4 ▶ ( $opt{verbose} ? "\n\nRaw error: $@" : "" )
Branch: 1 branch
BranchTrueFalseTotal
$opt{'verbose'} ? : 2 2 4
137 ;
138  
139 4 return 1;
140 }
141 0 die $@;
142 }
143  
144 20 my $site = eval { $wire->get( $opt{site} ) };
145  
146 20 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 4 16 20
147 4 ▶ if ( blessed $@ && $@->isa( 'Beam::Wire::Exception::NotFound' ) && $@->name eq $opt{site} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($@) and $@->isa("Beam::Wire::Exception::NotFound") and $@->name eq $opt{"site"}) 2 2 4
Condition83%5/6
builtin::blessed($@) and $@->isa("Beam::Wire::Exception::NotFound") and $@->name eq $opt{"site"}
!ll&&!rl&&r
112
builtin::blessed($@) and $@->isa("Beam::Wire::Exception::NotFound")
!ll&&!rl&&r
103
MC/DC66%2/3
builtin::blessed($@) and $@->isa("Beam::Wire::Exception::NotFound") and $@->name eq $opt{"site"}
builtin::blessed($@) $@->isa("Beam::Wire::Exception::NotFound") $@->name eq $opt{"site"}
Truth table75%3/4
A and B and C
A= builtin::blessed($@)B= $@->isa("Beam::Wire::Exception::NotFound")C= $@->name eq $opt{"site"}
ABCresult
0XX0
10X0
1100
1111
148 warn sprintf qq{ERROR: Could not find site named "%s" in config file "%s"\n},
149 2 $opt{site}, $opt{config};
150 2 return 1;
151 }
152 warn sprintf qq{ERROR: Could not create site object "%s" in config file "%s": %s\n},
153 2 $opt{site}, $opt{config}, $@;
154 2 return 1;
155 }
156  
157 16 return ( 0, $site );
158 }
159  
160 103 ▶sub log {
Subroutine log: called CC=4 SCAR=15.2
Pod undocumented
161 103 my ( $invocant, $level, @args ) = @_;
162 61 ▶ use Mojo::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
163 103 ▶ state $log = Mojo::Log->new( level => $ENV{MOJO_LOG_LEVEL} // 'info' );
Condition50%1/2
$ENV{"MOJO_LOG_LEVEL"} // "info"
l!l
07
MC/DC0%0/1
$ENV{"MOJO_LOG_LEVEL"} // "info"
$ENV{"MOJO_LOG_LEVEL"}
Truth table50%1/2
A
A= $ENV{"MOJO_LOG_LEVEL"}
Aresult
11
00
164 103 ▶ if ( $level && @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level and @args) 103 0 103
Condition33%1/3
$level and @args
!ll&&!rl&&r
00103
MC/DC0%0/2
$level and @args
$level @args
Truth table33%1/3
A and B
A= $levelB= @args
ABresult
0X0
100
111
165 103 return $log->$level( @args );
166 }
167 0 return $log;
168 }
169  
170 61 1;
171  
172 __END__
« blib/lib/Statocles/App/Role/Store.pm ↑ Summary blib/lib/Statocles/Page/Document.pm »