#!/usr/bin/env perl6
use nqp;
use Getopt:: Advance ;
my OptionSet $os .= new;
$os.push (
'c=a' ,
'c source file extension list.' ,
value => [ "c" , ]
) ;
$os.push (
'h=a' ,
'head file extension list.' ,
value => [ "h" , ]
) ;
$os.push (
'cpp|=a' ,
'cpp source file extension list.' ,
value => Q : w ! C cpp c++ cxx hpp cc h++ hh hxx!
) ;
$os.push (
'cfg|=a' ,
'config file extension list.' ,
value => Q : w ! ini config conf cfg xml !
) ;
$os.push (
'm=a' ,
'makefile extension list.' ,
value => [ "mk" , ]
) ;
$os.push (
'w=a' ,
'match whole filename.' ,
value => Q : w ! makefile Makefile !
) ;
$os.push (
'a=a' ,
'addition extension list.' ,
) ;
$os.push (
'i=b' ,
'enable ignore case mode.'
) ;
$os.push (
'no|=a' ,
'exclude file category.' ,
) ;
$os.push (
'only|=s' ,
'only search given category.' ,
) ;
$os.push (
'd|debug=b' ,
'print debug message.'
) ;
$os.push (
'd2|=b' ,
'show file in debug mode.'
) ;
my $id = $os.insert - pos(
"directory" ,
sub find- and- print- source( $os, $dira) {
my @stack = $dira.value ;
my @ext = [ ] ;
with $os< only> {
fail "Not recognized category: {$_}." unless $_ ( elem) < c h cpp cfg m a w >;
@ext = $os{ $_} // [];
} else {
for < c h cpp cfg m a > -> $opt {
if $opt ! ( elem) @( $os< no> ) {
@ext.append ( $os{ $opt} // []);
}
}
}
note "GET ALL EXT => " , @ext if $os< d>;
my $supplier = Supplier.new ;
react {
start whenever $supplier.Supply {
put Q : qq '"$_"' ;
LAST done
}
}
while @stack {
note "CURR FILES => " , @stack if $os< d2>;
my @stack- t = ( @stack.race .map (
-> $_ {
note "\t |GOT FILE => " , $_ if $os< d>;
when nqp:: lstat ( nqp:: unbox_s ( $_) , nqp:: const :: STAT_ISDIR ) == 1 {
.& getSubFiles;
}
# regex search is slow than ext match
when ( $_.substr ( ( $_.index ( "." ) // -1) + 1) (elem) @ext) || ($_ (elem) @($os<w>)) { # / \. @ext $/ {
$supplier.emit ( $_) ;
( ) ;
}
default {
( ) ;
}
}
) .flat ) ;
@stack = @stack- t;
} ;
} ,
: last
) ;
& getopt( $os) ;
sub getSubFiles( $path) {
my @ret := [ ] ;
my $dh := nqp:: opendir ( $path) ;
while ( my $f = nqp:: nextfiledir ( $dh) ) {
@ret.push ( "$path/$f" ) if $f ne "." | ".." ;
}
nqp:: closedir ( $dh) ;
return @ret;
}
IyEvdXNyL2Jpbi9lbnYgcGVybDYKCnVzZSBucXA7CnVzZSBHZXRvcHQ6OkFkdmFuY2U7CgpteSBPcHRpb25TZXQgJG9zIC49IG5ldzsKCiRvcy5wdXNoKAogICAgJ2M9YScsCiAgICAnYyBzb3VyY2UgZmlsZSBleHRlbnNpb24gbGlzdC4nLAogICAgdmFsdWUgPT4gWyAiYyIsIF0KKTsKJG9zLnB1c2goCiAgICAnaD1hJywKICAgICdoZWFkIGZpbGUgZXh0ZW5zaW9uIGxpc3QuJywKICAgIHZhbHVlID0+IFsgImgiLCBdCik7CiRvcy5wdXNoKAogICAgJ2NwcHw9YScsCiAgICAnY3BwIHNvdXJjZSBmaWxlIGV4dGVuc2lvbiBsaXN0LicsCiAgICB2YWx1ZSA9PiBRIDp3ICEgQyBjcHAgYysrIGN4eCBocHAgY2MgaCsrIGhoIGh4eCEKKTsKJG9zLnB1c2goCiAgICAnY2ZnfD1hJywKICAgICdjb25maWcgZmlsZSBleHRlbnNpb24gbGlzdC4nLAogICAgdmFsdWUgPT4gUSA6dyAhIGluaSBjb25maWcgY29uZiBjZmcgeG1sICEKKTsKJG9zLnB1c2goCiAgICAnbT1hJywKICAgICdtYWtlZmlsZSBleHRlbnNpb24gbGlzdC4nLAogICAgdmFsdWUgPT4gWyJtayIsIF0KKTsKJG9zLnB1c2goCiAgICAndz1hJywKICAgICdtYXRjaCB3aG9sZSBmaWxlbmFtZS4nLAogICAgdmFsdWUgPT4gUSA6dyAhIG1ha2VmaWxlIE1ha2VmaWxlICEKKTsKJG9zLnB1c2goCiAgICAnYT1hJywKIC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QGV4dCBpZiAkb3M8ZD47CgogICAgICAgIG15ICRzdXBwbGllciA9IFN1cHBsaWVyLm5ldzsKCiAgICAgICAgcmVhY3QgewogICAgICAgICAgICBzdGFydCB3aGVuZXZlciAkc3VwcGxpZXIuU3VwcGx5IHsKICAgICAgICAgICAgICAgIHB1dCBRIDpxcSAnIiRfIic7CiAgICAgICAgICAgICAgICBMQVNUIGRvbmUKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgd2hpbGUgQHN0YWNrIHsKICAgICAgICAgICAgbm90ZSAiQ1VSUiBGSUxFUyA9PiAiLCBAc3RhY2sgaWYgJG9zPGQyPjsKICAgICAgICAgICAgbXkgQHN0YWNrLXQgPSAoQHN0YWNrLnJhY2UubWFwKAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLT4gJF8gewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5vdGUgIlx0fEdPVCBGSUxFID0+ICIsICRfIGlmICRvczxkPjsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB3aGVuIG5xcDo6bHN0YXQobnFwOjp1bmJveF9zKCRfKSwgbnFwOjpjb25zdDo6U1RBVF9JU0RJUikgPT0gMSB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC4mZ2V0U3ViRmlsZXM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICMgcmVnZXggc2VhcmNoIGlzIHNsb3cgdGhhbiBleHQgbWF0Y2gKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB3aGVuICgkXy5zdWJzdHIoKCRfLmluZGV4KCIuIikgLy8gLTEpICsgMSkgKGVsZW0pIEBleHQpIHx8ICgkXyAoZWxlbSkgQCgkb3M8dz4pKSB7ICMgLyBcLiBAZXh0ICQvIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJHN1cHBsaWVyLmVtaXQoJF8pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZGVmYXVsdCB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICgpOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKS5mbGF0KTsKICAgICAgICAgICAgQHN0YWNrID0gQHN0YWNrLXQ7CiAgICAgICAgfTsKICAgIH0sCiAgICA6bGFzdAopOwoKJmdldG9wdCgkb3MpOwoKc3ViIGdldFN1YkZpbGVzKCRwYXRoKSB7CiAgICBteSBAcmV0IDo9IFtdOwogICAgbXkgJGRoIDo9IG5xcDo6b3BlbmRpcigkcGF0aCk7CgogICAgd2hpbGUgKG15ICRmID0gbnFwOjpuZXh0ZmlsZWRpcigkZGgpKSB7CiAgICAgICAgQHJldC5wdXNoKCIkcGF0aC8kZiIpIGlmICRmIG5lICIuIiB8ICIuLiI7CiAgICB9CgogICAgbnFwOjpjbG9zZWRpcigkZGgpOwoKICAgIHJldHVybiBAcmV0Owp9Cg==