File Coverage

blib/lib/JSAN/Mini.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package JSAN::Mini;
2              
3             =pod
4              
5             =head1 NAME
6              
7             JSAN::Mini - Creates a minimal local mirror of JSAN for offline installation
8              
9             =head1 SYNOPSIS
10              
11             # Update your local minijsan using default settings
12             JSAN::Mini->update_mirror;
13            
14             # ... and for now that's about it :)
15              
16             =head1 DESCRIPTION
17              
18             L is an application which scans the JSAN index and ensures that
19             the release tarballs for all of the libraries in the index are stored in
20             the local mirror provided by L.
21              
22             This allows for the installation of JSAN packages without the need to
23             connect to the internet. For example, it can be very useful for installing
24             packaging while on international flights for example :)
25              
26             C provides the primary API for implementing the functionality
27             for L, and also provides something that you can sub-class, and
28             thus add your own additional functionality.
29              
30             If you're a normal user, or you are ot going to do anything weird, you might
31             want to look at L instead.
32              
33             =head1 METHODS
34              
35             =cut
36              
37 2     2   27003 use 5.006;
  2         8  
  2         89  
38 2     2   11 use strict;
  2         5  
  2         75  
39 2     2   2108 use Params::Util '_INSTANCE';
  2         9950  
  2         167  
40 2     2   2455 use JSAN::Transport;
  0            
  0            
41             use JSAN::Index;
42              
43             our $VERSION = '1.04';
44              
45              
46              
47              
48              
49             #####################################################################
50             # Static Methods
51              
52             =pod
53              
54             =head2 update_mirror
55              
56             The C static method creates and executes a new L
57             object using the default params, normally pretty much Doing What You Mean.
58              
59             =cut
60              
61             sub update_mirror {
62             my $class = shift;
63             my $self = $class->new(@_);
64             $self->run;
65             }
66              
67              
68              
69              
70              
71             #####################################################################
72             # Constructor and Accessors
73              
74             =pod
75              
76             =head2 new value => 'param'
77              
78             The C constructor creates a new minijsan process.
79              
80             It takes as argument a set of key/value pairs controlling it.
81              
82             =over 4
83              
84             =item verbose
85              
86             The verbose flag controls the level of debugging output that the
87             object will produce.
88              
89             When set to true, it causes process information to be printed to
90             C. When set to false (the default) it prints nothing.
91              
92             =back
93              
94             Returns a C object.
95              
96             =cut
97              
98             sub new {
99             my $class = ref $_[0] ? ref shift : shift;
100            
101             # Create the basic object
102             my $self = bless {
103             added => 0,
104             }, $class;
105              
106             $self;
107             }
108              
109             =pod
110              
111             =head2 added
112              
113             Once the C object has been C, the C method returns
114             the number of new releases that were added to the local mirror.
115              
116             =cut
117              
118             sub added { $_[0]->{added} }
119              
120              
121              
122              
123              
124             #####################################################################
125             # JSAN::Mini Methods
126              
127             =pod
128              
129             =head2 run
130              
131             The C method initiates the minicpan process to syncronize the files
132             in the local mirror with those on the remote mirror.
133              
134             Returns the number of new files added to the L mirror.
135              
136             =cut
137              
138             sub run {
139             my $self = shift;
140             $self->{added} = 0;
141             $self->_verbose("JSAN::Mini starting...");
142              
143             # Add each of the releases
144             my @releases = $self->_releases;
145             foreach my $release ( @releases ) {
146             next if $release->file_mirrored;
147             $self->_verbose('Adding ' . $release->source . ' to minijsan');
148             $self->add_release( $release );
149             }
150              
151             # If there is a dist processing step, do that.
152             # We use a seperate loop in case there are processing
153             # operations that need to run across several files.
154             foreach my $release ( @releases ) {
155             $self->process_release( $release );
156             }
157              
158             $self->_verbose('JSAN::Mini run completed.');
159             $self->{added};
160             }
161              
162             # Find the list of releases
163             sub _releases {
164             my $self = shift;
165              
166             $self->_verbose("Generating JSAN::Index::Release list...");
167             my @libs = JSAN::Index::Library->select;
168             my @releases = map { $_->release } @libs;
169             my %seen = ();
170             @releases = sort { $a->source cmp $b->source }
171             grep { $seen{$_->id}++ } @releases;
172             $self->_verbose('Found ' . scalar(@releases) . ' releases to check');
173              
174             @releases;
175             }
176              
177             =pod
178              
179             =head2 add_release $release
180              
181             The C method is called when a release is to be added to the
182             local mirror.
183              
184             The method is passed a L object and, by default,
185             mirrors it from the remote repository.
186              
187             This is the method that you would typically subclass to add additional
188             functionality to the module (where such functionality does not on
189             information contained) in other releases in the repository.
190              
191             =cut
192              
193             sub add_release {
194             my $self = shift;
195             my $release = _INSTANCE($_[0], 'JSAN::Index::Release') ? shift
196             : Carp::croak("JSAN::Mini::add_release was not passed a JSAN::Index::Release object");
197             $release->mirror;
198             1;
199             }
200              
201             =pod
202              
203             =head2 process_release $release
204              
205             The optional C method can be defined by a C
206             sub-class, and can be used as a place to implement extended functionality,
207             where this functionality requires that all new releases by downloaded
208             before processing starts.
209              
210             The method is passed a L object and simply shortcuts
211             by default.
212              
213             =cut
214              
215             sub process_release {
216             1;
217             }
218              
219             # When in verbose mode (which is all the time for now) print a message
220             # to STDOUT
221             sub _verbose {
222             my $self = shift;
223             if ( $self->{verbose} ) {
224             my $msg = shift;
225             $msg =~ s/\n?$/\n/s;
226             print $msg;
227             }
228             1;
229             }
230              
231             1;
232              
233             =pod
234              
235             =head1 SUPPORT
236              
237             Bugs should be reported via the CPAN bug tracker at
238              
239             L
240              
241             For other issues, contact the author.
242              
243             =head1 AUTHOR
244              
245             Adam Kennedy Eadamk@cpan.orgE, L
246              
247             =head1 COPYRIGHT
248              
249             Copyright 2005, 2009 Adam Kennedy.
250              
251             This program is free software; you can redistribute
252             it and/or modify it under the same terms as Perl itself.
253              
254             The full text of the license can be found in the
255             LICENSE file included with this module.
256              
257             =cut