File Coverage

blib/lib/JSAN.pm
Criterion Covered Total %
statement 9 38 23.6
branch 0 10 0.0
condition 0 2 0.0
subroutine 3 7 42.8
pod 0 4 0.0
total 12 61 19.6


line stmt bran cond sub pod time code
1             package JSAN;
2              
3 1     1   1282 use JSAN::Shell;
  1         4  
  1         9  
4 1     1   1325 use Term::ReadLine;
  1         4474  
  1         40  
5 1     1   1820 use Getopt::Long;
  1         16246  
  1         9  
6              
7             our $PROMPT = 'jsan> ';
8             our $VERSION = '0.07';
9              
10             our %COMMAND;
11             our $OPTIONS = {
12             prefix => $ENV{JSAN_PREFIX} || $ENV{PREFIX},
13             mirror => $ENV{JSAN_MIRROR} || $ENV{MIRROR},
14             };
15             our @OPTIONS = (
16             q[prefix|p=s],
17             q[mirror|m=s],
18             );
19              
20             $COMMAND{index} = sub {
21             my ($shell, $opt) = @_;
22             if ($opt =~ /create/ ) {
23             print "Creating index... ";
24             $shell->index_create;
25             print "done.\n";
26             return;
27             }
28             $shell->index_get;
29             };
30              
31             $COMMAND{install} = sub {
32             my ($shell, $opt) = @_;
33             my ($library) = (split /\s/, $opt)[0];
34             $shell->install($library, $OPTIONS->{prefix});
35             };
36              
37             sub run {
38 0     0 0   my ($class) = @_;
39 0           print $class->motd();
40 0           my $term = Term::ReadLine->new;
41 0           while (defined(my $cmd_line = $term->readline($PROMPT))) {
42 0           chomp($cmd_line);
43 0           $cmd_line =~ s/^\s+//;
44 0           $cmd_line =~ s/\s+$//;
45 0 0         next unless $cmd_line;
46 0 0         exit if grep { $cmd_line =~ /^\s*$_/ } qw[exit quit q logout];
  0            
47 0           eval {
48 0           print "\n";
49 0           $class->execute($cmd_line);
50             };
51 0 0         if ( $@ ) {
52 0           warn "$@\n";
53             } else {
54 0           $term->addhistory($cmd_line);
55             }
56             }
57             }
58              
59             sub execute {
60 0     0 0   my ($class, $cmd) = @_;
61 0           my ($command, $options) = split /\s+/, $cmd, 2;
62 0   0       $options ||= '';
63              
64 0 0         die "Command $command not implemented" unless $COMMAND{$command};
65 0           $COMMAND{$command}->(JSAN::Shell->new(my_mirror => $OPTIONS->{mirror}), $options);
66             }
67              
68             sub control {
69 0     0 0   my ($class) = @_;
70 0           GetOptions($OPTIONS, @OPTIONS);
71 0 0         if ( @ARGV ) {
72 0           $class->execute(join ' ', @ARGV);
73 0           exit;
74             } else {
75 0           $class->run;
76 0           exit;
77             }
78             }
79              
80             sub motd {
81             <<__END__
82              
83             Welcome to the JavaScript Archive Network (JSAN) Shell. The very first
84             thing you probably want to do is setup your local index. Do do this, run
85             the following command.
86              
87             jsan> index
88              
89             In order to install libraries you must configure a prefix. Use the
90             --prefix command line option, or -p for short. Or, if you prefer, set
91             your PREFIX environment variable. For example.
92              
93             jsan --prefix=/usr/local/js
94              
95             If you install all your libraries to a central location, you could just
96             configure Apache (for example) to look for JavaScript in that one
97             location: Alias /js/ "/usr/local/js/". Next.
98              
99             jsan> install Test.Simple
100              
101             That's it for tips. Welcome to JSAN! -- Casey West
102              
103             __END__