Merge branch 'master' of git.shadowcat.co.uk:CatalystX-HelpText
[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 /;
5use MooseX::Types::Moose qw/Str Undef/;
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;
11use Capture::Tiny qw/capture/;
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) = @_;
12737b61 44 my $file_vs_keys = {};
45 my @undocumented_keys = ();
46 foreach my $fn ($self->find_files->flatten) {
47 $file_vs_keys->{$fn} = $self->find_helptext_keys($fn);
48 foreach my $key ($file_vs_keys->{$fn}->flatten) {
49 unless ($self->is_there_helptext_file_for_key($key)) {
50 push @undocumented_keys, $key
51 }
52 }
53 }
54 $self->print_result([ @undocumented_keys ], $file_vs_keys);
55}
56
57sub find_files {
58 my ($self) = @_;
eb8ff5ad 59 my $filename_pattern = $self->filename_pattern;
12737b61 60 my @files = ();
eb8ff5ad 61 find(
62 {
63 wanted => sub {
64 my $filename = $File::Find::name;
65 return unless -f $filename;
66 return unless $filename =~ /$filename_pattern/;
12737b61 67 push @files, $filename;
eb8ff5ad 68 },
69 bydepth => 1
70 }, $self->template_search_dir->flatten);
12737b61 71 return [ @files ];
72}
eb8ff5ad 73
12737b61 74sub find_helptext_keys {
75 my ($self, $fn) = @_;
76 my $dir = $self->template_search_dir;
77 my @keys = ();
78 my $t = Template->new({
79 INCLUDE_PATH => [ $self->template_search_dir ],
80 ABSOLUTE => 1,
81 });
82 my ($stdout, $stderr) = capture {
83 $t->process($fn, { help_text => sub { push @keys, shift } });
84 };
85 return [ @keys ];
86}
87
88sub is_there_helptext_file_for_key {
89 my ($self, $key) = @_;
90 my $file = $self->_get_file($key);
91 $file .= "." . $self->help_files_ext if defined($self->help_files_ext);
92 return (-e $file);
93}
94
95sub print_result {
96 my ($self, $undocumented_keys, $file_vs_keys) = @_;
97 print "Undocumented help text keys: \n";
98 print " - $_" for ($undocumented_keys->flatten);
eb8ff5ad 99}
100
101with qw/
102 MooseX::Getopt
103/;
104
105__PACKAGE__->meta->make_immutable;
106__PACKAGE__->new_with_options->run unless caller;
107
1081;
be01cec8 109
110=head1 NAME
111
112CatalystX::HelpText::Script::SearchUndocumentedHelpText
113
114=head1 SYNOPSIS
115
116 search_undocumented_templates.pl
117
118=head1 SEE ALSO
119
120=over
121
122=item L<CatalystX::HelpText>
123
124=back
125
126=head1 AUTHOR
127
128Toomas Doran, C<< t0m at state51.co.uk >>
129
130Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >>
131
132=head1 COPYRIGHT
133
134Copyright Oscar Music and Media 2011.
135
136=head1 LICENSE
137
138This sofware is free software, and is licensed under the same terms as perl itself.
139
140=cut
141