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