[libcode-tidyall-perl] 107/374: change method names to transform_ and validate_; implement plugin preprocess and postprocess

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:58 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 0511ed09cc539125e04f2c3cce57538f262401fd
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Jul 18 17:08:36 2012 -0700

    change method names to transform_ and validate_; implement plugin preprocess and postprocess
---
 lib/Code/TidyAll.pm                        |   48 +++++++++---------------
 lib/Code/TidyAll/Plugin.pm                 |   55 ++++++++++++++++------------
 lib/Code/TidyAll/Plugin/PerlTidy.pm        |    2 +-
 lib/Code/TidyAll/Plugin/PodTidy.pm         |    2 +-
 lib/Code/TidyAll/Test/Plugin/RepeatFoo.pm  |    2 +-
 lib/Code/TidyAll/Test/Plugin/ReverseFoo.pm |    2 +-
 lib/Code/TidyAll/Test/Plugin/UpperText.pm  |    2 +-
 7 files changed, 55 insertions(+), 58 deletions(-)

diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index ccc1514..7f88fe5 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -27,8 +27,6 @@ sub valid_params {
       no_cache
       output_suffix
       plugins
-      postfilter
-      prefilter
       quiet
       refresh_cache
       root_dir
@@ -198,26 +196,28 @@ sub process_source {
     my $basename = basename($path);
     my $error;
 
-    my $orig_contents = $contents;
-    $contents = $self->prefilter->($contents) if $self->prefilter;
-    foreach my $plugin (@plugins) {
-        try {
-            my $new_contents = $plugin->process_source_or_file( $contents, $basename );
-            if ( $new_contents ne $contents ) {
-                die "needs tidying\n" if $self->check_only;
-                $contents = $new_contents;
+    my $new_contents = my $orig_contents = $contents;
+    my $plugin;
+
+    try {
+        foreach my $method (qw(preprocess_source process_source_or_file postprocess_source)) {
+            foreach $plugin (@plugins) {
+                $new_contents = $plugin->$method( $new_contents, $basename );
             }
         }
-        catch {
-            chomp;
-            $error = sprintf( "*** '%s': %s", $plugin->name, $_ );
-        };
-        last if $error;
     }
-    $contents = $self->postfilter->($contents) if !$error && $self->postfilter;
-    my $new_contents = $contents;
+    catch {
+        chomp;
+        $error = $_;
+        $error = sprintf( "*** '%s': %s", $plugin->name, $_ ) if $plugin;
+    };
 
     my $was_tidied = !$error && ( $new_contents ne $orig_contents );
+    if ( $was_tidied && $self->check_only ) {
+        $error = "*** needs tidying";
+        undef $was_tidied;
+    }
+
     unless ( $self->quiet ) {
         my $status = $was_tidied ? "[tidied]  " : "[checked] ";
         my $plugin_names =
@@ -452,18 +452,6 @@ You must either pass C<conf_file>, or both C<plugins> and C<root_dir>.
 Specify a hash of plugins, each of which is itself a hash of options. This is
 equivalent to what would be parsed out of the sections in C<tidyall.ini>.
 
-=item prefilter
-
-A code reference that will be applied to code before processing. It is expected
-to take the full content as a string in its input, and output the transformed
-content.
-
-=item postfilter
-
-A code reference that will be applied to code after processing. It is expected
-to take the full content as a string in its input, and output the transformed
-content.
-
 =item backup_ttl
 
 =item check_only
@@ -515,7 +503,7 @@ Check the cache and return immediately if file has not changed
 
 =item *
 
-Apply prefilters, appropriate matching plugins, and postfilters
+Apply appropriate matching plugins
 
 =item *
 
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index b525d65..b91701b 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -26,31 +26,35 @@ sub new {
     return $self;
 }
 
+# No-ops by default; may be overridden in subclass
+sub preprocess_source {
+    return $_[1];
+}
+
+sub postprocess_source {
+    return $_[1];
+}
+
 sub process_source_or_file {
     my ( $self, $source, $basename ) = @_;
 
-    if ( $self->can('process_source') ) {
-        return $self->process_source($source);
+    if ( $self->can('transform_source') ) {
+        $source = $self->transform_source($source);
     }
-    elsif ( $self->can('process_file') ) {
+    if ( $self->can('transform_file') ) {
         my $tempfile = $self->_write_temp_file( $basename, $source );
-        $self->process_file($tempfile);
-        return read_file($tempfile);
+        $self->transform_file($tempfile);
+        $source = read_file($tempfile);
     }
-    elsif ( $self->can('validate_source') ) {
+    if ( $self->can('validate_source') ) {
         $self->validate_source($source);
-        return $source;
     }
-    elsif ( $self->can('validate_file') ) {
+    if ( $self->can('validate_file') ) {
         my $tempfile = $self->_write_temp_file( $basename, $source );
         $self->validate_file($tempfile);
-        return $source;
-    }
-    else {
-        die sprintf(
-            "plugin '%s' must implement one of process_file, process_source, validate_file, or validate_source",
-            $self->name );
     }
+
+    return $source;
 }
 
 sub _write_temp_file {
@@ -108,21 +112,21 @@ with a plus sign prefix in the config file, e.g.
 
 =head1 METHODS
 
-Your class should define I<one and only one> of these methods. The first two
-methods are for tidiers (which actually modify code); the second two are for
-validators (which simply check code for errors). C<tidyall> can be a bit more
-efficient with the latter, e.g. avoid a file copy.
+Your plugin may define one or more of these methods. They are all no-ops by default.
 
-=over
-
-=item process_source ($source)
+=item preprocess_source ($source)
 
 Receives source code as a string; returns the processed string, or dies with
+error. This runs on all plugins I<before> any of the other methods.
+
+=item transform_source ($source)
+
+Receives source code as a string; returns the transformed string, or dies with
 error.
 
-=item process_file ($file)
+=item transform_file ($file)
 
-Receives filename; processes the file in place, or dies with error. Note that
+Receives filename; transforms the file in place, or dies with error. Note that
 the file will be a temporary copy of the user's file with the same basename;
 your changes will only propagate back if there was no error reported from any
 plugin.
@@ -137,4 +141,9 @@ be ignored.
 Receives filename; validates file and dies with error if invalid. Should not
 modify file!
 
+=item postprocess_source ($source)
+
+Receives source code as a string; returns the processed string, or dies with
+error. This runs on all plugins I<after> any of the other methods.
+
 =back
diff --git a/lib/Code/TidyAll/Plugin/PerlTidy.pm b/lib/Code/TidyAll/Plugin/PerlTidy.pm
index 14d907f..04ae991 100644
--- a/lib/Code/TidyAll/Plugin/PerlTidy.pm
+++ b/lib/Code/TidyAll/Plugin/PerlTidy.pm
@@ -5,7 +5,7 @@ use strict;
 use warnings;
 use base qw(Code::TidyAll::Plugin);
 
-sub process_source {
+sub transform_source {
     my ( $self, $source ) = @_;
     my $options = $self->options;
 
diff --git a/lib/Code/TidyAll/Plugin/PodTidy.pm b/lib/Code/TidyAll/Plugin/PodTidy.pm
index 02d1490..a6c6b8e 100644
--- a/lib/Code/TidyAll/Plugin/PodTidy.pm
+++ b/lib/Code/TidyAll/Plugin/PodTidy.pm
@@ -6,7 +6,7 @@ use strict;
 use warnings;
 use base qw(Code::TidyAll::Plugin);
 
-sub process_file {
+sub transform_file {
     my ( $self, $file ) = @_;
     my $options = $self->options;
 
diff --git a/lib/Code/TidyAll/Test/Plugin/RepeatFoo.pm b/lib/Code/TidyAll/Test/Plugin/RepeatFoo.pm
index 98d952c..de79671 100644
--- a/lib/Code/TidyAll/Test/Plugin/RepeatFoo.pm
+++ b/lib/Code/TidyAll/Test/Plugin/RepeatFoo.pm
@@ -4,7 +4,7 @@ use base qw(Code::TidyAll::Plugin);
 use strict;
 use warnings;
 
-sub process_source {
+sub transform_source {
     my ( $self, $source ) = @_;
     my $times = $self->options->{times} || die "no times specified";
     return $source x $times;
diff --git a/lib/Code/TidyAll/Test/Plugin/ReverseFoo.pm b/lib/Code/TidyAll/Test/Plugin/ReverseFoo.pm
index cebe571..8aa5abf 100644
--- a/lib/Code/TidyAll/Test/Plugin/ReverseFoo.pm
+++ b/lib/Code/TidyAll/Test/Plugin/ReverseFoo.pm
@@ -4,7 +4,7 @@ use base qw(Code::TidyAll::Plugin);
 use strict;
 use warnings;
 
-sub process_file {
+sub transform_file {
     my ( $self, $file ) = @_;
     write_file( $file, scalar( reverse( read_file($file) ) ) );
 }
diff --git a/lib/Code/TidyAll/Test/Plugin/UpperText.pm b/lib/Code/TidyAll/Test/Plugin/UpperText.pm
index 5edea5d..d0893d8 100644
--- a/lib/Code/TidyAll/Test/Plugin/UpperText.pm
+++ b/lib/Code/TidyAll/Test/Plugin/UpperText.pm
@@ -3,7 +3,7 @@ use base qw(Code::TidyAll::Plugin);
 use strict;
 use warnings;
 
-sub process_source {
+sub transform_source {
     my ( $self, $source ) = @_;
     if ( $source =~ /^[A-Z]*$/i ) {
         return uc($source);

-- 
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