refactored script
Cinxgler Mariaca Minda [Mon, 3 Oct 2011 16:33:33 +0000 (17:33 +0100)]
lib/CatalystX/HelpText/Script/SearchUndocumentedHelpText.pm

index 251c076..1341335 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,47 +41,61 @@ 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)) {
+                push @undocumented_keys, $key
+            }
+        }
+    }
+    $self->print_result([ @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/;
-
-                #FIXME - Not a regex here, we should actually pass the templates through TT
-                #        and pass in our own callback!
-                # I guess the 'here is a file name, hand me the list of links in it'
-                # stuff should be delegated to another class (which is settable by command line)
-                # to allow people to have alternative methods of using this
-                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 ];
+}
 
-    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);
-    }
-    # XXX FIXME - This method is way too big!
-    # It'd be useless if you wanted to customise it by sub-classing (which we _will_)
-    # later want to do as part of our build process!
-    # We should have a method to find a list of all the potential files
-    # then a for each file method, which calls to the delegate class and
-    # then we should build a hash of all the filemame => [qw/ helptext1 helptext 2 /]
-    # and then finally we should hand that to a method for pretty printing
-    # (and that method needs to unique the finds, which we don't do at the moment)
-    print "\nMissing Help Text files:\n" if (scalar @notfound);
-    print " - $_\n" for (@notfound);
+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);
+}
+
+sub print_result {
+    my ($self, $undocumented_keys, $file_vs_keys) = @_;
+    print "Undocumented help text keys: \n";
+    print " - $_" for ($undocumented_keys->flatten);
 }
 
 with qw/