File Coverage

blib/lib/App/Dependencio.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package App::Dependencio;
2 1     1   12849 use base qw(App::Cmd::Simple);
  1         1  
  1         400  
3 1     1   27177 use strict;
  1         1  
  1         14  
4 1     1   2 use warnings;
  1         4  
  1         15  
5 1     1   3 use File::Find;
  1         1  
  1         36  
6 1     1   3 use Cwd;
  1         0  
  1         35  
7 1     1   182 use Data::Printer;
  0            
  0            
8             use Term::ANSIColor;
9             use List::MoreUtils qw(uniq);
10             use Module::Extract::Use;
11              
12             our $VERSION = '0.08';
13             my @mods_list = ();
14             my @mods_not_found = ();
15              
16             sub opt_spec {
17             return (
18             [ "testdirs|t", "Exclude dir named t (tests)" ],
19             [ "verbose|v", "Verbose output"],
20             [ "cpanm|c", "Automatic cpanm install missing modules"],
21             # [ "cpanfile|f", "outputs a list of modules to a cpanfile file"], #TODO
22             [ "help|h", "This help menu. (i am dependencio version $VERSION)"],
23             );
24             }
25              
26              
27              
28             sub validate_args {
29             my ($self, $opt, $args) = @_;
30             $self->usage_error("Bad command") if @$args;
31             $self->usage if $opt->{help};
32             }
33              
34              
35              
36             sub execute {
37             my ($self, $opt, $args) = @_;
38             our $opts = $opt;
39             $self->checkDeps;
40             }
41              
42              
43              
44             sub checkDeps{
45             my ($self, $opt) = @_;
46             our $cwd = getcwd();
47             my @dirs = ();
48             our $opts;
49             push (@dirs,$cwd);
50              
51             print STDOUT colored ['bright_blue'], "Searching modules dependencies recursively from $cwd \n";
52             find(\&_openFiles, @dirs);
53              
54             #p(@mods_not_found);
55             foreach my $mod_not_found (uniq(@mods_not_found)){
56             print STDOUT colored ['bright_red'], "module $mod_not_found not found\n";
57             system "cpanm $mod_not_found" if $opts->{cpanm};
58              
59             }
60              
61             exit -1 if @mods_not_found or print STDOUT colored ['bright_green'], "success! all dependencies met...\n";
62             }
63              
64              
65              
66             sub _openFiles{
67             our $opts;
68             our $cwd;
69             my $dir = $cwd.'/t';
70             my $tests = 1;
71             if( $dir eq $File::Find::dir and $opts->{testdirs} ){ $tests = 0; };
72              
73             #only open file types to search module declarations (.pm and .pl)
74             if(-f && m/\.(pm|pl)$/ and $tests == 1){
75             print STDOUT "* checking dependecies on $File::Find::name\n" if $opts->{verbose};
76             my $file = $File::Find::name;
77              
78             my $extractor = Module::Extract::Use->new;
79             @mods_list = $extractor->get_modules($file);
80              
81             foreach my $module (@mods_list) {
82             if($module =~ /\p{Uppercase}/){ #do not eval things like "warnings","strict",etc (at least one uppercase)
83             my $path = $module. ".pm";
84             $path =~ s{::}{/}g;
85             eval {require $path } or
86             do {
87             my $error = $@;
88             push( @mods_not_found, $module) unless grep{$_ eq $module} @mods_not_found;
89             }
90             }
91              
92             }
93             }
94             }
95             1;
96              
97              
98              
99             __END__