Skip to content

Commit

Permalink
add experimental CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Mar 26, 2012
1 parent 66f708f commit 78917de
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
82 changes: 82 additions & 0 deletions script/ppix_editortools
@@ -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

35 changes: 34 additions & 1 deletion t/07-renamevariable.t
Expand Up @@ -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 =~ /_/ ) {
Expand All @@ -18,7 +21,7 @@ BEGIN {
}
}

plan tests => 9;
plan tests => 17;

use PPIx::EditorTools::RenameVariable;

Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.