Created
November 17, 2010 03:50
-
-
Save yaotti/702953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# Search moduels using minicpan data | |
# $ minicpan-search plack middleware | |
# $ minicpan-search authorname | |
# $ minicpan-search --root=~/myminicpandir authorname | |
# $ cpanm `minicpan-search --colon-format module` | |
use strict; | |
use warnings; | |
use Cwd 'abs_path'; | |
use File::Basename; | |
use Getopt::Long; | |
use Perl6::Slurp; | |
my $colon_format; # Output like Foo::Bar | |
my $root = '~/minicpan'; | |
my $module_names_file = '~/.minicpan-search-cache'; | |
my $expire_cache; | |
GetOptions('colon-format' => \$colon_format, | |
'root=s' => \$root, | |
'module_names_file=s' => \$module_names_file, | |
'expire-cache' => \$expire_cache); | |
$root = glob($root); | |
$module_names_file = glob($module_names_file); | |
if ($expire_cache) { | |
printf "Remove cache file: %s\n", $module_names_file; | |
unlink($module_names_file) or die "$module_names_file: $!"; | |
} | |
my $all_modules = []; | |
if (-f $module_names_file) { | |
warn "Use cached module names\n"; | |
$all_modules = [split '\n', slurp $module_names_file]; | |
} | |
if (@$all_modules == 0) { | |
open my $fh, ">", glob($module_names_file) | |
or die "$module_names_file: $!"; | |
for (<$root/authors/id/*/*/*/*>) { | |
my $module = basename(dirname($_)) . "/" . basename($_); | |
next if $module =~ /CHECKSUMS$/; | |
push @$all_modules, $module; | |
print $fh $module, "\n"; | |
} | |
} | |
my $search_words = [@ARGV]; | |
my $results = $all_modules; | |
for my $q (@ARGV) { | |
$results = [grep {/$q/i} @$results]; | |
} | |
map { | |
if ($colon_format) { | |
s/^.+\///; | |
s/-[^-]+$//; | |
s/-/::/g; | |
} | |
print $_, "\n"; } | |
@$results; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment