don't expect a list of data and a tail in case of an error
[scpubgit/System-Introspector.git] / lib / System / Introspector / LibDirs / Perl.pm
CommitLineData
f356dcb0 1package System::Introspector::LibDirs::Perl;
2use Moo;
3use Module::Metadata;
4use Digest::SHA;
5
ff854047 6use System::Introspector::Util qw(
7 handle_from_command
8 transform_exceptions
9);
10
f356dcb0 11has root => (
12 is => 'ro',
13 default => sub { '/' },
14);
15
16sub gather {
17 my ($self) = @_;
ff854047 18 return transform_exceptions {
19 my $pipe = $self->_open_locate_libdirs_pipe;
20 my %libdir;
21 while (defined( my $line = <$pipe> )) {
22 chomp $line;
23 $libdir{ $line } = transform_exceptions {
24 return { modules => $self->_gather_libdir_info($line) };
25 };
26 }
27 return { libdirs_perl => \%libdir };
28 };
f356dcb0 29}
30
31sub _gather_libdir_info {
32 my ($self, $libdir) = @_;
33 my %module;
34 my $pipe = $self->_open_locate_pm_pipe($libdir);
35 while (defined( my $line = <$pipe> )) {
36 chomp $line;
37 my $metadata = Module::Metadata->new_from_file($line);
38 next unless $metadata->name;
39 my $sha = Digest::SHA->new(256);
40 $sha->addfile($line);
41 push @{ $module{ $metadata->name } //= [] }, {
42 file => $line,
43 version => $metadata->version,
44 size => scalar(-s $line),
45 sha256_hex => $sha->hexdigest,
46 };
47 }
48 return \%module;
49}
50
51sub _open_locate_pm_pipe {
52 my ($self, $libdir) = @_;
ff854047 53 return handle_from_command
54 sprintf q{find %s -name '*.pm'}, $libdir;
f356dcb0 55}
56
57sub _open_locate_libdirs_pipe {
58 my ($self) = @_;
59 my $root = $self->root;
60 $root .= '/'
61 unless $root =~ m{/$};
ff854047 62 return handle_from_command sprintf
63 q{locate --regex '^%s.*lib/perl5$'}, $root;
f356dcb0 64}
65
661;
535e84b6 67
68__END__
69
70=head1 NAME
71
72System::Introspector::LibDirs::Perl - Gather perl lib directory data
73
74=head1 DESCRIPTION
75
76Finds locations that look like L<local::lib> or comparable Perl library
77directories, and extracts module information from them.
78
79=head1 ATTRIBUTES
80
81=head2 root
82
83This is the root path to be searched for library directories. Defaults
84to C</>.
85
86=head1 SEE ALSO
87
88=over
89
90=item L<System::Introspector>
91
92=back
93
94=cut
95