| 1 |
#!/usr/local/bin/perl -w |
| 2 |
use strict; |
| 3 |
use Tk; |
| 4 |
use Tk::LabEntry; |
| 5 |
use Tk::ErrorDialog; |
| 6 |
use File::Find; |
| 7 |
use ExtUtils::Manifest "/mani/"; |
| 8 |
|
| 9 |
my $containing = '/perl\b'; |
| 10 |
my $matching = '\.p[ml]$'; |
| 11 |
my $mode = 'MANIFEST'; |
| 12 |
my %filefunc = ( MANIFEST => \&manifiles, |
| 13 |
Dummy => \&dummyfiles, |
| 14 |
'find' => \&findfiles ); |
| 15 |
|
| 16 |
sub accept_file |
| 17 |
{ |
| 18 |
my ($file) = @_; |
| 19 |
return 0 unless (-w $file); |
| 20 |
my $accept = $matching && ($file =~ /$matching/); |
| 21 |
if (!$accept && $containing) |
| 22 |
{ |
| 23 |
open(FILE,"<$file") || die "Cannot open $file:$!"; |
| 24 |
my $line = <FILE>; |
| 25 |
close(FILE); |
| 26 |
$accept = $line && $line =~ m#$containing#; |
| 27 |
} |
| 28 |
return $accept; |
| 29 |
} |
| 30 |
|
| 31 |
sub findfiles |
| 32 |
{ |
| 33 |
my ($dir) = @_; |
| 34 |
my @files = (); |
| 35 |
die "Not a directory $dir\n", unless (-d $dir); |
| 36 |
find(sub { |
| 37 |
$File::Find::prune = 0; |
| 38 |
if (-T $_ && !/%$/) |
| 39 |
{ |
| 40 |
push(@files,"$File::Find::dir/$_") if (accept_file($_)); |
| 41 |
} |
| 42 |
elsif (-d $_) |
| 43 |
{ |
| 44 |
$File::Find::prune = 1 if ($_ eq 'blib'); |
| 45 |
}},$dir); |
| 46 |
return @files; |
| 47 |
} |
| 48 |
|
| 49 |
sub dummyfiles |
| 50 |
{ |
| 51 |
my ($dir) = @_; |
| 52 |
my @files = (); |
| 53 |
my $file; |
| 54 |
foreach $file (map("$dir/$_",'aaaa'...'aaaz')) |
| 55 |
{ |
| 56 |
push(@files,$file); |
| 57 |
} |
| 58 |
return @files; |
| 59 |
} |
| 60 |
|
| 61 |
sub manifiles |
| 62 |
{ |
| 63 |
my ($dir) = @_; |
| 64 |
my @files = (); |
| 65 |
if (-d $dir && -f "$dir/MANIFEST") |
| 66 |
{ |
| 67 |
my $list = maniread("$dir/MANIFEST"); |
| 68 |
my $file; |
| 69 |
foreach $file (map("$dir/$_",keys %$list)) |
| 70 |
{ |
| 71 |
push(@files,$file) if (accept_file($file)); |
| 72 |
} |
| 73 |
} |
| 74 |
else |
| 75 |
{ |
| 76 |
die "No $dir/MANIFEST\n"; |
| 77 |
} |
| 78 |
return @files; |
| 79 |
} |
| 80 |
|
| 81 |
sub undo |
| 82 |
{ |
| 83 |
my $file; |
| 84 |
foreach $file (@_) |
| 85 |
{ |
| 86 |
if (-f "$file.bak") |
| 87 |
{ |
| 88 |
rename("$file.bak",$file) || die "Cannot rename $file $file.undone:$!"; |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
warn "No $file.bak\n"; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
sub apply |
| 98 |
{ |
| 99 |
my $expr = shift; |
| 100 |
if (@_) |
| 101 |
{ |
| 102 |
local $^I = ".bak"; |
| 103 |
my $sub = eval "sub { $expr }"; |
| 104 |
die "$@" if ($@); |
| 105 |
my @undo = (); |
| 106 |
@ARGV = @_; |
| 107 |
my $changes = 0; |
| 108 |
while (<>) |
| 109 |
{ |
| 110 |
my $line = $_; |
| 111 |
if (&$sub) |
| 112 |
{ |
| 113 |
print STDERR "$ARGV:$.:$_"; |
| 114 |
$changes++ if ($line ne $_); |
| 115 |
} |
| 116 |
print; |
| 117 |
if (eof) |
| 118 |
{ |
| 119 |
if ($changes) |
| 120 |
{ |
| 121 |
print STDERR "$changes in $ARGV due to $expr\n"; |
| 122 |
$changes = 0; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
print STDERR "No changes in $ARGV due to $expr\n"; |
| 127 |
push(@undo,$ARGV); |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
undo(@undo) if (@undo); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
sub populate |
| 136 |
{ |
| 137 |
my ($lb,$dir) = @_; |
| 138 |
$lb->delete(0,'end'); |
| 139 |
$lb->Busy; |
| 140 |
$lb->insert('end',sort &{$filefunc{$mode}}($dir)); |
| 141 |
$lb->Unbusy; |
| 142 |
} |
| 143 |
|
| 144 |
my $mw = MainWindow->new; |
| 145 |
my $ft = $mw->Frame->pack(-fill => 'x'); |
| 146 |
my $expr = ""; |
| 147 |
my $dir = `pwd`; |
| 148 |
chomp($dir); |
| 149 |
my $ed = $mw->LabEntry(-label => 'Directory: ', -textvariable => \$dir, -width => 60)->pack(-fill => 'x'); |
| 150 |
$ed->bind('<Return>', sub { populate(shift,$dir)}); |
| 151 |
$mw->LabEntry(-label => 'Matching: ', -textvariable => \$matching, -width => 20)->pack(-fill => 'x'); |
| 152 |
$mw->LabEntry(-label => 'Containing: ', -textvariable => \$containing, -width => 20)->pack(-fill => 'x'); |
| 153 |
my $lb = $mw->Scrolled('Listbox')->pack(-fill => 'both', -expand => 1); |
| 154 |
my $ee = $mw->LabEntry(-label => 'Expression: ',-textvariable => \$expr, -width => 60)->pack(-fill => 'x'); |
| 155 |
$ft->Button(-text => 'Quit', -command => [destroy => $mw])->pack(-side => 'left'); |
| 156 |
$ft->Button(-text => 'Files', -command => sub { populate($lb,$dir)})->pack(-side => 'left'); |
| 157 |
$ft->Button(-text => 'Apply', -command => sub { apply($expr,$lb->get(0,'end')) })->pack(-side => 'left'); |
| 158 |
$ft->Button(-text => 'Undo', -command => sub { undo($lb->get(0,'end')) })->pack(-side => 'left'); |
| 159 |
$ft->Optionmenu(-options => [ keys %filefunc ], -textvariable => \$mode )->pack(-side => 'right'); |
| 160 |
MainLoop; |