[libcode-tidyall-perl] 55/374: add prefilter, postfilter; allow tidyall_class to be specified in conf file

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:48 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 2082fda0b2141e608e9c80d5da7ddec52077cfe9
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Wed Jun 27 09:35:28 2012 -0700

    add prefilter, postfilter; allow tidyall_class to be specified in conf file
---
 bin/tidyall         |   16 +++++++---------
 lib/Code/TidyAll.pm |   53 +++++++++++++++++++++++++++++++++++++--------------
 2 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/bin/tidyall b/bin/tidyall
index 8a04788..3a9be3a 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -2,6 +2,7 @@
 use Cwd qw(cwd realpath);
 use Getopt::Long;
 use Pod::Usage;
+use Code::TidyAll;
 use Code::TidyAll::Util qw(can_load dirname);
 use Hash::MoreUtils qw(slice_def);
 use strict;
@@ -15,11 +16,9 @@ sub usage {
 }
 
 my ( %params, $help, $all );
-my $class = 'Code::TidyAll';
 
 GetOptions(
     'backup-ttl=i'    => \$params{backup_ttl},
-    'class=s'         => \$class,
     'conf-file=s'     => \$params{conf_file},
     'data-dir=s'      => \$params{data_dir},
     'no-backups'      => \$params{no_backups},
@@ -28,6 +27,7 @@ GetOptions(
     'output-suffix=s' => \$params{output_suffix},
     'refresh-cache'   => \$params{refresh_cache},
     'root-dir=s'      => \$params{root_dir},
+    'tidyall-class=s' => \$params{tidyall_class},
     'a|all'           => \$all,
     'h|help'          => \$help,
     'q|quiet'         => \$params{quiet},
@@ -36,8 +36,6 @@ GetOptions(
 
 Pod::Usage::pod2usage( { verbose => 2 } ) if $help;
 
-die "cannot load '$class'" unless can_load($class);
-
 %params = slice_def( \%params );
 
 $params{conf_file} ||= "$params{root_dir}/tidyall.ini" if ( $params{root_dir} );
@@ -45,14 +43,14 @@ $params{only_plugins} = [ split( /\s*,\s*/, $params{only_plugins} ) ] if $params
 
 my $result;
 if ($all) {
-    $params{conf_file} ||= $class->find_conf_file( cwd() );
-    my $ct = $class->new(%params);
+    $params{conf_file} ||= Code::TidyAll->find_conf_file( cwd() );
+    my $ct = Code::TidyAll->new(%params);
     $result = $ct->process_all();
 }
 else {
     my @files = @ARGV or die "file(s) or -a required";
-    $params{conf_file} ||= $class->find_conf_file( dirname( $files[0] ) );
-    my $ct = $class->new(%params);
+    $params{conf_file} ||= Code::TidyAll->find_conf_file( dirname( $files[0] ) );
+    my $ct = Code::TidyAll->new(%params);
     $result = $ct->process_files(@files);
 }
 
@@ -100,7 +98,6 @@ tidyall - Your all-in-one code tidier and validator
  -q, --quiet      Suppress output except for errors
  -v, --verbose    Show extra output
  --backup-ttl     When backup files can be purged. Defaults to "1h"
- --class          Code::TidyAll subclass to use. Defaults to "Code::TidyAll"
  --conf-file      Specify conf file explicitly; usually inferred from specified files or cwd
  --data-dir       Contains data like backups and cache. Defaults to root_dir/.tidyall.d
  --no-backups     Don't backup files
@@ -108,6 +105,7 @@ tidyall - Your all-in-one code tidier and validator
  --output-suffix  Suffix to add to output file, e.g. "tdy"; default is none (overwrite file)
  --refresh-cache  Erase any existing cache info before processing each file
  --root-dir       Specify root dir explicitly; usually inferred from specified files or cwd
+ --tidyall-class  Code::TidyAll subclass to use. Defaults to "Code::TidyAll"
 
 =head1 DESCRIPTION
 
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index b8a3b30..8115b06 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -24,9 +24,11 @@ sub valid_params {
       only_plugins
       output_suffix
       plugins
+      postfilter
+      prefilter
+      quiet
       refresh_cache
       root_dir
-      quiet
       verbose
     );
 }
@@ -50,19 +52,11 @@ sub new {
     my $class  = shift;
     my %params = @_;
 
-    # Check param validity
-    #
-    my $valid_params_hash = $valid_params_hash{$class} ||=
-      { map { ( $_, 1 ) } $class->valid_params() };
-    if ( my @bad_params = grep { !$valid_params_hash->{$_} } keys(%params) ) {
-        die sprintf( "unknown constructor param(s) %s",
-            join( ", ", sort map { "'$_'" } @bad_params ) );
-    }
-
     # Read params from conf file
     #
-    if ( my $conf_file = $params{conf_file} ) {
+    if ( my $conf_file = delete( $params{conf_file} ) ) {
         my $conf_params = $class->_read_conf_file($conf_file);
+        my $main_params = delete( $conf_params->{'_'} ) || {};
         if ( my $only_plugins = $params{only_plugins} ) {
             $conf_params = {
                 map {
@@ -72,7 +66,6 @@ sub new {
                 } @$only_plugins
             };
         }
-        my $main_params = delete( $conf_params->{'_'} ) || {};
         %params = (
             plugins  => $conf_params,
             root_dir => realpath( dirname($conf_file) ),
@@ -85,6 +78,22 @@ sub new {
         $params{root_dir} = realpath( $params{root_dir} );
     }
 
+    # Initialize with alternate class if given
+    #
+    if ( my $tidyall_class = delete( $params{tidyall_class} ) ) {
+        die "cannot load '$tidyall_class'" unless can_load($tidyall_class);
+        return $tidyall_class->new(%params);
+    }
+
+    # Check param validity
+    #
+    my $valid_params_hash = $valid_params_hash{$class} ||=
+      { map { ( $_, 1 ) } $class->valid_params() };
+    if ( my @bad_params = grep { !$valid_params_hash->{$_} } keys(%params) ) {
+        die sprintf( "unknown constructor param(s) %s",
+            join( ", ", sort map { "'$_'" } @bad_params ) );
+    }
+
     $class->msg( "constructing %s with these params: %s", $class, \%params )
       if ( $params{verbose} );
 
@@ -138,7 +147,7 @@ sub load_plugin {
 sub process_all {
     my $self = shift;
 
-    return $self->process_files( keys( %{ $self->matched_files } ) );
+    return $self->process_files( sort keys( %{ $self->matched_files } ) );
 }
 
 sub process_files {
@@ -175,6 +184,8 @@ sub _process_file {
         }
     }
 
+    $new_contents = $self->prefilter->($new_contents) if $self->prefilter;
+
     foreach my $plugin (@plugins) {
         try {
             $new_contents = $plugin->process_source_or_file( $new_contents, $file );
@@ -185,7 +196,9 @@ sub _process_file {
         last if $error;
     }
 
-    my $was_tidied = $orig_contents ne $new_contents;
+    $new_contents = $self->postfilter->($new_contents) if $self->postfilter;
+
+    my $was_tidied = ( $orig_contents ne $new_contents ) && !$error;
     unless ( $self->quiet ) {
         my $status = $was_tidied ? "[tidied]  " : "[checked] ";
         my $plugin_names =
@@ -403,6 +416,18 @@ 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 conf_file

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