#!/usr/bin/env perl6
#!/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.'
) ;
my $id = $os.insert - pos(
"directory" ,
sub find- and- print- source( $os, $dira) {
my @stack = $dira.value ;
my % ext := set (
with $os< only> {
fail "Not recognized category: {$_}." unless $_ ( elem) < c h cpp cfg m a w >;
$os{ $_} // [];
} else {
my @ext = [ ] ;
for < c h cpp cfg m a > {
if $_ ! ( elem) @( $os< no> ) {
@ext.append ( $os{ $_} // []);
}
}
@ext;
}
) ;
my % whole := set( $os< w> // []);
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< d>;
my @stack- t = ( @stack.race .map (
sub ( $_) {
note "\t |GOT FILE => " , $_ if $os< d>;
if nqp:: lstat ( nqp:: unbox_s ( $_) , nqp:: const :: STAT_ISDIR ) == 1 {
return .& getSubFiles;
} else {
my $fp = & basename( $_) ;
if $fp.substr ( ( $fp.rindex ( "." ) // -1) + 1) (elem) %ext {
$supplier.emit ( $_) ;
} elsif $os< w> .defined && $fp ( elem) % whole {
$supplier.emit ( $_) ;
}
}
return ( ) ;
}
) .flat ) ;
@stack = @stack- t;
} ;
} ,
: last
) ;
& getopt( $os) ;
sub basename( $filepath) {
return $filepath.substr ( ( $filepath.rindex ( '/' ) // -1) + 1);
}
sub getSubFiles( $path) {
my @ret := [ ] ;
my $dh := nqp:: opendir ( $path) ;
while ( my $f = nqp:: nextfiledir ( $dh) ) {
@ret.push ( "$path/$f" ) if $f ne ".." && $f ne "." ;
}
nqp:: closedir ( $dh) ;
return @ret;
}
IyEvdXNyL2Jpbi9lbnYgcGVybDYKCiMhL3Vzci9iaW4vZW52IHBlcmw2Cgp1c2UgbnFwOwp1c2UgR2V0b3B0OjpBZHZhbmNlOwoKbXkgT3B0aW9uU2V0ICRvcyAuPSBuZXc7Cgokb3MucHVzaCgKICAgICdjPWEnLAogICAgJ2Mgc291cmNlIGZpbGUgZXh0ZW5zaW9uIGxpc3QuJywKICAgIHZhbHVlID0+IFsgImMiLCBdCik7CiRvcy5wdXNoKAogICAgJ2g9YScsCiAgICAnaGVhZCBmaWxlIGV4dGVuc2lvbiBsaXN0LicsCiAgICB2YWx1ZSA9PiBbICJoIiwgXQopOwokb3MucHVzaCgKICAgICdjcHB8PWEnLAogICAgJ2NwcCBzb3VyY2UgZmlsZSBleHRlbnNpb24gbGlzdC4nLAogICAgdmFsdWUgPT4gUSA6dyAhIEMgY3BwIGMrKyBjeHggaHBwIGNjIGgrKyBoaCBoeHghCik7CiRvcy5wdXNoKAogICAgJ2NmZ3w9YScsCiAgICAnY29uZmlnIGZpbGUgZXh0ZW5zaW9uIGxpc3QuJywKICAgIHZhbHVlID0+IFEgOncgISBpbmkgY29uZmlnIGNvbmYgY2ZnIHhtbCAhCik7CiRvcy5wdXNoKAogICAgJ209YScsCiAgICAnbWFrZWZpbGUgZXh0ZW5zaW9uIGxpc3QuJywKICAgIHZhbHVlID0+IFsibWsiLCBdCik7CiRvcy5wdXNoKAogICAgJ3c9YScsCiAgICAnbWF0Y2ggd2hvbGUgZmlsZW5hbWUuJywKICAgIHZhbHVlID0+IFEgOncgISBtYWtlZmlsZSBNYWtlZmlsZSAhCik7CiRvcy5wdXNoKAogICAgJ2E9YScsCiAgICAnYWRkaXRpb24gZXh0ZW5zaW9uIGxpc3QuJywKKTsKJG9zLnB1c2goCiAgICAnaT1iJywKICAgICdlbmFibGUgaWdub3JlIGNhc2UgbW9kZS4nCik7CiRvcy5wdXNoKAogICAgJ25vfD1hJywKICAgICdleGNsdWRlIGZpbGUgY2F0ZWdvcnkuJywKKTsKJG9zLnB1c2goCiAgICAnb25seXw9cycsCiAgICAnb25seSBzZWFyY2ggZ2l2ZW4gY2F0ZWdvcnkuJywKKTsKJG9zLnB1c2goCiAgICAnZHxkZWJ1Zz1iJywKICAgICdwcmludCBkZWJ1ZyBtZXNzYWdlLicKKTsKbXkgJGlkID0gJG9zLmluc2VydC1wb3MoCiAgICAiZGlyZWN0b3J5IiwKICAgIHN1YiBmaW5kLWFuZC1wcmludC1zb3VyY2UoJG9zLCAkZGlyYSkgewogICAgICAgIG15IEBzdGFjayA9ICRkaXJhLnZhbHVlOwogICAgICAgIG15ICVleHQgOj0gc2V0ICgKICAgICAgICAgICAgd2l0aCAkb3M8b25seT4gewogICAgICAgICAgICAgICAgZmFpbCAiTm90IHJlY29nbml6ZWQgY2F0ZWdvcnk6IHskX30uIiB1bmxlc3MgJF8gKGVsZW0pIDwgYyBoIGNwcCBjZmcgbSBhIHcgPjsKICAgICAgICAgICAgICAgICRvc3skX30gLy8gW107CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBteSBAZXh0ID0gW107CiAgICAgICAgICAgICAgICBmb3IgPCBjIGggY3BwIGNmZyBtIGEgPiB7CiAgICAgICAgICAgICAgICAgICAgaWYgJF8gIShlbGVtKSBAKCRvczxubz4pIHsKICAgICAgICAgICAgICAgICAgICAgICAgQGV4dC5hcHBlbmQoJG9zeyRffSAvLyBbXSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgQGV4dDsKICAgICAgICAgICAgfQogICAgICAgICk7CiAgICAgICAgbXkgJXdob2xlIDo9IHNldCgkb3M8dz4gLy8gW10pOwoKICAgICAgICBub3RlICJHRVQgQUxMIEVYVCA9PiAiLCAlZXh0IGlmICRvczxkPjsKCiAgICAgICAgbXkgJHN1cHBsaWVyID0gU3VwcGxpZXIubmV3OwoKICAgICAgICByZWFjdCB7CiAgICAgICAgICAgIHN0YXJ0IHdoZW5ldmVyICRzdXBwbGllci5TdXBwbHkgewogICAgICAgICAgICAgICAgcHV0IFEgOnFxICciJF8iJzsKICAgICAgICAgICAgICAgIExBU1QgZG9uZQogICAgICAgICAgICB9CiAgICAgICAgfQoKICAgICAgICB3aGlsZSBAc3RhY2sgewogICAgICAgICAgICBub3RlICJDVVJSIEZJTEVTID0+ICIsIEBzdGFjayBpZiAkb3M8ZD47CiAgICAgICAgICAgIG15IEBzdGFjay10ID0gKEBzdGFjay5yYWNlLm1hcCgKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHN1YiAoJF8pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBub3RlICJcdHxHT1QgRklMRSA9PiAiLCAkXyBpZiAkb3M8ZD47CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgbnFwOjpsc3RhdChucXA6OnVuYm94X3MoJF8pLCBucXA6OmNvbnN0OjpTVEFUX0lTRElSKSA9PSAxIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgcmV0dXJuIC4mZ2V0U3ViRmlsZXM7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgbXkgJGZwID0gJmJhc2VuYW1lKCRfKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgJGZwLnN1YnN0cigoJGZwLnJpbmRleCgiLiIpIC8vIC0xKSArIDEpIChlbGVtKSAlZXh0IHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICRzdXBwbGllci5lbWl0KCRfKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfSAgZWxzaWYgJG9zPHc+LmRlZmluZWQgJiYgJGZwIChlbGVtKSAld2hvbGUgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgJHN1cHBsaWVyLmVtaXQoJF8pOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHJldHVybiAoKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKS5mbGF0KTsKICAgICAgICAgICAgQHN0YWNrID0gQHN0YWNrLXQ7CiAgICAgICAgfTsKICAgIH0sCiAgICA6bGFzdAopOwoKJmdldG9wdCgkb3MpOwoKc3ViIGJhc2VuYW1lKCRmaWxlcGF0aCkgewogICAgcmV0dXJuICRmaWxlcGF0aC5zdWJzdHIoKCRmaWxlcGF0aC5yaW5kZXgoJy8nKSAvLyAtMSkgKyAxKTsKfQoKc3ViIGdldFN1YkZpbGVzKCRwYXRoKSB7CiAgICBteSBAcmV0IDo9IFtdOwogICAgbXkgJGRoIDo9IG5xcDo6b3BlbmRpcigkcGF0aCk7CgogICAgd2hpbGUgKG15ICRmID0gbnFwOjpuZXh0ZmlsZWRpcigkZGgpKSB7CiAgICAgICAgQHJldC5wdXNoKCIkcGF0aC8kZiIpIGlmICRmIG5lICIuLiIgJiYgJGYgbmUgIi4iOwogICAgfQoKICAgIG5xcDo6Y2xvc2VkaXIoJGRoKTsKCiAgICByZXR1cm4gQHJldDsKfQo=