Commit | Line | Data |
eb8ff5ad |
1 | package CatalystX::HelpText::Script::SearchUndocumentedHelpText; |
2 | use Moose; |
3 | use Moose::Autobox; |
4 | use MooseX::Types::Path::Class qw/ Dir /; |
e3ea8b7b |
5 | use MooseX::Types::Moose qw/Str Undef HashRef ArrayRef Bool/; |
eb8ff5ad |
6 | use File::Find; |
7 | use Data::Dumper; |
8 | use Getopt::Long::Descriptive; # Force GLD as we override bits.. |
12737b61 |
9 | use MooseX::Types::LoadableClass qw/ LoadableClass /; |
e5459d76 |
10 | use Moose::Util::TypeConstraints qw/ duck_type /; |
12737b61 |
11 | use Template; |
e3ea8b7b |
12 | use List::MoreUtils qw/ uniq /; |
eb8ff5ad |
13 | use namespace::autoclean; |
14 | |
54b1774e |
15 | has finder_class => ( |
16 | isa => LoadableClass, |
17 | coerce => 1, |
18 | default => 'CatalystX::HelpText::Finder::TemplateToolkit', |
e5459d76 |
19 | handles => { |
20 | new_finder => 'new', |
21 | } |
16a04c5b |
22 | ); |
23 | |
24 | has finder => ( |
25 | isa => duck_type([qw/ find_helptext_keys_in_fn /]), |
26 | default => sub { |
27 | my $self = shift; |
360a7ff7 |
28 | $self->new_finder( |
e5459d76 |
29 | template_search_dir => $self->template_search_dir |
16a04c5b |
30 | ); |
31 | }, |
32 | lazy => 1, |
54b1774e |
33 | handles => { |
16a04c5b |
34 | _find_helptext_keys_in_fn => 'find_helptext_keys_in_fn', |
54b1774e |
35 | } |
36 | ); |
37 | |
eb8ff5ad |
38 | has help_files_path => ( |
39 | is => 'ro', |
40 | isa => Dir, |
41 | coerce => 1, |
42 | required => 1, |
43 | handles => { |
44 | _get_file => 'file', |
45 | } |
46 | ); |
47 | |
48 | has template_search_dir => ( |
49 | is => 'ro', |
50 | isa => Str, |
51 | default => './', |
52 | ); |
53 | |
54 | has filename_pattern => ( |
55 | is => 'ro', |
56 | isa => Str, |
57 | default => '\.(html|tt)$', |
58 | ); |
59 | |
60 | has help_files_ext => ( |
61 | is => 'ro', |
62 | isa => Str|Undef, |
63 | default => 'html', |
64 | ); |
65 | |
66 | sub run { |
67 | my ($self) = @_; |
e3ea8b7b |
68 | $self->print_result(); |
12737b61 |
69 | } |
70 | |
e3ea8b7b |
71 | has all_keys => ( |
72 | is => 'ro', |
73 | isa => ArrayRef[Str], |
74 | lazy => 1, |
75 | builder => '_build_all_keys', |
76 | ); |
77 | |
78 | sub _build_all_keys { |
79 | my $self = shift; |
80 | [ uniq map { $self->_find_helptext_keys_in_fn($_)->flatten } $self->all_files->flatten ]; |
81 | } |
82 | |
83 | has keys_to_helptext_exist_map => ( |
84 | isa => HashRef[Bool], |
85 | lazy => 1, |
86 | builder => '_build_keys_to_helptext_exist_map', |
87 | traits => ['Hash'], |
88 | handles => { |
89 | does_helptext_exist_for_key => 'get', |
90 | }, |
91 | ); |
92 | |
93 | sub _build_keys_to_helptext_exist_map { |
94 | my $self = shift; |
95 | return { |
96 | map { $_ => $self->_helptext_file_for_key_exists($_) } |
97 | $self->all_keys->flatten |
98 | }; |
99 | } |
100 | |
101 | |
102 | has documented_keys => ( |
103 | isa => ArrayRef[Str], |
104 | is => 'ro', |
105 | lazy => 1, |
106 | default => sub { |
107 | my $self = shift; |
108 | [ grep { $self->does_helptext_exist_for_key($_) } $self->all_keys->flatten ]; |
109 | }, |
110 | traits => ['Array'], |
111 | handles => { |
112 | has_documented_keys => 'count', |
113 | }, |
114 | ); |
115 | |
116 | has undocumented_keys => ( |
117 | isa => ArrayRef[Str], |
118 | is => 'ro', |
119 | lazy => 1, |
120 | default => sub { |
121 | my $self = shift; |
122 | [ grep { ! $self->does_helptext_exist_for_key($_) } $self->all_keys->flatten ]; |
123 | }, |
124 | traits => ['Array'], |
125 | handles => { |
126 | has_undocumented_keys => 'count', |
127 | }, |
128 | ); |
129 | |
130 | has all_files => ( |
131 | isa => ArrayRef[Str], |
132 | is => 'ro', |
133 | lazy => 1, |
134 | builder => '_build_all_files', |
135 | ); |
136 | |
137 | sub _build_all_files { |
12737b61 |
138 | my ($self) = @_; |
eb8ff5ad |
139 | my $filename_pattern = $self->filename_pattern; |
12737b61 |
140 | my @files = (); |
eb8ff5ad |
141 | find( |
142 | { |
143 | wanted => sub { |
144 | my $filename = $File::Find::name; |
145 | return unless -f $filename; |
146 | return unless $filename =~ /$filename_pattern/; |
12737b61 |
147 | push @files, $filename; |
eb8ff5ad |
148 | }, |
149 | bydepth => 1 |
150 | }, $self->template_search_dir->flatten); |
12737b61 |
151 | return [ @files ]; |
152 | } |
eb8ff5ad |
153 | |
e3ea8b7b |
154 | sub _helptext_file_for_key_exists { |
12737b61 |
155 | my ($self, $key) = @_; |
156 | my $file = $self->_get_file($key); |
157 | $file .= "." . $self->help_files_ext if defined($self->help_files_ext); |
158 | return (-e $file); |
159 | } |
160 | |
161 | sub print_result { |
e3ea8b7b |
162 | my ($self) = @_; |
163 | if ($self->has_undocumented_keys) { |
2f5dbb79 |
164 | print "Undocumented help text keys: \n"; |
e3ea8b7b |
165 | print " - $_\n" for ($self->undocumented_keys->flatten); |
2f5dbb79 |
166 | } |
eb8ff5ad |
167 | } |
168 | |
169 | with qw/ |
170 | MooseX::Getopt |
171 | /; |
172 | |
173 | __PACKAGE__->meta->make_immutable; |
174 | __PACKAGE__->new_with_options->run unless caller; |
175 | |
176 | 1; |
be01cec8 |
177 | |
178 | =head1 NAME |
179 | |
180 | CatalystX::HelpText::Script::SearchUndocumentedHelpText |
181 | |
182 | =head1 SYNOPSIS |
183 | |
184 | search_undocumented_templates.pl |
185 | |
186 | =head1 SEE ALSO |
187 | |
188 | =over |
189 | |
190 | =item L<CatalystX::HelpText> |
191 | |
192 | =back |
193 | |
194 | =head1 AUTHOR |
195 | |
196 | Toomas Doran, C<< t0m at state51.co.uk >> |
197 | |
198 | Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >> |
199 | |
200 | =head1 COPYRIGHT |
201 | |
202 | Copyright Oscar Music and Media 2011. |
203 | |
204 | =head1 LICENSE |
205 | |
206 | This sofware is free software, and is licensed under the same terms as perl itself. |
207 | |
208 | =cut |
209 | |