File Coverage

blib/lib/OrePAN2/Repository.pm
Criterion Covered Total %
statement 64 64 100.0
branch 9 10 90.0
condition n/a
subroutine 20 20 100.0
pod 0 5 0.0
total 93 99 93.9


line stmt bran cond sub pod time code
1             package OrePAN2::Repository;
2              
3 6     6   146531 use utf8;
  6         555  
  6         37  
4              
5 6     6   3487 use Moo;
  6         49453  
  6         23  
6              
7 6     6   9030 use File::Find ();
  6         11  
  6         77  
8 6     6   18 use File::Spec ();
  6         10  
  6         83  
9 6     6   2624 use File::pushd ();
  6         129169  
  6         174  
10 6     6   2842 use OrePAN2::Indexer ();
  6         25  
  6         226  
11 6     6   3253 use OrePAN2::Injector ();
  6         28  
  6         216  
12 6     6   2706 use OrePAN2::Repository::Cache ();
  6         27  
  6         251  
13 6     6   46 use Types::Standard qw( Bool InstanceOf Str );
  6         13  
  6         32  
14 6     6   19008 use Types::Path::Tiny qw( Path );
  6         15  
  6         27  
15              
16 6     6   3487 use namespace::clean;
  6         11  
  6         31  
17              
18             #<<<
19             has compress_index => ( is => 'ro', isa => Bool, default => !!1 );
20             has cache => ( is => 'lazy', isa => InstanceOf ['OrePAN2::Repository::Cache'], builder => 1, handles => { has_cache => 'is_hit', save_cache => 'save' } );
21             has directory => ( is => 'ro', isa => Path, coerce => 1, required => 1 );
22             has indexer => ( is => 'lazy', isa => InstanceOf ['OrePAN2::Indexer'], builder => 1 );
23             has injector => ( is => 'lazy', isa => InstanceOf ['OrePAN2::Injector'], builder => 1 );
24             has simple => ( is => 'ro', isa => Bool, default => !!0 );
25             #>>>
26              
27             sub _build_cache {
28 5     5   112 my $self = shift;
29 5         100 return OrePAN2::Repository::Cache->new( directory => $self->directory );
30             }
31              
32             sub _build_indexer {
33 4     4   51 my $self = shift;
34 4         121 return OrePAN2::Indexer->new(
35             directory => $self->directory,
36             simple => $self->simple
37             );
38             }
39              
40             sub _build_injector {
41 4     4   48 my $self = shift;
42 4         102 return OrePAN2::Injector->new( directory => $self->directory );
43             }
44              
45             sub make_index {
46 4     4 0 99 my $self = shift;
47 4         121 $self->indexer->make_index( no_compress => !$self->compress_index );
48             }
49              
50             sub inject {
51 4     4 0 250216 my ( $self, $stuff, $opts ) = @_;
52              
53 4         99 my $tarpath = $self->injector->inject( $stuff, $opts );
54 4         584 $self->cache->set( $stuff, $tarpath );
55             }
56              
57             sub index_file {
58 6     6 0 13 my $self = shift;
59 6 100       163 return File::Spec->catfile(
60             $self->directory, 'modules',
61             '02packages.details.txt' . ( $self->compress_index ? '.gz' : q{} )
62             );
63             }
64              
65             sub load_index {
66 3     3 0 8 my $self = shift;
67              
68 3         199 my $index = OrePAN2::Index->new();
69 3         143 $index->load( $self->index_file );
70 3         2841 $index;
71             }
72              
73             # Remove files that are not referenced by the index file.
74             sub gc {
75 3     3 0 4371 my ( $self, $callback ) = @_;
76              
77 3 50       20 return unless -f $self->index_file;
78              
79 3         122 my $index = $self->load_index;
80 3         8 my %registered;
81 3         16 for my $package ( $index->packages ) {
82 2         8 my ( $version, $path ) = $index->lookup($package);
83 2         9 $registered{$path}++;
84             }
85              
86 3         40 my $pushd = File::pushd::pushd(
87             File::Spec->catdir( $self->directory, 'authors', 'id' ) );
88             File::Find::find(
89             {
90             no_chdir => 1,
91             wanted => sub {
92 17 100   17   1614 return unless -f $_;
93 5         35 $_ = File::Spec->canonpath($_);
94 5 100       21 unless ( $registered{$_} ) {
95 3 100       319 $callback ? $callback->($_) : unlink $_;
96             }
97 5         1852 1;
98             },
99             },
100 3         1019 '.'
101             );
102             }
103              
104             1;
105