little display fixes
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / Script / SearchUndocumentedHelpText.pm
index 4599d87..109c5cf 100644 (file)
@@ -6,6 +6,9 @@ use MooseX::Types::Moose qw/Str Undef/;
 use File::Find;
 use Data::Dumper;
 use Getopt::Long::Descriptive; # Force GLD as we override bits..
+use MooseX::Types::LoadableClass qw/ LoadableClass /;
+use Template;
+use Capture::Tiny qw/capture/;
 use namespace::autoclean;
 
 has help_files_path => (
@@ -38,35 +41,63 @@ has help_files_ext => (
 
 sub run {
     my ($self) = @_;
+    my $file_vs_keys = {};
+    my $undocumented_keys = {};
+    foreach my $fn ($self->find_files->flatten) {
+        $file_vs_keys->{$fn} = $self->find_helptext_keys($fn);
+        foreach my $key ($file_vs_keys->{$fn}->flatten) {
+            unless ($self->is_there_helptext_file_for_key($key)) {
+                $undocumented_keys->{$key} = 1;
+            }
+        }
+    }
+    $self->print_result([ keys %$undocumented_keys ], $file_vs_keys);
+}
+
+sub find_files {
+    my ($self) = @_;
     my $filename_pattern = $self->filename_pattern;
-    my %helpkeys = ();
+    my @files = ();
     find(
         {
             wanted => sub {
                 my $filename = $File::Find::name;
                 return unless -f $filename;
                 return unless $filename =~ /$filename_pattern/;
-
-                #warn $filename;
-                open(FILE, $filename) or warn "Can't open $filename\n" && return;
-                while (<FILE>) {
-                    if (my ($key) = m/help_text\('(.*)'\)/o) {
-                        $helpkeys{$key} = 1;
-                    }
-                }
-                close(FILE);
+                push @files, $filename;
             },
             bydepth => 1
         }, $self->template_search_dir->flatten);
+    return [ @files ];
+}
+
+sub find_helptext_keys {
+    my ($self, $fn) = @_;
+    my $dir = $self->template_search_dir;
+    my @keys = ();
+    my $t = Template->new({
+        INCLUDE_PATH => [ $self->template_search_dir ],
+        ABSOLUTE => 1,
+    });
+    my ($stdout, $stderr) = capture {
+        $t->process($fn, {  help_text => sub { push @keys, shift } });
+    };
+    return [ @keys ];
+}
+
+sub is_there_helptext_file_for_key {
+    my ($self, $key) = @_;
+    my $file = $self->_get_file($key);
+    $file .= "." . $self->help_files_ext if defined($self->help_files_ext);
+    return (-e $file);
+}
 
-    my @notfound = ();
-    foreach (keys %helpkeys) {
-        my $file = $self->_get_file($_);
-        $file .= "." . $self->help_files_ext if defined($self->help_files_ext);
-        push (@notfound, $file) unless (-e $file);
+sub print_result {
+    my ($self, $undocumented_keys, $file_vs_keys) = @_;
+    if (scalar @$undocumented_keys) {
+        print "Undocumented help text keys: \n";
+        print " - $_" for ($undocumented_keys->flatten);
     }
-    print "\nMissing Help Text files:\n" if (scalar @notfound);
-    print " - $_\n" for (@notfound);
 }
 
 with qw/
@@ -77,3 +108,36 @@ __PACKAGE__->meta->make_immutable;
 __PACKAGE__->new_with_options->run unless caller;
 
 1;
+
+=head1 NAME
+
+CatalystX::HelpText::Script::SearchUndocumentedHelpText
+
+=head1 SYNOPSIS
+
+    search_undocumented_templates.pl
+
+=head1 SEE ALSO
+
+=over
+
+=item L<CatalystX::HelpText>
+
+=back
+
+=head1 AUTHOR
+
+Toomas Doran, C<< t0m at state51.co.uk >>
+
+Cinxgler Mariaca Minda, C<< cinxgler at ci-info.com >>
+
+=head1 COPYRIGHT
+
+Copyright Oscar Music and Media 2011.
+
+=head1 LICENSE
+
+This sofware is free software, and is licensed under the same terms as perl itself.
+
+=cut
+