Make an instance of the finder
[catagits/CatalystX-HelpText.git] / lib / CatalystX / HelpText / Script / SearchUndocumentedHelpText.pm
index 4599d87..01ba939 100644 (file)
@@ -2,12 +2,35 @@ package CatalystX::HelpText::Script::SearchUndocumentedHelpText;
 use Moose;
 use Moose::Autobox;
 use MooseX::Types::Path::Class qw/ Dir /;
-use MooseX::Types::Moose qw/Str Undef/;
+use MooseX::Types::Moose qw/Str Undef HashRef ArrayRef Bool/;
 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 List::MoreUtils qw/ uniq /;
 use namespace::autoclean;
 
+has finder_class => (
+    isa => LoadableClass,
+    coerce => 1,
+    default => 'CatalystX::HelpText::Finder::TemplateToolkit',
+    handles => "_construct_finder",
+);
+
+has finder => (
+    isa => duck_type([qw/ find_helptext_keys_in_fn /]),
+    default => sub {
+        my $self = shift;
+        $self->_construct_finder( # Args here
+        );
+    },
+    lazy => 1,
+    handles => {
+        _find_helptext_keys_in_fn => 'find_helptext_keys_in_fn',
+    }
+);
+
 has help_files_path => (
     is => 'ro',
     isa => Dir,
@@ -38,35 +61,105 @@ has help_files_ext => (
 
 sub run {
     my ($self) = @_;
+    $self->print_result();
+}
+
+has all_keys => (
+    is => 'ro',
+    isa => ArrayRef[Str],
+    lazy => 1,
+    builder => '_build_all_keys',
+);
+
+sub _build_all_keys {
+    my $self = shift;
+    [ uniq map { $self->_find_helptext_keys_in_fn($_)->flatten } $self->all_files->flatten ];
+}
+
+has keys_to_helptext_exist_map => (
+    isa => HashRef[Bool],
+    lazy => 1,
+    builder => '_build_keys_to_helptext_exist_map',
+    traits => ['Hash'],
+    handles => {
+        does_helptext_exist_for_key => 'get',
+    },
+);
+
+sub _build_keys_to_helptext_exist_map {
+    my $self = shift;
+    return {
+        map { $_ => $self->_helptext_file_for_key_exists($_) }
+        $self->all_keys->flatten
+    };
+}
+
+
+has documented_keys => (
+    isa => ArrayRef[Str],
+    is => 'ro',
+    lazy => 1,
+    default => sub {
+        my $self = shift;
+        [ grep { $self->does_helptext_exist_for_key($_) } $self->all_keys->flatten ];
+    },
+    traits => ['Array'],
+    handles => {
+        has_documented_keys => 'count',
+    },
+);
+
+has undocumented_keys => (
+    isa => ArrayRef[Str],
+    is => 'ro',
+    lazy => 1,
+    default => sub {
+        my $self = shift;
+        [ grep { ! $self->does_helptext_exist_for_key($_) } $self->all_keys->flatten ];
+    },
+    traits => ['Array'],
+    handles => {
+        has_undocumented_keys => 'count',
+    },
+);
+
+has all_files => (
+    isa => ArrayRef[Str],
+    is => 'ro',
+    lazy => 1,
+    builder => '_build_all_files',
+);
+
+sub _build_all_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 ];
+}
 
-    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 _helptext_file_for_key_exists {
+    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) = @_;
+    if ($self->has_undocumented_keys) {
+        print "Undocumented help text keys: \n";
+        print " - $_\n" for ($self->undocumented_keys->flatten);
     }
-    print "\nMissing Help Text files:\n" if (scalar @notfound);
-    print " - $_\n" for (@notfound);
 }
 
 with qw/
@@ -77,3 +170,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
+