File Coverage

blib/lib/CPANPLUS/Shell/Default/Plugins/Bundler.pm
Criterion Covered Total %
statement 6 58 10.3
branch 0 26 0.0
condition 0 9 0.0
subroutine 2 9 22.2
pod 0 3 0.0
total 8 105 7.6


line stmt bran cond sub pod time code
1             package CPANPLUS::Shell::Default::Plugins::Bundler;
2 1     1   2403 use CPAN::Version;
  1         2521  
  1         32  
3 1     1   8 use strict;
  1         1  
  1         1222  
4             our %handlers;
5              
6             ### return command => method mapping
7 0     0 0   sub plugins { ( bundle => 'bnd' ) }
8              
9             ### method called when the command '/myplugin1' is issued
10             sub bnd {
11              
12             # /helloworld bob --nofoo --bar=2 joe
13            
14 0     0 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 $handler_id;
22 0 0         unless ($input){
23 0           $handler_id = 'install::dry-run';
24             }else{
25 0           s/\s//g for $input;
26 0 0         my $mode = $opts->{'dry-run'} ? 'dry-run' : 'real';
27 0           $handler_id = $input.'::'.$mode;
28             }
29 0 0         if (exists $handlers{$handler_id}){
30 0           print "fire handler : $handler_id \n";
31 0   0       _bundle_file_itterator(
32             $opts->{'bundle_file'} || "$ENV{PWD}/.bundle",
33             $handlers{$handler_id},
34             $cb
35             );
36             }else{
37 0           print "handler [$handler_id] not found \n";
38             }
39            
40 0           return;
41             }
42              
43              
44             sub _mod_is_uptodate {
45 0     0     my $m = shift;
46 0 0         (CPAN::Version->vcmp($m->package_version,$m->installed_version)>=0) ? 0 : 1
47             }
48              
49             sub _mod_need_upgrade {
50 0     0     my $m = shift;
51 0           my $required_version = shift;
52 0           my $st;
53 0 0 0       if ((defined $required_version) && defined $m->installed_version) {
54 0           $st = CPAN::Version->vgt($required_version,$m->installed_version)
55             }else{
56 0 0         $st = (defined $m->installed_version) ? 0 : 1;
57             }
58 0           return $st;
59             }
60              
61             sub _parse_module_item_line {
62 0     0     my $line = shift;
63 0           my $cb = shift;
64 0           my ($mod_name,$v) = split /\s+/, $line;
65 0           s/\s//g for ($mod_name,$v);
66 0           my $m_obj = $cb->parse_module(module => $mod_name);
67 0 0         if ($m_obj){
68 0 0 0       $v || $m_obj->package_version unless defined $v;
69             }
70 0           return ($mod_name,$v,$m_obj);
71             }
72              
73              
74              
75             $handlers{'install::dry-run'} = sub {
76             my $line = shift;
77             my $cb = shift;
78             my ($mod_name,$v,$m) = _parse_module_item_line($line,$cb);
79             my $info; my $status;
80             if ($m){
81             if ($m->installed_version){
82             if (_mod_is_uptodate($m)){
83             $info = 'is uptodate';
84             $status = 'SKIP';
85             }elsif(_mod_need_upgrade($m,$v)){
86             $info = "UPDATE from version ".($m->installed_version)." to version : ".($m->package_version);
87             $status = 'OK';
88             }else{
89             $info = "KEEP current version installed version ".($m->installed_version).' is higher or equal than required - '.$v;
90             $status = 'SKIP';
91             }
92             }else{
93             $info = "INSTALL at version : ".($m->package_version);
94             $status = 'OK';
95             }
96             }else{
97             $status = 'FAIL';
98             $info = "[$mod_name] - not found!";
99             }
100             print "[$status] - [$line] - $info \n";
101             };
102              
103             $handlers{'install::real'} = sub {
104             my $line = shift;
105             my $cb = shift;
106             my ($mod_name,$v,$m) = _parse_module_item_line($line,$cb);
107             if ($m){
108             if ((! _mod_is_uptodate($m)) && _mod_need_upgrade($m,$v)){
109             $cb->install(modules=>[$mod_name]);
110             }
111             }else{
112             print "[FAIL] - [$line] - $mod_name not found! \n";
113             }
114              
115             };
116              
117             $handlers{'remove::dry-run'} = sub {
118             my $line = shift;
119             my $cb = shift;
120             my ($mod_name,$v,$m) = _parse_module_item_line($line,$cb);
121             if ($m){
122             if ($m->installed_version){
123             print "[OK] - [$line] - remove $mod_name \n";
124             }else{
125             print "[SKIP] - [$line] - $mod_name is not installed \n";
126             }
127             }else{
128             print "[FAIL] - [$line] - $mod_name not found! \n";
129             }
130             };
131              
132             $handlers{'remove::real'} = sub {
133             my $line = shift;
134             my $cb = shift;
135             my ($mod_name,$v,$m) = _parse_module_item_line($line,$cb);
136             if ($m){
137             $m->uninstall();
138             }else{
139             print "[FAIL] - [$line] - $mod_name not found! \n";
140             }
141              
142             };
143              
144              
145             ### method called when the command '/? myplugin1' is issued
146             sub bnd_help {
147              
148 0     0 0   return <
149              
150             # Install all packages form .bundle file in current directory
151             # or from file chosen by --bundle-file option.
152             # See Bundler for details.
153              
154             /bundle [install|remove] [--bundle_file ] [--dry-run]
155              
156             MESSAGE
157              
158             }
159              
160              
161             sub _bundle_file_itterator {
162              
163 0     0     my $bundle_file = shift;
164 0           my $handler = shift;
165 0           my $cb = shift;
166              
167 0 0         if (-f $bundle_file){
168 0           print "found bundle file [$bundle_file] \n";
169 0 0         if (open BUNDLE_F, $bundle_file){
170 0           while (my $line = ){
171 0           chomp $line;
172 0 0         next if $line=~/^#\s/;
173 0 0         next if $line=~/^#/;
174 0           s/(.*?)#.*/$1/ for $line; # cutoff comments chunks
175 0 0         next unless $line=~/\S/;
176 0           $handler->($line,$cb);
177             }
178 0           close BUNDLE_F;
179             }else{
180 0           print "error: cannot open .bundle file [$bundle_file]: $!\n";
181             }
182             }else{
183 0           print "error: .bundle file [$bundle_file] not found\n";
184             }
185              
186             }
187              
188             1;
189