make-docs.pl untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 167 CC 15 SCAR 29.9
CC
15
Cov
0%
make_element_section
42.0
make_attribute_section
30.0
make_description_section
20.0
CRAP 19.9
1 #!/usr/bin/perl
2 ## ----------------------------------------------------------------------------
3 # This program is designed to create some POD for each of the XML::EPP classes
4 # contained in this distribution.
5 #
6 # It will create a new *.pod file for every XML/EPP/*.pm file it sees in %INC.
7 #
8 # Run as:
9 # $ ./make-docs.pl
10 ## ----------------------------------------------------------------------------
11  
12 use strict;
13 use warnings;
14  
15 use Data::Dumper;
16 use Pod::POM;
17 use XML::EPP::Domain;
18 use XML::EPP::Host;
19 use XML::EPP::Contact;
20 use File::Slurp;
21  
22 my $parser = Pod::POM->new();
23  
24 my @module_files = grep { m{^XML/EPP/} } keys %INC;
25  
26 foreach my $file ( @module_files ) {
27 print qq{--- $file\n};
28 my $module_name = make_module_name($file);
29 my $pom = $parser->parse("lib/$file");
30  
31 # loop through all the content and print it out as applicable
32 my $seen = {};
33 my $pod;
34 # get the docs in the following order
35 foreach my $title ( 'NAME', 'SYNOPSIS', 'DESCRIPTION' ) {
36 my $section = get_section($pom, 1, $title);
37 if ( defined $section ) {
38  
39 # for 'DESCRIPTION' check it has an 'XML Schema Definition'
40 if ( $title eq 'DESCRIPTION' ) {
41 $pod .= make_description_section($section);
42 }
43 else {
44 $pod .= $section;
45 }
46 }
47 else {
48 print qq{Warning: Couldn't find any "=head1 $title" section\n};
49 $pod .= "=head1 $title\n\n" . ($title eq 'NAME' ? $module_name : 'TO WRITE!') . "\n\n";
50 }
51 }
52  
53 # now do the XML Schema Attributes and Elements
54 $pod .= "=head2 Attributes\n\n";
55 $pod .= make_attribute_section($module_name);
56 $pod .= "=head2 Elements\n\n";
57 $pod .= make_element_section($module_name);
58  
59 # SEE ALSO
60 $pod .= "=head1 SEE ALSO\n\n";
61 $pod .= "ToMake\n\n";
62  
63 # AUTHOR AND LICENSE
64 $pod .= "=head1 AUTHOR AND LICENSE\n\n";
65 $pod .= <<'EOF';
66 Development commissioned by NZ Registry Services, and carried out by
67 Catalyst IT - L<http://www.catalyst.net.nz/>
68  
69 Copyright 2009, 2010, NZ Registry Services. This module is licensed
70 under the Artistic License v2.0, which permits relicensing under other
71 Free Software licenses.
72  
73 EOF
74  
75 # finish the pod off
76 $pod .= "=cut\n\n";
77  
78 # write out this file
79 my $outfile = "lib/$file";
80 $outfile =~ s{ \.pm \z }{.pod}gxms;
81 write_file($outfile, $pod);
82 }
83  
84 ## ----------------------------------------------------------------------------
85  
86 sub get_section {
87 my ($pom, $level, $name) = @_;
88 die "need a $level" unless defined $level;
89  
90 my $method_name = "head$level";
91 foreach my $head ( $pom->$method_name ) {
92 if ( $head->title eq $name ) {
93 return $head;
94 }
95 }
96 return;
97 }
98  
99 sub make_description_section {
100 my ($pom) = @_;
101  
102 my $pod = "=head1 DESCRIPTION\n\n";
103  
104 # firstly, print out all the sections up until the first head (of some sort)
105 my @sections = $pom->content();
106 foreach my $section ( @sections ) {
107 last if $section->type() ne 'text';
108 $pod .= $section;
109 }
110  
111 my $section = get_section($pom, 2, 'XML Schema Definition');
112 $pod .= "=head2 XML Schema Definition\n\n";
113 if ( defined $section ) {
114 unless ( $section->content =~ m{ \A \s }xms ) {
115 print qq{Warning: "=head2 XML Schema Definition" doesn't begin with a space\n};
116 }
117 $pod .= $section->content;
118 }
119 else {
120 print qq{Warning: Couldn't find a "=head2 XML Schema Definition" inside 'DESCRIPTION'\n};
121 $pod .= "TO WRITE\n\n";
122 }
123 return $pod;
124 }
125  
126 sub make_attribute_section {
127 my ($module_name) = @_;
128  
129 # my $module_name = make_module_name($file);
130  
131 my @attrs;
132 eval {
133 @attrs = grep { $_->does('PRANG::Graph::Meta::Attr') } $module_name->meta->get_all_attributes;
134 };
135 if ( $@ ) {
136 return "Failed to find any attributes on this element.\n\n";
137 }
138 unless ( @attrs ) {
139 return "There are no xml attributes on this element.\n\n";
140 }
141  
142 my $pod = "=over\n\n";
143 foreach my $attr ( @attrs ) {
144 $pod .= "=item B<$attr->{name}>\n\n";
145  
146 $pod .= "This attribute is of type '$attr->{isa}' and is ";
147 $pod .= $attr->{xml_required} ? "required." : "not required.";
148 $pod .= " It corresponds to the XML element of name '$attr->{xml_nodeName}'."
149 if $attr->{xml_nodeName};
150 $pod .= "\n\n";
151 }
152 $pod .= "=back\n\n";
153  
154 return $pod;
155 }
156  
157 sub make_element_section {
158 my ($module_name) = @_;
159  
160 # my $module_name = make_module_name($file);
161  
162 my @elems;
163 eval {
164 @elems = grep { $_->does('PRANG::Graph::Meta::Element') } $module_name->meta->get_all_attributes;
165 };
166 if ( $@ ) {
167 return "Failed to find any child elements on this element.\n\n";
168 }
169  
170 my $pod = "=over\n\n";
171 foreach my $elem ( @elems ) {
172 $pod .= "=item B<$elem->{name}>\n\n";
173  
174 $pod .= "This element is of type '$elem->{isa}' and is ";
175 $pod .= $elem->{xml_required} ? "required." : "not required.";
176 $pod .= " It corresponds to the XML element of name '$elem->{xml_nodeName}'."
177 if $elem->{xml_nodeName};
178  
179 # do min/max
180 $pod .= " It should be provided at least $elem->{xml_min} times."
181 if $elem->{xml_min};
182 $pod .= " It can be provided at most $elem->{xml_max} times."
183 if $elem->{xml_max};
184  
185 # end this section
186 $pod .= "\n\n";
187 }
188 $pod .= "=back\n\n";
189  
190 return $pod;
191 }
192  
193 sub make_module_name {
194 my ($filename) = @_;
195 $filename =~ s{\/}{::}gmxs;
196 $filename =~ s{\.pm \z }{}gmxs;
197 return $filename;
198 }
199  
200 ## ----------------------------------------------------------------------------