blib/lib/CPANPLUS/Shell/Default/Plugins/CustomSource.pm untested

filter: Statementstmt 0.0% 0 / 81 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 20 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 149 SCAR 56.1
CC
16
Cov
0%
custom_source
110.0
_uri_from_cache
30.0
_list_custom_sources
6.0
CRAP 272.0
1 package CPANPLUS::Shell::Default::Plugins::CustomSource;
2  
3 use strict;
4 use CPANPLUS::Error qw[error msg];
5 use CPANPLUS::Internals::Constants;
6  
7 use Data::Dumper;
8 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
9  
10 use vars qw[$VERSION];
11 $VERSION = "0.9912";
12  
13 =head1 NAME
14  
15 CPANPLUS::Shell::Default::Plugins::CustomSource - add custom sources to CPANPLUS
16  
17 =head1 SYNOPSIS
18  
19 ### elaborate help text
20 CPAN Terminal> /? cs
21  
22 ### add a new custom source
23 CPAN Terminal> /cs --add file:///path/to/releases
24  
25 ### list all your custom sources by
26 CPAN Terminal> /cs --list
27  
28 ### display the contents of a custom source by URI or ID
29 CPAN Terminal> /cs --contents file:///path/to/releases
30 CPAN Terminal> /cs --contents 1
31  
32 ### Update a custom source by URI or ID
33 CPAN Terminal> /cs --update file:///path/to/releases
34 CPAN Terminal> /cs --update 1
35  
36 ### Remove a custom source by URI or ID
37 CPAN Terminal> /cs --remove file:///path/to/releases
38 CPAN Terminal> /cs --remove 1
39  
40 ### Write an index file for a custom source, to share
41 ### with 3rd parties or remote users
42 CPAN Terminal> /cs --write file:///path/to/releases
43  
44 ### Make sure to save your sources when adding/removing
45 ### sources, so your changes are reflected in the cache:
46 CPAN Terminal> x
47  
48 =head1 DESCRIPTION
49  
50 This is a C<CPANPLUS::Shell::Default> plugin that can add
51 custom sources to your CPANPLUS installation. This is a
52 wrapper around the C<custom module sources> code as outlined
53 in L<CPANPLUS::Backend/CUSTOM MODULE SOURCES>.
54  
55 This allows you to extend your index of available modules
56 beyond what's available on C<CPAN> with your own local
57 distributions, or ones offered by third parties.
58  
59 =cut
60  
61  
62 sub plugins {
63 return ( cs => 'custom_source' )
64 }
65  
66 my $Cb;
67 my $Shell;
68 my @Index = ();
69  
70 sub _uri_from_cache {
71 my $self = shift;
72 my $input = shift or return;
73  
74 ### you gave us a search number
75 my $uri = $input =~ /^\d+$/
76 ? $Index[ $input - 1 ] # remember, off by 1!
77 : $input;
78  
79 my %files = reverse $Cb->list_custom_sources;
80  
81 ### it's an URI we know
82 ### VMS can lower case all files, so make sure we check that too
83 my $local = $files{ $uri };
84 $local = $files{ lc $uri } if !$local && ON_VMS;
85  
86 if( $local ) {
87 return wantarray
88 ? ($uri, $local)
89 : $uri;
90 }
91  
92 ### couldn't resolve the input
93 error(loc("Unknown URI/index: '%1'", $input));
94 return;
95 }
96  
97 sub _list_custom_sources {
98 my $class = shift;
99  
100 my %files = $Cb->list_custom_sources;
101  
102 $Shell->__print( loc("Your remote sources:"), $/ ) if keys %files;
103  
104 my $i = 0;
105 while(my($local,$remote) = each %files) {
106 $Shell->__printf( " [%2d] %s\n", ++$i, $remote );
107  
108 ### remember, off by 1!
109 push @Index, $remote;
110 }
111  
112 $Shell->__print( $/ );
113 }
114  
115 sub _list_contents {
116 my $class = shift;
117 my $input = shift;
118  
119 my ($uri,$local) = $class->_uri_from_cache( $input );
120 unless( $uri ) {
121 error(loc("--contents needs URI parameter"));
122 return;
123 }
124  
125 my $fh = OPEN_FILE->( $local ) or return;
126  
127 $Shell->__printf( " %s", $_ ) for sort <$fh>;
128 $Shell->__print( $/ );
129 }
130  
131 sub custom_source {
132 my $class = shift;
133 my $shell = shift; $Shell = $shell; # available to all methods now
134 my $cb = shift; $Cb = $cb; # available to all methods now
135 my $cmd = shift;
136 my $input = shift || '';
137 my $opts = shift || {};
138  
139 ### show a list
140 if( $opts->{'list'} ) {
141 $class->_list_custom_sources;
142  
143 } elsif ( $opts->{'contents'} ) {
144 $class->_list_contents( $input );
145  
146 } elsif ( $opts->{'add'} ) {
147 unless( $input ) {
148 error(loc("--add needs URI parameter"));
149 return;
150 }
151  
152 $cb->add_custom_source( uri => $input )
153 and $shell->__print(loc("Added remote source '%1'", $input), $/);
154  
155 $Shell->__print($/, loc("Remote source contains:"), $/, $/);
156 $class->_list_contents( $input );
157  
158 } elsif ( $opts->{'remove'} ) {
159 my($uri,$local) = $class->_uri_from_cache( $input );
160 unless( $uri ) {
161 error(loc("--remove needs URI parameter"));
162 return;
163 }
164  
165 1 while unlink $local;
166  
167 $shell->__print( loc("Removed remote source '%1'", $uri), $/ );
168  
169 } elsif ( $opts->{'update'} ) {
170 ### did we get input? if so, it's a remote part
171 my $uri = $class->_uri_from_cache( $input );
172  
173 $cb->update_custom_source( $uri ? ( remote => $uri ) : () )
174 and do { $shell->__print( loc("Updated remote sources"), $/ ) };
175  
176 } elsif ( $opts->{'write'} ) {
177 $cb->write_custom_source_index( path => $input ) and
178 $shell->__print( loc("Wrote remote source index for '%1'", $input), $/);
179  
180 } else {
181 error(loc("Unrecognized command, see '%1' for help", '/? cs'));
182 }
183  
184 return;
185 }
186  
187 sub custom_source_help {
188 return loc(
189 $/ .
190 ' # Plugin to manage custom sources from the default shell' . $/ .
191 " # See the 'CUSTOM MODULE SOURCES' section in the " . $/ .
192 ' # CPANPLUS::Backend documentation for details.' . $/ .
193 ' /cs --list # list available sources' . $/ .
194 ' /cs --add URI # add source' . $/ .
195 ' /cs --remove URI | INDEX # remove source' . $/ .
196 ' /cs --contents URI | INDEX # show packages from source'. $/ .
197 ' /cs --update [URI | INDEX] # update source index' . $/ .
198 ' /cs --write PATH # write source index' . $/
199 );
200  
201 }
202  
203 1;
204