Skip to content
Browse files

implementation with directory walking instead of komodo parameter

  • Loading branch information...
1 parent dce24e4 commit 9c68a35e0d6300dbbcc77a3f404a1767c7f81285 @wchristian committed Mar 31, 2012
Showing with 96 additions and 3 deletions.
  1. +5 −1 META.json
  2. +11 −1 README.pod
  3. +25 −0 bin/perltidy_ko
  4. +1 −0 corpus/lib/dir with space/test.pl
  5. +33 −1 lib/Perl/Tidy/Komodo.pm
  6. +21 −0 t/basic.t
View
6 META.json
@@ -1,5 +1,5 @@
{
- "abstract" : "#",
+ "abstract" : "tidy perl files in Komodo with a project rc",
"author" : [
"Christian Walde <walde.christian@googlemail.com>"
],
@@ -32,13 +32,17 @@
},
"runtime" : {
"requires" : {
+ "File::chdir" : 0,
+ "Perl::Tidy" : 0,
"perl" : "5.006",
"strictures" : 0
}
},
"test" : {
"requires" : {
+ "Capture::Tiny" : 0,
"File::Find" : 0,
+ "File::Slurp" : 0,
"File::Temp" : 0,
"Test::InDistDir" : 0,
"Test::More" : 0,
View
12 README.pod
@@ -1,11 +1,19 @@
=head1 NAME
-Perl::Tidy::Komodo
+Perl::Tidy::Komodo - tidy perl files in Komodo with a project rc
=head1 VERSION
version 0.000001
+=head1 METHODS
+
+=head2 run
+
+Tries to find a directory that looks like a project directory, upwards
+from CWD and stores its path in $ENV{PERLTIDY}. Then Perl::Tidy itself is
+run.
+
=for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
=head1 SUPPORT
@@ -31,10 +39,12 @@ Christian Walde <walde.christian@googlemail.com>
=head1 COPYRIGHT AND LICENSE
+
Christian Walde has dedicated the work to the Commons by waiving all of his
or her rights to the work worldwide under copyright law and all related or
neighboring legal rights he or she had in the work, to the extent allowable by
law.
Works under CC0 do not require attribution. When citing the work, you should
not imply endorsement by the author.
+
View
25 bin/perltidy_ko
@@ -0,0 +1,25 @@
+#!/usr/bin/perl
+use strictures;
+
+package perltidy_ko;
+
+# VERSION
+
+# ABSTRACT: tidy perl files in Komodo with a project rc
+
+# COPYRIGHT
+
+=head1 DESCRIPTION
+
+Define this script as a formatter in your Komodo preferences. Perl::Tidy
+will then use either .perltidyrc in the same directory as the currently
+open file, or if that does not exist, walk up the directories until it
+finds a .perltidyrc in a directory that also contains a lib directory and
+use that, or if that does not exist defer to Perl::Tidy's inbuilt search
+algorithms.
+
+=cut
+
+use Perl::Tidy::Komodo;
+
+Perl::Tidy::Komodo->run;
View
1 corpus/lib/dir with space/test.pl
@@ -0,0 +1 @@
+1,2,3,4,5;
View
34 lib/Perl/Tidy/Komodo.pm
@@ -4,8 +4,40 @@ package Perl::Tidy::Komodo;
# VERSION
-# ABSTRACT:
+# ABSTRACT: tidy perl files in Komodo with a project rc
# COPYRIGHT
+use Perl::Tidy;
+use File::chdir;
+
+=head1 METHODS
+
+=head2 run
+
+Tries to find a directory that looks like a project directory, upwards
+from CWD and stores its path in $ENV{PERLTIDY}. Then Perl::Tidy itself is
+run.
+
+=cut
+
+sub run {
+ _try_set_perltidy_env();
+ Perl::Tidy::perltidy();
+}
+
+sub _try_set_perltidy_env {
+ return if exists $ENV{PERLTIDY};
+ my @cwd_store = @CWD;
+ while ( $CWD[0] ) {
+ if ( -d 'lib' and -f '.perltidyrc' ) {
+ $ENV{PERLTIDY} = $CWD;
+ last;
+ }
+ pop @CWD;
+ }
+ @CWD = @cwd_store;
+ return;
+}
+
1;
View
21 t/basic.t
@@ -4,6 +4,9 @@ package basic_test;
use Test::InDistDir;
use Test::More;
+use Capture::Tiny 'capture';
+use File::Slurp 'write_file';
+use File::chdir;
use Perl::Tidy::Komodo;
@@ -12,6 +15,24 @@ done_testing;
exit;
sub run {
+ delete $ENV{PERLTIDY};
+ $CWD = "corpus/lib/dir with space";
+
+ my @tidy_call = ( "$^X", qw( -I../../../lib ../../../bin/perltidy_ko -st test.pl ), );
+
+ my $rc = "../../.perltidyrc";
+ eval { unlink $rc };
+
+ {
+ my ( $out, $err ) = capture { system @tidy_call };
+ is $out, "1, 2, 3, 4, 5;\n";
+ }
+ write_file $rc, "-l=4";
+
+ {
+ my ( $out, $err ) = capture { system @tidy_call };
+ is $out, "1,\n 2,\n 3,\n 4,\n 5;\n";
+ }
return;
}

0 comments on commit 9c68a35

Please sign in to comment.
Something went wrong with that request. Please try again.