Librarian.pm

filter: Statementstmt 84.1% 85 / 101 Branchbran 34.6% 9 / 26 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 78.5% 22 / 28 Podpod 100% 8 / 8 totaltotal 73.6% 126 / 171 CC 19 SCAR 13.1
CC
19
Cov
73%
make_index
10.5
new
6.1
_new_param
6.0
CRAP 3.7
↑ Summary Librarian/Library.pm »
1 package JSAN::Librarian;
2  
3 =pod
4  
5 =head1 NAME
6  
7 JSAN::Librarian - JavaScript::Librarian adapter for a JSAN installation
8  
9 =head1 DESCRIPTION
10  
11 L<JavaScript::Librarian> works on the concept of "libraries" of JavaScript
12 files each of which may depend on other files to be loaded before them.
13  
14 C<JSAN::Librarian> provides a mechanism for detecting and indexing a
15 L<JavaScript::Librarian::Library> object for a L<JSAN> installation.
16  
17 =head1 METHODS
18  
19 =cut
20  
21 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Config::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Params::Util qw{ _STRING };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use File::Find::Rule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use JSAN::Parse::FileDeps ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use JSAN::Librarian::Book ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use JSAN::Librarian::Library ();
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 2 ▶use vars qw{$VERSION $VERBOSE @DEFAULT};
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
36 2 $VERSION = '0.03';
37  
38 # Silent by default
39 2 ▶ $VERBOSE ||= 0;
Condition50%1/2
$JSAN::Librarian::VERBOSE ||= 0
l!l
02
MC/DC0%0/1
$JSAN::Librarian::VERBOSE ||= 0
$JSAN::Librarian::VERBOSE
Truth table50%1/2
A
A= $JSAN::Librarian::VERBOSE
Aresult
11
00
40  
41 # Look for the index at existing places.
42 # If none found, assume the first.
43 2 @DEFAULT = qw{
44 openjsan.deps
45 .openjsan.deps
46 };
47 }
48  
49  
50  
51  
52  
53 #####################################################################
54 # Constructor
55  
56 =pod
57  
58 =head2 new $path, $index
59  
60 The C<new> constructor creates a new C<JSAN::Librarian> object for a
61 JSAN installation library/prefix located at a local directory.
62  
63 Because a JSAN installation library does not have a definitive method
64 by which its existance can be verified, at this time the only check
65 actually made is that the directory exists.
66  
67 An optional second parameter can be provided, which will be taken to
68 be the location of the index file. Relative paths will be interpreted
69 as being relative to the root path passed as the first param.
70  
71 Note: As long as the root path exists, a new C<JSAN::Librarian> object
72 will be created whether index file exists or not.
73  
74 Returns a new C<JSAN::Librarian> object, or undef if the directory
75 does not exist.
76  
77 =cut
78  
79 1 ▶sub new {
Subroutine new: called CC=5 SCAR=18.1
Pod documented
80 1 my $class = shift;
81 1 ▶ my $root = (defined _STRING($_[0]) and -d $_[0]) ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined &_STRING($_[0]) && -d $_[0] ? : 1 0 1
Condition33%1/3
defined &_STRING($_[0]) and -d $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
defined &_STRING($_[0]) and -d $_[0]
defined &_STRING($_[0]) -d $_[0]
Truth table33%1/3
A and B
A= defined &_STRING($_[0])B= -d $_[0]
ABresult
0X0
100
111
82  
83 # Create the object
84 1 my $self = bless {
85 root => $root,
86 }, $class;
87  
88 # Check passed index file or use a default
89 $self->{index_file} = @_
90 1 ▶ ? $self->_new_param(shift)
Branch: 2 branches
BranchTrueFalseTotal
$self->{"index_file"} = @_ ? $self->_new_param(shift()) : $self->_new_default or return undef 1 0 1
@_ ? : 0 1 1
91 : $self->_new_default
92 or return undef;
93  
94 1 return $self;
95 }
96  
97 # Check index file param
98 0 ▶sub _new_param {
Subroutine _new_param: not called CC=2 SCAR=17.9
99 0 my $self = shift;
100 0 ▶ my $param = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $param = shift() or return undef 0 0 0
101 0 return "$param";
102 }
103  
104 # Determine default
105 1 ▶sub _new_default {
Subroutine _new_default: called CC=3 SCAR=11.4
106 1 my $self = shift;
107 1 my $root = $self->root;
108  
109 # Does it have an existing index
110 1 foreach my $file ( @DEFAULT ) {
111 2 my $path = File::Spec->catfile( $root, $file );
112 2 ▶ next unless -f $path;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $path 2 0 2
113 0 $self->_print("Found index at $path");
114 0 return $file;
115 }
116  
117 # It doesn't exist, but use the primary default
118 1 my $path = File::Spec->catfile( $root, $DEFAULT[0] );
119 1 $self->_print("Using default path $DEFAULT[0]");
120 1 return $DEFAULT[0];
121 }
122  
123 =pod
124  
125 =head2 root
126  
127 The C<root> accessor returns the root path of the installed JSAN library.
128  
129 =cut
130  
131 5 ▶sub root {
Subroutine root: called CC=1 SCAR=0.0
Pod documented
132 5 $_[0]->{root};
133 }
134  
135 =pod
136  
137 =head2 index_file
138  
139 The C<index_file> accessor returns the location of index file, as
140 provided to the constructor (or the default), which may be a path
141 relative to the root.
142  
143 =cut
144  
145 1 ▶sub index_file {
Subroutine index_file: called CC=1 SCAR=0.0
Pod documented
146 1 $_[0]->{index_file};
147 }
148  
149  
150  
151  
152  
153 #####################################################################
154 # JSAN::Librarian Methods
155  
156 =pod
157  
158 =head2 index_path
159  
160 The C<index_path> method returns the path to the index file,
161 with relative file locations converted to the full path
162 relative to the root.
163  
164 =cut
165  
166 1 ▶sub index_path {
Subroutine index_path: called CC=2 SCAR=7.1
Pod documented
167 1 my $self = shift;
168 1 my $file = $self->index_file;
169 1 ▶ return File::Spec->file_name_is_absolute($file)
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($file) ? : 0 1 1
170 ? $file
171 : File::Spec->catfile( $self->root, $file );
172 }
173  
174 =pod
175  
176 =head2 index_exists
177  
178 The C<index_exists> method checks to see if the index file exists.
179  
180 Returns true if the index file exists, or false if not.
181  
182 =cut
183  
184 0 ▶sub index_exists {
Subroutine index_exists: not called CC=1 SCAR=6.9
Pod documented
185 0 return -f $_[0]->index_path;
186 }
187  
188 =pod
189  
190 =head2 build_index $lib
191  
192 The C<build_index> method scans the library to find all perl-file
193 dependencies and builds them into an index object.
194  
195 Returns a L<Config::Tiny> object, or throws an exception on error.
196  
197 =cut
198  
199 2 ▶sub build_index {
Subroutine build_index: called CC=3 SCAR=11.0
Pod documented
200 2 my $self = shift;
201 2 my $config = Config::Tiny->new;
202 2 my $root = $self->root;
203  
204 # Find all the files
205 2 $self->_print("Searching $root for .js files...");
206 2 my @files = File::Find::Rule->name('*.js')
207 ->not_name(qr/_deps\.js$/)
208 ->file
209 ->relative
210 ->in( $root );
211 2 foreach my $js ( @files ) {
212 6 $config->{$js} = {};
213 6 my $path = File::Spec->catfile( $root, $js );
214 6 $self->_print("Scanning $js");
215 6 my @deps = JSAN::Parse::FileDeps->file_deps( $path );
216 6 foreach ( @deps ) {
217 6 $config->{$js}->{$_} = 1;
218 }
219 }
220  
221 2 return $config;
222 }
223  
224 =pod
225  
226 =head2 make_index
227  
228 The C<make_index> static method scans the installed L<JSAN> tree and
229 creates an index file (written from a L<Config::Tiny> object) containing
230 the file-level dependency information.
231  
232 Returns true on success, or throws an exception on error.
233  
234 =cut
235  
236 1 ▶sub make_index {
Subroutine make_index: called CC=6 SCAR=23.5
Pod documented
237 1 my $self = shift;
238 1 my $path = $self->index_path;
239  
240 # Make sure the output path exists
241 1 ▶ if ( -e $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $path) { } 0 1 1
242 0 ▶ -w $path or Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
-w $path or Carp::croak("Insufficient permissions to change index file '${path}'") 0 0 0
243 "Insufficient permissions to change index file '$path'"
244 );
245 } else {
246 1 my $dir = File::Basename::dirname( $path );
247 1 ▶ unless ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 1 1
248 0 eval { File::Path::mkpath( $dir, $VERBOSE ); };
249 0 ▶ Carp::croak("$!: Failed to mkdir '$dir' for JSAN::Librarian index file") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
250 }
251 }
252  
253 # Generate the Config::Tiny object
254 1 my $config = $self->build_index( $self->root );
255  
256 # Save the index file
257 1 $self->_print("Saving $path");
258 1 ▶ $config->write( $path ) or Carp::croak(
Branch: 1 branch
BranchTrueFalseTotal
$config->write($path) or Carp::croak("Failed to write JSAN::Librarian index file '${path}'") 1 0 1
259 "Failed to write JSAN::Librarian index file '$path'"
260 );
261 }
262  
263 =pod
264  
265 =head2 library
266  
267 The C<library> method creates and returns a L<JSAN::Librarian::Library>
268 for the installed L<JSAN> library.
269  
270 If an index file exists, the pre-built index in the file will be used.
271  
272 If there is no index file, the installed JSAN library will be scanned
273 and an index built in-memory as needed.
274  
275 Returns a new L<JSAN::Librarian::Library>, or throws an exception
276 on error.
277  
278 =cut
279  
280 0 ▶sub library {
Subroutine library: not called CC=2 SCAR=17.9
Pod documented
281 0 my $self = shift;
282 0 ▶ my $from = $self->index_exists
Branch: 1 branch
BranchTrueFalseTotal
$self->index_exists ? : 0 0 0
283 ? $self->index_path
284 : $self->build_index;
285 0 return JSAN::Librarian::Library->new( $from );
286 }
287  
288  
289  
290  
291  
292 #####################################################################
293 # Coercion Support
294  
295 0 ▶sub __as_JSAN_Librarian_Library { shift->library }
Subroutine __as_JSAN_Librarian_Library: not called CC=1 SCAR=6.9
296 0 ▶sub __as_JavaScript_Librarian_Library { shift->library }
Subroutine __as_JavaScript_Librarian_Library: not called CC=1 SCAR=6.9
297 0 ▶sub __as_Algorithm_Dependency_Source { shift->library }
Subroutine __as_Algorithm_Dependency_Source: not called CC=1 SCAR=6.9
298  
299  
300  
301  
302  
303 #####################################################################
304 # Support Methods
305  
306 10 ▶sub _print {
Subroutine _print: called CC=2 SCAR=7.0
307 10 my $msg = shift;
308 10 $msg =~ s/\n*/\n/g;
309 10 ▶ print $msg if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSAN::Librarian::VERBOSE 0 10 10
310 10 return 1;
311 }
312  
313 2 1;
314  
315 =pod
316  
317 =head1 SUPPORT
318  
319 Bugs should always be submitted via the CPAN bug tracker
320  
321 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSAN-Librarian>
322  
323 For other issues, contact the maintainer.
324  
325 =head1 AUTHORS
326  
327 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
328  
329 =head1 COPYRIGHT
330  
331 Copyright 2005 - 2008 Adam Kennedy.
332  
333 This program is free software; you can redistribute
334 it and/or modify it under the same terms as Perl itself.
335  
336 The full text of the license can be found in the
337 LICENSE file included with this module.
338  
339 =cut
↑ Summary Librarian/Library.pm »