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 1 3..5 # install search results 1, 3, 4 and 5
108 cpanp> i * # install all search results
109 cpanp> a KANE; i *; # find modules by kane, install all results
110 cpanp> t Acme::Foo # test Acme::Foo, without installing it
111 cpanp> u Acme::Foo # uninstall Acme::Foo
112 cpanp> d Acme::Foo # download Acme::Foo
113 cpanp> z Acme::Foo # download & extract Acme::Foo, then open a
114 # shell in the extraction directory
116 cpanp> c Acme::Foo # get a list of test results for Acme::Foo
117 cpanp> l Acme::Foo # view details about the Acme::Foo package
118 cpanp> r Acme::Foo # view Acme::Foo's README file
119 cpanp> o # get a list of all installed modules that
121 cpanp> o 1..3 # list uptodateness from a previous search
123 cpanp> s conf # show config settings
124 cpanp> s conf md5 1 # enable md5 checks
125 cpanp> s program # show program settings
126 cpanp> s edit # edit config file
127 cpanp> s reconfigure # go through initial configuration again
128 cpanp> s selfupdate # update your CPANPLUS install
129 cpanp> s save # save config to disk
130 cpanp> s mirrors # show currently selected mirrors
132 cpanp> ! [PERL CODE] # execute the following perl code
134 cpanp> b # create an autobundle for this computers
136 cpanp> x # reload index files (purges cache)
137 cpanp> x --update_source # reload index files, get fresh source files
138 cpanp> p [FILE] # print error stack (to a file)
139 cpanp> v # show the banner
140 cpanp> w # show last search results again
142 cpanp> q # quit the shell
144 cpanp> /plugins # list avialable plugins
145 cpanp> /? PLUGIN # list help test of <PLUGIN>
148 cpanp> i ... --skiptest # skip tests
149 cpanp> i ... --force # force all operations
150 cpanp> i ... --verbose # run in verbose mode
154 This module provides the default user interface to C<CPANPLUS>. You
155 can start it via the C<cpanp> binary, or as detailed in the L<SYNOPSIS>.
162 my $cb = CPANPLUS::Backend->new( @_ );
163 my $self = $class->SUPER::_init(
165 term => Term::ReadLine->new( $Brand ),
168 format => "%4s %-55s %8s %-10s\n",
169 dist_format => "%4s %-42s %-12s %8s %-10s\n",
171 ### make it available package wide ###
174 my $rc_file = File::Spec->catfile(
175 $cb->configure_object->get_conf('base'),
176 DOT_SHELL_DEFAULT_RC,
180 if( -e $rc_file && -r _ ) {
181 $rc = $self->_read_configuration_from_rc( $rc_file );
184 ### register install callback ###
185 $cb->_register_callback(
186 name => 'install_prerequisite',
187 code => \&__ask_about_install,
190 ### execute any login commands specified ###
191 $self->dispatch_on_input( input => $rc->{'login'} )
192 if defined $rc->{'login'};
194 ### register test report callbacks ###
195 $cb->_register_callback(
196 name => 'edit_test_report',
197 code => \&__ask_about_edit_test_report,
200 $cb->_register_callback(
201 name => 'send_test_report',
202 code => \&__ask_about_send_test_report,
205 $cb->_register_callback(
206 name => 'proceed_on_test_failure',
207 code => \&__ask_about_test_failure,
210 ### load all the plugins
211 $self->_plugins_init;
218 my $term = $self->term;
219 my $conf = $self->backend->configure_object;
222 $self->__print( "*** Type 'p' now to show start up log\n" ); # XXX add to banner?
223 $self->_show_random_tip if $conf->get_conf('show_startup_tip');
224 $self->_input_loop && $self->__print( "\n" );
230 my $term = $self->term;
231 my $cb = $self->backend;
235 defined (my $input = eval { $term->readline($self->prompt) } )
236 or $self->_signals->{INT}{count} == 1
238 ### re-initiate all signal handlers
239 while (my ($sig, $entry) = each %{$self->_signals} ) {
240 $SIG{$sig} = $entry->{handler} if exists($entry->{handler});
243 $self->__print( "\n" );
244 last if $self->dispatch_on_input( input => $input );
246 ### flush the lib cache ###
247 $cb->_flush( list => [qw|lib load|] );
250 $self->_signals->{INT}{count}--
251 if $self->_signals->{INT}{count}; # clear the sigint count
257 ### return 1 to quit ###
258 sub dispatch_on_input {
260 my $conf = $self->backend->configure_object();
261 my $term = $self->term;
264 my($string, $noninteractive);
266 input => { required => 1, store => \$string },
267 noninteractive => { required => 0, store => \$noninteractive },
270 check( $tmpl, \%hash ) or return;
272 ### indicates whether or not the user will receive a shell
273 ### prompt after the command has finished.
274 $self->noninteractive($noninteractive) if defined $noninteractive;
276 my @cmds = split ';', $string;
277 while( my $input = shift @cmds ) {
279 ### to send over the socket ###
280 my $org_input = $input;
282 my $key; my $options;
283 { ### make whitespace not count when using special chars
284 { $input =~ s|^\s*([!?/])|$1 |; }
286 ### get the first letter of the input
287 $input =~ s|^\s*([\w\?\!/])\w*||;
292 ### we figured out what the command was...
293 ### if we have more input, that DOES NOT start with a white
294 ### space char, we misparsed.. like 'Test::Foo::Bar', which
295 ### would turn into 't', '::Foo::Bar'...
296 if( $input and $input !~ s/^\s+// ) {
297 $self->__print( loc("Could not understand command: %1\n".
298 "Possibly missing command before argument(s)?\n",
303 ### allow overrides from the config file ###
304 if( defined $rc->{$key} ) {
305 $input = $rc->{$key} . $input;
308 ### grab command line options like --no-force and --verbose ###
309 ($options,$input) = $term->parse_options($input)
317 return 1 if $key eq 'q';
319 my $method = $map->{$key};
321 ### dispatch meta locally at all times ###
322 $self->$method(input => $input, options => $options), next
325 ### flush unless we're trying to print the stack
326 CPANPLUS::Error->flush unless $key eq 'p';
328 ### connected over a socket? ###
329 if( $self->remote ) {
331 ### unsupported commands ###
333 ($key eq 's' and $input =~ /^\s*edit/)
335 $self->__print( "\n",
336 loc( "Command '%1' not supported over remote connection",
337 join ' ', $key, $input
341 my($status,$buff) = $self->__send_remote_command($org_input);
343 $self->__print( "\n", loc("Command failed!"), "\n\n" )
346 $self->_pager_open if $buff =~ tr/\n// > $self->_term_rowcount;
347 $self->__print( $buff );
351 ### or just a plain local shell? ###
354 unless( $self->can($method) ) {
355 $self->__print(loc("Unknown command '%1'. Usage:", $key), "\n");
360 ### some methods don't need modules ###
362 @mods = $self->_select_modules($input)
363 unless grep {$key eq $_} qw[! m a v w x p s b / ? h];
365 eval { $self->$method( modules => \@mods,
378 sub _select_modules {
380 my $input = shift or return;
381 my $cache = $self->cache;
382 my $cb = $self->backend;
384 ### expand .. in $input
385 $input =~ s{\b(\d+)\s*\.\.\s*(\d+)\b}
386 {join(' ', ($1 < 1 ? 1 : $1) .. ($2 > $#{$cache} ? $#{$cache} : $2))}eg;
388 $input = join(' ', 1 .. $#{$cache}) if $input eq '*';
389 $input =~ s/'/::/g; # perl 4 convention
392 for my $mod (split /\s+/, $input) {
394 ### it's a cache look up ###
395 if( $mod =~ /^\d+/ and $mod > 0 ) {
396 unless( scalar @$cache ) {
397 $self->__print( loc("No search was done yet!"), "\n" );
399 } elsif ( my $obj = $cache->[$mod] ) {
403 $self->__print( loc("No such module: %1", $mod), "\n" );
407 my $obj = $cb->parse_module( module => $mod );
410 $self->__print( loc("No such module: %1", $mod), "\n" );
418 unless( scalar @rv ) {
419 $self->__print( loc("No modules found to operate on!\n") );
426 sub _format_version {
430 ### fudge $version into the 'optimal' format
431 $version = 0 if $version eq 'undef';
432 $version =~ s/_//g; # everything after gets stripped off otherwise
434 ### allow 6 digits after the dot, as that's how perl stringifies
436 $version = sprintf('%3.6f', $version);
437 $version = '' if $version == '0.00';
438 $version =~ s/(00{0,3})$/' ' x (length $1)/e;
443 sub __display_results {
445 my $cache = $self->cache;
451 $self->_pager_open if $#{$cache} >= $self->_term_rowcount;
455 next unless $mod; # first one is undef
456 # humans start counting at 1
458 ### for dists only -- we have checksum info
465 $self->_format_version( $mod->version ),
474 $self->_format_version( $mod->version ),
484 $self->__print( loc("No results to display"), "\n" );
492 $self->dispatch_on_input( input => $rc->{'logout'} )
493 if defined $rc->{'logout'};
495 $self->__print( loc("Exiting CPANPLUS shell"), "\n" );
498 ###########################
499 ### actual command subs ###
500 ###########################
503 ### print out the help message ###
504 ### perhaps, '?' should be a slightly different version ###
511 { local $Params::Check::ALLOW_UNKNOWN = 1;
514 input => { required => 0, store => \$input }
517 my $args = check( $tmpl, \%hash ) or return;
522 loc(' h | ? # display help' ),
524 loc(' v # version information' ),
526 loc(' a AUTHOR ... # search by author(s)' ),
527 loc(' m MODULE ... # search by module(s)' ),
528 loc(' f MODULE ... # list all releases of a module' ),
529 loc(" o [ MODULE ... ] # list installed module(s) that aren't up to date" ),
530 loc(' w # display the result of your last search again' ),
531 loc('[Operations]' ),
532 loc(' i MODULE | NUMBER ... # install module(s), by name or by search number' ),
533 loc(' i URI | ... # install module(s), by URI (ie http://foo.com/X.tgz)' ),
534 loc(' t MODULE | NUMBER ... # test module(s), by name or by search number' ),
535 loc(' u MODULE | NUMBER ... # uninstall module(s), by name or by search number' ),
536 loc(' d MODULE | NUMBER ... # download module(s)' ),
537 loc(' l MODULE | NUMBER ... # display detailed information about module(s)' ),
538 loc(' r MODULE | NUMBER ... # display README files of module(s)' ),
539 loc(' c MODULE | NUMBER ... # check for module report(s) from cpan-testers' ),
540 loc(' z MODULE | NUMBER ... # extract module(s) and open command prompt in it' ),
541 loc('[Local Administration]' ),
542 loc(' b # write a bundle file for your configuration' ),
543 loc(' s program [OPT VALUE] # set program locations for this session' ),
544 loc(' s conf [OPT VALUE] # set config options for this session' ),
545 loc(' s mirrors # show currently selected mirrors' ),
546 loc(' s reconfigure # reconfigure settings ' ),
547 loc(' s selfupdate # update your CPANPLUS install '),
548 loc(' s save [user|system] # save settings for this user or systemwide' ),
549 loc(' s edit [user|system] # open configuration file in editor and reload' ),
550 loc(' ! EXPR # evaluate a perl statement' ),
551 loc(' p [FILE] # print the error stack (optionally to a file)' ),
552 loc(' x # reload CPAN indices (purges cache)' ),
553 loc(' x --update_source # reload CPAN indices, get fresh source files' ),
554 loc('[Common Options]' ),
555 loc(' i ... --skiptest # skip tests' ),
556 loc(' i ... --force # force all operations' ),
557 loc(' i ... --verbose # run in verbose mode' ),
559 loc(' /plugins # list available plugins' ),
560 loc(' /? [PLUGIN NAME] # show usage for (a particular) plugin(s)' ),
564 $self->_pager_open if (@help >= $self->_term_rowcount);
565 ### XXX: functional placeholder for actual 'detailed' help.
566 $self->__print( "Detailed help for the command '$input' is " .
567 "not available.\n\n" ) if length $input;
568 $self->__print( map {"$_\n"} @help );
569 $self->__print( $/ );
574 ### eval some code ###
577 my $cb = $self->backend;
582 { local $Params::Check::ALLOW_UNKNOWN = 1;
585 input => { required => 1, store => \$input }
588 my $args = check( $tmpl, \%hash ) or return;
591 local $Data::Dumper::Indent = 1; # for dumpering from !
594 $self->__print( "\n" );
600 my $cb = $self->backend;
604 { local $Params::Check::ALLOW_UNKNOWN = 1;
607 input => { required => 1, },
608 options => { default => { } },
611 $args = check( $tmpl, \%hash ) or return;
614 my @regexes = map { qr/$_/i } split /\s+/, $args->{'input'};
616 ### XXX this is rather slow, because (probably)
617 ### of the many method calls
618 ### XXX need to profile to speed it up =/
620 ### find the modules ###
621 my @rv = sort { $a->module cmp $b->module }
623 %{$args->{'options'}},
628 ### store the result in the cache ###
629 $self->cache([undef,@rv]);
631 $self->__display_results;
638 my $cb = $self->backend;
642 { local $Params::Check::ALLOW_UNKNOWN = 1;
645 input => { required => 1, },
646 options => { default => { } },
649 $args = check( $tmpl, \%hash ) or return;
652 my @regexes = map { qr/$_/i } split /\s+/, $args->{'input'};
655 for my $type (qw[author cpanid]) {
656 push @rv, $cb->search(
657 %{$args->{'options'}},
664 my @list = sort { $a->module cmp $b->module }
667 grep { not $seen{$_}++ } @rv;
669 $self->cache([undef,@list]);
671 $self->__display_results;
677 my $cb = $self->backend;
680 my $args; my $mods; my $opts;
681 { local $Params::Check::ALLOW_UNKNOWN = 1;
684 modules => { required => 1, store => \$mods },
685 options => { default => { }, store => \$opts },
688 $args = check( $tmpl, \%hash ) or return;
691 return unless scalar @$mods;
694 for my $mod ( @$mods ) {
695 $self->__print( $mod->readme( %$opts ) );
705 my $cb = $self->backend;
708 my $args; my $mods; my $opts;
709 { local $Params::Check::ALLOW_UNKNOWN = 1;
712 modules => { required => 1, store => \$mods },
713 options => { default => { }, store => \$opts },
716 $args = check( $tmpl, \%hash ) or return;
719 $self->_pager_open if @$mods >= $self->_term_rowcount;
720 for my $mod (@$mods) {
721 my $where = $mod->fetch( %$opts );
725 ? loc("Successfully fetched '%1' to '%2'",
726 $mod->module, $where )
727 : loc("Failed to fetch '%1'", $mod->module)
729 $self->__print( "\n" );
737 my $cb = $self->backend;
738 my $conf = $cb->configure_object;
741 my $shell = $conf->get_program('shell');
744 loc("Your config does not specify a subshell!"), "\n",
745 loc("Perhaps you need to re-run your setup?"), "\n"
750 my $args; my $mods; my $opts;
751 { local $Params::Check::ALLOW_UNKNOWN = 1;
754 modules => { required => 1, store => \$mods },
755 options => { default => { }, store => \$opts },
758 $args = check( $tmpl, \%hash ) or return;
761 my $cwd = Cwd::cwd();
762 for my $mod (@$mods) {
763 $mod->fetch( %$opts ) or next;
764 $mod->extract( %$opts ) or next;
766 $cb->_chdir( dir => $mod->status->extract() ) or next;
768 #local $ENV{PERL5OPT} = CPANPLUS::inc->original_perl5opt;
770 if( system($shell) and $! ) {
772 loc("Error executing your subshell '%1': %2",
778 $cb->_chdir( dir => $cwd );
785 my $cb = $self->backend;
786 my $conf = $cb->configure_object;
789 my $args; my $mods; my $opts;
790 { local $Params::Check::ALLOW_UNKNOWN = 1;
793 modules => { required => 1, store => \$mods },
794 options => { default => { }, store => \$opts },
797 $args = check( $tmpl, \%hash ) or return;
801 for my $mod (@$mods) {
802 push @list, sort { $a->version <=> $b->version }
803 grep { defined } $mod->distributions( %$opts );
806 my @rv = sort { $a->module cmp $b->module } @list;
808 $self->cache([undef,@rv]);
809 $self->__display_results;
814 sub _reload_indices {
816 my $cb = $self->backend;
820 { local $Params::Check::ALLOW_UNKNOWN = 1;
823 options => { default => { }, store => \$opts },
826 $args = check( $tmpl, \%hash ) or return;
829 my $rv = $cb->reload_indices( %$opts );
831 ### so the update failed, but you didnt give it any options either
832 if( !$rv and !(keys %$opts) ) {
834 "\nFailure may be due to corrupt source files\n" .
835 "Try this:\n\tx --update_source\n\n" );
844 my $cb = $self->backend;
845 my $conf = $cb->configure_object;
848 my $args; my $mods; my $opts; my $choice;
849 { local $Params::Check::ALLOW_UNKNOWN = 1;
852 modules => { required => 1, store => \$mods },
853 options => { default => { }, store => \$opts },
854 choice => { required => 1, store => \$choice,
855 allow => [qw|i t|] },
858 $args = check( $tmpl, \%hash ) or return;
861 unless( scalar @$mods ) {
862 $self->__print( loc("Nothing done\n") );
866 my $target = $choice eq 'i' ? TARGET_INSTALL : TARGET_CREATE;
867 my $prompt = $choice eq 'i' ? loc('Installing ') : loc('Testing ');
868 my $action = $choice eq 'i' ? 'install' : 'test';
871 ### first loop over the mods to install them ###
872 for my $mod (@$mods) {
873 $self->__print( $prompt, $mod->module, " (".$mod->version.")", "\n" );
875 my $log_length = length CPANPLUS::Error->stack_as_string;
877 ### store the status for look up when we're done with all
879 $status->{$mod} = $mod->install( %$opts, target => $target );
881 ### would you like a log file of what happened?
882 if( $conf->get_conf('write_install_logs') ) {
884 my $dir = File::Spec->catdir(
885 $conf->get_conf('base'),
886 $conf->_get_build('install_log_dir'),
888 ### create the dir if it doesn't exit yet
889 $cb->_mkdir( dir => $dir ) unless -d $dir;
891 my $file = File::Spec->catfile(
893 INSTALL_LOG_FILE->( $mod )
895 if ( open my $fh, ">$file" ) {
896 my $stack = CPANPLUS::Error->stack_as_string;
897 ### remove everything in the log that was there *before*
898 ### we started this install
899 substr( $stack, 0, $log_length, '' );
905 loc("*** Install log written to:\n %1\n\n", $file)
908 warn "Could not open '$file': $!\n";
915 ### then report whether all this went ok or not ###
916 for my $mod (@$mods) {
917 # if( $mod->status->installed ) {
918 if( $status->{$mod} ) {
920 loc("Module '%1' %tense(%2,past) successfully\n",
921 $mod->module, $action)
926 loc("Error %tense(%1,present) '%2'\n", $action, $mod->module)
935 loc("No errors %tense(%1,present) all modules", $action), "\n"
939 loc("Problem %tense(%1,present) one or more modules", $action)
941 $self->__print( "\n" );
944 loc("*** You can view the complete error buffer by pressing ".
946 ) unless $conf->get_conf('verbose') || $self->noninteractive;
948 $self->__print( "\n" );
953 sub __ask_about_install {
954 my $mod = shift or return;
955 my $prereq = shift or return;
956 my $term = $Shell->term;
958 $Shell->__print( "\n" );
959 $Shell->__print( loc("Module '%1' requires '%2' to be installed",
960 $mod->module, $prereq->module ) );
961 $Shell->__print( "\n\n" );
963 loc( "If you don't wish to see this question anymore\n".
964 "you can disable it by entering the following ".
965 "commands on the prompt:\n '%1'",
966 's conf prereqs 1; s save' ) );
967 $Shell->__print("\n\n");
969 my $bool = $term->ask_yn(
970 prompt => loc("Should I install this module?"),
977 sub __ask_about_send_test_report {
978 my($mod, $grade) = @_;
979 return 1 unless $grade eq GRADE_FAIL;
981 my $term = $Shell->term;
983 $Shell->__print( "\n" );
985 loc("Test report prepared for module '%1'.\n Would you like to ".
986 "send it? (You can edit it if you like)", $mod->module ) );
987 $Shell->__print( "\n\n" );
988 my $bool = $term->ask_yn(
989 prompt => loc("Would you like to send the test report?"),
996 sub __ask_about_edit_test_report {
997 my($mod, $grade) = @_;
998 return 0 unless $grade eq GRADE_FAIL;
1000 my $term = $Shell->term;
1002 $Shell->__print( "\n" );
1004 loc("Test report prepared for module '%1'. You can edit this ".
1005 "report if you would like", $mod->module ) );
1006 $Shell->__print("\n\n");
1007 my $bool = $term->ask_yn(
1008 prompt => loc("Would you like to edit the test report?"),
1015 sub __ask_about_test_failure {
1017 my $captured = shift || '';
1018 my $term = $Shell->term;
1020 $Shell->__print( "\n" );
1022 loc( "The tests for '%1' failed. Would you like me to proceed ".
1023 "anyway or should we abort?", $mod->module ) );
1024 $Shell->__print( "\n\n" );
1026 my $bool = $term->ask_yn(
1027 prompt => loc("Proceed anyway?"),
1037 my $cb = $self->backend;
1038 my $conf = $cb->configure_object;
1041 my $args; my $mods; my $opts;
1042 { local $Params::Check::ALLOW_UNKNOWN = 1;
1045 modules => { required => 1, store => \$mods },
1046 options => { default => { }, store => \$opts },
1049 $args = check( $tmpl, \%hash ) or return;
1052 ### every module has about 10 lines of details
1053 ### maybe more later with Module::CPANTS etc
1054 $self->_pager_open if scalar @$mods * 10 > $self->_term_rowcount;
1057 my $format = "%-30s %-30s\n";
1058 for my $mod (@$mods) {
1059 my $href = $mod->details( %$opts );
1060 my @list = sort { $a->module cmp $b->module } $mod->contains;
1064 loc("No details for %1 - it might be outdated.",
1065 $mod->module), "\n" );
1069 $self->__print( loc( "Details for '%1'\n", $mod->module ) );
1070 for my $item ( sort keys %$href ) {
1071 $self->__printf( $format, $item, $href->{$item} );
1075 for my $item ( @list ) {
1077 $format, ($showed ? '' : 'Contains:'), $item->module
1081 $self->__print( "\n" );
1084 $self->_pager_close;
1085 $self->__print( "\n" );
1094 my $args; my $opts; my $file;
1095 { local $Params::Check::ALLOW_UNKNOWN = 1;
1098 options => { default => { }, store => \$opts },
1099 input => { default => '', store => \$file },
1102 $args = check( $tmpl, \%hash ) or return;
1107 $fh = FileHandle->new( ">$file" )
1108 or( warn loc("Could not open '%1': '%2'", $file, $!),
1115 $self->_pager_open if !$file;
1117 $self->__print( CPANPLUS::Error->stack_as_string );
1119 $self->_pager_close;
1121 select $old if $old;
1122 $self->__print( "\n" );
1130 my $cb = $self->backend;
1131 my $conf = $cb->configure_object;
1133 ### possible options
1134 ### XXX hard coded, not optimal :(
1137 save => q([user | system | boxed]),
1139 program => q([key => val]),
1140 conf => q([key => val]),
1142 selfupdate => '', # XXX add all opts here?
1146 my $args; my $opts; my $input;
1147 { local $Params::Check::ALLOW_UNKNOWN = 1;
1150 options => { default => { }, store => \$opts },
1151 input => { default => '', store => \$input },
1154 $args = check( $tmpl, \%hash ) or return;
1157 my ($type,$key,$value) = $input =~ m/(\w+)\s*(\w*)\s*(.*?)\s*$/;
1160 if( $type eq 'reconfigure' ) {
1161 my $setup = CPANPLUS::Configure::Setup->new(
1162 configure_object => $conf,
1163 term => $self->term,
1166 return $setup->init;
1168 } elsif ( $type eq 'save' ) {
1170 user => CONFIG_USER,
1171 system => CONFIG_SYSTEM,
1172 boxed => CONFIG_BOXED,
1173 }->{ $key } || CONFIG_USER;
1175 ### boxed is special, so let's get it's value from %INC
1176 ### so we can tell it where to save
1177 ### XXX perhaps this logic should be generic for all
1178 ### types, and put in the ->save() routine
1180 if( $where eq CONFIG_BOXED ) {
1181 my $file = join( '/', split( '::', CONFIG_BOXED ) ) . '.pm';
1182 my $file_re = quotemeta($file);
1184 my $path = $INC{$file} || '';
1185 $path =~ s/$file_re$//;
1189 my $rv = $cb->configure_object->save( $where => $dir );
1193 ? loc("Configuration successfully saved to %1\n (%2)\n",
1195 : loc("Failed to save configuration\n" )
1199 } elsif ( $type eq 'edit' ) {
1201 my $editor = $conf->get_program('editor')
1202 or( print(loc("No editor specified")), return );
1205 user => CONFIG_USER,
1206 system => CONFIG_SYSTEM,
1207 }->{ $key } || CONFIG_USER;
1209 my $file = $conf->_config_pm_to_file( $where );
1210 system("$editor $file");
1213 ### disable warnings for this
1214 { require Module::Loaded;
1215 Module::Loaded::mark_as_unloaded( $_ ) for $conf->configs;
1217 ### reinitialize the config
1224 } elsif ( $type eq 'mirrors' ) {
1227 loc("Readonly list of mirrors (in order of preference):\n\n" ) );
1230 for my $host ( @{$conf->get_conf('hosts')} ) {
1231 my $uri = $cb->_host_to_uri( %$host );
1234 $self->__print( "\t[$i] $uri\n" );
1237 } elsif ( $type eq 'selfupdate' ) {
1238 my %valid = map { $_ => $_ }
1239 $cb->selfupdate_object->list_categories;
1241 unless( $valid{$key} ) {
1243 loc( "To update your current CPANPLUS installation, ".
1244 "choose one of the these options:\n%1",
1246 sprintf "\ts selfupdate %-17s " .
1247 "[--latest=0] [--dryrun]", $_
1248 } sort keys %valid )
1259 my %list = $cb->selfupdate_object
1260 ->list_modules_to_update( %update_args );
1262 $self->__print(loc("The following updates will take place:"),$/.$/);
1264 for my $feature ( sort keys %list ) {
1265 my $aref = $list{$feature};
1267 ### is it a 'feature' or a built in?
1270 ? " " . ucfirst($feature) . ":\n"
1271 : " Modules for '$feature' support:\n"
1274 ### show what modules would be installed
1277 ? map { sprintf " %-42s %-6s -> %-6s \n",
1278 $_->name, $_->installed_version, $_->version
1280 : " No upgrades required\n"
1282 $self->__print( $/ );
1286 unless( $opts->{'dryrun'} ) {
1287 $self->__print( loc("Updating your CPANPLUS installation\n") );
1288 $cb->selfupdate_object->selfupdate( %update_args );
1294 if ( $type eq 'program' or $type eq 'conf' ) {
1298 program => '%-12s %s',
1302 my @list = grep { $_ ne 'hosts' }
1303 $conf->options( type => $type );
1305 my $method = 'get_' . $type;
1307 local $Data::Dumper::Indent = 0;
1308 for my $name ( @list ) {
1309 my $val = $conf->$method($name) || '';
1311 ? (Data::Dumper::Dumper($val) =~ /= (.*);$/)
1314 $self->__printf( " $format\n", $name, $val );
1317 } elsif ( $key eq 'hosts' ) {
1319 loc( "Setting hosts is not trivial.\n" .
1320 "It is suggested you use '%1' and edit the " .
1321 "configuration file manually", 's edit')
1324 my $method = 'set_' . $type;
1325 $conf->$method( $key => defined $value ? $value : '' )
1326 and $self->__print( loc("Key '%1' was set to '%2'", $key,
1327 defined $value ? $value : 'EMPTY STRING') );
1331 $self->__print( loc("Unknown type '%1'",$type || 'EMPTY' ) );
1332 $self->__print( $/ );
1333 $self->__print( loc("Try one of the following:") );
1334 $self->__print( $/, join $/,
1335 map { sprintf "\t%-11s %s", $_, $types{$_} }
1339 $self->__print( "\n" );
1346 my $cb = $self->backend;
1347 my $conf = $cb->configure_object;
1350 { local $Params::Check::ALLOW_UNKNOWN = 1;
1353 options => { default => { }, store => \$opts },
1354 modules => { required => 1, store => \$mods },
1357 check( $tmpl, \%hash ) or return;
1360 ### long listing? short is default ###
1361 my $long = $opts->{'long'} ? 1 : 0;
1363 my @list = scalar @$mods ? @$mods : @{$cb->_all_installed};
1366 for my $mod (@list) {
1367 ### skip this mod if it's up to date ###
1368 next if $mod->is_uptodate;
1369 ### skip this mod if it's core ###
1370 next if $mod->package_is_perl_core;
1372 if( $long or !$seen{$mod->package}++ ) {
1377 @rv = sort { $a->module cmp $b->module } @rv;
1379 $self->cache([undef,@rv]);
1381 $self->_pager_open if scalar @rv >= $self->_term_rowcount;
1383 my $format = "%5s %12s %12s %-36s %-10s\n";
1386 for my $mod ( @rv ) {
1390 $self->_format_version($mod->installed_version) || 'Unparsable',
1391 $self->_format_version( $mod->version ),
1393 $mod->author->cpanid
1397 $self->_pager_close;
1405 my $cb = $self->backend;
1406 my $conf = $cb->configure_object;
1408 my $opts; my $input;
1409 { local $Params::Check::ALLOW_UNKNOWN = 1;
1412 options => { default => { }, store => \$opts },
1413 input => { default => '', store => \$input },
1416 check( $tmpl, \%hash ) or return;
1419 $opts->{'path'} = $input if $input;
1421 my $where = $cb->autobundle( %$opts );
1425 ? loc("Wrote autobundle to '%1'", $where)
1426 : loc("Could not create autobundle" )
1428 $self->__print( "\n" );
1430 return $where ? 1 : 0;
1436 my $cb = $self->backend;
1437 my $term = $self->term;
1438 my $conf = $cb->configure_object;
1441 { local $Params::Check::ALLOW_UNKNOWN = 1;
1444 options => { default => { }, store => \$opts },
1445 modules => { default => [], store => \$mods },
1448 check( $tmpl, \%hash ) or return;
1451 my $force = $opts->{'force'} || $conf->get_conf('force');
1454 my $list = join "\n", map { ' ' . $_->module } @$mods;
1456 $self->__print( loc("
1457 This will uninstall the following modules:
1460 Note that if you installed them via a package manager, you probably
1461 should use the same package manager to uninstall them
1465 return unless $term->ask_yn(
1466 prompt => loc("Are you sure you want to continue?"),
1471 ### first loop over all the modules to uninstall them ###
1472 for my $mod (@$mods) {
1473 $self->__print( loc("Uninstalling '%1'", $mod->module), "\n" );
1475 $mod->uninstall( %$opts );
1479 ### then report whether all this went ok or not ###
1480 for my $mod (@$mods) {
1481 if( $mod->status->uninstall ) {
1483 loc("Module '%1' %tense(uninstall,past) successfully\n",
1488 loc("Error %tense(uninstall,present) '%1'\n", $mod->module) );
1494 loc("All modules %tense(uninstall,past) successfully"), "\n" );
1497 loc("Problem %tense(uninstalling,present) one or more modules" ),
1501 loc("*** You can view the complete error buffer by pressing '%1'".
1502 "***\n", 'p') ) unless $conf->get_conf('verbose');
1504 $self->__print( "\n" );
1512 my $cb = $self->backend;
1513 my $term = $self->term;
1514 my $conf = $cb->configure_object;
1517 { local $Params::Check::ALLOW_UNKNOWN = 1;
1520 options => { default => { }, store => \$opts },
1521 modules => { default => '', store => \$mods },
1524 check( $tmpl, \%hash ) or return;
1527 ### XXX might need to be conditional ###
1530 for my $mod (@$mods) {
1531 my @list = $mod->fetch_report( %$opts )
1532 or( print(loc("No reports available for this distribution.")),
1538 sort { $a->[1] cmp $b->[1] }
1539 map { [$_, $_->{'dist'}.':'.$_->{'platform'}] } @list;
1543 ### XXX this may need to be sorted better somehow ###
1545 my $format = "%8s %s %s\n";
1548 for my $href (@list ) {
1550 "[" . $mod->author->cpanid .'/'. $href->{'dist'} . "]\n"
1551 ) unless $seen{ $href->{'dist'} }++;
1556 $href->{'platform'},
1557 ($href->{'details'} ? '(*)' : '')
1560 $url ||= $href->{'details'};
1563 $self->__print( "\n==> $url\n" ) if $url;
1564 $self->__print( "\n" );
1566 $self->_pager_close;
1573 { my @PluginModules;
1575 showtip => [ __PACKAGE__, '_show_random_tip'],
1576 plugins => [ __PACKAGE__, '_list_plugins' ],
1577 '?' => [ __PACKAGE__, '_plugins_usage' ],
1580 sub plugin_modules { return @PluginModules }
1581 sub plugin_table { return %Dispatch }
1585 ### only initialize once
1586 return if $init_done++;
1588 ### find all plugins first
1589 if( check_install( module => 'Module::Pluggable', version => '2.4') ) {
1590 require Module::Pluggable;
1592 my $only_re = __PACKAGE__ . '::Plugins::\w+$';
1594 Module::Pluggable->import(
1595 sub_name => '_plugins',
1596 search_path => __PACKAGE__,
1597 only => qr/$only_re/,
1598 #except => [ INSTALLER_MM, INSTALLER_SAMPLE ]
1601 push @PluginModules, __PACKAGE__->_plugins;
1604 ### now try to load them
1605 for my $p ( __PACKAGE__->plugin_modules ) {
1606 my %map = eval { load $p; $p->import; $p->plugins };
1607 error(loc("Could not load plugin '$p': $@")), next if $@;
1609 ### register each plugin
1610 while( my($name, $func) = each %map ) {
1612 if( not length $name or not length $func ) {
1613 error(loc("Empty plugin name or dispatch function detected"));
1617 if( exists( $Dispatch{$name} ) ) {
1618 error(loc("'%1' is already registered by '%2'",
1619 $name, $Dispatch{$name}->[0]));
1623 ### register name, package and function
1624 $Dispatch{$name} = [ $p, $func ];
1629 ### dispatch a plugin command to it's function
1633 my $cb = $self->backend;
1634 my $term = $self->term;
1635 my $conf = $cb->configure_object;
1637 my $opts; my $input;
1638 { local $Params::Check::ALLOW_UNKNOWN = 1;
1641 options => { default => { }, store => \$opts },
1642 input => { default => '', store => \$input },
1645 check( $tmpl, \%hash ) or return;
1648 $input =~ s/\s*(\S+)\s*//;
1651 ### look up the command, or go to the default
1652 my $aref = $Dispatch{ $cmd } || [ __PACKAGE__, '_plugin_default' ];
1654 my($pkg,$func) = @$aref;
1656 my $rv = eval { $pkg->$func( $self, $cb, $cmd, $input, $opts ) };
1660 ### return $rv instead, so input loop can be terminated?
1664 sub _plugin_default { error(loc("No such plugin command")) }
1668 { my $help_format = " /%-21s # %s\n";
1673 $self->__print( loc("Available plugins:\n") );
1674 $self->__print( loc(" List usage by using: /? PLUGIN_NAME\n" ) );
1675 $self->__print( $/ );
1677 my %table = __PACKAGE__->plugin_table;
1678 for my $name( sort keys %table ) {
1679 my $pkg = $table{$name}->[0];
1680 my $this = __PACKAGE__;
1682 my $who = $pkg eq $this
1684 : do { $pkg =~ s/^$this/../; "Provided by: $pkg" };
1686 $self->__printf( $help_format, $name, $who );
1689 $self->__print( $/.$/ );
1692 " Write your own plugins? Read the documentation of:\n" .
1693 " CPANPLUS::Shell::Default::Plugins::HOWTO\n" );
1695 $self->__print( $/ );
1698 sub _list_plugins_help {
1699 return sprintf $help_format, 'plugins', loc("lists available plugins");
1702 ### registered as a plugin too
1703 sub _show_random_tip_help {
1704 return sprintf $help_format, 'showtip', loc("show usage tips" );
1707 sub _plugins_usage {
1713 my %table = $self->plugin_table;
1715 my @list = length $input ? split /\s+/, $input : sort keys %table;
1717 for my $name( @list ) {
1719 ### no such plugin? skip
1720 error(loc("No such plugin '$name'")), next unless $table{$name};
1722 my $pkg = $table{$name}->[0];
1723 my $func = $table{$name}->[1] . '_help';
1725 if ( my $sub = $pkg->can( $func ) ) {
1726 eval { $self->__print( $sub->() ) };
1730 $self->__print(" No usage for '$name' -- try perldoc $pkg");
1733 $self->__print( $/ );
1736 $self->__print( $/.$/ );
1739 sub _plugins_usage_help {
1740 return sprintf $help_format, '? [NAME ...]',
1741 loc("show usage for plugins");
1745 ### send a command to a remote host, retrieve the answer;
1746 sub __send_remote_command {
1749 my $remote = $self->remote or return;
1750 my $user = $remote->{'username'};
1751 my $pass = $remote->{'password'};
1752 my $conn = $remote->{'connection'};
1753 my $end = "\015\012";
1756 my $send = join "\0", $user, $pass, $cmd;
1758 print $conn $send . $end;
1760 ### XXX why doesn't something like this just work?
1761 #1 while recv($conn, $answer, 1024, 0);
1764 $conn->recv( $buff, 1024, 0 );
1766 last if $buff =~ /$end$/;
1769 my($status,$buffer) = split "\0", $answer;
1771 return ($status, $buffer);
1775 sub _read_configuration_from_rc {
1777 my $rc_file = shift;
1780 if( can_load( modules => { 'Config::Auto' => '0.0' } ) ) {
1781 $Config::Auto::DisablePerl = 1;
1783 eval { $href = Config::Auto::parse( $rc_file, format => 'space' ) };
1786 loc( "Unable to read in config file '%1': %2", $rc_file, $@ )
1794 loc( "You can update CPANPLUS by running: '%1'", 's selfupdate' ),
1795 loc( "You can install modules by URL using '%1'", 'i URL' ),
1796 loc( "You can turn off these tips using '%1'",
1797 's conf show_startup_tip 0' ),
1798 loc( "You can use wildcards like '%1' and '%2' on search results",
1800 loc( "You can use plugins. Type '%1' to list available plugins",
1802 loc( "You can show all your out of date modules using '%1'", 'o' ),
1803 loc( "Many operations take options, like '%1', '%2' or '%3'",
1804 '--verbose', '--force', '--skiptest' ),
1805 loc( "The documentation in %1 and %2 is very useful",
1806 "CPANPLUS::Module", "CPANPLUS::Backend" ),
1807 loc( "You can type '%1' for help and '%2' to exit", 'h', 'q' ),
1808 loc( "You can run an interactive setup using '%1'", 's reconfigure' ),
1809 loc( "You can add custom sources to your index. See '%1' for details",
1813 sub _show_random_tip {
1815 $self->__print( $/, "Did you know...\n ",
1816 $tips[ int rand scalar @tips ], $/ );
1829 Please report bugs or other issues to E<lt>bug-cpanplus@rt.cpan.org<gt>.
1833 This module by Jos Boumans E<lt>kane@cpan.orgE<gt>.
1837 The CPAN++ interface (of which this module is a part of) is copyright (c)
1838 2001 - 2007, Jos Boumans E<lt>kane@cpan.orgE<gt>. All rights reserved.
1840 This library is free software; you may redistribute and/or modify it
1841 under the same terms as Perl itself.
1845 L<CPANPLUS::Shell::Classic>, L<CPANPLUS::Shell>, L<cpanp>
1850 # c-indentation-style: bsd
1852 # indent-tabs-mode: nil
1854 # vim: expandtab shiftwidth=4:
1859 e => "_expand_inc", # scratch it, imho -- not used enough
1861 ### free letters: g j k n y ###