01ba939b05620f12779d4f819e4fd7c333ae66a3
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / Script / SearchUndocumentedHelpText.pm
1 package CatalystX::HelpText::Script::SearchUndocumentedHelpText;
2 use Moose;
3 use Moose::Autobox;
4 use MooseX::Types::Path::Class qw/ Dir /;
5 use MooseX::Types::Moose qw/Str Undef HashRef ArrayRef Bool/;
6 use File::Find;
7 use Data::Dumper;
8 use Getopt::Long::Descriptive; # Force GLD as we override bits..
9 use MooseX::Types::LoadableClass qw/ LoadableClass /;
10 use Template;
11 use List::MoreUtils qw/ uniq /;
12 use namespace::autoclean;
13
14 has finder_class => (
15     isa => LoadableClass,
16     coerce => 1,
17     default => 'CatalystX::HelpText::Finder::TemplateToolkit',
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,
29     handles => {
30         _find_helptext_keys_in_fn => 'find_helptext_keys_in_fn',
31     }
32 );
33
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) = @_;
64     $self->print_result();
65 }
66
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 {
134     my ($self) = @_;
135     my $filename_pattern = $self->filename_pattern;
136     my @files = ();
137     find(
138         {
139             wanted => sub {
140                 my $filename = $File::Find::name;
141                 return unless -f $filename;
142                 return unless $filename =~ /$filename_pattern/;
143                 push @files, $filename;
144             },
145             bydepth => 1
146         }, $self->template_search_dir->flatten);
147     return [ @files ];
148 }
149
150 sub _helptext_file_for_key_exists {
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 {
158     my ($self) = @_;
159     if ($self->has_undocumented_keys) {
160         print "Undocumented help text keys: \n";
161         print " - $_\n" for ($self->undocumented_keys->flatten);
162     }
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;
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