Permalink
Browse files

add experimental CLI

  • Loading branch information...
1 parent 66f708f commit 78917de1bd95d6aba52f87ebcd390a54128f05dc @szabgab committed Mar 26, 2012
Showing with 116 additions and 1 deletion.
  1. +82 −0 script/ppix_editortools
  2. +34 −1 t/07-renamevariable.t
View
@@ -0,0 +1,82 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+
+use Getopt::Long qw(GetOptions);
+use Pod::Usage qw(pod2usage);
+
+# This is a command line script to use the capabilities of this package
+# with a temporary API and a temporary name!
+
+pod2usage() if not @ARGV;
+my %opt;
+GetOptions(\%opt,
+ 'inplace',
+ 'RenameVariable',
+ 'line=i',
+ 'column=i',
+ 'replacement=s',
+ 'to-camel-case=s',
+
+ 'help',
+) or pod2usage();
+pod2usage() if $opt{help};
+
+if ($opt{RenameVariable}) {
+ require PPIx::EditorTools::RenameVariable;
+ my $file = shift @ARGV;
+
+ my $code = read_file($file);
+
+ my %param;
+ if (exists $opt{replacement}) {
+ $param{replacement} = $opt{replacement};
+ } elsif (exists $opt{'to-camel-case'}) {
+ $param{'to_camel_case'} = $opt{'to-camel-case'};
+ } else {
+ die 'Need eiher replacement or to-camel-case';
+ }
+
+
+ my $result =
+ PPIx::EditorTools::RenameVariable->new->rename(
+ code => $code,
+ line => $opt{line},
+ column => $opt{column},
+ %param,
+ )->code;
+ ;
+ write_file($file, $result);
+} else {
+ pod2usage();
+}
+
+exit;
+
+
+sub read_file {
+ my ($file) = @_;
+ open my $in, '<', $file or die "Could not open file '$file' for reading: $!";
+ local $/ = undef;
+ return <$in>;
+}
+
+sub write_file {
+ my ($file, $data) = @_;
+ open my $out, '>', $file or die;
+ print $out $data;
+}
+
+
+=head1 NAME
+
+ppix_editortools - command line interface for the PPIx::EditorTools
+
+=head1 SYNOPSIS
+
+ --RenameVariable --line 8 column 12 --replacement NEW_NAME --inplace
+
+
+=cut
+
View
@@ -10,6 +10,9 @@ use Test::More;
use Test::Differences;
use PPI;
+use File::Temp qw(tempdir);
+
+my $tempdir = tempdir( CLEANUP => 1 );
BEGIN {
if ( $PPI::VERSION =~ /_/ ) {
@@ -18,7 +21,7 @@ BEGIN {
}
}
-plan tests => 9;
+plan tests => 17;
use PPIx::EditorTools::RenameVariable;
@@ -78,6 +81,10 @@ eq_or_diff(
'replace scalar'
);
+test_cli($code, "--RenameVariable --line 8 --column 12 --replacement shiny", $shiny_replacement, 'replace scalar on command line');
+
+
+
eq_or_diff(
PPIx::EditorTools::RenameVariable->new->rename(
code => $code,
@@ -89,6 +96,8 @@ eq_or_diff(
'replace scalar'
);
+test_cli($code, "--RenameVariable --line 11 --column 9 --replacement shiny", $shiny_replacement, 'replace scalar on command line');
+
my $stuff_replacement = <<'STUFF_REPLACEMENT';
use MooseX::Declare;
@@ -120,6 +129,7 @@ eq_or_diff(
$stuff_replacement,
'replace hash'
);
+test_cli($code, "--RenameVariable --line 15 --column 13 --replacement stuff", $stuff_replacement, 'replace hash on command line');
my $munged = PPIx::EditorTools::RenameVariable->new->rename(
code => $code,
@@ -158,6 +168,7 @@ eq_or_diff(
$xvar_replacement,
'camelCase xVar'
);
+test_cli($code, "--RenameVariable --line 2 --column 8 --to-camel-case 1", $xvar_replacement, 'camelCase xVar on command line');
$xvar_replacement =~ s/x_?var/XVar/gi; # yes, this is simple
@@ -202,3 +213,25 @@ eq_or_diff(
'from camelCase _some_variable (ucfirst)'
);
+# exerimental test code for experimental command line tool
+sub test_cli {
+ my ($original, $params, $expected, $title) = @_;
+
+ my $file = "$tempdir/source.pl";
+
+ open my $out, '>', $file or die;
+ print $out $original;
+ close $out;
+
+ my $cmd = "$^X -Ilib script/ppix_editortools --inplace $params $file";
+ #diag $cmd;
+ is system($cmd), 0, 'system';
+
+ open my $in, '<', $file or die;
+ my $result = do {local $/ = undef; <$in>; };
+ close $in;
+
+ eq_or_diff($result, $expected, $title);
+}
+
+

0 comments on commit 78917de

Please sign in to comment.