[libcode-tidyall-perl] 81/374: refactor into process_source
Jonas Smedegaard
js at alioth.debian.org
Sun Sep 29 22:25:53 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 e1a8b3691ec96923e238e78e30bb5361069e9961
Author: Jonathan Swartz <swartz at pobox.com>
Date: Fri Jul 6 03:40:54 2012 -0700
refactor into process_source
---
lib/Code/TidyAll.pm | 90 ++++++++++++++++++++++++++++----------------
lib/Code/TidyAll/Plugin.pm | 27 +++++++++----
lib/Code/TidyAll/Result.pm | 11 ++++--
3 files changed, 85 insertions(+), 43 deletions(-)
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 4702cef..d8f7ed3 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -133,9 +133,11 @@ sub _load_plugin {
catch {
die "could not load plugin class '$class_name': $_";
};
+
return $class_name->new(
- conf => $plugin_conf,
- name => $plugin_name
+ conf => $plugin_conf,
+ name => $plugin_name,
+ tidyall => $self
);
}
@@ -161,36 +163,53 @@ sub process_files {
sub process_file {
my ( $self, $file ) = @_;
- my $small_path = $self->_small_path($file);
- my @plugins =
- $self->matched_files
- ? @{ $self->matched_files->{$file} }
- : $self->_plugins_for_path($small_path);
- if ( !@plugins ) {
- $self->msg( "[no plugins apply%s] %s",
- $self->mode ? " for mode '" . $self->mode . "'" : "", $small_path )
- unless $self->quiet;
- return Code::TidyAll::Result->new( file => $file, state => 'no_match' );
- }
-
+ my $path = $self->_small_path($file);
my $cache = $self->cache;
- my $cache_key = "sig/$small_path";
- my $error;
- my $contents = my $orig_contents = read_file($file);
+ my $cache_key = "sig/$path";
+ 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 Code::TidyAll::Result->new( file => $file, state => 'cached' )
- if $sig eq $self->_file_sig( $file, $orig_contents );
+ elsif ( $sig eq $self->_file_sig( $file, $orig_contents ) ) {
+ $self->msg( "[cached] %s", $path ) if $self->verbose;
+ return Code::TidyAll::Result->new( path => $path, state => 'cached' );
}
}
+ my $result = $self->process_source( $orig_contents, $path );
+
+ if ( $result->state eq 'tidied' ) {
+ $self->_backup_file( $file, $contents );
+ $contents = $result->new_contents;
+ write_file( join( '', $file, $self->output_suffix ), $contents );
+ }
+ $cache->set( $cache_key, $self->_file_sig( $file, $contents ) ) if $cache && $result->ok;
+
+ return $result;
+}
+
+sub process_source {
+ my ( $self, $contents, $path ) = @_;
+
+ my @plugins =
+ $self->matched_files
+ ? @{ $self->matched_files->{ join( "/", $self->root_dir, $path ) } }
+ : $self->_plugins_for_path($path);
+ if ( !@plugins ) {
+ $self->msg( "[no plugins apply%s] %s",
+ $self->mode ? " for mode '" . $self->mode . "'" : "", $path )
+ unless $self->quiet;
+ return Code::TidyAll::Result->new( path => $path, state => 'no_match' );
+ }
+
+ my $orig_contents = $contents;
$contents = $self->prefilter->($contents) if $self->prefilter;
+ my $basename = basename($path);
+ my $error;
foreach my $plugin (@plugins) {
try {
- my $new_contents = $plugin->process_source_or_file( $contents, $file );
+ my $new_contents = $plugin->process_source_or_file( $contents, $basename );
if ( $new_contents ne $contents ) {
die "needs tidying\n" if $self->check_only;
$contents = $new_contents;
@@ -202,28 +221,29 @@ sub process_file {
last if $error;
}
$contents = $self->postfilter->($contents) if !$error && $self->postfilter;
+ my $new_contents = $contents;
- my $was_tidied = ( $contents ne $orig_contents ) && !$error;
+ my $was_tidied = !$error && ( $new_contents ne $orig_contents );
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( join( '', $file, $self->output_suffix ), $contents );
+ $self->msg( "%s%s%s", $status, $path, $plugin_names );
}
if ($error) {
$self->msg( "%s", $error );
- return Code::TidyAll::Result->new( file => $file, state => 'error', msg => $error );
+ return Code::TidyAll::Result->new( path => $path, state => 'error', msg => $error );
+ }
+ elsif ($was_tidied) {
+ return Code::TidyAll::Result->new(
+ path => $path,
+ state => 'tidied',
+ new_contents => $new_contents
+ );
}
else {
- $cache->set( $cache_key, $self->_file_sig( $file, $contents ) ) if $cache;
- my $state = $was_tidied ? 'tidied' : 'checked';
- return Code::TidyAll::Result->new( file => $file, state => $state );
+ return Code::TidyAll::Result->new( path => $path, state => 'checked' );
}
}
@@ -364,6 +384,12 @@ sub _sig {
return sha1_hex( join( ",", @$data ) );
}
+sub _tempdir {
+ my ($self) = @_;
+ $self->{tempdir} ||= tempdir_simple();
+ return $self->{tempdir};
+}
+
sub msg {
my ( $self, $format, @params ) = @_;
printf "$format\n", @params;
diff --git a/lib/Code/TidyAll/Plugin.pm b/lib/Code/TidyAll/Plugin.pm
index d1f6157..28a776a 100644
--- a/lib/Code/TidyAll/Plugin.pm
+++ b/lib/Code/TidyAll/Plugin.pm
@@ -1,17 +1,20 @@
package Code::TidyAll::Plugin;
-use Object::Tiny qw(conf ignore name options root_dir select);
-use Code::TidyAll::Util qw(basename read_file tempdir_simple write_file);
+use Object::Tiny qw(conf ignore name options root_dir select tidyall);
+use Code::TidyAll::Util qw(basename read_file write_file);
use Code::TidyAll::Util::Zglob qw(zglob_to_regex);
+use Scalar::Util qw(weaken);
use strict;
use warnings;
sub new {
my $class = shift;
my $self = $class->SUPER::new(@_);
- die "conf required" unless $self->{conf};
- die "name required" unless $self->{name};
+ die "conf required" unless $self->{conf};
+ die "name required" unless $self->{name};
+ die "tidyall required" unless $self->{tidyall};
my $name = $self->{name};
+ weaken( $self->{tidyall} );
$self->{select} = $self->{conf}->{select}
or die "select required for '$name'";
die "select for '$name' should not begin with /" if substr( $self->{select}, 0, 1 ) eq '/';
@@ -24,14 +27,13 @@ sub new {
}
sub process_source_or_file {
- my ( $self, $source, $file ) = @_;
+ my ( $self, $source, $basename ) = @_;
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 );
+ my $tempfile = $self->_write_temp_file( $basename, $source );
$self->process_file($tempfile);
return read_file($tempfile);
}
@@ -40,7 +42,8 @@ sub process_source_or_file {
return $source;
}
elsif ( $self->can('validate_file') ) {
- $self->validate_file($file);
+ my $tempfile = $self->_write_temp_file( $basename, $source );
+ $self->validate_file($tempfile);
return $source;
}
else {
@@ -50,6 +53,14 @@ sub process_source_or_file {
}
}
+sub _write_temp_file {
+ my ( $self, $basename, $source ) = @_;
+
+ my $tempfile = join( "/", $self->tidyall->_tempdir(), $basename );
+ write_file( $tempfile, $source );
+ return $tempfile;
+}
+
sub _build_options {
my $self = shift;
my %options = %{ $self->{conf} };
diff --git a/lib/Code/TidyAll/Result.pm b/lib/Code/TidyAll/Result.pm
index 44781c5..f686789 100644
--- a/lib/Code/TidyAll/Result.pm
+++ b/lib/Code/TidyAll/Result.pm
@@ -2,7 +2,7 @@ package Code::TidyAll::Result;
use strict;
use warnings;
-use Object::Tiny qw(msg state);
+use Object::Tiny qw(msg state new_contents);
sub error { return $_[0]->state eq 'error' }
sub ok { return $_[0]->state ne 'error' }
@@ -15,7 +15,7 @@ __END__
=head1 NAME
-Code::TidyAll::Result - Result returned from Code::TidyAll::process_file
+Code::TidyAll::Result - Result returned from processing a file/source
=head1 SYNOPSIS
@@ -28,7 +28,8 @@ Code::TidyAll::Result - Result returned from Code::TidyAll::process_file
=head1 DESCRIPTION
Represents the result of
-L<Code::TidyAll::process_file|Code::TidyAll/process_file>. A list of these is
+L<Code::TidyAll::process_file|Code::TidyAll/process_file> and
+L<Code::TidyAll::process_file|Code::TidyAll/process_source>. A list of these is
returned from L<Code::TidyAll::process_files|Code::TidyAll/process_files>.
=head1 METHODS
@@ -53,6 +54,10 @@ A string, one of
=back
+=item new_contents
+
+Contains the new contents if state is 'tidied'
+
=item error
Returns true iff state is 'error'
--
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