]> gitweb @ CieloNegro.org - sugar.git/blob - tools/list-files.pl
Auto commit by The Sugar System.
[sugar.git] / tools / list-files.pl
1 use strict;
2 use warnings;
3 use Smart::Comments;
4 use List::MoreUtils qw(any);
5 use File::Spec;
6
7 my @RE_IGNORED = do {
8     open my $fh, '<', 'IGNORE' or die $!;
9     map {
10         s/\./\\./g;
11         s/\+/\\+/g;
12
13         s/\?/.?/g;
14         s/\*/.*?/g;
15
16         qr/^$_$/;
17       }
18       grep {length}
19       map  {s/^\s*#//; $_}
20       map  {tr/\n//d; $_}
21       <$fh>;
22 };
23
24 main();
25
26 sub main {
27     my @current = find($ENV{HOME}, '.', sub {$_[0] =~ m/^\./});
28
29     foreach (@current) {
30         print "$_\n";
31     }
32 }
33
34 sub find {
35     my $base = shift;
36     my $dir  = shift || '.';
37     my $sel  = shift || sub {1};
38
39     my $dirpath = File::Spec->catfile($base, $dir);
40     opendir my $dh, $dirpath or die "opendir: $dirpath: $!";
41
42     my @d;
43     while (my $name = readdir $dh) {
44         next if $name =~ /^\.\.?$/;
45         next if !$sel->($name);
46
47         my $abs = File::Spec->rel2abs($name, $dirpath);
48         my $rel = File::Spec->abs2rel($abs, $base);
49
50         next if any { $rel =~ m/$_/ } @RE_IGNORED;
51         next if !-e $abs;
52
53         if (-d $abs) {
54             push @d, find($base, $rel);
55         }
56         else {
57             push @d, $rel;
58         }
59     }
60     @d;
61 }