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