| 1 | package CPANPLUS::Shell::Default::Plugins::Repo; | ||||||||||||
| 2 | 1 | our $VERSION = '0.0.3'; | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | our %handlers; | |||||||||||
| 5 | |||||||||||||
| 6 | 1 | my $cache_file = "$ENV{HOME}/.cpan-repo"; | |||||||||||
| 7 | |||||||||||||
| 8 | 0 | ▶ | sub plugins { ( repo => 'repo' ) } | ||||||||||
|
Subroutine
plugins: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 9 | |||||||||||||
| 10 | 0 | ▶ | sub repo { | ||||||||||
|
Subroutine
repo: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 11 | |||||||||||||
| 12 | # /helloworld bob --nofoo --bar=2 joe | ||||||||||||
| 13 | |||||||||||||
| 14 | 0 | my $class = shift; # CPANPLUS::Shell::Default::Plugins::HW | |||||||||||
| 15 | 0 | my $shell = shift; # CPANPLUS::Shell::Default object | |||||||||||
| 16 | 0 | my $cb = shift; # CPANPLUS::Backend object | |||||||||||
| 17 | 0 | my $cmd = shift; # 'helloworld' | |||||||||||
| 18 | 0 | my $input = shift; # 'bob joe' | |||||||||||
| 19 | 0 | my $opts = shift; # { foo => 0, bar => 2 } | |||||||||||
| 20 | |||||||||||||
| 21 | 0 | my ($action,@params) = split /\s+/, $input; | |||||||||||
| 22 | |||||||||||||
| 23 | 0 | ▶ | $action ||= 'list'; | ||||||||||
|
Condition0%0/2
$action ||= "list"
MC/DC0%0/1
$action ||= "list"
$action
Truth table0%0/2
A
A= $action
| |||||||||||||
| 24 | |||||||||||||
| 25 | 0 | s/\s//g for ($action,@params); | |||||||||||
| 26 | |||||||||||||
| 27 | 0 | my $handler_id = $action; | |||||||||||
| 28 | 0 | ▶ | if (exists $handlers{$handler_id}){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 29 | 0 | print "fire handler : $handler_id \n"; | |||||||||||
| 30 | 0 | $handlers{$handler_id}->($cb,@params), | |||||||||||
| 31 | }else{ | ||||||||||||
| 32 | 0 | print "handler [$handler_id] not found \n"; | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 0 | return; | |||||||||||
| 36 | } | ||||||||||||
| 37 | |||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | $handlers{'list'} = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 41 | 0 | my $cb = shift; | |||||||||||
| 42 | 0 | ▶ | if(my $repo_server = load_repo_server_from_file()){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 43 | 0 | print "[fetching repos from $repo_server]\n"; | |||||||||||
| 44 | 0 | system ("curl -s $repo_server/list/"); | |||||||||||
| 45 | 0 | print "\n"; | |||||||||||
| 46 | }else{ | ||||||||||||
| 47 | 0 | print "CPAN::Repo server not set; use /repo server <url> to setup it\n" | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | |||||||||||||
| 51 | |||||||||||||
| 52 | 1 | }; | |||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | $handlers{'set'} = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 55 | 0 | my $cb = shift; | |||||||||||
| 56 | 0 | my @repos = @_; | |||||||||||
| 57 | 0 | commit_repos_to_cs($cb,@repos); | |||||||||||
| 58 | 0 | return; | |||||||||||
| 59 | 1 | }; | |||||||||||
| 60 | |||||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | $handlers{'server'} = sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 63 | 0 | my $cb = shift; | |||||||||||
| 64 | 0 | my $uri = shift; | |||||||||||
| 65 | 0 | save_repo_server_to_file($uri); | |||||||||||
| 66 | 0 | return; | |||||||||||
| 67 | 1 | }; | |||||||||||
| 68 | |||||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | sub repo_help { | ||||||||||
|
Subroutine
repo_help: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 71 | |||||||||||||
| 72 | 0 | return <<MESSAGE; | |||||||||||
| 73 | |||||||||||||
| 74 | # Provides interface to CPAN::Repo server. | ||||||||||||
| 75 | # See http://search.cpan.org/perldoc?CPAN::Repo for details. | ||||||||||||
| 76 | |||||||||||||
| 77 | /? repo | ||||||||||||
| 78 | /repo server <url> # setup CPAN::Repo server | ||||||||||||
| 79 | /repo list # list available repos from CPAN::Repo server | ||||||||||||
| 80 | /repo set <repo-id> <repo_id> ... # setup repos and save it as custom sources | ||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | MESSAGE | ||||||||||||
| 84 | |||||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | 0 | ▶ | sub load_repo_server_from_file { | ||||||||||
|
Subroutine
load_repo_server_from_file: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 88 | |||||||||||||
| 89 | 0 | `touch $cache_file`; | |||||||||||
| 90 | 0 | my $uri; | |||||||||||
| 91 | 0 | print "load $cache_file ...\n"; | |||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | if(open F, $cache_file){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | ▶ | while (my $l = <F>){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | chomp $l; s/\s//g for $l; | |||||||||||
| 96 | 0 | ▶ | $l=~/\S+/ or next; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 0 | $uri = $l; | |||||||||||
| 98 | 0 | last; | |||||||||||
| 99 | } | ||||||||||||
| 100 | 0 | close F; | |||||||||||
| 101 | }else{ | ||||||||||||
| 102 | 0 | print "error : cannot open file [$cache_file] : $!\n"; | |||||||||||
| 103 | } | ||||||||||||
| 104 | 0 | return $uri; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | 0 | ▶ | sub save_repo_server_to_file { | ||||||||||
|
Subroutine
save_repo_server_to_file: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 108 | 0 | my $uri = shift; | |||||||||||
| 109 | 0 | `touch $cache_file`; | |||||||||||
| 110 | 0 | ▶ | if(open F, '>', $cache_file){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 111 | 0 | print F $uri, "\n"; close F; | |||||||||||
| 112 | 0 | print "save $uri to $cache_file\n"; | |||||||||||
| 113 | }else{ | ||||||||||||
| 114 | 0 | print "error : cannot open repo file [$cache_file] to write : $!\n"; | |||||||||||
| 115 | } | ||||||||||||
| 116 | 0 | return; | |||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | ▶ | sub commit_repos_to_cs { | ||||||||||
|
Subroutine
commit_repos_to_cs: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 120 | |||||||||||||
| 121 | 0 | my $cb = shift; | |||||||||||
| 122 | 0 | my @repos = @_; | |||||||||||
| 123 | |||||||||||||
| 124 | 0 | print "commit repos to custom source file ...\n"; | |||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | if (@repos){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 127 | 0 | my $uri = load_repo_server_from_file(); | |||||||||||
| 128 | 0 | my %cs = $cb->list_custom_sources; | |||||||||||
| 129 | 0 | for my $cs (values %cs){ | |||||||||||
| 130 | 0 | $cb->remove_custom_source( uri => $cs, verbose => 1 ); | |||||||||||
| 131 | } | ||||||||||||
| 132 | 0 | ▶ | $uri = "http://" unless $uri=~/^http:\/\//; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 0 | $uri.='/'.(join '/', @repos); | |||||||||||
| 134 | 0 | $cb->add_custom_source( uri => $uri, verbose => 1 ); | |||||||||||
| 135 | 0 | $cb->update_custom_source(); | |||||||||||
| 136 | }else{ | ||||||||||||
| 137 | 0 | print "warn : repos list is empty, nothing to do with it\n"; | |||||||||||
| 138 | } | ||||||||||||
| 139 | 0 | return; | |||||||||||
| 140 | } | ||||||||||||
| 141 | |||||||||||||
| 142 | |||||||||||||
| 143 | 1 | 1; | |||||||||||
| 144 | |||||||||||||
| 145 | __END__ | ||||||||||||