File: ppix_editortools

package info (click to toggle)
libppix-editortools-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 324 kB
  • sloc: perl: 2,295; xml: 201; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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