blib/lib/App/CPANModuleSite.pm

filter: Statementstmt 51.7% 44 / 85 Branchbran 12.5% 1 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 10 / 25 Podpod 33.3% 1 / 3 totaltotal 45.2% 57 / 126 CC 8 SCAR 15.3
CC
8
Cov
48%
run
20.0
_build_tt_config
6.0
process_template
6.0
CRAP 4.6
↑ Summary
1 =head1 NAME
2  
3 App::CPANModuleSite - Automatically create a web site for a CPAN module.
4  
5 =head1 SYNOPSIS
6  
7 # From a command line
8 $ mksite My::Lovely::Module
9  
10 It's probably not particularly useful to use the class directly.
11  
12 =cut
13  
14 package App::CPANModuleSite;
15  
16 2 our $VERSION = '0.0.10';
17  
18 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use MetaCPAN::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Path::Iterator::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use File::ShareDir 'dist_dir';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 subtype 'App::CPANModuleSite::Str',
29 as 'Str';
30  
31 coerce 'MetaCPAN::Client::Distribution',
32 from 'App::CPANModuleSite::Str',
33 1 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 MetaCPAN::Client->new->distribution($_);
35 2 };
36  
37 2 has distribution => (
38 is => 'ro',
39 isa => 'MetaCPAN::Client::Distribution',
40 required => 1,
41 coerce => 1,
42 );
43  
44 2 has metacpan => (
45 is => 'ro',
46 isa => 'MetaCPAN::Client',
47 lazy_build => 1,
48 );
49  
50 0 ▶sub _build_metacpan {
Subroutine _build_metacpan: not called CC=1 SCAR=6.9
51 0 return MetaCPAN::Client->new;
52 }
53  
54 2 has release => (
55 is => 'ro',
56 isa => 'MetaCPAN::Client::Release',
57 lazy_build => 1,
58 );
59  
60 0 ▶sub _build_release {
Subroutine _build_release: not called CC=1 SCAR=6.9
61 0 my $self = shift;
62  
63 0 return $self->metacpan->release($self->distribution->name);
64 }
65  
66 2 has modules => (
67 is => 'ro',
68 isa => 'ArrayRef[MetaCPAN::Client::Module]',
69 lazy_build => 1,
70 );
71  
72 0 ▶sub _build_modules {
Subroutine _build_modules: not called CC=1 SCAR=6.9
73 0 my $self = shift;
74  
75 0 return [ map { $self->metacpan->module($_) } @{ $self->release->provides } ];
76 }
77  
78 2 has tt => (
79 is => 'ro',
80 isa => 'Template',
81 lazy_build => 1,
82 );
83  
84 0 ▶sub _build_tt {
Subroutine _build_tt: not called CC=1 SCAR=6.9
85 0 my $self = shift;
86  
87 0 return Template->new($self->tt_config);
88 }
89  
90 2 has tt_config => (
91 is => 'ro',
92 isa => 'HashRef',
93 lazy_build => 1,
94 );
95  
96 0 ▶sub _build_tt_config {
Subroutine _build_tt_config: not called CC=2 SCAR=17.9
97 0 my $self = shift;
98  
99 return {
100 0 ▶ INCLUDE_PATH => $self->include_path,
Branch: 1 branch
BranchTrueFalseTotal
$self->wrapper ? : 0 0 0
101 OUTPUT_PATH => $self->output_path,
102 ( $self->wrapper ? ( WRAPPER => $self->wrapper ) : () ),
103 RELATIVE => 1,
104 VARIABLES => {
105 distribution => $self->distribution,
106 release => $self->release,
107 modules => $self->modules,
108 base => $self->base,
109 },
110 }
111 }
112  
113 2 has [ qw[site_src local_site_src tt_lib local_tt_lib] ] => (
114 is => 'ro',
115 isa => 'Str',
116 lazy_build => 1,
117 );
118  
119 0 ▶sub _build_site_src {
Subroutine _build_site_src: not called CC=1 SCAR=6.9
120 0 return dist_dir('App-CPANModuleSite') . '/site_src';
121 }
122  
123 0 ▶sub _build_local_site_src {
Subroutine _build_local_site_src: not called CC=1 SCAR=6.9
124 0 return './site_src';
125 }
126  
127 0 ▶sub _build_tt_lib {
Subroutine _build_tt_lib: not called CC=1 SCAR=6.9
128 0 return dist_dir('App-CPANModuleSite') . '/tt_lib';
129 }
130  
131 0 ▶sub _build_local_tt_lib {
Subroutine _build_local_tt_lib: not called CC=1 SCAR=6.9
132 0 return './tt_lib';
133 }
134  
135 2 has include_path => (
136 is => 'ro',
137 isa => 'ArrayRef',
138 lazy_build => 1,
139 );
140  
141 0 ▶sub _build_include_path {
Subroutine _build_include_path: not called CC=1 SCAR=6.9
142 0 my $self = shift;
143  
144 return [
145 0 $self->local_tt_lib,
146 $self->local_site_src,
147 $self->tt_lib,
148 $self->site_src,
149 ];
150 }
151  
152 2 has output_path => (
153 is => 'ro',
154 isa => 'Str',
155 lazy_build => 1,
156 );
157  
158 0 ▶sub _build_output_path {
Subroutine _build_output_path: not called CC=1 SCAR=6.9
159 0 return './docs';
160 }
161  
162 2 has wrapper => (
163 is => 'ro',
164 isa => 'Str',
165 lazy_build => 1,
166 );
167  
168 0 ▶sub _build_wrapper {
Subroutine _build_wrapper: not called CC=1 SCAR=6.9
169 0 return 'page.tt';
170 }
171  
172 2 has base => (
173 is => 'ro',
174 isa => 'Str',
175 default => '',
176 );
177  
178 1 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=3 SCAR=12.7
179 1 my $orig = shift;
180 1 my $class = shift;
181  
182 1 ▶ if ( @_ == 1 && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 1 0 1
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
183 1 return $class->$orig( distribution => $_[0] );
184 } else {
185 0 return $class->$orig(@_);
186 }
187 2 };
188  
189 =head1 METHODS
190  
191 =head2 run
192  
193 The main driver method for the class. Does the following steps:
194  
195 =over 4
196  
197 =item * Creates a list of input files
198  
199 =item * Uses Template Toolkit to process the templates
200  
201 =item * Copies any non-template files into the output tree
202  
203 =item * Creates a HTML version of the Pod from all the modules
204  
205 =back
206  
207 =cut
208  
209 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod documented
210 0 my $self = shift;
211  
212 0 my $finder = Path::Iterator::Rule->new->file;
213  
214 0 my @src_dirs = ($self->local_site_src, $self->site_src);
215 0 my %src_files = map { $_ => 1 }
216 $finder->all($self->site_src, { relative => 1 });
217  
218 0 for ( keys %src_files ) {
219 0 ▶ if (/\.tt$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\.tt$/u) { } 0 0 0
220 0 $self->process_template($_);
221 } else {
222 0 $self->copy_file($_);
223 }
224 }
225  
226 0 foreach (@{ $self->modules }) {
227 0 my $outpath = $_->path =~ s/\.pm$/.html/r;
228 0 my $pod = $_->pod('html');
229 0 $self->tt->process(\$pod, undef, $outpath);
230 }
231 }
232  
233 0 ▶sub process_template {
Subroutine process_template: not called CC=2 SCAR=17.9
Pod undocumented
234 0 my $self = shift;
235 0 my ($template) = @_;
236  
237 0 my $output = $template =~ s/\.tt$//r;
238  
239 0 ▶ $self->tt->process($template, undef, $output)
Branch: 1 branch
BranchTrueFalseTotal
$self->tt->process($template, undef, $output) or die $self->tt->error 0 0 0
240 or die $self->tt->error;
241 }
242  
243 0 ▶sub copy_file {
Subroutine copy_file: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my $self = shift;
245 0 my ($file) = @_;
246  
247 0 copy($file, $self->output_path . "/$file");
248 }
249  
250 2 1;
251  
252 =head1 AUTHOR
253  
254 Dave Cross <dave@perlhacks.com>
255  
256 =head1 SEE ALSO
257  
258 L<mksite>
259  
260 =head1 COPYRIGHT AND LICENSE
261  
262 Copyright (C) 2021, Magnum Solutions Ltd. All Rights Reserved.
263  
264 This script is free software; you can redistribute it and/or modify it
265 under the same terms as Perl itself.
266  
267 =cut
↑ Summary