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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
#!/usr/local/bin/perl -w
use strict;
use Tk;
use Tk::LabEntry;
use Tk::ErrorDialog;
use File::Find;
use ExtUtils::Manifest "/mani/";
my $containing = '/perl\b';
my $matching = '\.p[ml]$';
my $mode = 'MANIFEST';
my %filefunc = ( MANIFEST => \&manifiles,
Dummy => \&dummyfiles,
'find' => \&findfiles );
sub accept_file
{
my ($file) = @_;
return 0 unless (-w $file);
my $accept = $matching && ($file =~ /$matching/);
if (!$accept && $containing)
{
open(FILE,"<$file") || die "Cannot open $file:$!";
my $line = <FILE>;
close(FILE);
$accept = $line && $line =~ m#$containing#;
}
return $accept;
}
sub findfiles
{
my ($dir) = @_;
my @files = ();
die "Not a directory $dir\n", unless (-d $dir);
find(sub {
$File::Find::prune = 0;
if (-T $_ && !/%$/)
{
push(@files,"$File::Find::dir/$_") if (accept_file($_));
}
elsif (-d $_)
{
$File::Find::prune = 1 if ($_ eq 'blib');
}},$dir);
return @files;
}
sub dummyfiles
{
my ($dir) = @_;
my @files = ();
my $file;
foreach $file (map("$dir/$_",'aaaa'...'aaaz'))
{
push(@files,$file);
}
return @files;
}
sub manifiles
{
my ($dir) = @_;
my @files = ();
if (-d $dir && -f "$dir/MANIFEST")
{
my $list = maniread("$dir/MANIFEST");
my $file;
foreach $file (map("$dir/$_",keys %$list))
{
push(@files,$file) if (accept_file($file));
}
}
else
{
die "No $dir/MANIFEST\n";
}
return @files;
}
sub undo
{
my $file;
foreach $file (@_)
{
if (-f "$file.bak")
{
rename("$file.bak",$file) || die "Cannot rename $file $file.undone:$!";
}
else
{
warn "No $file.bak\n";
}
}
}
sub apply
{
my $expr = shift;
if (@_)
{
local $^I = ".bak";
my $sub = eval "sub { $expr }";
die "$@" if ($@);
my @undo = ();
@ARGV = @_;
my $changes = 0;
while (<>)
{
my $line = $_;
if (&$sub)
{
print STDERR "$ARGV:$.:$_";
$changes++ if ($line ne $_);
}
print;
if (eof)
{
if ($changes)
{
print STDERR "$changes in $ARGV due to $expr\n";
$changes = 0;
}
else
{
print STDERR "No changes in $ARGV due to $expr\n";
push(@undo,$ARGV);
}
}
}
undo(@undo) if (@undo);
}
}
sub populate
{
my ($lb,$dir) = @_;
$lb->delete(0,'end');
$lb->Busy;
$lb->insert('end',sort &{$filefunc{$mode}}($dir));
$lb->Unbusy;
}
my $mw = MainWindow->new;
my $ft = $mw->Frame->pack(-fill => 'x');
my $expr = "";
my $dir = `pwd`;
chomp($dir);
my $ed = $mw->LabEntry(-label => 'Directory: ', -textvariable => \$dir, -width => 60)->pack(-fill => 'x');
$ed->bind('<Return>', sub { populate(shift,$dir)});
$mw->LabEntry(-label => 'Matching: ', -textvariable => \$matching, -width => 20)->pack(-fill => 'x');
$mw->LabEntry(-label => 'Containing: ', -textvariable => \$containing, -width => 20)->pack(-fill => 'x');
my $lb = $mw->Scrolled('Listbox')->pack(-fill => 'both', -expand => 1);
my $ee = $mw->LabEntry(-label => 'Expression: ',-textvariable => \$expr, -width => 60)->pack(-fill => 'x');
$ft->Button(-text => 'Quit', -command => [destroy => $mw])->pack(-side => 'left');
$ft->Button(-text => 'Files', -command => sub { populate($lb,$dir)})->pack(-side => 'left');
$ft->Button(-text => 'Apply', -command => sub { apply($expr,$lb->get(0,'end')) })->pack(-side => 'left');
$ft->Button(-text => 'Undo', -command => sub { undo($lb->get(0,'end')) })->pack(-side => 'left');
$ft->Optionmenu(-options => [ keys %filefunc ], -textvariable => \$mode )->pack(-side => 'right');
MainLoop;
|