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