blib/lib/JSAN/Shell.pm

filter: Statementstmt 23.1% 81 / 350 Branchbran 7.7% 9 / 116 Conditioncond 23.8% 5 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 29.1% 21 / 72 Podpod 0.0% 0 / 52 totaltotal 18.6% 116 / 623 CC 70 SCAR 27.9
CC
70
Cov
20%
write_config
56.0
command_set
56.0
command_find
42.0
CRAP 16.3
↑ Summary
1 package JSAN::Shell;
2  
3 =pod
4  
5 =head1 NAME
6  
7 JSAN::Shell - JavaScript Archive Network Client Shell
8  
9 =head1 DESCRIPTION
10  
11 C<JSAN::Shell> provides command handling and dispatch for the L<jsan>
12 user application. It interprates these commands and provides the
13 appropriate instructions to the L<JSAN::Client> and L<JSAN::Transport>
14 APIs to download and install JSAN modules.
15  
16 =head2 Why Do A New Shell?
17  
18 The JavaScript Archive Network, like its predecessor CPAN, is a large
19 system with quite a number of different parts.
20  
21 In an effort to have a usable repository up, running and usable as
22 quickly as possible, some systems (such as the original JSAN shell)
23 were built with the understanding that they would be replaced by lighter,
24 more scalable and more comprehensive (but much slower to write)
25 replacements eventually.
26  
27 C<JSAN::Shell> represents the rewrite of the end-user JSAN shell
28 component, with L<JSAN::Client> providing the seperate and more
29 general programmatic client interface.
30  
31 =head1 METHODS
32  
33 =cut
34  
35 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 3 ▶use Params::Util qw{ _IDENTIFIER _INSTANCE };
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Term::ReadLine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use File::ShareDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use File::UserConfig ();
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Mirror::JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use LWP::Online ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use JSAN::Index ();
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use JSAN::Client ();
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 3 our $VERSION = '2.04';
52 3 $VERSION = eval $VERSION;
53  
54 # Locate the starting mirror.json
55 3 ▶use constant MIRROR_INIT => File::ShareDir::dist_dir(
Subroutine BEGIN: called CC=1 SCAR=0.0
56 'JSAN-Shell'
57 3 );
58  
59  
60  
61  
62  
63 #####################################################################
64 # Constructor
65  
66 2 ▶sub new {
Subroutine new: called CC=11 SCAR=29.0
Pod undocumented
67 2 my $class = shift;
68 2 my %params = @_;
69  
70 # Find a terminal to use
71 2 ▶ my $term = _INSTANCE($params{term}, 'Term::Readline') # Use an explicitly passed terminal...
Condition33%2/6
&_INSTANCE($params{'term'}, 'Term::Readline') || $Term::ReadLine::Perl::term || 'Term::ReadLine'->new('JSAN Shell')
l!l&&r!l&&!r
020
&_INSTANCE($params{"term"}, "Term::Readline") or $Term::ReadLine::Perl::term
l!l&&r!l&&!r
002
MC/DC0%0/3
&_INSTANCE($params{'term'}, 'Term::Readline') || $Term::ReadLine::Perl::term || 'Term::ReadLine'->new('JSAN Shell')
&_INSTANCE($params{"term"}, "Term::Readline") $Term::ReadLine::Perl::term 'Term::ReadLine'->new('JSAN Shell')
Truth table25%1/4
A or B || C
A= &_INSTANCE($params{"term"}, "Term::Readline")B= $Term::ReadLine::Perl::termC= 'Term::ReadLine'->new('JSAN Shell')
ABCresult
1XX1
01X1
0011
0000
72 || $Term::ReadLine::Perl::term # ... or an existing terminal...
73 || Term::ReadLine->new('JSAN Shell'); # ... or create a new one.
74  
75 # Create the actual object
76 my $self = bless {
77 prompt => 'jsan> ',
78 term => $term,
79 client => undef, # Create this later
80 2 ▶ configdir => $params{configdir} || File::UserConfig->configdir,
Condition66%2/3
$params{'configdir'} || 'File::UserConfig'->configdir
l!l&&r!l&&!r
110
MC/DC0%0/2
$params{'configdir'} || 'File::UserConfig'->configdir
$params{'configdir'} 'File::UserConfig'->configdir
Truth table66%2/3
A || B
A= $params{'configdir'}B= 'File::UserConfig'->configdir
ABresult
1X1
011
000
81 config => undef,
82 }, $class;
83
84
85 2 $self->{config} = $self->read_config();
86
87  
88 # Are we online?
89 2 ▶ unless ( $self->{config}->{offline} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}{"offline"}) 2 0 2
90 2 $self->_print("Checking for Internet access...");
91 2 ▶ unless ( LWP::Online::online('http') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (LWP::Online::online("http")) 0 2 2
92 0 $self->{config}->{offline} = 1;
93 }
94 }
95  
96 # Shortcut if offline
97 2 ▶ if ( $self->{config}->{offline} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"offline"}) 0 2 2
98 0 $self->_print("No direct access, offline mode enabled.");
99 0 return $self;
100 }
101  
102 # Locate the best mirror
103 2 ▶ unless ( $self->{config}->{mirror} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}{"mirror"}) 2 0 2
104 2 $self->_print("Locating closest JSAN mirror...");
105 2 my $mirror_yaml = Mirror::JSON->read( MIRROR_INIT );
106 2 my @mirrors = $mirror_yaml->mirrors;
107 2 my $mirror = $mirrors[ int rand scalar @mirrors ];
108 2 $self->{config}->{mirror} = $mirror;
109 }
110  
111 # Initialize the Index layer
112 2 ▶ if (JSAN::Index->self && $self->{config}->{mirror}) {
Branch: 2 branches
BranchTrueFalseTotal
if ('JSAN::Index'->self and $self->{'config'}{'mirror'}) { } 0 2 2
elsif (not 'JSAN::Index'->self) { } 2 0 2
Condition33%1/3
"JSAN::Index"->self and $self->{"config"}{"mirror"}
!ll&&!rl&&r
200
MC/DC0%0/2
"JSAN::Index"->self and $self->{"config"}{"mirror"}
"JSAN::Index"->self $self->{"config"}{"mirror"}
Truth table33%1/3
A and B
A= "JSAN::Index"->selfB= $self->{"config"}{"mirror"}
ABresult
0X0
100
111
113 0 Carp::croak("Cannot re-initialize JSAN::Index with different mirror url");
114 } elsif (not JSAN::Index->self) {
115 JSAN::Index->init(
116 verbose => $self->{config}->{verbose},
117 mirror_remote => $self->{config}->{mirror},
118 2 );
119 }
120  
121 0 $self;
122 }
123  
124 2 ▶sub config_file {
Subroutine config_file: called CC=1 SCAR=0.0
Pod undocumented
125 2 File::Spec->catfile($_[0]->{configdir}, 'config.json')
126 }
127  
128  
129 2 ▶sub read_config {
Subroutine read_config: called CC=3 SCAR=18.4
Pod undocumented
130 2 my $self = shift;
131
132 2 my $filename = $self->config_file;
133
134 2 ▶ return {} unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 2 0 2
135
136 0 my $config_content;
137
138 #slurping
139 {
140 0 local($/);
141 0 ▶ open(my $fh, $filename) or Carp::croak("Cannot open config file: $filename");
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $filename or Carp::croak("Cannot open config file: $filename") 0 0 0
142 0 $config_content = <$fh>;
143 0 close $fh;
144 }
145
146 0 return JSON->new->relaxed->decode($config_content);
147 }
148  
149  
150 0 ▶sub write_config {
Subroutine write_config: not called CC=7 SCAR=40.3
Pod undocumented
151 0 my ($self, $config) = @_;
152
153 0 my $filename = $self->config_file;
154
155 0 my ($vol, $dir, $file) = File::Spec->splitpath($filename);
156
157 0 my $directory = File::Spec::Unix->catpath($vol, $dir, '');
158
159
160 0 ▶ if (-d $directory) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $directory) { } 0 0 0
161 0 ▶ unless (-w $directory) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $directory) 0 0 0
162 0 Carp::croak("No permissions to write to config file directory '$directory'");
163 }
164 } else {
165 0 ▶ File::Path::mkpath($directory, 0, 0755) or die "Couldn't create config file directory '$directory'";
Branch: 1 branch
BranchTrueFalseTotal
File::Path::mkpath($directory, 0, 493) or die "Couldn't create config file directory '${directory}'" 0 0 0
166 }
167
168  
169 # Save it
170 0 ▶ unless ( open( CONFIG, '>', $filename ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open CONFIG, ">", $filename) 0 0 0
171 0 Carp::croak( "Failed to open '$filename' for writing: $!" );
172 }
173 0 ▶ unless ( print CONFIG JSON->new->pretty(1)->encode($config) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print CONFIG "JSON"->new->pretty(1)->encode($config)) 0 0 0
174 0 Carp::croak( "Failed to write to '$filename'" );
175 }
176 0 ▶ unless ( close CONFIG ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (close CONFIG) 0 0 0
177 0 Carp::croak( "Failed to close '$filename' after writing" );
178 }
179
180 0 return 1;
181 }
182  
183  
184 0 ▶sub remember_config_option {
Subroutine remember_config_option: not called CC=1 SCAR=6.9
Pod undocumented
185 0 my ($self, $option, $value) = @_;
186
187 0 my $current_config = $self->read_config();
188
189 0 $current_config->{ $option } = $value;
190
191 0 $self->write_config($current_config);
192 }
193  
194  
195 0 ▶sub term {
Subroutine term: not called CC=1 SCAR=6.9
Pod undocumented
196 0 $_[0]->{term};
197 }
198  
199 0 ▶sub prompt {
Subroutine prompt: not called CC=1 SCAR=6.9
Pod undocumented
200 0 $_[0]->{prompt};
201 }
202  
203 # Get or create the JSAN::Client object for the shell
204 0 ▶sub client {
Subroutine client: not called CC=2 SCAR=17.9
Pod undocumented
205 0 my $self = shift;
206 $self->{client} or
207 0 ▶ $self->{client} = JSAN::Client->new( %{$self->{config}} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"client"} or $self->{"client"} = "JSAN::Client"->new(%{$$self{"config"};}) 0 0 0
208 }
209  
210  
211 0 ▶sub prefix {
Subroutine prefix: not called CC=2 SCAR=17.9
Pod undocumented
212 0 my ($self, $value) = @_;
213
214 0 ▶ return $self->{config}->{prefix} unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
215
216 # Change the prefix and flush the client
217 0 $self->{config}->{prefix} = $value;
218 0 $self->{client} = undef;
219
220 0 return $value;
221 }
222  
223  
224 0 ▶sub mirror {
Subroutine mirror: not called CC=1 SCAR=6.9
Pod undocumented
225 0 $_[0]->{config}->{mirror};
226 }
227  
228 0 ▶sub verbose {
Subroutine verbose: not called CC=2 SCAR=17.9
Pod undocumented
229 0 my $self = shift;
230 0 my $config = $self->{config};
231
232 0 ▶ return $config->{verbose} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
233
234 0 $config->{verbose} = shift;
235 0 $self->client->verbose($config->{verbose});
236 }
237  
238 0 ▶sub offline {
Subroutine offline: not called CC=1 SCAR=6.9
Pod undocumented
239 0 $_[0]->{config}->{offline};
240 }
241  
242  
243  
244  
245  
246 #####################################################################
247 # JSAN::Shell Main Methods
248  
249 0 ▶sub run {
Subroutine run: not called CC=4 SCAR=30.0
Pod undocumented
250 0 local $| = 1;
251 0 my $self = shift;
252 0 $self->execute('help motd');
253 0 ▶ while ( defined(my $cmd_line = $self->term->readline($self->prompt)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cmd_line = $self->term->readline($self->prompt))) 0 0 0
254 0 $cmd_line = $self->_clean($cmd_line);
255 0 ▶ next unless length($cmd_line);
Branch: 1 branch
BranchTrueFalseTotal
unless length $cmd_line 0 0 0
256 0 eval { $self->execute($cmd_line) };
257 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
258 0 warn "$@\n";
259 } else {
260 0 $self->term->addhistory($cmd_line);
261 }
262 }
263 }
264  
265 # Execute a single command
266 0 ▶sub execute {
Subroutine execute: not called CC=3 SCAR=24.8
Pod undocumented
267 0 my $self = shift;
268 0 my $line = shift;
269 0 my %options = (
270 force => 0,
271 );
272  
273 # Split and find the command
274 0 my @words = split /[ ]/, $line;
275 0 my $word = shift(@words);
276 0 my $cmd = $self->resolve_command($word);
277 0 ▶ unless ( $cmd ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd) 0 0 0
278 0 return $self->_show("Unknown command '$word'. Type 'help' for a list of commands");
279 }
280  
281 # Is the command implemented
282 0 my $method = "command_$cmd";
283 0 ▶ unless ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can($method)) 0 0 0
284 0 return $self->_show("The command '$cmd' is not currently implemented");
285 }
286  
287 # Hand off to the specific command
288 0 $options{params} = \@words;
289 0 $self->$method( %options );
290 }
291  
292  
293  
294  
295  
296 #####################################################################
297 # General Commands
298  
299 0 ▶sub command_quit {
Subroutine command_quit: not called CC=1 SCAR=6.9
Pod undocumented
300 0 my $self = shift;
301 0 $self->_show('K TNX BYE!!!');
302 0 exit(0);
303 }
304  
305 0 ▶sub command_help {
Subroutine command_help: not called CC=3 SCAR=24.8
Pod undocumented
306 0 my $self = shift;
307 0 my %args = @_;
308 0 my @params = @{$args{params}};
309  
310 # Get the command to show help for
311 0 ▶ my $command = $params[0] || 'commands';
Condition0%0/2
$params[0] || 'commands'
l!l
00
MC/DC0%0/1
$params[0] || 'commands'
$params[0]
Truth table0%0/2
A
A= $params[0]
Aresult
11
00
312 0 my $method = "help_$command";
313  
314 0 ▶ return $self->can($method)
Branch: 1 branch
BranchTrueFalseTotal
$self->can($method) ? : 0 0 0
315 ? $self->_show($self->$method())
316 : $self->_show("No help page for command '$command'");
317 }
318  
319  
320  
321  
322  
323 #####################################################################
324 # Investigation
325  
326 0 ▶sub help_a { shift->help_author }
Subroutine help_a: not called CC=1 SCAR=6.9
Pod undocumented
327 0 ▶sub help_author { <<'END_HELP' }
Subroutine help_author: not called CC=1 SCAR=6.9
Pod undocumented
328 jsan> author adamk
329
330 Author ID = adamk
331 Name: Adam Kennedy
332 Email: jsan@ali.as
333 Website: http://ali.as/
334  
335 The "author" command is used to to locate an author and information
336 about them.
337  
338 It takes a single argument which should be the full JSAN identifier
339 for the author.
340 END_HELP
341  
342 0 ▶sub command_author {
Subroutine command_author: not called CC=3 SCAR=24.8
Pod undocumented
343 0 my $self = shift;
344 0 my %args = @_;
345 0 my @params = @{$args{params}};
346 0 ▶ my $name = lc _IDENTIFIER($params[0])
Branch: 1 branch
BranchTrueFalseTotal
my $name = lc &_IDENTIFIER($params[0]) or return $self->_show("Not a valid author identifier") 0 0 0
347 or return $self->_show("Not a valid author identifier");
348  
349 # Find the author
350 0 my $author = JSAN::Index::Author->retrieve( login => $name );
351 0 ▶ unless ( $author ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($author) 0 0 0
352 0 return $self->_show("Could not find the author '$name'");
353 }
354  
355 0 $self->show_author( $author );
356 }
357  
358 0 ▶sub help_d { shift->help_dist }
Subroutine help_d: not called CC=1 SCAR=6.9
Pod undocumented
359 0 ▶sub help_dist { <<'END_HELP' }
Subroutine help_dist: not called CC=1 SCAR=6.9
Pod undocumented
360 jsan> dist JSAN
361
362 Distribution = JSAN
363 Latest Release = /dist/c/cw/cwest/JSAN-0.10.tar.gz
364 Version: 0.10
365 Created: Tue Jul 26 17:26:35 2005
366 Author: cwest
367 Name: Casey West
368 Email: casey@geeknest.com
369 Website:
370 Library: JSAN 0.10
371
372 The "dist" command is used to fetch information about a Distribution,
373 including the current release package, the author, and what Libraries
374 are contained in it.
375
376 The dist command takes a single argument which should be the full name
377 of the distribution.
378
379 In the JSAN, a Distribution represents an overall product/release-series.
380 Each distribution will have one or more Release package, which are the
381 actual archive files in the repository, and each distribution contains
382 one more more Library, which are the actual classes and APIs.
383
384 For various reasons, it is occasionally necesary for a Library to move
385 from one Distribution to another. For this reason, most of the time
386 operations (such as installation) are done at the Library level, and the
387 JSAN client will automatically determine which Distribution (and thus
388 which Release package) to install.
389
390 However, for cases when you do need information about the actual
391 Distribution, this command is made available.
392 END_HELP
393  
394 0 ▶sub command_dist {
Subroutine command_dist: not called CC=2 SCAR=17.9
Pod undocumented
395 0 my $self = shift;
396 0 my %args = @_;
397 0 my @params = @{$args{params}};
398 0 my $name = $params[0];
399  
400 # Find the author
401 0 my $dist = JSAN::Index::Distribution->retrieve( name => $name );
402 0 ▶ unless ( $dist ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dist) 0 0 0
403 0 return $self->_show("Could not find the distribution '$name'");
404 }
405  
406 0 $self->show_dist( $dist );
407 }
408  
409 0 ▶sub help_l { shift->help_library(@_) }
Subroutine help_l: not called CC=1 SCAR=6.9
Pod undocumented
410 0 ▶sub help_library { <<'END_HELP' }
Subroutine help_library: not called CC=1 SCAR=6.9
Pod undocumented
411 jsan> library Test.Simple
412
413 Library = Test.Simple
414 Version: 0.20
415 In Distribution = Test.Simple
416 Latest Release = /dist/t/th/theory/Test.Simple-0.20.tar.gz
417 Version: 0.20
418 Created: Thu Aug 18 04:09:19 2005
419 Author: theory
420 Name: David Wheeler
421 Email: david@justatheory.com
422 Website: http://www.justatheory.com/
423 Library: Test.Builder 0.20
424 Library: Test.Harness 0.20
425 Library: Test.Harness.Browser 0.20
426 Library: Test.Harness.Director 0.20
427 Library: Test.More 0.20
428 Library: Test.Simple 0.20
429  
430 The "library" command is used to fetch information about a Library,
431 including the distribution that contains it, the current release, and
432 any other libraries that will be installed at the same time as the one
433 you are searching for.
434
435 The library command takes a single argument which should be the full name
436 of the library/class.
437
438 In the JSAN, a Library represents a single JavaScript prototype/class,
439 and most actions are Library-oriented.
440
441 For various reasons, it is occasionally necesary for a Library to move
442 from one Distribution to another. For this reason, most of the time
443 operations (such as installation) are done at the Library level, and the
444 JSAN client will automatically determine which Distribution (and thus
445 which Release package) to install.
446 END_HELP
447  
448 0 ▶sub command_library {
Subroutine command_library: not called CC=2 SCAR=17.9
Pod undocumented
449 0 my $self = shift;
450 0 my %args = @_;
451 0 my @params = @{$args{params}};
452 0 my $name = $params[0];
453  
454 # Find the library
455 0 my $library = JSAN::Index::Library->retrieve( name => $name );
456 0 ▶ unless ( $library ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($library) 0 0 0
457 0 return $self->_show("Could not find the library '$name'");
458 }
459  
460 0 $self->show_library( $library );
461 }
462  
463 0 ▶sub help_f { shift->help_find(@_) }
Subroutine help_f: not called CC=1 SCAR=6.9
Pod undocumented
464 0 ▶sub help_find { <<'END_HELP' }
Subroutine help_find: not called CC=1 SCAR=6.9
Pod undocumented
465 jsan> f Display.Hide
466  
467 Library: Display.Hide
468 Distribution: Display.Hide
469 Release: /dist/r/ro/rooneg/Display.Hide-0.02.tar.gz
470 Release: /dist/r/ro/rooneg/Display.Hide-0.01.tar.gz
471  
472 Found 4 matching objects in the index
473  
474 The "find" command is an aggregate of the four other search commands
475 ("author", "dist", and "library").
476
477 If a single index entry is found, the details of the result will be listed
478 in complete detail as per each specific search.
479
480 If multiple index entries are found, a summary line of all the index
481 entries will be listed.
482 END_HELP
483  
484 0 ▶sub command_find {
Subroutine command_find: not called CC=6 SCAR=37.4
Pod undocumented
485 0 my $self = shift;
486 0 my %args = @_;
487 0 my @params = @{$args{params}};
488 0 my $name = $params[0];
489 0 my $search = "%$name%";
490  
491 # Do the search
492 0 my @objects = ();
493 0 push @objects, sort JSAN::Index::Author->search_like(
494 login => $search,
495 );
496 0 push @objects, sort JSAN::Index::Library->search_like(
497 name => $search,
498 );
499 0 push @objects, sort JSAN::Index::Distribution->search_like(
500 name => $search,
501 );
502 0 push @objects, sort JSAN::Index::Release->search_like(
503 source => $search,
504 );
505  
506 # Did we find anything?
507 0 ▶ unless ( @objects ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@objects) 0 0 0
508 0 return $self->_show( "No objects found of any type like '$name'" );
509 }
510  
511 # If we only found one thing, go directly to it
512 0 ▶ if ( @objects == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@objects == 1) 0 0 0
513 0 ▶ if ( $objects[0]->isa('JSAN::Index::Author') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($objects[0]->isa('JSAN::Index::Author')) { } 0 0 0
elsif ($objects[0]->isa('JSAN::Index::Distribution')) { } 0 0 0
elsif ($objects[0]->isa('JSAN::Index::Release')) { } 0 0 0
514 0 return $self->show_author( $objects[0] );
515 } elsif ( $objects[0]->isa('JSAN::Index::Distribution') ) {
516 0 return $self->show_dist( $objects[0] );
517 } elsif ( $objects[0]->isa('JSAN::Index::Release') ) {
518 0 return $self->show_release( $objects[0] );
519 } else {
520 0 return $self->show_library( $objects[0] );
521 }
522 }
523  
524 # Show all of the objects
525 0 $self->show_list( @objects );
526 }
527  
528 0 ▶sub help_c { shift->help_config(@_) }
Subroutine help_c: not called CC=1 SCAR=6.9
Pod undocumented
529 0 ▶sub help_config { <<'END_HELP' }
Subroutine help_config: not called CC=1 SCAR=6.9
Pod undocumented
530 jsan> config
531  
532 jsan configuration
533 ------------------
534 verbose: disabled
535 offline: disabled
536 mirror: http://master.openjsan.org
537 prefix: (none)
538  
539 The "config" command lists the current configuration settings for the
540 client session.
541 END_HELP
542  
543 0 ▶sub command_config {
Subroutine command_config: not called CC=1 SCAR=6.9
Pod undocumented
544 0 shift()->show_config;
545 }
546  
547 0 ▶sub help_s { shift->help_config(@_) }
Subroutine help_s: not called CC=1 SCAR=6.9
Pod undocumented
548 0 ▶sub help_set { <<'END_HELP' }
Subroutine help_set: not called CC=1 SCAR=6.9
Pod undocumented
549 jsan> set verbose 1
550  
551 Verbose mode is enabled.
552  
553 The "set" command is used to change the current configuration. It takes
554 the name of a configuration setting, and a value.
555
556 Changes to configuration currently only apply for the duration of the
557 current session (this will be corrected in future).
558 END_HELP
559  
560 0 ▶sub command_set {
Subroutine command_set: not called CC=7 SCAR=40.3
Pod undocumented
561 0 my $self = shift;
562 0 my %args = @_;
563 0 my @params = @{$args{params}};
564 0 my $name = shift(@params);
565 0 ▶ unless ( @params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@params) 0 0 0
566 0 return $self->_show("Did not provide a value to set the option to");
567 }
568  
569 # Handle the valid options
570 0 ▶ if ( _IDENTIFIER($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_IDENTIFIER($name)) 0 0 0
571 0 ▶ return $self->command_set_verbose( @params ) if $name eq 'verbose';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "verbose" 0 0 0
572 0 ▶ return $self->command_set_offline( @params ) if $name eq 'offline';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "offline" 0 0 0
573 0 ▶ return $self->command_set_mirror( @params ) if $name eq 'mirror';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "mirror" 0 0 0
574 0 ▶ return $self->command_set_prefix( @params ) if $name eq 'prefix';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "prefix" 0 0 0
575 }
576  
577 0 $self->_show("Invalid or unknown configuration option '$params[0]'");
578 }
579  
580 0 ▶sub command_set_verbose {
Subroutine command_set_verbose: not called CC=3 SCAR=24.8
Pod undocumented
581 0 my $self = shift;
582 0 my $value = shift;
583 0 ▶ if ( $value =~ /^(?:y|yes|t|true|1|on)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^(?:y|yes|t|true|1|on)$/i) { } 0 0 0
elsif ($value =~ /^(?:n|no|f|false|0|off)$/i) { } 0 0 0
584 0 $self->verbose(1);
585 0 $self->_show("Verbose mode is enabled.");
586 } elsif ( $value =~ /^(?:n|no|f|false|0|off)$/i ) {
587 0 $self->verbose('');
588 0 $self->_show("Verbose mode is disabled.");
589 } else {
590 0 $self->_show("Unknown verbose mode '$value'. Try 'on' or 'off'");
591 }
592 }
593  
594 0 ▶sub command_set_offline {
Subroutine command_set_offline: not called CC=3 SCAR=24.8
Pod undocumented
595 0 my $self = shift;
596 0 my $value = shift;
597 0 ▶ if ( $value =~ /^(?:y|yes|t|true|1|on)$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value =~ /^(?:y|yes|t|true|1|on)$/i) { } 0 0 0
elsif ($value =~ /^(?:n|no|f|false|0|off)$/i) { } 0 0 0
598 0 $self->{config}->{offline} = 1;
599 0 $self->_show("Offline mode is enabled.");
600 } elsif ( $value =~ /^(?:n|no|f|false|0|off)$/i ) {
601 0 $self->{config}->{offline} = '';
602 0 $self->_show("Offline mode is disabled.");
603 } else {
604 0 $self->_show("Unknown offline mode '$value'. Try 'on' or 'off'");
605 }
606 }
607  
608 0 ▶sub command_set_mirror {
Subroutine command_set_mirror: not called CC=1 SCAR=6.9
Pod undocumented
609 0 my $self = shift;
610 0 my $value = shift;
611  
612 ### FIXME - Once JSAN::URI works, add validation here
613  
614 # Change the mirror and reset JSAN::Transport
615 0 $self->{config}->{mirror} = $value;
616  
617 0 $self->_show("mirror changed to '$value'");
618 }
619  
620 0 ▶sub command_set_prefix {
Subroutine command_set_prefix: not called CC=5 SCAR=30.8
Pod undocumented
621 0 my $self = shift;
622 0 ▶ my $value = (glob shift)[0];
Subroutine BEGIN: called CC=1 SCAR=0.2
623  
624 # Check the prefix directory
625
626 0 ▶ unless ( -d $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $value) 0 0 0
627 0 return $self->_show("The directory '$value' does not exist.");
628 }
629 0 ▶ unless ( -w $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $value) 0 0 0
630 0 return $self->_show("You do not have write permissions to '$value'.");
631 }
632  
633 # Change the prefix
634 0 $self->prefix($value);
635  
636 0 $self->_show("prefix changed to '$value'");
637
638 0 my $remember = $self->term->readline("Remember this setting? [Y/n]");
639
640 0 ▶ if (!$remember || $remember =~ /^y(es)?/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $remember or $remember =~ /^y(es)?/i) 0 0 0
Condition0%0/3
not $remember or $remember =~ /^y(es)?/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not $remember or $remember =~ /^y(es)?/i
not $remember $remember =~ /^y(es)?/i
Truth table0%0/3
not(A) or B
A= not $rememberB= $remember =~ /^y(es)?/i
ABresult
1X1
011
000
641 0 $self->remember_config_option('prefix', $value);
642
643 0 $self->_show("prefix saved to configuration file: " . $self->config_file);
644 }
645 }
646  
647 0 ▶sub help_p { shift->help_pull(@_) }
Subroutine help_p: not called CC=1 SCAR=6.9
Pod undocumented
648 0 ▶sub help_pull { <<'END_HELP' }
Subroutine help_pull: not called CC=1 SCAR=6.9
Pod undocumented
649  
650 END_HELP
651  
652 0 ▶sub command_pull {
Subroutine command_pull: not called CC=2 SCAR=17.9
Pod undocumented
653 0 my $self = shift;
654 0 my %args = @_;
655 0 my @params = @{$args{params}};
656 0 my $name = shift(@params);
657  
658 # Find the library they are refering to
659 0 my $library = JSAN::Index::Library->retrieve( name => $name );
660 0 ▶ unless ( $library ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($library) 0 0 0
661 0 return $self->_show("Could not find the library '$name'");
662 }
663  
664 # Mirror the file to local disk
665 0 my $path = $library->release->mirror;
666 0 $self->_show("Library '$name' downloaded in release file '$path'");
667 }
668  
669 0 ▶sub command_install {
Subroutine command_install: not called CC=3 SCAR=24.8
Pod undocumented
670 0 my $self = shift;
671 0 my %args = @_;
672 0 my @params = @{$args{params}};
673 0 my $name = shift(@params);
674  
675 # Find the library they are refering to
676 0 my $library = JSAN::Index::Library->retrieve( name => $name );
677 0 ▶ unless ( $library ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($library) 0 0 0
678 0 return $self->_show("Could not find the library '$name'");
679 }
680  
681 # Do we have a prefix to install to
682 0 ▶ unless ( $self->prefix ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->prefix) 0 0 0
683 0 return $self->_show("No install prefix set. Try 'set prefix /install/path'");
684 }
685  
686 # Get the client object and install the package (and it's dependencies)
687 0 $self->client->install_library($name);
688 }
689  
690 0 ▶sub show_author {
Subroutine show_author: not called CC=1 SCAR=6.9
Pod undocumented
691 0 my $self = shift;
692 0 my $author = shift;
693 0 $self->_show(
694 "Author ID = " . $author->login,
695 " Name: " . $author->name,
696 " Email: " . $author->email,
697 " Website: " . $author->url,
698 );
699 }
700  
701 0 ▶sub show_dist {
Subroutine show_dist: not called CC=3 SCAR=24.8
Pod undocumented
702 0 my $self = shift;
703 0 my $dist = shift;
704 0 my $release = $dist->latest_release;
705 0 my $author = $release->author;
706  
707 # Get the list of libraries in this release.
708 # This only works because we are using the latest release.
709 my @libraries =
710 0 sort { $a->name cmp $b->name }
711 JSAN::Index::Library->search( release => $release->id );
712  
713 # Find the max library name length and create the formatting string
714 0 my $max = 0;
715 0 foreach ( @libraries ) {
716 0 ▶ next if length($_->name) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $_->name <= $max 0 0 0
717 0 $max = length($_->name);
718 }
719 0 my $string = " Library: %-${max}s %s";
720  
721 $self->_show(
722 "Distribution = " . $dist->name,
723 "Latest Release = " . $release->source,
724 " Version: " . $release->version,
725 " Created: " . $release->created_string,
726 " Author: " . $author->login,
727 " Name: " . $author->name,
728 " Email: " . $author->email,
729 " Website: " . $author->url,
730 map {
731 0 sprintf( $string, $_->name, $_->version )
732 } @libraries
733 );
734 }
735  
736 0 ▶sub show_release {
Subroutine show_release: not called CC=2 SCAR=17.9
Pod undocumented
737 0 my $self = shift;
738 0 my $release = shift;
739 0 my $dist = $release->distribution;
740 0 my $author = $release->author;
741  
742 0 ▶ $self->_show(
Branch: 1 branch
BranchTrueFalseTotal
$release->latest ? : 0 0 0
743 "Release = " . $release->source,
744 " Distribution: " . $dist->name,
745 " Version: " . $release->version,
746 " Created: " . $release->created_string,
747 " Latest Release: " . ($release->latest ? 'Yes' : 'No'),
748 " Author: " . $author->login,
749 " Name: " . $author->name,
750 " Email: " . $author->email,
751 " Website: " . $author->url,
752 );
753 }
754  
755 0 ▶sub show_library {
Subroutine show_library: not called CC=3 SCAR=24.8
Pod undocumented
756 0 my $self = shift;
757 0 my $library = shift;
758 0 my $release = $library->release;
759 0 my $dist = $release->distribution;
760 0 my $author = $release->author;
761  
762 # Get the list of libraries in this release.
763 # This only works because we are using the latest release.
764 my @libraries = sort {
765 0 $a->name cmp $b->name
766 } JSAN::Index::Library->search(
767 release => $release->id,
768 );
769  
770 # Find the max library name length and create the formatting string
771 0 my $max = 0;
772 0 foreach ( @libraries ) {
773 0 ▶ next if length($_->name) <= $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $_->name <= $max 0 0 0
774 0 $max = length($_->name);
775 }
776 0 my $string = " Library: %-${max}s %s";
777  
778 $self->_show(
779 "Library = " . $library->name,
780 " Version: " . $library->version,
781 "In Distribution = " . $dist->name,
782 "Latest Release = " . $release->source,
783 " Version: " . $release->version,
784 " Created: " . $release->created_string,
785 " Author: " . $author->login,
786 " Name: " . $author->name,
787 " Email: " . $author->email,
788 " Website: " . $author->url,
789 map {
790 0 sprintf( $string, $_->name, $_->version )
791 } @libraries,
792 );
793 }
794  
795 0 ▶sub show_list {
Subroutine show_list: not called CC=6 SCAR=37.4
Pod undocumented
796 0 my $self = shift;
797  
798 # Show each one
799 0 my @output = ();
800 0 foreach my $object ( @_ ) {
801 0 ▶ if ( $object->isa('JSAN::Index::Author') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($object->isa('JSAN::Index::Author')) { } 0 0 0
elsif ($object->isa('JSAN::Index::Distribution')) { } 0 0 0
elsif ($object->isa('JSAN::Index::Release')) { } 0 0 0
elsif ($object->isa('JSAN::Index::Library')) { } 0 0 0
802 0 push @output, sprintf(
803 " Author: %-10s (\"%s\" <%s>)",
804 $object->login,
805 $object->name,
806 $object->email,
807 );
808  
809 } elsif ( $object->isa('JSAN::Index::Distribution') ) {
810 0 push @output, sprintf(
811 " Distribution: %s",
812 $object->name,
813 );
814  
815 } elsif ( $object->isa('JSAN::Index::Release') ) {
816 0 push @output, sprintf(
817 " Release: %s",
818 $object->source,
819 );
820  
821 } elsif ( $object->isa('JSAN::Index::Library') ) {
822 0 push @output, sprintf(
823 " Library: %s",
824 $object->name,
825 );
826 }
827 }
828  
829 # Summary
830 0 push @output, "";
831 0 push @output, " Found "
832 . scalar(@_)
833 . " matching objects in the index";
834  
835 0 $self->_show( @output );
836 }
837
838 0 ▶sub show_config {
Subroutine show_config: not called CC=5 SCAR=34.0
Pod undocumented
839 0 my $self = shift;
840 0 ▶ $self->_show(
Branch: 2 branches
BranchTrueFalseTotal
$self->verbose ? : 0 0 0
$self->offline ? : 0 0 0
Condition0%0/4
$self->mirror || '(none)'
l!l
00
$self->prefix || '(none)'
l!l
00
MC/DC0%0/1
$self->mirror || '(none)'
$self->mirror
MC/DC0%0/1
$self->prefix || '(none)'
$self->prefix
Truth table0%0/2
A
A= $self->mirror
Aresult
11
00
Truth table0%0/2
A
A= $self->prefix
Aresult
11
00
841 " jsan configuration",
842 " ------------------",
843 " verbose: " . ($self->verbose ? 'enabled' : 'disabled'),
844 " offline: " . ($self->offline ? 'enabled' : 'disabled'),
845 " mirror: " . ($self->mirror || '(none)'),
846 " prefix: " . ($self->prefix || '(none)'),
847 );
848 }
849  
850  
851  
852  
853  
854 #####################################################################
855 # Localisation and Content
856  
857 # For a given string, find the command for it
858 3 my %COMMANDS = (
859 'q' => 'quit',
860 'quit' => 'quit',
861 'exit' => 'quit',
862 'h' => 'help',
863 'help' => 'help',
864 '?' => 'help',
865 'a' => 'author',
866 'author' => 'author',
867 'd' => 'dist',
868 'dist' => 'dist',
869 'distribution' => 'dist',
870 'l' => 'library',
871 'lib' => 'library',
872 'library' => 'library',
873 'f' => 'find',
874 'find' => 'find',
875 'c' => 'config',
876 'conf' => 'config',
877 'config' => 'config',
878 's' => 'set',
879 'set' => 'set',
880 'p' => 'pull',
881 'pull' => 'pull',
882 'i' => 'install',
883 'install' => 'install',
884 'r' => 'readme',
885 'readme' => 'readme',
886 );
887  
888 0 ▶sub resolve_command {
Subroutine resolve_command: not called CC=1 SCAR=6.9
Pod undocumented
889 0 $COMMANDS{$_[1]};
890 }
891  
892  
893  
894  
895  
896 0 ▶sub help_usage { <<"END_HELP" }
Subroutine help_usage: not called CC=1 SCAR=6.9
Pod undocumented
897 Usage: cpan [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_COMMAND ...]
898  
899 For more details run
900 perldoc -F /usr/bin/jsan
901 END_HELP
902  
903  
904  
905  
906  
907 0 ▶sub help_motd { <<"END_HELP" }
Subroutine help_motd: not called CC=1 SCAR=6.9
Pod undocumented
908 jsan shell -- JSAN repository explorer and package installer (v$VERSION)
909 -- Copyright 2005 - 2009 Adam Kennedy.
910 -- Type 'help' for a summary of available commands.
911 END_HELP
912  
913  
914  
915  
916  
917 0 ▶sub help_commands { <<"END_HELP" }
Subroutine help_commands: not called CC=1 SCAR=6.9
Pod undocumented
918 ------------------------------------------------------------
919 | Display Information |
920 | ------------------------------------------------------------ |
921 | command | argument | description |
922 | ------------------------------------------------------------ |
923 | a,author | WORD | about an author |
924 | d,dist | WORD | about a distribution |
925 | l,library | WORD | about a library |
926 | f,find | SUBSTRING | all matches from above |
927 | ------------------------------------------------------------ |
928 | Download, Test, Install... |
929 | ------------------------------------------------------------ |
930 | p,pull | WORD | download from the mirror |
931 | i,install | WORD | install (implies get) |
932 | r,readme | WORD | display the README file |
933 | ------------------------------------------------------------ |
934 | Other |
935 | ------------------------------------------------------------ |
936 | h,help,? | | display this menu |
937 | h,help,? | COMMAND | command details |
938 | c,config | | show all config options |
939 | s,set | OPTION, VALUE | set a config option |
940 | q,quit,exit | | quit the jsan shell |
941 ------------------------------------------------------------
942 END_HELP
943  
944  
945  
946  
947  
948 #####################################################################
949 # Support Methods
950  
951 # Clean a single command
952 0 ▶sub _clean {
Subroutine _clean: not called CC=1 SCAR=6.9
953 0 my ($self, $line) = @_;
954 0 $line =~ s/\s+/ /s;
955 0 $line =~ s/^\s+//s;
956 0 $line =~ s/\s+$//s;
957 0 $line;
958 }
959  
960 # Print a single line to screen
961 4 ▶sub _print {
Subroutine _print: called CC=2 SCAR=6.9
962 4 my $self = shift;
963 4 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 4 8
964 4 my $line = shift;
965 4 chomp($line);
966 4 print STDOUT "$line\n";
967 }
968 4 1;
969 }
970  
971 # Print something with a leading and trailing blank line
972 0 ▶sub _show {
Subroutine _show: not called CC=1 SCAR=6.9
973 0 my $self = shift;
974 0 $self->_print( '', @_, '' );
975 }
976  
977 3 1;
978  
979 =pod
980  
981 =head1 SUPPORT
982  
983 Bugs should be reported via the CPAN bug tracker at
984  
985 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSAN-Shell>
986  
987 For other issues, contact the author.
988  
989 =head1 AUTHORS
990  
991 Adam Kennedy E<lt>adam@ali.asE<gt>
992  
993 =head1 SEE ALSO
994  
995 L<jsan>, L<JSAN::Client>, L<http://openjsan.org>
996  
997 =head1 COPYRIGHT
998  
999 Copyright 2005 - 2010 Adam Kennedy.
1000  
1001 This module is free software; you can redistribute it and/or modify it
1002 under the same terms as Perl itself.
1003  
1004 =cut
↑ Summary