[libcode-tidyall-perl] 53/374: process_file works on a copy of 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 996960f120d47075b098880ecaa493c139db6627
Author: Jonathan Swartz <swartz at pobox.com>
Date:   Tue Jun 26 16:47:21 2012 -0700

    process_file works on a copy of file
---
 lib/Code/TidyAll.pm        |   17 ++++++++++-------
 lib/Code/TidyAll/Plugin.pm |   28 +++++++++++++++++-----------
 2 files changed, 27 insertions(+), 18 deletions(-)

diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index f925265..81663e3 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -3,7 +3,7 @@ use Cwd qw(realpath);
 use Config::INI::Reader;
 use Code::TidyAll::Cache;
 use Code::TidyAll::Util
-  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file rel2abs uniq write_file);
+  qw(abs2rel basename can_load dirname dump_one_line mkpath read_dir read_file rel2abs tempdir_simple uniq write_file);
 use Code::TidyAll::Result;
 use Date::Format;
 use Digest::SHA1 qw(sha1_hex);
@@ -156,14 +156,14 @@ sub _process_file {
 
     my $cache = $self->cache;
     my $error;
-    my $orig_contents = read_file($file);
+    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 );
     }
 
     foreach my $plugin (@plugins) {
         try {
-            $plugin->process_file($file);
+            $new_contents = $plugin->process_source_or_file( $new_contents, $file );
         }
         catch {
             $error = sprintf( "*** '%s': %s", $plugin->name, $_ );
@@ -171,13 +171,16 @@ sub _process_file {
         last if $error;
     }
 
-    my $new_contents = read_file($file);
-    my $was_tidied   = $orig_contents ne $new_contents;
-    my $status       = $was_tidied ? "[tidied]  " : "[checked] ";
+    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;
-    $self->_backup_file( $file, $orig_contents ) if $was_tidied;
+
+    if ($was_tidied) {
+        $self->_backup_file( $file, $orig_contents );
+        write_file( $file, $new_contents );
+    }
 
     if ($error) {
         $self->msg( "%s", $error );
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index 9ba07ce..8a595cf 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -1,6 +1,6 @@
 package Code::TidyAll::Plugin;
-use Object::Tiny qw(conf ignore matcher name options root_dir select);
-use Code::TidyAll::Util qw(read_file write_file);
+use Object::Tiny qw(conf ignore name options root_dir select);
+use Code::TidyAll::Util qw(basename read_file tempdir_simple write_file);
 use strict;
 use warnings;
 
@@ -22,16 +22,22 @@ sub new {
     return $self;
 }
 
-sub process_file {
-    my ( $self, $file ) = @_;
-    my $source = read_file($file);
-    my $dest   = $self->process_source($source);
-    write_file( $file, $dest ) if $dest ne $source;
-}
+sub process_source_or_file {
+    my ( $self, $source, $file ) = @_;
 
-sub process_source {
-    my ( $self, $source ) = @_;
-    die sprintf( "plugin '%s' must implement either process_file or process_source", $self->name );
+    if ( $self->can('process_source') ) {
+        return $self->process_source($source);
+    }
+    elsif ( $self->can('process_file') ) {
+        my $tempfile = join( "/", tempdir_simple(), basename($file) );
+        write_file( $tempfile, $source );
+        $self->process_file($tempfile);
+        return read_file($tempfile);
+    }
+    else {
+        die sprintf( "plugin '%s' must implement either process_file or process_source",
+            $self->name );
+    }
 }
 
 sub _build_options {

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