1 package CPANPLUS::Shell::Default;
8 use CPANPLUS::Configure::Setup;
9 use CPANPLUS::Internals::Constants;
10 use CPANPLUS::Internals::Constants::Report qw[GRADE_FAIL];
18 use Module::Load qw[load];
19 use Params::Check qw[check];
20 use Module::Load::Conditional qw[can_load check_install];
21 use Locale::Maketext::Simple Class => 'CPANPLUS', Style => 'gettext';
23 local $Params::Check::VERBOSE = 1;
24 local $Data::Dumper::Indent = 1; # for dumpering from !
27 use vars qw[ $VERSION @ISA ];
28 @ISA = qw[ CPANPLUS::Shell::_Base::ReadLine ];
36 'm' => '_search_module',
37 'a' => '_search_author',
43 'v' => '_show_banner',
44 'w' => '__display_results',
47 'f' => '_distributions',
48 'x' => '_reload_indices',
57 '/' => '_meta', # undocumented for now
60 ### free letters: e g j k n y ###
63 ### will be filled if you have a .default-shell.rc and
64 ### Config::Auto installed
67 ### the shell object, scoped to the file ###
69 my $Brand = loc('CPAN Terminal');
70 my $Prompt = $Brand . '> ';
76 CPANPLUS::Shell::Default
80 ### loading the shell:
81 $ cpanp # run 'cpanp' from the command line
82 $ perl -MCPANPLUS -eshell # load the shell from the command line
85 use CPANPLUS::Shell qw[Default]; # load this shell via the API
86 # always done via CPANPLUS::Shell
88 my $ui = CPANPLUS::Shell->new;
89 $ui->shell; # run the shell
90 $ui->dispatch_on_input( input => 'x'); # update the source using the
93 ### when in the shell:
94 ### Note that all commands can also take options.
95 ### Look at their underlying CPANPLUS::Backend methods to see
96 ### what options those are.
97 cpanp> h # show help messages
98 cpanp> ? # show help messages
100 cpanp> m Acme # find acme modules, allows regexes
101 cpanp> a KANE # find modules by kane, allows regexes
102 cpanp> f Acme::Foo # get a list of all releases of Acme::Foo
104 cpanp> i Acme::Foo # install Acme::Foo
105 cpanp> i Acme-Foo-1.3 # install version 1.3 of Acme::Foo
106 cpanp> i <URI> # install from URI, like ftp://foo.com/X.tgz
107 cpanp> i <DIR> # install from an absolute or relative directory
108 cpanp> i 1 3..5 # install search results 1, 3, 4 and 5
109 cpanp> i * # install all search results
110 cpanp> a KANE; i *; # find modules by kane, install all results
111 cpanp> t Acme::Foo # test Acme::Foo, without installing it
112 cpanp> u Acme::Foo # uninstall Acme::Foo
113 cpanp> d Acme::Foo # download Acme::Foo
114 cpanp> z Acme::Foo # download & extract Acme::Foo, then open a
115 # shell in the extraction directory
117 cpanp> c Acme::Foo # get a list of test results for Acme::Foo
118 cpanp> l Acme::Foo # view details about the Acme::Foo package
119 cpanp> r Acme::Foo # view Acme::Foo's README file
120 cpanp> o # get a list of all installed modules that
122 cpanp> o 1..3 # list uptodateness from a previous search
124 cpanp> s conf # show config settings
125 cpanp> s conf md5 1 # enable md5 checks
126 cpanp> s program # show program settings
127 cpanp> s edit # edit config file
128 cpanp> s reconfigure # go through initial configuration again
129 cpanp> s selfupdate # update your CPANPLUS install
130 cpanp> s save # save config to disk
131 cpanp> s mirrors # show currently selected mirrors
133 cpanp> ! [PERL CODE] # execute the following perl code
135 cpanp> b # create an autobundle for this computers
137 cpanp> x # reload index files (purges cache)
138 cpanp> x --update_source # reload index files, get fresh source files
139 cpanp> p [FILE] # print error stack (to a file)
140 cpanp> v # show the banner
141 cpanp> w # show last search results again
143 cpanp> q # quit the shell
145 cpanp> /plugins # list avialable plugins
146 cpanp> /? PLUGIN # list help test of <PLUGIN>
149 cpanp> i ... --skiptest # skip tests
150 cpanp> i ... --force # force all operations
151 cpanp> i ... --verbose # run in verbose mode
155 This module provides the default user interface to C<CPANPLUS>. You
156 can start it via the C<cpanp> binary, or as detailed in the L<SYNOPSIS>.
163 my $cb = CPANPLUS::Backend->new( @_ );
164 my $self = $class->SUPER::_init(
166 term => Term::ReadLine->new( $Brand ),
169 format => "%4s %-55s %8s %-10s\n",
170 dist_format => "%4s %-42s %-12s %8s %-10s\n",
172 ### make it available package wide ###
175 my $rc_file = File::Spec->catfile(
176 $cb->configure_object->get_conf('base'),
177 DOT_SHELL_DEFAULT_RC,
181 if( -e $rc_file && -r _ ) {
182 $rc = $self->_read_configuration_from_rc( $rc_file );
185 ### register install callback ###
186 $cb->_register_callback(
187 name => 'install_prerequisite',
188 code => \&__ask_about_install,
191 ### execute any login commands specified ###
192 $self->dispatch_on_input( input => $rc->{'login'} )
193 if defined $rc->{'login'};
195 ### register test report callbacks ###
196 $cb->_register_callback(
197 name => 'edit_test_report',
198 code => \&__ask_about_edit_test_report,
201 $cb->_register_callback(
202 name => 'send_test_report',
203 code => \&__ask_about_send_test_report,
206 $cb->_register_callback(
207 name => 'proceed_on_test_failure',
208 code => \&__ask_about_test_failure,
211 ### load all the plugins
212 $self->_plugins_init;
219 my $term = $self->term;
220 my $conf = $self->backend->configure_object;
223 $self->__print( "*** Type 'p' now to show start up log\n" ); # XXX add to banner?
224 $self->_show_random_tip if $conf->get_conf('show_startup_tip');
225 $self->_input_loop && $self->__print( "\n" );
231 my $term = $self->term;
232 my $cb = $self->backend;
236 defined (my $input = eval { $term->readline($self->prompt) } )
237 or $self->_signals->{INT}{count} == 1
239 ### re-initiate all signal handlers
240 while (my ($sig, $entry) = each %{$self->_signals} ) {
241 $SIG{$sig} = $entry->{handler} if exists($entry->{handler});
244 $self->__print( "\n" );
245 last if $self->dispatch_on_input( input => $input );
247 ### flush the lib cache ###
248 $cb->_flush( list => [qw|lib load|] );
251 ### clear the sigint count
252 $self->_signals->{INT}{count}--
253 if $self->_signals->{INT}{count};
255 ### reset the 'install prereq?' cached answer
256 $self->settings->{'install_all_prereqs'} = undef;
263 ### return 1 to quit ###
264 sub dispatch_on_input {
266 my $conf = $self->backend->configure_object();
267 my $term = $self->term;
270 my($string, $noninteractive);
272 input => { required => 1, store => \$string },
273 noninteractive => { required => 0, store => \$noninteractive },
276 check( $tmpl, \%hash ) or return;
278 ### indicates whether or not the user will receive a shell
279 ### prompt after the command has finished.
280 $self->noninteractive($noninteractive) if defined $noninteractive;
282 my @cmds = split ';', $string;
283 while( my $input = shift @cmds ) {
285 ### to send over the socket ###
286 my $org_input = $input;
288 my $key; my $options;
289 { ### make whitespace not count when using special chars
290 { $input =~ s|^\s*([!?/])|$1 |; }
292 ### get the first letter of the input
293 $input =~ s|^\s*([\w\?\!/])\w*||;
298 ### we figured out what the command was...
299 ### if we have more input, that DOES NOT start with a white
300 ### space char, we misparsed.. like 'Test::Foo::Bar', which
301 ### would turn into 't', '::Foo::Bar'...
302 if( $input and $input !~ s/^\s+// ) {
303 $self->__print( loc("Could not understand command: %1\n".
304 "Possibly missing command before argument(s)?\n",
309 ### allow overrides from the config file ###
310 if( defined $rc->{$key} ) {
311 $input = $rc->{$key} . $input;
314 ### grab command line options like --no-force and --verbose ###
315 ($options,$input) = $term->parse_options($input)
323 return 1 if $key eq 'q';
325 my $method = $map->{$key};
327 ### dispatch meta locally at all times ###
328 $self->$method(input => $input, options => $options), next
331 ### flush unless we're trying to print the stack
332 CPANPLUS::Error->flush unless $key eq 'p';
334 ### connected over a socket? ###
335 if( $self->remote ) {
337 ### unsupported commands ###
339 ($key eq 's' and $input =~ /^\s*edit/)
341 $self->__print( "\n",
342 loc( "Command '%1' not supported over remote connection",
343 join ' ', $key, $input
347 my($status,$buff) = $self->__send_remote_command($org_input);
349 $self->__print( "\n", loc("Command failed!"), "\n\n" )
352 $self->_pager_open if $buff =~ tr/\n// > $self->_term_rowcount;
353 $self->__print( $buff );
357 ### or just a plain local shell? ###
360 unless( $self->can($method) ) {
361 $self->__print(loc("Unknown command '%1'. Usage:", $key), "\n");
366 ### some methods don't need modules ###
368 @mods = $self->_select_modules($input)
369 unless grep {$key eq $_} qw[! m a v w x p s b / ? h];
371 eval { $self->$method( modules => \@mods,
384 sub _select_modules {
386 my $input = shift or return;
387 my $cache = $self->cache;
388 my $cb = $self->backend;
390 ### expand .. in $input
391 $input =~ s{\b(\d+)\s*\.\.\s*(\d+)\b}
392 {join(' ', ($1 < 1 ? 1 : $1) .. ($2 > $#{$cache} ? $#{$cache} : $2))}eg;
394 $input = join(' ', 1 .. $#{$cache}) if $input eq '*';
395 $input =~ s/'/::/g; # perl 4 convention
398 for my $mod (split /\s+/, $input) {
400 ### it's a cache look up ###
401 if( $mod =~ /^\d+/ and $mod > 0 ) {
402 unless( scalar @$cache ) {
403 $self->__print( loc("No search was done yet!"), "\n" );
405 } elsif ( my $obj = $cache->[$mod] ) {
409 $self->__print( loc("No such module: %1", $mod), "\n" );
413 my $obj = $cb->parse_module( module => $mod );
416 $self->__print( loc("No such module: %1", $mod), "\n" );
424 unless( scalar @rv ) {
425 $self->__print( loc("No modules found to operate on!\n") );
432 sub _format_version {
434 my $version = shift || 0;
436 ### fudge $version into the 'optimal' format
437 $version = 0 if $version eq 'undef';
438 $version =~ s/_//g; # everything after gets stripped off otherwise
440 ### allow 6 digits after the dot, as that's how perl stringifies
442 $version = sprintf('%3.6f', $version);
443 $version = '' if $version == '0.00';
444 $version =~ s/(00{0,3})$/' ' x (length $1)/e;
449 sub __display_results {
451 my $cache = $self->cache;
457 $self->_pager_open if $#{$cache} >= $self->_term_rowcount;
461 next unless $mod; # first one is undef
462 # humans start counting at 1
464 ### for dists only -- we have checksum info
471 $self->_format_version( $mod->version ),
480 $self->_format_version( $mod->version ),
490 $self->__print( loc("No results to display"), "\n" );
498 $self->dispatch_on_input( input => $rc->{'logout'} )
499 if defined $rc->{'logout'};
501 $self->__print( loc("Exiting CPANPLUS shell"), "\n" );
504 ###########################
505 ### actual command subs ###
506 ###########################
509 ### print out the help message ###
510 ### perhaps, '?' should be a slightly different version ###
517 { local $Params::Check::ALLOW_UNKNOWN = 1;
520 input => { required => 0, store => \$input }
523 my $args = check( $tmpl, \%hash ) or return;
528 loc(' h | ? # display help' ),
530 loc(' v # version information' ),
532 loc(' a AUTHOR ... # search by author(s)' ),
533 loc(' m MODULE ... # search by module(s)' ),
534 loc(' f MODULE ... # list all releases of a module' ),
535 loc(" o [ MODULE ... ] # list installed module(s) that aren't up to date" ),
536 loc(' w # display the result of your last search again' ),
537 loc('[Operations]' ),
538 loc(' i MODULE | NUMBER ... # install module(s), by name or by search number' ),
539 loc(' i URI | ... # install module(s), by URI (ie http://foo.com/X.tgz)' ),
540 loc(' i DIR | ... # install module(s), by path (ie ./Module-1.0)' ),
541 loc(' t MODULE | NUMBER ... # test module(s), by name or by search number' ),
542 loc(' u MODULE | NUMBER ... # uninstall module(s), by name or by search number' ),
543 loc(' d MODULE | NUMBER ... # download module(s)' ),
544 loc(' l MODULE | NUMBER ... # display detailed information about module(s)' ),
545 loc(' r MODULE | NUMBER ... # display README files of module(s)' ),
546 loc(' c MODULE | NUMBER ... # check for module report(s) from cpan-testers' ),
547 loc(' z MODULE | NUMBER ... # extract module(s) and open command prompt in it' ),
548 loc('[Local Administration]' ),
549 loc(' b # write a bundle file for your configuration' ),
550 loc(' s program [OPT VALUE] # set program locations for this session' ),
551 loc(' s conf [OPT VALUE] # set config options for this session' ),
552 loc(' s mirrors # show currently selected mirrors' ),
553 loc(' s reconfigure # reconfigure settings ' ),
554 loc(' s selfupdate # update your CPANPLUS install '),
555 loc(' s save [user|system] # save settings for this user or systemwide' ),
556 loc(' s edit [user|system] # open configuration file in editor and reload' ),
557 loc(' ! EXPR # evaluate a perl statement' ),
558 loc(' p [FILE] # print the error stack (optionally to a file)' ),
559 loc(' x # reload CPAN indices (purges cache)' ),
560 loc(' x --update_source # reload CPAN indices, get fresh source files' ),
561 loc('[Common Options]' ),
562 loc(' i ... --skiptest # skip tests' ),
563 loc(' i ... --force # force all operations' ),
564 loc(' i ... --verbose # run in verbose mode' ),
566 loc(' /plugins # list available plugins' ),
567 loc(' /? [PLUGIN NAME] # show usage for (a particular) plugin(s)' ),
571 $self->_pager_open if (@help >= $self->_term_rowcount);
572 ### XXX: functional placeholder for actual 'detailed' help.
573 $self->__print( "Detailed help for the command '$input' is " .
574 "not available.\n\n" ) if length $input;
575 $self->__print( map {"$_\n"} @help );
576 $self->__print( $/ );
581 ### eval some code ###
584 my $cb = $self->backend;
589 { local $Params::Check::ALLOW_UNKNOWN = 1;
592 input => { required => 1, store => \$input }
595 my $args = check( $tmpl, \%hash ) or return;
598 local $Data::Dumper::Indent = 1; # for dumpering from !
601 $self->__print( "\n" );
607 my $cb = $self->backend;
611 { local $Params::Check::ALLOW_UNKNOWN = 1;
614 input => { required => 1, },
615 options => { default => { } },
618 $args = check( $tmpl, \%hash ) or return;
621 my @regexes = map { qr/$_/i } split /\s+/, $args->{'input'};
623 ### XXX this is rather slow, because (probably)
624 ### of the many method calls
625 ### XXX need to profile to speed it up =/
627 ### find the modules ###
628 my @rv = sort { $a->module cmp $b->module }
630 %{$args->{'options'}},
635 ### store the result in the cache ###
636 $self->cache([undef,@rv]);
638 $self->__display_results;
645 my $cb = $self->backend;
649 { local $Params::Check::ALLOW_UNKNOWN = 1;
652 input => { required => 1, },
653 options => { default => { } },
656 $args = check( $tmpl, \%hash ) or return;
659 my @regexes = map { qr/$_/i } split /\s+/, $args->{'input'};
662 for my $type (qw[author cpanid]) {
663 push @rv, $cb->search(
664 %{$args->{'options'}},
671 my @list = sort { $a->module cmp $b->module }
674 grep { not $seen{$_}++ } @rv;
676 $self->cache([undef,@list]);
678 $self->__display_results;
684 my $cb = $self->backend;
687 my $args; my $mods; my $opts;
688 { local $Params::Check::ALLOW_UNKNOWN = 1;
691 modules => { required => 1, store => \$mods },
692 options => { default => { }, store => \$opts },
695 $args = check( $tmpl, \%hash ) or return;
698 return unless scalar @$mods;
701 for my $mod ( @$mods ) {
702 $self->__print( $mod->readme( %$opts ) );
712 my $cb = $self->backend;
715 my $args; my $mods; my $opts;
716 { local $Params::Check::ALLOW_UNKNOWN = 1;
719 modules => { required => 1, store => \$mods },
720 options => { default => { }, store => \$opts },
723 $args = check( $tmpl, \%hash ) or return;
726 $self->_pager_open if @$mods >= $self->_term_rowcount;
727 for my $mod (@$mods) {
728 my $where = $mod->fetch( %$opts );
732 ? loc("Successfully fetched '%1' to '%2'",
733 $mod->module, $where )
734 : loc("Failed to fetch '%1'", $mod->module)
736 $self->__print( "\n" );
744 my $cb = $self->backend;
745 my $conf = $cb->configure_object;
748 my $shell = $conf->get_program('shell');
751 loc("Your config does not specify a subshell!"), "\n",
752 loc("Perhaps you need to re-run your setup?"), "\n"
757 my $args; my $mods; my $opts;
758 { local $Params::Check::ALLOW_UNKNOWN = 1;
761 modules => { required => 1, store => \$mods },
762 options => { default => { }, store => \$opts },
765 $args = check( $tmpl, \%hash ) or return;
768 my $cwd = Cwd::cwd();
769 for my $mod (@$mods) {
770 $mod->fetch( %$opts ) or next;
771 $mod->extract( %$opts ) or next;
773 $cb->_chdir( dir => $mod->status->extract() ) or next;
775 #local $ENV{PERL5OPT} = CPANPLUS::inc->original_perl5opt;
777 if( system($shell) and $! ) {
779 loc("Error executing your subshell '%1': %2",
785 $cb->_chdir( dir => $cwd );
792 my $cb = $self->backend;
793 my $conf = $cb->configure_object;
796 my $args; my $mods; my $opts;
797 { local $Params::Check::ALLOW_UNKNOWN = 1;
800 modules => { required => 1, store => \$mods },
801 options => { default => { }, store => \$opts },
804 $args = check( $tmpl, \%hash ) or return;
808 for my $mod (@$mods) {
809 push @list, sort { $a->version <=> $b->version }
810 grep { defined } $mod->distributions( %$opts );
813 my @rv = sort { $a->module cmp $b->module } @list;
815 $self->cache([undef,@rv]);
816 $self->__display_results;
821 sub _reload_indices {
823 my $cb = $self->backend;
827 { local $Params::Check::ALLOW_UNKNOWN = 1;
830 options => { default => { }, store => \$opts },
833 $args = check( $tmpl, \%hash ) or return;
836 my $rv = $cb->reload_indices( %$opts );
838 ### so the update failed, but you didnt give it any options either
839 if( !$rv and !(keys %$opts) ) {
841 "\nFailure may be due to corrupt source files\n" .
842 "Try this:\n\tx --update_source\n\n" );
851 my $cb = $self->backend;
852 my $conf = $cb->configure_object;
855 my $args; my $mods; my $opts; my $choice;
856 { local $Params::Check::ALLOW_UNKNOWN = 1;
859 modules => { required => 1, store => \$mods },
860 options => { default => { }, store => \$opts },
861 choice => { required => 1, store => \$choice,
862 allow => [qw|i t|] },
865 $args = check( $tmpl, \%hash ) or return;
868 unless( scalar @$mods ) {
869 $self->__print( loc("Nothing done\n") );
873 my $target = $choice eq 'i' ? TARGET_INSTALL : TARGET_CREATE;
874 my $prompt = $choice eq 'i' ? loc('Installing ') : loc('Testing ');
875 my $action = $choice eq 'i' ? 'install' : 'test';
878 ### first loop over the mods to install them ###
879 for my $mod (@$mods) {
880 $self->__print( $prompt, $mod->module, " (".$mod->version.")", "\n" );
882 my $log_length = length CPANPLUS::Error->stack_as_string;
884 ### store the status for look up when we're done with all
886 $status->{$mod} = $mod->install( %$opts, target => $target );
888 ### would you like a log file of what happened?
889 if( $conf->get_conf('write_install_logs') ) {
891 my $dir = File::Spec->catdir(
892 $conf->get_conf('base'),
893 $conf->_get_build('install_log_dir'),
895 ### create the dir if it doesn't exit yet
896 $cb->_mkdir( dir => $dir ) unless -d $dir;
898 my $file = File::Spec->catfile(
900 INSTALL_LOG_FILE->( $mod )
902 if ( open my $fh, ">$file" ) {
903 my $stack = CPANPLUS::Error->stack_as_string;
904 ### remove everything in the log that was there *before*
905 ### we started this install
906 substr( $stack, 0, $log_length, '' );
912 loc("*** Install log written to:\n %1\n\n", $file)
915 warn "Could not open '$file': $!\n";
922 ### then report whether all this went ok or not ###
923 for my $mod (@$mods) {
924 # if( $mod->status->installed ) {
925 if( $status->{$mod} ) {
927 loc("Module '%1' %tense(%2,past) successfully\n",
928 $mod->module, $action)
933 loc("Error %tense(%1,present) '%2'\n", $action, $mod->module)
942 loc("No errors %tense(%1,present) all modules", $action), "\n"
946 loc("Problem %tense(%1,present) one or more modules", $action)
948 $self->__print( "\n" );
951 loc("*** You can view the complete error buffer by pressing ".
953 ) unless $conf->get_conf('verbose') || $self->noninteractive;
955 $self->__print( "\n" );
960 sub __ask_about_install {
961 my $mod = shift or return;
962 my $prereq = shift or return;
963 my $term = $Shell->term;
965 $Shell->__print( "\n" );
966 $Shell->__print( loc("Module '%1' requires '%2' to be installed",
967 $mod->module, $prereq->module ) );
968 $Shell->__print( "\n\n" );
970 ### previously cached answer?
971 return $Shell->settings->{'install_all_prereqs'}
972 if defined $Shell->settings->{'install_all_prereqs'};
976 loc( "If you don't wish to see this question anymore\n".
977 "you can disable it by entering the following ".
978 "commands on the prompt:\n '%1'",
979 's conf prereqs 1; s save' ) );
980 $Shell->__print("\n\n");
982 my $yes = loc("Yes");
984 my $all = loc("Yes to all (for this module)");
985 my $none = loc("No to all (for this module)");
987 my $reply = $term->get_reply(
988 prompt => loc("Should I install this module?"),
989 choices => [ $yes, $no, $all, $none ],
993 ### if 'all' or 'none', save this, so we can apply it to
994 ### other prereqs in this chain.
995 $Shell->settings->{'install_all_prereqs'} =
997 $reply eq $none ? 0 :
1000 ### if 'yes' or 'all', the user wants it installed
1001 return $reply eq $all ? 1 :
1002 $reply eq $yes ? 1 :
1006 sub __ask_about_send_test_report {
1007 my($mod, $grade) = @_;
1008 return 1 unless $grade eq GRADE_FAIL;
1010 my $term = $Shell->term;
1012 $Shell->__print( "\n" );
1014 loc("Test report prepared for module '%1'.\n Would you like to ".
1015 "send it? (You can edit it if you like)", $mod->module ) );
1016 $Shell->__print( "\n\n" );
1017 my $bool = $term->ask_yn(
1018 prompt => loc("Would you like to send the test report?"),
1025 sub __ask_about_edit_test_report {
1026 my($mod, $grade) = @_;
1027 return 0 unless $grade eq GRADE_FAIL;
1029 my $term = $Shell->term;
1031 $Shell->__print( "\n" );
1033 loc("Test report prepared for module '%1'. You can edit this ".
1034 "report if you would like", $mod->module ) );
1035 $Shell->__print("\n\n");
1036 my $bool = $term->ask_yn(
1037 prompt => loc("Would you like to edit the test report?"),
1044 sub __ask_about_test_failure {
1046 my $captured = shift || '';
1047 my $term = $Shell->term;
1049 $Shell->__print( "\n" );
1051 loc( "The tests for '%1' failed. Would you like me to proceed ".
1052 "anyway or should we abort?", $mod->module ) );
1053 $Shell->__print( "\n\n" );
1055 my $bool = $term->ask_yn(
1056 prompt => loc("Proceed anyway?"),
1066 my $cb = $self->backend;
1067 my $conf = $cb->configure_object;
1070 my $args; my $mods; my $opts;
1071 { local $Params::Check::ALLOW_UNKNOWN = 1;
1074 modules => { required => 1, store => \$mods },
1075 options => { default => { }, store => \$opts },
1078 $args = check( $tmpl, \%hash ) or return;
1081 ### every module has about 10 lines of details
1082 ### maybe more later with Module::CPANTS etc
1083 $self->_pager_open if scalar @$mods * 10 > $self->_term_rowcount;
1086 my $format = "%-24s %-45s\n";
1087 my $cformat = "%-24s %-45s %-10s\n";
1088 for my $mod (@$mods) {
1089 my $href = $mod->details( %$opts );
1090 my @list = sort { $a->module cmp $b->module } $mod->contains;
1094 loc("No details for %1 - it might be outdated.",
1095 $mod->module), "\n" );
1099 $self->__print( loc( "Details for '%1'\n", $mod->module ) );
1100 for my $item ( sort keys %$href ) {
1101 $self->__printf( $format, $item, $href->{$item} );
1105 for my $item ( @list ) {
1107 $cformat, ($showed ? '' : 'Contains:'),
1108 $item->module, $item->version
1112 $self->__print( "\n" );
1115 $self->_pager_close;
1116 $self->__print( "\n" );
1125 my $args; my $opts; my $file;
1126 { local $Params::Check::ALLOW_UNKNOWN = 1;
1129 options => { default => { }, store => \$opts },
1130 input => { default => '', store => \$file },
1133 $args = check( $tmpl, \%hash ) or return;
1138 $fh = FileHandle->new( ">$file" )
1139 or( warn loc("Could not open '%1': '%2'", $file, $!),
1146 $self->_pager_open if !$file;
1148 $self->__print( CPANPLUS::Error->stack_as_string );
1150 $self->_pager_close;
1152 select $old if $old;
1153 $self->__print( "\n" );
1161 my $cb = $self->backend;
1162 my $conf = $cb->configure_object;
1164 ### possible options
1165 ### XXX hard coded, not optimal :(
1168 save => q([user | system | boxed]),
1170 program => q([key => val]),
1171 conf => q([key => val]),
1173 selfupdate => '', # XXX add all opts here?
1177 my $args; my $opts; my $input;
1178 { local $Params::Check::ALLOW_UNKNOWN = 1;
1181 options => { default => { }, store => \$opts },
1182 input => { default => '', store => \$input },
1185 $args = check( $tmpl, \%hash ) or return;
1188 my ($type,$key,$value) = $input =~ m/(\w+)\s*(\w*)\s*(.*?)\s*$/;
1191 if( $type eq 'reconfigure' ) {
1192 my $setup = CPANPLUS::Configure::Setup->new(
1193 configure_object => $conf,
1194 term => $self->term,
1197 return $setup->init;
1199 } elsif ( $type eq 'save' ) {
1201 user => CONFIG_USER,
1202 system => CONFIG_SYSTEM,
1203 boxed => CONFIG_BOXED,
1204 }->{ $key } || CONFIG_USER;
1206 ### boxed is special, so let's get its value from %INC
1207 ### so we can tell it where to save
1208 ### XXX perhaps this logic should be generic for all
1209 ### types, and put in the ->save() routine
1211 if( $where eq CONFIG_BOXED ) {
1212 my $file = join( '/', split( '::', CONFIG_BOXED ) ) . '.pm';
1213 my $file_re = quotemeta($file);
1215 my $path = $INC{$file} || '';
1216 $path =~ s/$file_re$//;
1220 my $rv = $cb->configure_object->save( $where => $dir );
1224 ? loc("Configuration successfully saved to %1\n (%2)\n",
1226 : loc("Failed to save configuration\n" )
1230 } elsif ( $type eq 'edit' ) {
1232 my $editor = $conf->get_program('editor')
1233 or( print(loc("No editor specified")), return );
1236 user => CONFIG_USER,
1237 system => CONFIG_SYSTEM,
1238 }->{ $key } || CONFIG_USER;
1240 my $file = $conf->_config_pm_to_file( $where );
1241 system("$editor $file");
1244 ### disable warnings for this
1245 { require Module::Loaded;
1246 Module::Loaded::mark_as_unloaded( $where );
1248 ### reinitialize the config
1255 } elsif ( $type eq 'mirrors' ) {
1258 loc("Readonly list of mirrors (in order of preference):\n\n" ) );
1261 for my $host ( @{$conf->get_conf('hosts')} ) {
1262 my $uri = $cb->_host_to_uri( %$host );
1265 $self->__print( "\t[$i] $uri\n" );
1269 loc("\nTo edit this list, please type: '%1'\n", 's edit') );
1271 } elsif ( $type eq 'selfupdate' ) {
1272 my %valid = map { $_ => $_ }
1273 $cb->selfupdate_object->list_categories;
1275 unless( $valid{$key} ) {
1277 loc( "To update your current CPANPLUS installation, ".
1278 "choose one of the these options:\n%1",
1280 sprintf "\ts selfupdate %-17s " .
1281 "[--latest=0] [--dryrun]", $_
1282 } sort keys %valid )
1293 my %list = $cb->selfupdate_object
1294 ->list_modules_to_update( %update_args );
1296 $self->__print(loc("The following updates will take place:"),$/.$/);
1298 for my $feature ( sort keys %list ) {
1299 my $aref = $list{$feature};
1301 ### is it a 'feature' or a built in?
1304 ? " " . ucfirst($feature) . ":\n"
1305 : " Modules for '$feature' support:\n"
1308 ### show what modules would be installed
1311 ? map { sprintf " %-42s %-6s -> %-6s \n",
1312 $_->name, $_->installed_version, $_->version
1314 : " No upgrades required\n"
1316 $self->__print( $/ );
1320 unless( $opts->{'dryrun'} ) {
1321 $self->__print( loc("Updating your CPANPLUS installation\n") );
1322 $cb->selfupdate_object->selfupdate( %update_args );
1328 if ( $type eq 'program' or $type eq 'conf' ) {
1332 program => '%-12s %s',
1336 my @list = grep { $_ ne 'hosts' }
1337 $conf->options( type => $type );
1339 my $method = 'get_' . $type;
1341 local $Data::Dumper::Indent = 0;
1342 for my $name ( @list ) {
1343 my $val = $conf->$method($name) || '';
1345 ? (Data::Dumper::Dumper($val) =~ /= (.*);$/)
1348 $self->__printf( " $format\n", $name, $val );
1351 } elsif ( $key eq 'hosts' or $key eq 'lib' ) {
1353 loc( "Setting %1 is not trivial.\n" .
1354 "It is suggested you use '%2' and edit the " .
1355 "configuration file manually", $key, 's edit')
1358 my $method = 'set_' . $type;
1359 $conf->$method( $key => defined $value ? $value : '' )
1360 and $self->__print( loc("Key '%1' was set to '%2'", $key,
1361 defined $value ? $value : 'EMPTY STRING') );
1365 $self->__print( loc("Unknown type '%1'",$type || 'EMPTY' ) );
1366 $self->__print( $/ );
1367 $self->__print( loc("Try one of the following:") );
1368 $self->__print( $/, join $/,
1369 map { sprintf "\t%-11s %s", $_, $types{$_} }
1373 $self->__print( "\n" );
1380 my $cb = $self->backend;
1381 my $conf = $cb->configure_object;
1384 { local $Params::Check::ALLOW_UNKNOWN = 1;
1387 options => { default => { }, store => \$opts },
1388 modules => { required => 1, store => \$mods },
1391 check( $tmpl, \%hash ) or return;
1394 ### long listing? short is default ###
1395 my $long = $opts->{'long'} ? 1 : 0;
1397 my @list = scalar @$mods ? @$mods : @{$cb->_all_installed};
1400 for my $mod (@list) {
1401 ### skip this mod if it's up to date ###
1402 next if $mod->is_uptodate;
1403 ### skip this mod if it's core ###
1404 next if $mod->package_is_perl_core;
1406 if( $long or !$seen{$mod->package}++ ) {
1411 @rv = sort { $a->module cmp $b->module } @rv;
1413 $self->cache([undef,@rv]);
1415 $self->_pager_open if scalar @rv >= $self->_term_rowcount;
1417 my $format = "%5s %12s %12s %-36s %-10s\n";
1420 for my $mod ( @rv ) {
1424 $self->_format_version($mod->installed_version) || 'Unparsable',
1425 $self->_format_version( $mod->version ),
1427 $mod->author->cpanid
1431 $self->_pager_close;
1439 my $cb = $self->backend;
1440 my $conf = $cb->configure_object;
1442 my $opts; my $input;
1443 { local $Params::Check::ALLOW_UNKNOWN = 1;
1446 options => { default => { }, store => \$opts },
1447 input => { default => '', store => \$input },
1450 check( $tmpl, \%hash ) or return;
1453 $opts->{'path'} = $input if $input;
1455 my $where = $cb->autobundle( %$opts );
1459 ? loc("Wrote autobundle to '%1'", $where)
1460 : loc("Could not create autobundle" )
1462 $self->__print( "\n" );
1464 return $where ? 1 : 0;
1470 my $cb = $self->backend;
1471 my $term = $self->term;
1472 my $conf = $cb->configure_object;
1475 { local $Params::Check::ALLOW_UNKNOWN = 1;
1478 options => { default => { }, store => \$opts },
1479 modules => { default => [], store => \$mods },
1482 check( $tmpl, \%hash ) or return;
1485 my $force = $opts->{'force'} || $conf->get_conf('force');
1488 my $list = join "\n", map { ' ' . $_->module } @$mods;
1490 $self->__print( loc("
1491 This will uninstall the following modules:
1494 Note that if you installed them via a package manager, you probably
1495 should use the same package manager to uninstall them
1499 return unless $term->ask_yn(
1500 prompt => loc("Are you sure you want to continue?"),
1505 ### first loop over all the modules to uninstall them ###
1506 for my $mod (@$mods) {
1507 $self->__print( loc("Uninstalling '%1'", $mod->module), "\n" );
1509 $mod->uninstall( %$opts );
1513 ### then report whether all this went ok or not ###
1514 for my $mod (@$mods) {
1515 if( $mod->status->uninstall ) {
1517 loc("Module '%1' %tense(uninstall,past) successfully\n",
1522 loc("Error %tense(uninstall,present) '%1'\n", $mod->module) );
1528 loc("All modules %tense(uninstall,past) successfully"), "\n" );
1531 loc("Problem %tense(uninstalling,present) one or more modules" ),
1535 loc("*** You can view the complete error buffer by pressing '%1'".
1536 "***\n", 'p') ) unless $conf->get_conf('verbose');
1538 $self->__print( "\n" );
1546 my $cb = $self->backend;
1547 my $term = $self->term;
1548 my $conf = $cb->configure_object;
1551 { local $Params::Check::ALLOW_UNKNOWN = 1;
1554 options => { default => { }, store => \$opts },
1555 modules => { default => '', store => \$mods },
1558 check( $tmpl, \%hash ) or return;
1561 ### XXX might need to be conditional ###
1564 for my $mod (@$mods) {
1565 my @list = $mod->fetch_report( %$opts )
1566 or( print(loc("No reports available for this distribution.")),
1572 sort { $a->[1] cmp $b->[1] }
1573 map { [$_, $_->{'dist'}.':'.$_->{'platform'}] } @list;
1577 ### XXX this may need to be sorted better somehow ###
1579 my $format = "%8s %s %s\n";
1582 for my $href (@list ) {
1584 "[" . $mod->author->cpanid .'/'. $href->{'dist'} . "]\n"
1585 ) unless $seen{ $href->{'dist'} }++;
1590 $href->{'platform'},
1591 ($href->{'details'} ? '(*)' : '')
1594 $url ||= $href->{'details'};
1597 $self->__print( "\n==> $url\n" ) if $url;
1598 $self->__print( "\n" );
1600 $self->_pager_close;
1607 { my @PluginModules;
1609 showtip => [ __PACKAGE__, '_show_random_tip'],
1610 plugins => [ __PACKAGE__, '_list_plugins' ],
1611 '?' => [ __PACKAGE__, '_plugins_usage' ],
1614 sub plugin_modules { return @PluginModules }
1615 sub plugin_table { return %Dispatch }
1619 ### only initialize once
1620 return if $init_done++;
1622 ### find all plugins first
1623 if( check_install( module => 'Module::Pluggable', version => '2.4') ) {
1624 require Module::Pluggable;
1626 my $only_re = __PACKAGE__ . '::Plugins::\w+$';
1628 Module::Pluggable->import(
1629 sub_name => '_plugins',
1630 search_path => __PACKAGE__,
1631 only => qr/$only_re/,
1632 #except => [ INSTALLER_MM, INSTALLER_SAMPLE ]
1635 push @PluginModules, __PACKAGE__->_plugins;
1638 ### now try to load them
1639 for my $p ( __PACKAGE__->plugin_modules ) {
1640 my %map = eval { load $p; $p->import; $p->plugins };
1641 error(loc("Could not load plugin '$p': $@")), next if $@;
1643 ### register each plugin
1644 while( my($name, $func) = each %map ) {
1646 if( not length $name or not length $func ) {
1647 error(loc("Empty plugin name or dispatch function detected"));
1651 if( exists( $Dispatch{$name} ) ) {
1652 error(loc("'%1' is already registered by '%2'",
1653 $name, $Dispatch{$name}->[0]));
1657 ### register name, package and function
1658 $Dispatch{$name} = [ $p, $func ];
1663 ### dispatch a plugin command to its function
1667 my $cb = $self->backend;
1668 my $term = $self->term;
1669 my $conf = $cb->configure_object;
1671 my $opts; my $input;
1672 { local $Params::Check::ALLOW_UNKNOWN = 1;
1675 options => { default => { }, store => \$opts },
1676 input => { default => '', store => \$input },
1679 check( $tmpl, \%hash ) or return;
1682 $input =~ s/\s*(\S+)\s*//;
1685 ### look up the command, or go to the default
1686 my $aref = $Dispatch{ $cmd } || [ __PACKAGE__, '_plugin_default' ];
1688 my($pkg,$func) = @$aref;
1690 my $rv = eval { $pkg->$func( $self, $cb, $cmd, $input, $opts ) };
1694 ### return $rv instead, so input loop can be terminated?
1698 sub _plugin_default { error(loc("No such plugin command")) }
1702 { my $help_format = " /%-21s # %s\n";
1707 $self->__print( loc("Available plugins:\n") );
1708 $self->__print( loc(" List usage by using: /? PLUGIN_NAME\n" ) );
1709 $self->__print( $/ );
1711 my %table = __PACKAGE__->plugin_table;
1712 for my $name( sort keys %table ) {
1713 my $pkg = $table{$name}->[0];
1714 my $this = __PACKAGE__;
1716 my $who = $pkg eq $this
1718 : do { my $v = $self->_format_version($pkg->VERSION) || '';
1719 $pkg =~ s/^$this/../;
1720 sprintf "Provided by: %-30s %-10s", $pkg, $v;
1723 $self->__printf( $help_format, $name, $who );
1726 $self->__print( $/.$/ );
1729 " Write your own plugins? Read the documentation of:\n" .
1730 " CPANPLUS::Shell::Default::Plugins::HOWTO\n" );
1732 $self->__print( $/ );
1735 sub _list_plugins_help {
1736 return sprintf $help_format, 'plugins', loc("lists available plugins");
1739 ### registered as a plugin too
1740 sub _show_random_tip_help {
1741 return sprintf $help_format, 'showtip', loc("show usage tips" );
1744 sub _plugins_usage {
1750 my %table = $self->plugin_table;
1752 my @list = length $input ? split /\s+/, $input : sort keys %table;
1754 for my $name( @list ) {
1756 ### no such plugin? skip
1757 error(loc("No such plugin '$name'")), next unless $table{$name};
1759 my $pkg = $table{$name}->[0];
1760 my $func = $table{$name}->[1] . '_help';
1762 if ( my $sub = $pkg->can( $func ) ) {
1763 eval { $self->__print( $sub->() ) };
1767 $self->__print(" No usage for '$name' -- try perldoc $pkg");
1770 $self->__print( $/ );
1773 $self->__print( $/.$/ );
1776 sub _plugins_usage_help {
1777 return sprintf $help_format, '? [NAME ...]',
1778 loc("show usage for plugins");
1782 ### send a command to a remote host, retrieve the answer;
1783 sub __send_remote_command {
1786 my $remote = $self->remote or return;
1787 my $user = $remote->{'username'};
1788 my $pass = $remote->{'password'};
1789 my $conn = $remote->{'connection'};
1790 my $end = "\015\012";
1793 my $send = join "\0", $user, $pass, $cmd;
1795 print $conn $send . $end;
1797 ### XXX why doesn't something like this just work?
1798 #1 while recv($conn, $answer, 1024, 0);
1801 $conn->recv( $buff, 1024, 0 );
1803 last if $buff =~ /$end$/;
1806 my($status,$buffer) = split "\0", $answer;
1808 return ($status, $buffer);
1812 sub _read_configuration_from_rc {
1814 my $rc_file = shift;
1817 if( can_load( modules => { 'Config::Auto' => '0.0' } ) ) {
1818 $Config::Auto::DisablePerl = 1;
1820 eval { $href = Config::Auto::parse( $rc_file, format => 'space' ) };
1823 loc( "Unable to read in config file '%1': %2", $rc_file, $@ )
1831 loc( "You can update CPANPLUS by running: '%1'", 's selfupdate' ),
1832 loc( "You can install modules by URL using '%1'", 'i URL' ),
1833 loc( "You can turn off these tips using '%1'",
1834 's conf show_startup_tip 0' ),
1835 loc( "You can use wildcards like '%1' and '%2' on search results",
1837 loc( "You can use plugins. Type '%1' to list available plugins",
1839 loc( "You can show all your out of date modules using '%1'", 'o' ),
1840 loc( "Many operations take options, like '%1', '%2' or '%3'",
1841 '--verbose', '--force', '--skiptest' ),
1842 loc( "The documentation in %1 and %2 is very useful",
1843 "CPANPLUS::Module", "CPANPLUS::Backend" ),
1844 loc( "You can type '%1' for help and '%2' to exit", 'h', 'q' ),
1845 loc( "You can run an interactive setup using '%1'", 's reconfigure' ),
1846 loc( "You can add custom sources to your index. See '%1' for details",
1848 loc( "CPANPLUS now has an experimental SQLite backend. You can enable ".
1849 "it via: '%1'. Update dependencies via '%2'",
1850 's conf source_engine CPANPLUS::Internals::Source::SQLite; s save',
1851 's selfupdate enabled_features ' ),
1854 sub _show_random_tip {
1856 $self->__print( $/, "Did you know...\n ",
1857 $tips[ int rand scalar @tips ], $/ );
1870 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
1874 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
1878 The CPAN++ interface (of which this module is a part of) is copyright (c)
1879 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
1881 This library is free software; you may redistribute and/or modify it
1882 under the same terms as Perl itself.
1886 L<CPANPLUS::Shell::Classic>, L<CPANPLUS::Shell>, L<cpanp>
1891 # c-indentation-style: bsd
1893 # indent-tabs-mode: nil
1895 # vim: expandtab shiftwidth=4:
1900 e => "_expand_inc", # scratch it, imho -- not used enough
1902 ### free letters: g j k n y ###