Title: [opsview] [9595] Accept stdin.
- Revision
- 9595
- Author
- pknight
- Date
- 2012-07-20 16:24:01 +0100 (Fri, 20 Jul 2012)
Log Message
Accept stdin. Don't strip last line -- doesn't appear to do anything now.
Modified Paths
Modified: trunk/tools/run_perltidy
===================================================================
--- trunk/tools/run_perltidy 2012-07-20 12:10:13 UTC (rev 9594)
+++ trunk/tools/run_perltidy 2012-07-20 15:24:01 UTC (rev 9595)
@@ -27,95 +27,108 @@
use Regexp::Common qw /balanced/;
use FindBin qw($Bin);
-my @files = @ARGV;
+if (@ARGV) {
+ tidy_files(@ARGV);
+}
+else {
+ my $code_to_tidy = read_file( \*STDIN );
+ print tidy($code_to_tidy);
+ print "\n" if $code_to_tidy =~ /\n\n$/;
+}
-# Need to reset @ARGV, otherwise Perl::Tidy seems to use it
-@ARGV = ();
+sub tidy_files {
-while ( my $search_base = shift @files ) {
- my $file_iterator = File::Next::files($search_base);
+ my @files = @_;
- while ( defined( my $file = $file_iterator->() ) ) {
+ # Need to reset @ARGV, otherwise Perl::Tidy seems to use it
+ @ARGV = ();
- # ignore all svn and git dirs
- next if ( $file =~ m!\.(?:svn|git)! );
+ while ( my $search_base = shift @files ) {
+ my $file_iterator = File::Next::files($search_base);
- # skip opsview-base
- next if $file =~ m!opsview\-base/!;
+ while ( defined( my $file = $file_iterator->() ) ) {
- # Ignore certain dirs if running out of a repository root
- # as they should be catered for elsewhere
- next if ( $file =~ m!(?:opsview-core\/)?(?:perl|nmis)/! );
+ # ignore all svn and git dirs
+ next if ( $file =~ m!\.(?:svn|git)! );
- my $type = qx/ file "$file" /;
+ # skip opsview-base
+ next if $file =~ m!opsview\-base/!;
- # remove filename to avoid catching filenames with 'perl' in them
- $type =~ s/^.*?://;
+ # Ignore certain dirs if running out of a repository root
+ # as they should be catered for elsewhere
+ next if ( $file =~ m!(?:opsview-core\/)?(?:perl|nmis)/! );
- # ignore all non-perl files
- next if ( $type !~ m/perl/i && $file !~ m/\.(:?pl|pm|t)$/i );
+ my $type = qx/ file "$file" /;
- print "Running perltidy against $file", $/;
+ # remove filename to avoid catching filenames with 'perl' in them
+ $type =~ s/^.*?://;
- my $code_to_tidy = read_file($file);
- my $tidy_code = '';
- my $stderr = '';
- my $logfile = '';
- my $errorfile = '';
+ # ignore all non-perl files
+ next if ( $type !~ m/perl/i && $file !~ m/\.(:?pl|pm|t)$/i );
- # if the very last line is blank, remove it as perltidy sees this
- # as a problem
- $code_to_tidy =~ s/^\Z//xsm;
+ print "Running perltidy against $file", $/;
- my %commas_added = ();
- Perl::Tidy::perltidy(
- perltidyrc => "$Bin/perltidyrc",
- source => \$code_to_tidy,
- destination => \$tidy_code,
- stderr => \$stderr,
- logfile => \$logfile,
- errorfile => \$errorfile,
- prefilter => sub {
- my $input = $_[0];
- my @parens =
- $input =~ /$RE{balanced}{-begin=>'('}{-end=>');'}/g;
- for my $parens (@parens) {
- ( my $with_comma = $parens )
- =~ s/([\)\}\]'"])(\s*)\);\Z/$1,$2\);/;
+ my $code_to_tidy = read_file($file);
+ my $tidy_code = tidy($code_to_tidy);
- # no change so go to next
- next if $with_comma eq $parens;
+ write_file(
+ $file,
+ {
+ atomix => 1,
+ buf_ref => \$tidy_code,
+ err_mode => 'carp',
+ }
+ );
+ }
+ }
- $input =~ s/\Q$parens\E/$with_comma/;
+}
- $with_comma =~ s/\s*//g;
- $commas_added{$with_comma} = 1;
- }
- return $input;
- },
- postfilter => sub {
- my $output = $_[0];
- my @parens =
- $output =~ /$RE{balanced}{-begin=>'('}{-end=>');'}/g;
- for my $parens (@parens) {
- ( my $with_comma = $parens ) =~ s/\s*//g;
- next unless exists $commas_added{$with_comma};
+sub tidy {
+ my $code_to_tidy = shift;
+ my $tidy_code = '';
+ my $stderr = '';
+ my $logfile = '';
+ my $errorfile = '';
- ( my $without_comma = $parens )
- =~ s/([\)\}\]'"]\s*),(\s*)\);\Z/$1$2\);/;
- $output =~ s/\Q$parens\E/$without_comma/;
- }
- return $output;
+ my %commas_added = ();
+ Perl::Tidy::perltidy(
+ perltidyrc => "$Bin/perltidyrc",
+ source => \$code_to_tidy,
+ destination => \$tidy_code,
+ stderr => \$stderr,
+ logfile => \$logfile,
+ errorfile => \$errorfile,
+ prefilter => sub {
+ my $input = $_[0];
+ my @parens = $input =~ /$RE{balanced}{-begin=>'('}{-end=>');'}/g;
+ for my $parens (@parens) {
+ ( my $with_comma = $parens )
+ =~ s/([\)\}\]'"])(\s*)\);\Z/$1,$2\);/;
+
+ # no change so go to next
+ next if $with_comma eq $parens;
+
+ $input =~ s/\Q$parens\E/$with_comma/;
+
+ $with_comma =~ s/\s*//g;
+ $commas_added{$with_comma} = 1;
}
- );
+ return $input;
+ },
+ postfilter => sub {
+ my $output = $_[0];
+ my @parens = $output =~ /$RE{balanced}{-begin=>'('}{-end=>');'}/g;
+ for my $parens (@parens) {
+ ( my $with_comma = $parens ) =~ s/\s*//g;
+ next unless exists $commas_added{$with_comma};
- write_file(
- $file,
- {
- atomix => 1,
- buf_ref => \$tidy_code,
- err_mode => 'carp',
+ ( my $without_comma = $parens )
+ =~ s/([\)\}\]'"]\s*),(\s*)\);\Z/$1$2\);/;
+ $output =~ s/\Q$parens\E/$without_comma/;
}
- );
- }
+ return $output;
+ }
+ );
+ return $tidy_code;
}
_______________________________________________
Opsview-checkins mailing list
[email protected]
http://lists.opsview.org/lists/listinfo/opsview-checkins