[libcode-tidyall-perl] 288/374: Add -r/--recursive flag to process directories recursively
Jonas Smedegaard
js at alioth.debian.org
Sun Sep 29 22:26:37 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 8b7658140f53d156e6c237b78b10c62be4367315
Author: Jonathan Swartz <swartz at pobox.com>
Date: Tue Oct 2 11:40:06 2012 -0700
Add -r/--recursive flag to process directories recursively
---
Changes | 3 +++
bin/tidyall | 16 ++++++++++++----
lib/Code/TidyAll.pm | 29 ++++++++++++++++++++++++++---
lib/Code/TidyAll/Util.pm | 9 ++++++++-
lib/Code/TidyAll/t/Basic.pm | 33 ++++++++++++++++++++++++++++++++-
5 files changed, 81 insertions(+), 9 deletions(-)
diff --git a/Changes b/Changes
index b2d0f95..f5565c8 100644
--- a/Changes
+++ b/Changes
@@ -4,6 +4,9 @@ Revision history for Code-TidyAll
0.14 Sep 30, 2012
+* Improvements
+ - Add -r/--recursive flag to process directories recursively
+
0.13 Sep 30, 2012
* Fixes
diff --git a/bin/tidyall b/bin/tidyall
index 0e271a5..0a114bb 100755
--- a/bin/tidyall
+++ b/bin/tidyall
@@ -16,6 +16,7 @@ Usage: tidyall [options] [file] ...
-l, --list List each file along with the plugins it matches
-m, --mode Mode (e.g. "editor", "commit") - affects which plugins run
-p path, --pipe path Read from STDIN, output to STDOUT/STDERR
+ -r, --recursive Descend recursively into directories listed on command line
-s, --svn Process all added/modified files according to svn
-q, --quiet Suppress output except for errors
-v, --verbose Show extra output
@@ -63,6 +64,7 @@ GetOptions(
'l|list' => \$list,
'm|mode=s' => \$params{mode},
'p|pipe=s' => \$pipe,
+ 'r|recursive' => \$params{recursive},
's|svn' => \$svn_files,
'q|quiet' => \$params{quiet},
'v|verbose' => \$params{verbose},
@@ -185,15 +187,15 @@ tidyall - Your all-in-one code tidier and validator
#
% tidyall -a
- # Process all files in a particular project
- #
- % tidyall -a --root-dir /home/joe/project
-
# Process one or more specific files,
# look upwards from the first file for conf file
#
% tidyall file [file...]
+ # Process a directory recursively
+ #
+ % tidyall -r dir
+
=head1 DESCRIPTION
There are a lot of great code tidiers and validators out there. C<tidyall>
@@ -459,6 +461,12 @@ result).
#
% tidyall --pipe some/path.pl
+=item -r, --recursive
+
+Recursively enter any directories listed on the command-line and process all
+the files within. By default, directories encountered on the command-line will
+generate a warning.
+
=item -s, --svn
Process all added or modified files in the current svn working directory.
diff --git a/lib/Code/TidyAll.pm b/lib/Code/TidyAll.pm
index 8568ee5..bbe8549 100644
--- a/lib/Code/TidyAll.pm
+++ b/lib/Code/TidyAll.pm
@@ -3,7 +3,7 @@ use Cwd qw(realpath);
use Code::TidyAll::Config::INI::Reader;
use Code::TidyAll::Cache;
use Code::TidyAll::Util
- qw(abs2rel basename can_load dirname dump_one_line mkpath read_file rel2abs tempdir_simple 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);
@@ -29,6 +29,7 @@ has 'no_cache' => ( is => 'ro' );
has 'output_suffix' => ( is => 'ro', default => sub { '' } );
has 'plugins' => ( is => 'ro', required => 1 );
has 'quiet' => ( is => 'ro' );
+has 'recursive' => ( is => 'ro' );
has 'refresh_cache' => ( is => 'ro' );
has 'root_dir' => ( is => 'ro', required => 1 );
has 'verbose' => ( is => 'ro' );
@@ -177,7 +178,7 @@ sub process_all {
sub process_files {
my ( $self, @files ) = @_;
- return map { $self->process_file( realpath($_) ) } @files;
+ return map { $self->process_file( realpath($_) || rel2abs($_) ) } @files;
}
sub list_files {
@@ -193,8 +194,22 @@ sub list_files {
sub process_file {
my ( $self, $file ) = @_;
+ my $path = $self->_small_path($file);
+
+ if ( -d $file ) {
+ if ( $self->recursive ) {
+ return $self->process_dir($file);
+ }
+ else {
+ print "$path: is a directory (try -r/--recursive)";
+ return;
+ }
+ }
+ elsif ( !-f $file ) {
+ print "$path: not a file or directory\n";
+ return;
+ }
- my $path = $self->_small_path($file);
my $cache = $self->no_cache ? undef : $self->cache;
my $cache_key = "sig/$path";
my $contents = my $orig_contents = read_file($file);
@@ -220,6 +235,14 @@ sub process_file {
return $result;
}
+sub process_dir {
+ my ( $self, $dir ) = @_;
+
+ foreach my $subfile ( read_dir($dir) ) {
+ $self->process_file("$dir/$subfile");
+ }
+}
+
sub process_source {
my ( $self, $contents, $path ) = @_;
diff --git a/lib/Code/TidyAll/Util.pm b/lib/Code/TidyAll/Util.pm
index 97315a8..d1a3c6c 100644
--- a/lib/Code/TidyAll/Util.pm
+++ b/lib/Code/TidyAll/Util.pm
@@ -13,7 +13,7 @@ use warnings;
use base qw(Exporter);
our @EXPORT_OK =
- qw(abs2rel basename can_load dirname dump_one_line mkpath pushd read_file realpath rel2abs tempdir_simple trim uniq write_file );
+ qw(abs2rel basename can_load dirname dump_one_line mkpath pushd read_dir read_file realpath rel2abs tempdir_simple trim uniq write_file);
sub can_load {
@@ -64,6 +64,13 @@ sub trim {
return $str;
}
+sub read_dir {
+ my ($dir) = @_;
+ opendir( my $dirh, $dir ) or die "could not open $dir: $!";
+ my @dir_entries = grep { $_ ne "." && $_ ne ".." } readdir($dirh);
+ return @dir_entries;
+}
+
sub read_file {
my ($file) = @_;
open( my $fh, "<", $file ) or die "could not open $file: $!";
diff --git a/lib/Code/TidyAll/t/Basic.pm b/lib/Code/TidyAll/t/Basic.pm
index f571396..0ec2ba4 100644
--- a/lib/Code/TidyAll/t/Basic.pm
+++ b/lib/Code/TidyAll/t/Basic.pm
@@ -237,6 +237,34 @@ sub test_selects_and_ignores : Tests {
[ test_plugin('UpperText') ] );
}
+sub test_dirs : Tests {
+ my $self = shift;
+
+ my @files = ( "a/foo.txt", "a/bar.txt", "a/bar.pl", "b/foo.txt" );
+ my $root_dir = $self->create_dir( { map { $_ => 'hi' } @files } );
+
+ foreach my $recursive ( 0 .. 1 ) {
+ my $output = capture_stdout {
+ my $ct = Code::TidyAll->new(
+ plugins => { %UpperText, %ReverseFoo },
+ root_dir => $root_dir,
+ ( $recursive ? ( recursive => 1 ) : () )
+ );
+ $ct->process_file("$root_dir/a");
+ };
+ if ($recursive) {
+ is( $output, "[tidied] a/bar.txt\n[tidied] a/foo.txt\n" );
+ is( read_file("$root_dir/a/foo.txt"), "IH" );
+ is( read_file("$root_dir/a/bar.txt"), "HI" );
+ is( read_file("$root_dir/a/bar.pl"), "hi" );
+ is( read_file("$root_dir/b/foo.txt"), "hi" );
+ }
+ else {
+ like( $output, qr/is a directory/ );
+ }
+ }
+}
+
sub test_errors : Tests {
my $self = shift;
@@ -273,7 +301,10 @@ sub test_errors : Tests {
qr/unknown options/;
my $ct = Code::TidyAll->new( plugins => {%UpperText}, root_dir => $root_dir );
- my $output = capture_stdout { $ct->process_files("$root_dir/foo/bar.txt") };
+ my $output = capture_stdout { $ct->process_files("$root_dir/baz/blargh.txt") };
+ like( $output, qr/baz\/blargh.txt: not a file or directory/, "file not found" );
+
+ $output = capture_stdout { $ct->process_files("$root_dir/foo/bar.txt") };
is( $output, "[tidied] foo/bar.txt\n", "filename output" );
is( read_file("$root_dir/foo/bar.txt"), "ABC", "tidied" );
my $other_dir = realpath( tempdir_simple() );
--
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