[libcode-tidyall-perl] 185/374: adjust plugin subclasses to be Moo classes, take arguments from config

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:15 UTC 2013


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libcode-tidyall-perl.

commit fc8dc4f3343713db03afb059c96a5a30dc1294ba
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Sep 5 22:21:18 2012 -0700

    adjust plugin subclasses to be Moo classes, take arguments from config
---
 lib/Code/TidyAll.pm                   |    4 +-
 lib/Code/TidyAll/Plugin.pm            |  124 ++++++++++++++++++++++++++-------
 lib/Code/TidyAll/Plugin/PerlCritic.pm |   24 +------
 lib/Code/TidyAll/Plugin/PerlTidy.pm   |   27 ++-----
 lib/Code/TidyAll/Plugin/PodTidy.pm    |   18 +++--
 5 files changed, 115 insertions(+), 82 deletions(-)

diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 60dcd31..55fc688 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -143,9 +143,9 @@ sub _load_plugin {
     };
 
     return $class_name->new(
-        conf    => $plugin_conf,
         name    => $plugin_name,
-        tidyall => $self
+        tidyall => $self,
+        %$plugin_conf
     );
 }
 
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index 0a4e062..5b83dc4 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -5,30 +5,42 @@ use Scalar::Util qw(weaken);
 use Moo;
 
 # External
-has 'conf'    => ( is => 'ro', required => 1 );
-has 'ignore'  => ( is => 'lazy' );
+has 'argv'    => ( is => 'ro', default => sub { '' } );
+has 'cmd'     => ( is => 'lazy' );
+has 'ignore'  => ( is => 'ro' );
 has 'name'    => ( is => 'ro', required => 1 );
-has 'select'  => ( is => 'lazy' );
+has 'select'  => ( is => 'ro' );
 has 'tidyall' => ( is => 'ro', required => 1, weak_ref => 1 );
 
 # Internal
-has 'options' => ( is => 'lazy', init_arg => undef );
+has 'ignore_regex' => ( is => 'lazy' );
+has 'select_regex' => ( is => 'lazy' );
 
-sub _build_select {
-    my $self = shift;
-    my $path = $self->conf->{select};
-    die sprintf( "select is required for '%s'", $self->name ) unless defined($path);
+sub BUILD {
+    my ( $self, $params ) = @_;
+
+    my $select = $self->{select};
+    die sprintf( "select is required for '%s'", $self->name ) unless defined($select);
     die sprintf( "select for '%s' should not begin with /", $self->name )
-      if ( substr( $path, 0, 1 ) eq '/' );
-    return $path;
+      if ( substr( $select, 0, 1 ) eq '/' );
+
+    my $ignore = $self->{ignore};
+    die sprintf( "ignore for '%s' should not begin with /", $self->name )
+      if ( defined($ignore) && substr( $ignore, 0, 1 ) eq '/' );
 }
 
-sub _build_ignore {
-    my $self = shift;
-    my $path = $self->conf->{ignore};
-    die sprintf( "select for '%s' should not begin with /", $self->name )
-      if ( defined($path) && substr( $path, 0, 1 ) eq '/' );
-    return $path;
+sub _build_cmd {
+    die "no default cmd specified";
+}
+
+sub _build_select_regex {
+    my ($self) = @_;
+    return zglob_to_regex( $self->select );
+}
+
+sub _build_ignore_regex {
+    my ($self) = @_;
+    return $self->ignore ? zglob_to_regex( $self->ignore ) : qr/(?!)/;
 }
 
 # No-ops by default; may be overridden in subclass
@@ -70,18 +82,9 @@ sub _write_temp_file {
     return $tempfile;
 }
 
-sub _build_options {
-    my $self    = shift;
-    my %options = %{ $self->{conf} };
-    delete( @options{qw(select ignore)} );
-    return \%options;
-}
-
 sub matches_path {
     my ( $self, $path ) = @_;
-    $self->{select_regex} ||= zglob_to_regex( $self->select );
-    $self->{ignore_regex} ||= ( $self->ignore ? zglob_to_regex( $self->ignore ) : qr/(?!)/ );
-    return $path =~ $self->{select_regex} && $path !~ $self->{ignore_regex};
+    return $path =~ $self->select_regex && $path !~ $self->ignore_regex;
 }
 
 1;
@@ -95,6 +98,28 @@ Code::TidyAll::Plugin - Create plugins for tidying or validating code
 
 =head1 SYNOPSIS
 
+    package Code::TidyAll::Plugin::SomeTidier;
+    use Moo;
+    extends 'Code::TidyAll::Plugin';
+    
+    sub transform_source {
+        my ( $self, source ) = @_;
+        ...
+        return $source;
+    }
+
+
+    package Code::TidyAll::Plugin::SomeValidator;
+    use Moo;
+    extends 'Code::TidyAll::Plugin';
+    
+    sub validate_file {
+        my ( $self, $file ) = @_;
+        die "not valid" if ...;
+    }
+
+=head1 DESCRIPTION
+
 To use a tidier or validator with C<tidyall> it must have a corresponding
 plugin class that inherits from this class. This document describes how to
 implement a new plugin.
@@ -115,6 +140,51 @@ with a plus sign prefix in the config file, e.g.
     [+My::Tidier::Class]
     select = **/*.{pl,pm,t}
 
+=head1 CONSTRUCTOR AND ATTRIBUTES
+
+Your plugin constructor will be called with the configuration key/value pairs
+as parameters. e.g. given
+
+    [PerlCritic]
+    select = lib/**/*.pm
+    ignore = lib/UtterHack.pm
+    argv = -severity 3
+
+then L<Code::TidyAll::Plugin::PerlCritic|Code::TidyAll::Plugin::PerlCritic>
+would be construted with parameters
+
+    select => 'lib/**/*.pm', 
+    ignore = 'lib/UtterHack.pm',
+    argv = '-severity 3'
+
+The following attributes are part of this base class. Your subclass can declare
+others, of course.
+
+=over
+
+=item argv
+
+A standard attribute for passing command line arguments.
+
+=item cmd
+
+A standard attribute for specifying the name of the command to run, e.g.
+"/usr/local/bin/perlcritic".
+
+=item name
+
+Name of the plugin to be used in error messages etc.
+
+=item tidyall
+
+A weak reference back to the L<Code::TidyAll|Code::TidyAll> object.
+
+=item select, ignore
+
+Select and ignore patterns - you can ignore these.
+
+=back
+
 =head1 METHODS
 
 Your plugin may define one or more of these methods. They are all no-ops by
@@ -147,7 +217,7 @@ be ignored.
 =item validate_file ($file)
 
 Receives filename; validates file and dies with error if invalid. Should not
-modify file!
+modify file! Return value will be ignored.
 
 =item postprocess_source ($source)
 
diff --git a/lib/Code/TidyAll/Plugin/PerlCritic.pm b/lib/Code/TidyAll/Plugin/PerlCritic.pm
index 37ba639..0e83a0a 100644
--- a/lib/Code/TidyAll/Plugin/PerlCritic.pm
+++ b/lib/Code/TidyAll/Plugin/PerlCritic.pm
@@ -1,21 +1,13 @@
 package Code::TidyAll::Plugin::PerlCritic;
 use Perl::Critic::Command qw();
 use Capture::Tiny qw(capture_merged);
-use strict;
-use warnings;
-use base qw(Code::TidyAll::Plugin);
+use Moo;
+extends 'Code::TidyAll::Plugin';
 
 sub validate_file {
     my ( $self, $file ) = @_;
-    my $options = $self->options;
 
-    # Determine arguments
-    #
-    my @argv = split( /\s/, $options->{argv} || '' );
-    push( @argv, $file );
-
-    # Run perlcritic
-    #
+    my @argv = ( split( /\s/, $self->argv ), $file );
     local @ARGV = @argv;
     my $output = capture_merged { Perl::Critic::Command::run() };
     die "$output\n" if $output !~ /^.* source OK\n/;
@@ -46,13 +38,3 @@ Code::TidyAll::Plugin::PerlCritic - use perlcritic with tidyall
    [PerlCritic]
    argv = --profile $ROOT/.perlcriticrc
    select = lib/**/*.pm
-
-=head1 OPTIONS
-
-=over
-
-=item argv
-
-Arguments to pass to perlcritic.
-
-=back
diff --git a/lib/Code/TidyAll/Plugin/PerlTidy.pm b/lib/Code/TidyAll/Plugin/PerlTidy.pm
index 04ae991..668bce6 100644
--- a/lib/Code/TidyAll/Plugin/PerlTidy.pm
+++ b/lib/Code/TidyAll/Plugin/PerlTidy.pm
@@ -1,22 +1,15 @@
 package Code::TidyAll::Plugin::PerlTidy;
 use Perl::Tidy;
-use Hash::MoreUtils qw(slice_exists);
-use strict;
-use warnings;
-use base qw(Code::TidyAll::Plugin);
+use Moo;
+extends 'Code::TidyAll::Plugin';
 
 sub transform_source {
     my ( $self, $source ) = @_;
-    my $options = $self->options;
-
-    # Determine parameters
-    #
-    my %params = slice_exists( $self->options, qw(argv) );
 
     my $errorfile;
     no strict 'refs';
     Perl::Tidy::perltidy(
-        %params,
+        argv        => $self->argv,
         source      => \$source,
         destination => \my $destination,
         errorfile   => \$errorfile
@@ -41,22 +34,12 @@ Code::TidyAll::Plugin::PerlTidy - use perltidy with tidyall
 
    # Configure in-line
    #
-   [Perltidy]
+   [PerlTidy]
    argv = --noll
    select = lib/**/*.pm
 
    # or refer to a .perltidyrc in the same directory
    #
-   [Perltidy]
+   [PerlTidy]
    argv = --profile=$ROOT/.perltidyrc
    select = lib/**/*.pm
-
-=head1 OPTIONS
-
-=over
-
-=item argv
-
-Arguments to pass to C<perltidy>.
-
-=back
diff --git a/lib/Code/TidyAll/Plugin/PodTidy.pm b/lib/Code/TidyAll/Plugin/PodTidy.pm
index a6c6b8e..23cdc66 100644
--- a/lib/Code/TidyAll/Plugin/PodTidy.pm
+++ b/lib/Code/TidyAll/Plugin/PodTidy.pm
@@ -1,23 +1,21 @@
 package Code::TidyAll::Plugin::PodTidy;
 use Capture::Tiny qw(capture_merged);
-use Hash::MoreUtils qw(slice_exists);
 use Pod::Tidy;
-use strict;
-use warnings;
-use base qw(Code::TidyAll::Plugin);
+use Moo;
+extends 'Code::TidyAll::Plugin';
+
+has 'columns' => ( is => 'ro' );
 
 sub transform_file {
     my ( $self, $file ) = @_;
-    my $options = $self->options;
 
-    my %params = slice_exists( $self->options, qw(columns) );
     my $output = capture_merged {
         Pod::Tidy::tidy_files(
-            %params,
             files    => [$file],
             inplace  => 1,
             nobackup => 1,
             verbose  => 1,
+            ( $self->columns ? ( columns => $self->columns ) : () ),
         );
     };
     die $output if $output =~ /\S/ && $output !~ /does not contain Pod/;
@@ -38,15 +36,15 @@ Code::TidyAll::Plugin::PodTidy - use podtidy with tidyall
    # In tidyall.ini:
 
    [PodTidy]
-   argv = --column=90
    select = lib/**/*.{pm,pod}
+   columns = 90
 
 =head1 OPTIONS
 
 =over
 
-=item argv
+=item columns
 
-Arguments to pass to podtidy.
+Number of columns to fill
 
 =back

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libcode-tidyall-perl.git



More information about the Pkg-perl-cvs-commits mailing list