[libcode-tidyall-perl] 280/374: Add --iterations flag to run tidier transforms multiple times

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:26:35 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 0d8823236517f32f9153936a5880a62948974a18
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Thu Sep 27 12:46:15 2012 -0700

    Add --iterations flag to run tidier transforms multiple times
---
 Changes                     |    1 +
 bin/tidyall                 |   17 +++++++++++++++++
 lib/Code/TidyAll.pm         |    3 +++
 lib/Code/TidyAll/Plugin.pm  |   14 ++++++++++----
 lib/Code/TidyAll/t/Basic.pm |   13 +++++++++++++
 5 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/Changes b/Changes
index e368f0e..b25acd8 100644
--- a/Changes
+++ b/Changes
@@ -8,6 +8,7 @@ Revision history for Code-TidyAll
   - Add podspell plugin (POD spell checker)
 
 * Improvements
+  - Add --iterations flag to run tidier transforms multiple times
   - Allow .tidyallrc as alternative to tidyall.ini
   - Allow git prereceive hook to be bypassed by pushing an identical set of commits
     several consecutive times (allow_repeated_push)
diff --git a/bin/tidyall b/bin/tidyall
index a8a54d5..f74b581 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -25,6 +25,7 @@ Usage: tidyall [options] [file] ...
    --conf-file <path>       Relative or absolute path to conf file
    --conf-name <name>       Conf file name to search for
    --data-dir <path>        Contains metadata, defaults to root/.tidyall.d
+   --iterations <count>     Number of times to repeat each transform - default is 1
    --no-backups             Don\'t backup files before processing
    --no-cache               Don\'t cache last processed times
    --output-suffix <suffix> Suffix to add to tidied file
@@ -500,6 +501,21 @@ Specify a conf file name to search for instead of the defaults (C<tidyall.ini>
 
 Contains data like backups and cache. Defaults to root_dir/.tidyall.d
 
+=item --iterations I<count>
+
+Run each tidier transform I<count> times. Default is 1.
+
+In some cases (hopefully rare) the output from a tidier can be different if it
+is applied multiple times. You may want to perform multiple iterations to make
+sure the content "settles" into its final tidied form -- especially if the
+tidiness is being enforced with a version-control hook or a test. Of course,
+performance will suffer a little. You should rarely need to set this higher
+than 2.
+
+This only affects tidiers, not validators; e.g.
+L<perlcritic|Code::TidyAll::Plugin::PerlCritic> and
+L<jshint|Code::TidyAll::Plugin::JSHint> would still only be run once.
+
 =item --no-backups
 
 Don't backup files before processing.
@@ -536,6 +552,7 @@ Almost any command-line option can be specified at the top of the config file,
 above the plugin sections. Replace dashes with underscores. e.g.
 
     backup_ttl = 4h
+    iterations = 2
     tidyall_class = My::Code::TidyAll
 
     [PerlTidy]
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index ff09447..8568ee5 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -22,6 +22,7 @@ sub default_conf_names { ( 'tidyall.ini', '.tidyallrc' ) }
 has 'backup_ttl'    => ( is => 'ro', default => sub { '1 hour' } );
 has 'check_only'    => ( is => 'ro' );
 has 'data_dir'      => ( is => 'lazy' );
+has 'iterations'    => ( is => 'ro', default => sub { 1 } );
 has 'mode'          => ( is => 'ro', default => sub { 'cli' } );
 has 'no_backups'    => ( is => 'ro' );
 has 'no_cache'      => ( is => 'ro' );
@@ -527,6 +528,8 @@ file.
 
 =item data_dir
 
+=item iterations
+
 =item mode
 
 =item no_backups
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index 464f834..8d057e0 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -89,11 +89,15 @@ sub process_source_or_file {
     my ( $self, $source, $basename ) = @_;
 
     if ( $self->can('transform_source') ) {
-        $source = $self->transform_source($source);
+        foreach my $iter ( 1 .. $self->tidyall->iterations ) {
+            $source = $self->transform_source($source);
+        }
     }
     if ( $self->can('transform_file') ) {
         my $tempfile = $self->_write_temp_file( $basename, $source );
-        $self->transform_file($tempfile);
+        foreach my $iter ( 1 .. $self->tidyall->iterations ) {
+            $self->transform_file($tempfile);
+        }
         $source = read_file($tempfile);
     }
     if ( $self->can('validate_source') ) {
@@ -233,14 +237,16 @@ 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.
+error. This is repeated multiple times if --iterations was passed or specified
+in the configuration file.
 
 =item transform_file ($file)
 
 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.
+plugin. This is repeated multiple times if --iterations was passed or specified
+in the configuration file.
 
 =item validate_source ($source)
 
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index 2c630aa..f571396 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -136,6 +136,19 @@ sub test_quiet_and_verbose : Tests {
     }
 }
 
+sub test_iterations : Tests {
+    my $self     = shift;
+    my $root_dir = $self->create_dir( { "foo.txt" => "abc" } );
+    my $ct       = Code::TidyAll->new(
+        plugins    => { test_plugin('RepeatFoo') => { select => '**/foo*', times => 3 } },
+        root_dir   => $root_dir,
+        iterations => 2
+    );
+    my $file = "$root_dir/foo.txt";
+    $ct->process_files($file);
+    is( read_file($file), scalar( "abc" x 9 ), "3^2 = 9" );
+}
+
 sub test_caching_and_backups : Tests {
     my $self = shift;
 

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