blib/lib/OODoc.pm untested

filter: Statementstmt 0.0% 0 / 208 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 59 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 394 CC 49 SCAR 53.7
CC
49
Cov
0%
processFiles
552.0
getPackageRelations
90.0
create
90.0
CRAP 215.2
1 # Copyrights 2003-2015 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.01.
5  
6 package OODoc;
7 use vars '$VERSION';
8 $VERSION = '2.01';
9  
10 use base 'OODoc::Object';
11  
12 use strict;
13 use warnings;
14  
15 use Log::Report 'oodoc';
16  
17 use OODoc::Manifest;
18  
19 use File::Copy;
20 use File::Spec;
21 use File::Basename;
22 use IO::File;
23 use List::Util 'first';
24  
25  
26 sub init($)
27 { my ($self, $args) = @_;
28  
29 $self->SUPER::init($args) or return;
30  
31 $self->{O_pkg} = {};
32  
33 my $distribution = $self->{O_distribution} = delete $args->{distribution};
34 defined $distribution
35 or error __x"the produced distribution needs a project description";
36  
37 $self->{O_project} = delete $args->{project} || $distribution;
38  
39 my $version = delete $args->{version};
40 unless(defined $version)
41 { my $fn = -f 'version' ? 'version'
42 : -f 'VERSION' ? 'VERSION'
43 : undef;
44 if(defined $fn)
45 { my $v = IO::File->new($fn, 'r')
46 or fault __x"cannot read version from file {file}", file=> $fn;
47 $version = $v->getline;
48 $version = $1 if $version =~ m/(\d+\.[\d\.]+)/;
49 chomp $version;
50 }
51 }
52  
53 defined $version
54 or error __x"no version specified for distribution '{dist}'"
55 , dist => $distribution;
56  
57 $self->{O_version} = $version;
58 $self;
59 }
60  
61 #-------------------------------------------
62  
63  
64 sub distribution() {shift->{O_distribution}}
65  
66  
67 sub version() {shift->{O_version}}
68  
69  
70 sub project() {shift->{O_project}}
71  
72 #-------------------------------------------
73  
74  
75 sub selectFiles($@)
76 { my ($self, $files) = (shift, shift);
77  
78 my $select
79 = ref $files eq 'Regexp' ? sub { $_[0] =~ $files }
80 : ref $files eq 'CODE' ? $files
81 : ref $files eq 'ARRAY' ? $files
82 : error __x"use regex, code reference or array for file selection";
83  
84 return ($select, [])
85 if ref $select eq 'ARRAY';
86  
87 my (@process, @copy);
88 foreach my $fn (@_)
89 { if($select->($fn)) {push @process, $fn}
90 else {push @copy, $fn}
91 }
92  
93 ( \@process, \@copy );
94 }
95  
96  
97 sub processFiles(@)
98 { my ($self, %args) = @_;
99  
100 exists $args{workdir}
101 or error __x"requires a directory to write the distribution to";
102  
103 my $dest = $args{workdir};
104 my $source = $args{source};
105 my $distr = $args{distribution} || $self->distribution;
106  
107 my $version = $args{version};
108 unless(defined $version)
109 { my $fn = defined $source ? File::Spec->catfile($source, 'version')
110 : 'version';
111 $fn = -f $fn ? $fn
112 : defined $source ? File::Spec->catfile($source, 'VERSION')
113 : 'VERSION';
114 if(defined $fn)
115 { my $v = IO::File->new($fn, "r")
116 or fault __x"cannot read version from {file}", file => $fn;
117 $version = $v->getline;
118 $version = $1 if $version =~ m/(\d+\.[\d\.]+)/;
119 chomp $version;
120 }
121 elsif($version = $self->version) { ; }
122 else
123 { error __x"there is no version defined for the source files";
124 }
125 }
126  
127 my $notice = '';
128 if($notice = $args{notice})
129 { $notice =~ s/^(\#\s)?/# /mg; # put comments if none
130 }
131  
132 #
133 # Split the set of files into those who do need special processing
134 # and those who do not.
135 #
136  
137 my $manfile
138 = exists $args{manifest} ? $args{manifest}
139 : defined $source ? File::Spec->catfile($source, 'MANIFEST')
140 : 'MANIFEST';
141  
142 my $manifest = OODoc::Manifest->new(filename => $manfile);
143  
144 my $manout;
145 if(defined $dest)
146 { my $manif = File::Spec->catfile($dest, 'MANIFEST');
147 $manout = OODoc::Manifest->new(filename => $manif);
148 $manout->add($manif);
149 }
150 else
151 { $manout = OODoc::Manifest->new(filename => undef);
152 }
153  
154 my $select = $args{select} || qr/\.(pm|pod)$/;
155 my ($process, $copy) = $self->selectFiles($select, @$manifest);
156  
157 trace @$process." files to process and ".@$copy." files to copy";
158  
159 #
160 # Copy all the files which do not contain pseudo doc
161 #
162  
163 if(defined $dest)
164 { foreach my $filename (@$copy)
165 { my $fn = defined $source ? File::Spec->catfile($source, $filename)
166 : $filename;
167  
168 my $dn = File::Spec->catfile($dest, $fn);
169 unless(-f $fn)
170 { warning __x"no file {file} to include in the distribution"
171 , file => $fn;
172 next;
173 }
174  
175 unless(-e $dn && ( -M $dn < -M $fn ) && ( -s $dn == -s $fn ))
176 { $self->mkdirhier(dirname $dn);
177  
178 copy $fn, $dn
179 or fault __x"cannot copy distribution file {from} to {to}"
180 , from => $fn, to => $dest;
181  
182 trace " copied $fn to $dest";
183 }
184  
185 $manout->add($dn);
186 }
187 }
188  
189 #
190 # Create the parser
191 #
192  
193 my $parser = $args{parser} || 'OODoc::Parser::Markov';
194 my $skip_links = delete $args{skip_links};
195  
196 unless(ref $parser)
197 { eval "require $parser";
198 error __x"cannot compile {pkg} class: {err}", pkg => $parser, err => $@
199 if $@;
200  
201 $parser = $parser->new(skip_links => $skip_links)
202 or error __x"parser {name} could not be instantiated", name=>$parser;
203 }
204  
205 #
206 # Now process the rest
207 #
208  
209 foreach my $filename (@$process)
210 { my $fn = $source ? File::Spec->catfile($source, $filename) : $filename;
211  
212 unless(-f $fn)
213 { warning __x"no file {file} to include in the distribution"
214 , file => $fn;
215 next;
216 }
217  
218 my $dn;
219 if($dest)
220 { $dn = File::Spec->catfile($dest, $fn);
221 $self->mkdirhier(dirname $dn);
222 $manout->add($dn);
223 }
224  
225 # do the stripping
226 my @manuals = $parser->parse
227 ( input => $fn
228 , output => $dn
229 , distribution => $distr
230 , version => $version
231 , notice => $notice
232 );
233  
234 trace "stripped $fn into $dn" if defined $dn;
235 trace $_->stats for @manuals;
236  
237 foreach my $man (@manuals)
238 { $self->addManual($man) if $man->chapters;
239 }
240 }
241  
242 # Some general subtotals
243 trace $self->stats;
244  
245 $self;
246 }
247  
248 #-------------------------------------------
249  
250  
251 sub prepare(@)
252 { my ($self, %args) = @_;
253  
254 info "collect package relations";
255 $self->getPackageRelations;
256  
257 info "expand manual contents";
258 foreach my $manual ($self->manuals)
259 { trace " expand manual $manual";
260 $manual->expand;
261 }
262  
263 info "Create inheritance chapter";
264 foreach my $manual ($self->manuals)
265 { trace " create inheritance for $manual";
266 $manual->createInheritance;
267 }
268  
269 $self;
270 }
271  
272  
273 sub getPackageRelations($)
274 { my $self = shift;
275 my @manuals = $self->manuals; # all
276  
277 #
278 # load all distributions (which are not loaded yet)
279 #
280  
281 info "compile all packages";
282  
283 foreach my $manual (@manuals)
284 { next if $manual->isPurePod;
285 trace " require package $manual";
286  
287 eval "require $manual";
288 warning __x"errors from {manual}: {err}", manual => $manual, err =>$@
289 if $@ && $@ !~ /can't locate/i && $@ !~ /attempt to reload/i;
290 }
291  
292 info "detect inheritance relationships";
293  
294 foreach my $manual (@manuals)
295 {
296 trace " relations for $manual";
297  
298 if($manual->name ne $manual->package) # autoloaded code
299 { my $main = $self->mainManual("$manual");
300 $main->extraCode($manual) if defined $main;
301 next;
302 }
303 my %uses = $manual->collectPackageRelations;
304  
305 foreach (defined $uses{isa} ? @{$uses{isa}} : ())
306 { my $isa = $self->mainManual($_) || $_;
307  
308 $manual->superClasses($isa);
309 $isa->subClasses($manual) if ref $isa;
310 }
311  
312 if(my $realizes = $uses{realizes})
313 { my $to = $self->mainManual($realizes) || $realizes;
314  
315 $manual->realizes($to);
316 $to->realizers($manual) if ref $to;
317 }
318 }
319  
320 $self;
321 }
322  
323 #-------------------------------------------
324  
325  
326 our %formatters =
327 ( pod => 'OODoc::Format::Pod'
328 , pod2 => 'OODoc::Format::Pod2'
329 , pod3 => 'OODoc::Format::Pod3'
330 , html => 'OODoc::Format::Html'
331 , html2 => 'OODoc::Format::Html2'
332 );
333  
334 sub create($@)
335 { my ($self, $format, %args) = @_;
336  
337 my $dest = $args{workdir}
338 or error __x"create requires a directory to write the manuals to";
339  
340 #
341 # Start manifest
342 #
343  
344 my $manfile = exists $args{manifest} ? $args{manifest}
345 : File::Spec->catfile($dest, 'MANIFEST');
346 my $manifest = OODoc::Manifest->new(filename => $manfile);
347  
348 # Create the formatter
349  
350 unless(ref $format)
351 { $format = $formatters{$format}
352 if exists $formatters{$format};
353  
354 eval "require $format";
355 error __x"formatter {name} has compilation errors: {err}"
356 , name => $format, err => $@ if $@;
357  
358 my $options = delete $args{format_options} || [];
359  
360 $format = $format->new
361 ( manifest => $manifest
362 , workdir => $dest
363 , project => $self->distribution
364 , version => $self->version
365 , @$options
366 );
367 }
368  
369 #
370 # Create the manual pages
371 #
372  
373 my $select = ! defined $args{select} ? sub {1}
374 : ref $args{select} eq 'CODE' ? $args{select}
375 : sub { $_[0]->name =~ $args{select}};
376  
377 foreach my $package (sort $self->packageNames)
378 {
379 foreach my $manual ($self->manualsForPackage($package))
380 { next unless $select->($manual);
381  
382 unless($manual->chapters)
383 { trace " skipping $manual: no chapters";
384 next;
385 }
386  
387 trace " creating manual $manual with ".(ref $format);
388  
389 $format->createManual
390 ( manual => $manual
391 , template => $args{manual_templates}
392 , append => $args{append}
393 , format_options => ($args{manual_format} || [])
394 );
395 }
396 }
397  
398 #
399 # Create other pages
400 #
401  
402 trace "creating other pages";
403 $format->createOtherPages
404 ( source => $args{other_templates}
405 , process => $args{process_files}
406 );
407  
408 $format;
409 }
410  
411  
412 sub stats()
413 { my $self = shift;
414 my @manuals = $self->manuals;
415 my $manuals = @manuals;
416 my $realpkg = $self->packageNames;
417  
418 my $subs = map {$_->subroutines} @manuals;
419 my @options = map { map {$_->options} $_->subroutines } @manuals;
420 my $options = @options;
421 my $examples = map {$_->examples} @manuals;
422  
423 my $diags = map {$_->diagnostics} @manuals;
424 my $distribution = $self->distribution;
425 my $version = $self->version;
426  
427 <<STATS;
428 $distribution version $version
429 Number of package manuals: $manuals
430 Real number of packages: $realpkg
431 documented subroutines: $subs
432 documented options: $options
433 documented diagnostics: $diags
434 shown examples: $examples
435 STATS
436 }
437  
438 #-------------------------------------------
439  
440  
441 1;