This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
better de-dup metagrep
[metaconfig.git] / bin / metagrep
CommitLineData
6092c506 1#!/usr/bin/perl
f83b5b07 2
9471df58 3use 5.12.0;
f83b5b07
MB
4use warnings;
5
9471df58
MB
6our $VERSION = "0.01 - 20181213";
7our $CMD = $0 =~ s{.*/}{}r;
8
9sub usage {
10 my $err = shift and select STDERR;
11 say "usage: $CMD [-l] [-w] [-F] [-v[#]] pattern";
12 say " -w --word Word-anchored matches";
13 say " -l --list List matched filenames only";
14 say " -F --fixed Fixed pattern (quotemeta regex)";
15 say " -v [#] --verbose[=#] Verbosity";
16 exit $err;
17 } # usage
18
f83b5b07 19use Getopt::Long qw(:config bundling nopermute passthrough);
f83b5b07 20GetOptions (
9471df58
MB
21 "w|word!" => \(my $opt_w = 0),
22 "l|list!" => \(my $opt_l = 0),
23 "F|fixed!" => \(my $opt_F = 0),
24 "v|verbose:1" => \(my $opt_v = 0),
0d4505f5 25 ) or die "usage: metagrep [-w] [-l] [-F] pattern\n";
f83b5b07 26
764afc4c 27use Cwd qw(getcwd abs_path);
f83b5b07 28use File::Find;
a8ae8817 29use FindBin;
f83b5b07
MB
30
31my $pat = shift or die "usage: metagrep pattern\n";
0d4505f5 32$opt_F and $pat = quotemeta $pat;
f83b5b07
MB
33$opt_w and $pat = "\\b$pat\\b";
34$pat = qr/$pat/i;
35
f6353537 36my $cwd = getcwd;
764afc4c 37my $mcpath = abs_path "$FindBin::Bin/../";
f6353537
MB
38my $onmeta = $cwd =~ m{CPAN/meta[^/]+$} ? 1 : 0;
39
aaedcd5e 40my @dir = grep { -d } $mcpath, $onmeta ? "dist/U" : "$mcpath/dist/U";
f83b5b07
MB
41my %dir; # I don't want a file for which any path component symlinks
42find (sub {
43 -l and return;
44 -d and $dir{$File::Find::name}++;
459d3fb5 45 }, @dir);
f83b5b07
MB
46
47print STDERR "<$pat>\n";
459d3fb5 48my %seen;
f83b5b07 49find (sub {
9471df58
MB
50 -l and return;
51 -f or return;
52 m/\.U$/ or return;
f83b5b07 53
9471df58
MB
54 exists $dir{$File::Find::dir} or return;
55 $opt_v and warn "$File::Find::dir - $_\n";
f83b5b07 56
9471df58 57 $seen{abs_path ($_)}++ and return;
f8ed6dd4 58
9471df58
MB
59 my $fqdn = abs_path ($File::Find::dir);
60 $fqdn =~ m{/dist-3} and return;
61 $fqdn =~ m{/_metaconfig_perl} and return;
459d3fb5 62
f6353537
MB
63 my $fnm = $File::Find::name;
64 $fnm =~ s{^$cwd/}{};
9471df58 65 open my $f, "<$_" or die "$fnm: $!\n";
459d3fb5
MBT
66 for (grep /$pat/, <$f>) {
67 if ($opt_l) {
f8ed6dd4
MBT
68 print "$fnm\n";
69 return;
459d3fb5
MBT
70 }
71 print "$fnm:$_";
72 }
73 }, @dir);