[libcode-tidyall-perl] 54/374: implement output_suffix and refresh_cache

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 87e21cb87dacf2166f8bf44299e95a4343adf6b1
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Tue Jun 26 21:28:16 2012 -0700

    implement output_suffix and refresh_cache
---
 bin/tidyall               |   31 +++++++++++++++++--------------
 lib/Code/TidyAll.pm       |   38 +++++++++++++++++++++++++++-----------
 lib/Code/TidyAll/Cache.pm |    7 +++++++
 3 files changed, 51 insertions(+), 25 deletions(-)

diff --git a/bin/tidyall b/bin/tidyall
index 506a152..8a04788 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -18,19 +18,20 @@ 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},
-    'no-cache'     => \$params{no_cache},
-    'only-plugins' => \$params{only_plugins},
-    'root-dir=s'   => \$params{root_dir},
-    'stdout'       => \$params{stdout},
-    'a|all'        => \$all,
-    'h|help'       => \$help,
-    'q|quiet'      => \$params{quiet},
-    'v|verbose'    => \$params{verbose},
+    '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},
+    'no-cache'        => \$params{no_cache},
+    'only-plugins=s'  => \$params{only_plugins},
+    'output-suffix=s' => \$params{output_suffix},
+    'refresh-cache'   => \$params{refresh_cache},
+    'root-dir=s'      => \$params{root_dir},
+    'a|all'           => \$all,
+    'h|help'          => \$help,
+    'q|quiet'         => \$params{quiet},
+    'v|verbose'       => \$params{verbose},
 ) or usage();
 
 Pod::Usage::pod2usage( { verbose => 2 } ) if $help;
@@ -102,8 +103,10 @@ tidyall - Your all-in-one code tidier and validator
  --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-backup      Don't backup files
+ --no-backups     Don't backup files
  --no-cache       Don't cache last processed times; process all files every time
+ --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
 
 =head1 DESCRIPTION
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 81663e3..b8a3b30 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -22,7 +22,9 @@ sub valid_params {
       no_backups
       no_cache
       only_plugins
+      output_suffix
       plugins
+      refresh_cache
       root_dir
       quiet
       verbose
@@ -62,8 +64,13 @@ sub new {
     if ( my $conf_file = $params{conf_file} ) {
         my $conf_params = $class->_read_conf_file($conf_file);
         if ( my $only_plugins = $params{only_plugins} ) {
-            my %allow = map { ( $_, 1 ) } @$only_plugins;
-            $conf_params = { slice_grep { $allow{$_} } $conf_params };
+            $conf_params = {
+                map {
+                    $conf_params->{$_}
+                      ? ( $_, $conf_params->{$_} )
+                      : die "no conf for plugin '$_'"
+                } @$only_plugins
+            };
         }
         my $main_params = delete( $conf_params->{'_'} ) || {};
         %params = (
@@ -84,6 +91,7 @@ sub new {
     my $self = $class->SUPER::new(%params);
 
     $self->{data_dir} ||= $self->root_dir . "/.tidyall.d";
+    $self->{output_suffix} ||= '';
 
     unless ( $self->no_cache ) {
         $self->{cache} = Code::TidyAll::Cache->new( cache_dir => $self->data_dir . "/cache" );
@@ -154,11 +162,17 @@ sub _process_file {
         return;
     }
 
-    my $cache = $self->cache;
+    my $cache     = $self->cache;
+    my $cache_key = "sig/$small_path";
     my $error;
     my $new_contents = my $orig_contents = read_file($file);
-    if ( $cache && ( my $sig = $cache->get("sig/$small_path") ) ) {
-        return if $sig eq $self->_file_sig( $file, $orig_contents );
+    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 );
+        }
     }
 
     foreach my $plugin (@plugins) {
@@ -172,14 +186,16 @@ sub _process_file {
     }
 
     my $was_tidied = $orig_contents ne $new_contents;
-    my $status = $was_tidied ? "[tidied]  " : "[checked] ";
-    my $plugin_names =
-      $self->verbose ? sprintf( " (%s)", join( ", ", map { $_->name } @plugins ) ) : "";
-    $self->msg( "%s%s%s", $status, $small_path, $plugin_names ) unless $self->quiet;
+    unless ( $self->quiet ) {
+        my $status = $was_tidied ? "[tidied]  " : "[checked] ";
+        my $plugin_names =
+          $self->verbose ? sprintf( " (%s)", join( ", ", map { $_->name } @plugins ) ) : "";
+        $self->msg( "%s%s%s", $status, $small_path, $plugin_names );
+    }
 
     if ($was_tidied) {
         $self->_backup_file( $file, $orig_contents );
-        write_file( $file, $new_contents );
+        write_file( join( '', $file, $self->output_suffix ), $new_contents );
     }
 
     if ($error) {
@@ -187,7 +203,7 @@ sub _process_file {
         return 1;
     }
     else {
-        $cache->set( "sig/$small_path", $self->_file_sig( $file, $new_contents ) ) if $cache;
+        $cache->set( $cache_key, $self->_file_sig( $file, $new_contents ) ) if $cache;
         return;
     }
 }
diff --git a/lib/Code/TidyAll/Cache.pm b/lib/Code/TidyAll/Cache.pm
index cfe2a7a..540c4bf 100644
--- a/lib/Code/TidyAll/Cache.pm
+++ b/lib/Code/TidyAll/Cache.pm
@@ -38,4 +38,11 @@ sub set {
     write_file( $file, $value );
 }
 
+sub remove {
+    my ( $self, $key, $value ) = @_;
+
+    my $file = $self->path_to_key($key);
+    unlink($file);
+}
+
 1;

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