[libcode-tidyall-perl] 63/374: implement check_only/--check-only; return result from each process_file

Jonas Smedegaard js at alioth.debian.org
Sun Sep 29 22:25:49 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 663d8b811a59ccdff75177fc6ec1d18fa7fc1f3a
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Tue Jul 3 04:49:48 2012 -0700

    implement check_only/--check-only; return result from each process_file
---
 bin/tidyall                |   11 +++++++----
 lib/Code/TidyAll.pm        |   41 ++++++++++++++++++++++++-----------------
 lib/Code/TidyAll/Result.pm |    9 ++++++++-
 3 files changed, 39 insertions(+), 22 deletions(-)

diff --git a/bin/tidyall b/bin/tidyall
index 099f8ae..400375f 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -19,6 +19,7 @@ my ( %params, $help, $all );
 
 GetOptions(
     'backup-ttl=i'    => \$params{backup_ttl},
+    'check-only=s'    => \$params{check_only},
     'conf-file=s'     => \$params{conf_file},
     'data-dir=s'      => \$params{data_dir},
     'no-backups'      => \$params{no_backups},
@@ -40,20 +41,21 @@ Pod::Usage::pod2usage( { verbose => 2 } ) if $help;
 
 $params{conf_file} ||= "$params{root_dir}/tidyall.ini" if ( $params{root_dir} );
 
-my $result;
+my @results;
 if ($all) {
     $params{conf_file} ||= Code::TidyAll->find_conf_file( cwd() );
     my $ct = Code::TidyAll->new(%params);
-    $result = $ct->process_all();
+    @results = $ct->process_all();
 }
 else {
     my @files = @ARGV or die "file(s) or -a required";
     $params{conf_file} ||= Code::TidyAll->find_conf_file( dirname( $files[0] ) );
     my $ct = Code::TidyAll->new(%params);
-    $result = $ct->process_files(@files);
+    @results = $ct->process_files(@files);
 }
 
-exit( $result->error_count ? 1 : 0 );
+my $status = ( grep { $_->error } @results ) ? 1 : 0;
+exit($status);
 
 1;
 
@@ -98,6 +100,7 @@ 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"
+ --check-only     Just check if files are tidied and throw error if not; don't modify files
  --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
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index e73731f..577bb0c 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -18,6 +18,7 @@ use warnings;
 sub valid_params {
     return qw(
       backup_ttl
+      check_only
       conf_file
       data_dir
       mode
@@ -86,7 +87,7 @@ sub new {
             join( ", ", sort map { "'$_'" } @bad_params ) );
     }
 
-    $class->msg( "constructing %s with these params: %s", $class, \%params )
+    $class->msg( "constructing %s with these params: %s", $class, dump_one_line( \%params ) )
       if ( $params{verbose} );
 
     my $self = $class->SUPER::new(%params);
@@ -149,48 +150,55 @@ sub process_files {
     my ( $self, @files ) = @_;
 
     my $error_count = 0;
+    my @results;
     foreach my $file (@files) {
         $file = realpath($file);
-        $error_count++ if $self->_process_file($file);
+        push( @results, $self->process_file($file) );
     }
-    return Code::TidyAll::Result->new( error_count => $error_count );
+    return @results;
 }
 
-sub _process_file {
+sub process_file {
     my ( $self, $file ) = @_;
 
     my @plugins = @{ $self->matched_files->{$file} || [] };
     my $small_path = $self->_small_path($file);
     if ( !@plugins ) {
         $self->msg( "[no plugins apply] %s", $small_path ) unless $self->quiet;
-        return;
+        return Code::TidyAll::Result->new( state => 'no_match' );
     }
 
     my $cache     = $self->cache;
     my $cache_key = "sig/$small_path";
     my $error;
-    my $new_contents = my $orig_contents = read_file($file);
+    my $contents = my $orig_contents = read_file($file);
     if ( $cache && ( my $sig = $cache->get($cache_key) ) ) {
         if ( $self->refresh_cache ) {
             $cache->remove($cache_key);
         }
         else {
-            return if $sig eq $self->_file_sig( $file, $orig_contents );
+            return Code::TidyAll::Result->new( state => 'cached' )
+              if $sig eq $self->_file_sig( $file, $orig_contents );
         }
     }
 
-    $new_contents = $self->prefilter->($new_contents) if $self->prefilter;
+    $contents = $self->prefilter->($contents) if $self->prefilter;
     foreach my $plugin (@plugins) {
         try {
-            $new_contents = $plugin->process_source_or_file( $new_contents, basename($file) );
+            my $new_contents = $plugin->process_source_or_file( $contents, basename($file) );
+            if ( $new_contents ne $contents ) {
+                die "needs tidying\n" if $self->check_only;
+                $contents = $new_contents;
+            }
         }
         catch {
             $error = sprintf( "*** '%s': %s", $plugin->name, $_ );
         };
+        last if $error;
     }
-    $new_contents = $self->postfilter->($new_contents) if !$error && $self->postfilter;
+    $contents = $self->postfilter->($contents) if !$error && $self->postfilter;
 
-    my $was_tidied = ( $orig_contents ne $new_contents ) && !$error;
+    my $was_tidied = ( $contents ne $orig_contents ) && !$error;
     unless ( $self->quiet ) {
         my $status = $was_tidied ? "[tidied]  " : "[checked] ";
         my $plugin_names =
@@ -200,16 +208,16 @@ sub _process_file {
 
     if ($was_tidied) {
         $self->_backup_file( $file, $orig_contents );
-        write_file( join( '', $file, $self->output_suffix ), $new_contents );
+        write_file( join( '', $file, $self->output_suffix ), $contents );
     }
 
     if ($error) {
         $self->msg( "%s", $error );
-        return 1;
+        return Code::TidyAll::Result->new( state => 'error', msg => $error );
     }
     else {
-        $cache->set( $cache_key, $self->_file_sig( $file, $new_contents ) ) if $cache;
-        return;
+        $cache->set( $cache_key, $self->_file_sig( $file, $contents ) ) if $cache;
+        return Code::TidyAll::Result->new( state => ( $was_tidied ? 'tidied' : 'checked' ) );
     }
 }
 
@@ -346,8 +354,7 @@ sub _sig {
 
 sub msg {
     my ( $self, $format, @params ) = @_;
-    @params = map { ref($_) ? dump_one_line($_) : $_ } @params;
-    printf( "$format\n", @params );
+    printf "$format\n", @params;
 }
 
 1;
diff --git a/lib/Code/TidyAll/Result.pm b/lib/Code/TidyAll/Result.pm
index bb460d6..477bdd4 100644
--- a/lib/Code/TidyAll/Result.pm
+++ b/lib/Code/TidyAll/Result.pm
@@ -2,7 +2,10 @@ package Code::TidyAll::Result;
 use strict;
 use warnings;
 
-use Object::Tiny qw(error_count);
+use Object::Tiny qw(msg state);
+
+sub error { return $_[0]->state eq 'error' }
+sub ok { return $_[0]->state ne 'error' }
 
 1;
 
@@ -22,4 +25,8 @@ Code::TidyAll::Result - Result returned from Code::TidyAll methods
 
 The number of errors that occurred.
 
+=item ok
+
+Returns true iff there were no errors.
+
 =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