blib/lib/Getopt/Complete/Cache.pm

filter: Statementstmt 37.3% 53 / 142 Branchbran 20.9% 13 / 62 Conditioncond 16.6% 4 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 80.0% 8 / 10 Podpod 0.0% 0 / 2 totaltotal 30.5% 78 / 255 CC 43 SCAR 44.8
CC
43
Cov
31%
generate
155.0
import
76.8
module_and_cache_paths_for_package
72.0
CRAP 88.4
↑ Summary blib/lib/Getopt/Complete/Args.pm »
1 package Getopt::Complete::Cache;
2  
3 11 our $VERSION = $Getopt::Complete::VERSION;
4  
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 our $cache_path;
9 11 our $cache_is_stale;
10  
11 sub import {
12 11 ▶ my $self = shift;
Subroutine import: called CC=22 SCAR=43.4
13 11 my %args = @_;
14  
15 11 my $class = delete $args{class};
16 11 ▶ my $file = delete $args{file} if exists $args{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"file"} 11 0 11
17 11 ▶ my $above = 0; $above = delete $args{above} if exists $args{above};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"above"} 0 11 11
18 11 ▶ my $dynamic_caching = 0; $dynamic_caching = delete $args{dynamic_caching} if exists $args{dynamic_caching};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"dynamic_caching"} 0 11 11
19 11 ▶ my $comp_line = $ENV{COMP_LINE}; $comp_line = delete $args{comp_line} if exists $args{comp_line};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"comp_line"} 0 11 11
20  
21 11 ▶ if (%args) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) 0 11 11
22 0 require Data::Dumper;
23 0 die "Unknown params passed to " . __PACKAGE__ .
24 Data::Dumper::Dumper(\@_);
25 }
26  
27 11 ▶ if ($class and $file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class and $file) 0 11 11
Condition33%1/3
$class and $file
!ll&&!rl&&r
1100
MC/DC0%0/2
$class and $file
$class $file
Truth table33%1/3
A and B
A= $classB= $file
ABresult
0X0
100
111
28 0 die __PACKAGE__ . " received both a class and file param: $class, $file!";
29 }
30  
31 11 ▶ return unless ($comp_line);
Branch: 1 branch
BranchTrueFalseTotal
unless $comp_line 0 11 11
32  
33 # doesn't detect
34 11 my $module_path;
35  
36 11 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) { } 0 11 11
37 0 ($module_path, $cache_path) = $self->module_and_cache_paths_for_package($class, $above);
38 0 ▶ $cache_path ||= $module_path . '.opts';
Condition0%0/2
$Getopt::Complete::Cache::cache_path ||= $module_path . '.opts'
l!l
00
MC/DC0%0/1
$Getopt::Complete::Cache::cache_path ||= $module_path . '.opts'
$Getopt::Complete::Cache::cache_path
Truth table0%0/2
A
A= $Getopt::Complete::Cache::cache_path
Aresult
11
00
39 }
40 else {
41 11 ▶ use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 11 $module_path = $FindBin::RealBin . '/' . $FindBin::RealScript;
43 11 ▶ $cache_path = $file || $module_path . '.opts';
Condition50%1/2
$file || $module_path . '.opts'
l!l
110
MC/DC0%0/1
$file || $module_path . '.opts'
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
44 }
45
46 # TODO: This does not quite work yet.
47 11 ▶ if ($dynamic_caching && -e $cache_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dynamic_caching and -e $Getopt::Complete::Cache::cache_path) 0 11 11
Condition33%1/3
$dynamic_caching and -e $Getopt::Complete::Cache::cache_path
!ll&&!rl&&r
1100
MC/DC0%0/2
$dynamic_caching and -e $Getopt::Complete::Cache::cache_path
$dynamic_caching -e $Getopt::Complete::Cache::cache_path
Truth table33%1/3
A and B
A= $dynamic_cachingB= -e $Getopt::Complete::Cache::cache_path
ABresult
0X0
100
111
48 0 my $my_mtime = (stat($module_path))[9];
49  
50 # if the module has a directory with changes newer than the module,
51 # use its mtime as the change time
52 0 my $module_dir = $module_path;
53 0 $module_dir =~ s/.pm$//;
54 0 ▶ if (-e $module_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $module_dir) 0 0 0
55 0 ▶ if ((stat($module_dir))[9] > $my_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if ((stat $module_dir)[9] > $my_mtime) 0 0 0
56 0 $my_mtime = (stat($module_dir))[9];
57 }
58 }
59
60 0 my $cache_mtime = (stat($cache_path))[9];
61 0 ▶ unless ($cache_mtime >= $my_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache_mtime >= $my_mtime) 0 0 0
62 0 print STDERR "\nstale completion cache: refreshing $cache_path...\n";
63 0 unlink $cache_path;
64 }
65 }
66
67 11 ▶ $cache_path = $file if ($file);
Branch: 1 branch
BranchTrueFalseTotal
if $file 11 0 11
68
69 11 ▶ if ( -f $cache_path && -s $cache_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $Getopt::Complete::Cache::cache_path and -s $Getopt::Complete::Cache::cache_path) { } 11 0 11
Condition33%1/3
-f $Getopt::Complete::Cache::cache_path and -s $Getopt::Complete::Cache::cache_path
!ll&&!rl&&r
0011
MC/DC0%0/2
-f $Getopt::Complete::Cache::cache_path and -s $Getopt::Complete::Cache::cache_path
-f $Getopt::Complete::Cache::cache_path -s $Getopt::Complete::Cache::cache_path
Truth table33%1/3
A and B
A= -f $Getopt::Complete::Cache::cache_pathB= -s $Getopt::Complete::Cache::cache_path
ABresult
0X0
100
111
70 11 my $fh;
71 11 open($fh, $cache_path);
72 11 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 11 0 11
73 11 my $src = join('', <$fh>);
74 11 require Getopt::Complete;
75 11 my $spec = eval $src;
76 11 ▶ if (@$spec) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$spec) 11 0 11
77 11 Getopt::Complete->import(@$spec);
78 }
79 }
80 0 return 1;
81 }
82 else {
83 0 die "Unable to open file: $cache_path\n";
84 }
85 }
86  
87 sub module_and_cache_paths_for_package {
88 0 ▶ my $self = shift;
Subroutine module_and_cache_paths_for_package: not called CC=8 SCAR=42.8
Pod undocumented
89 0 my $class = shift;
90 0 my $above = shift;
91 0 my $path = $class;
92 0 $path =~ s/::/\//g;
93 0 $path = '/' . $path . '.pm';
94
95 0 my ($mod_path, $opt_path);
96
97 # if above, check cwd upwards for class location
98 0 ▶ if ($above) {
Branch: 1 branch
BranchTrueFalseTotal
if ($above) 0 0 0
99 0 require Cwd;
100 0 $mod_path = Cwd::cwd();
101 0 $mod_path =~ s/([^\/])$/$1\//;
102 0 ▶ until (-e "$mod_path$path" || $mod_path eq '/') {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e "$mod_path$path" or $mod_path eq "/") 0 0 0
Condition0%0/3
-e "$mod_path$path" or $mod_path eq "/"
l!l&&r!l&&!r
000
MC/DC0%0/2
-e "$mod_path$path" or $mod_path eq "/"
-e "$mod_path$path" $mod_path eq "/"
Truth table0%0/3
A or B
A= -e "$mod_path$path"B= $mod_path eq "/"
ABresult
1X1
011
000
103 0 $mod_path =~ s/[^\/]+\/$//; # remove last folder and try again
104 }
105 0 $mod_path .= $path;
106 0 $opt_path = $mod_path . '.opts';
107 }
108
109 # otherwise search perl's path
110 0 ▶ unless ($mod_path && -e "$mod_path") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod_path and -e "$mod_path") 0 0 0
Condition0%0/3
$mod_path and -e "$mod_path"
!ll&&!rl&&r
000
MC/DC0%0/2
$mod_path and -e "$mod_path"
$mod_path -e "$mod_path"
Truth table0%0/3
A and B
A= $mod_pathB= -e "$mod_path"
ABresult
0X0
100
111
111 0 my (@mod_paths) = map { ($_ . $path) } @INC;
112 0 ($mod_path) = grep { -e $_ } @mod_paths;
113 }
114 0 ▶ unless ($opt_path && -e "$opt_path") {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_path and -e "$opt_path") 0 0 0
Condition0%0/3
$opt_path and -e "$opt_path"
!ll&&!rl&&r
000
MC/DC0%0/2
$opt_path and -e "$opt_path"
$opt_path -e "$opt_path"
Truth table0%0/3
A and B
A= $opt_pathB= -e "$opt_path"
ABresult
0X0
100
111
115 0 my (@opt_paths) = map { ($_ . $path . '.opts' ) } @INC;
116 0 ($opt_path) = grep { -e $_ } @opt_paths;
117 }
118  
119 0 return ($mod_path, $opt_path);
120 }
121  
122 sub generate {
123 0 ▶ print STDERR "ending\n";
Subroutine generate: not called CC=15 SCAR=50.4
Pod undocumented
124 0 eval {
125 0 print STDERR "evaling $cache_path\n";
126 0 ▶ unless (-e $cache_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $Getopt::Complete::Cache::cache_path) 0 0 0
127 0 print STDERR "found $cache_path\n";
128 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 0 my $a = $Getopt::Complete::ARGS;
130 0 print STDERR "args are $a\n";
131 11 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 0 ▶ if ($a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a) 0 0 0
133 0 print STDERR ">> got args $a\n";
134 0 ▶ if (my $o = $a->options) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $a->options) 0 0 0
135 0 print STDERR ">> got opts $o\n";
136 0 my $c = $o->{completion_handlers};
137 0 my @modules;
138 0 ▶ if ($c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c) 0 0 0
139 0 print STDERR ">> got completions $c\n";
140 0 my $has_callbacks = 0;
141 0 for my $key (keys %$c) {
142 0 my $completions = $c->{$key};
143 0 ▶ if (ref($completions) eq 'SCALAR') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $completions eq 'SCALAR') { } 0 0 0
elsif (ref $completions eq 'CODE') { } 0 0 0
144 0 push @modules, $$completions;
145 }
146 elsif(ref($completions) eq 'CODE') {
147 0 warn "cannot use cached completions with anonymous callbacks!";
148 0 $has_callbacks = 1;
149 }
150 }
151 0 ▶ unless ($has_callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_callbacks) 0 0 0
152 0 my $fh;
153 0 open($fh,$cache_path);
154 0 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
155 0 warn "caching options for $cache_path...\n";
156 0 my $src = Data::Dumper::Dumper($c);
157 #$src =~ s/^\$VAR1/\$${class}::OPTS_SPEC/;
158 #print STDERR ">> $src\n";
159 0 $fh->print($src);
160 #require Data::Dumper;
161 #my $src = Data::Dumper::Dumper($c);
162 }
163 }
164 0 for my $module (@modules) {
165 0 print STDERR "trying mod $module\n";
166 0 local $ENV{GETOPT_COMPLETE_CACHE} = 1;
167 0 eval "use $module";
168 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
169 11 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
170 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 0 $spec = ${ $class . '::OPTS_SPEC' };
172 0 my ($other_module_path,$other_cache_path) = $self->module_and_cache_paths_for_package($module);
173 0 ▶ $other_cache_path ||= $other_module_path . '.opts';
Condition0%0/2
$other_cache_path ||= $other_module_path . '.opts'
l!l
00
MC/DC0%0/1
$other_cache_path ||= $other_module_path . '.opts'
$other_cache_path
Truth table0%0/2
A
A= $other_cache_path
Aresult
11
00
174 0 my $fh;
175 0 open($fh,$other_cache_path);
176 0 ▶ if ($fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh) 0 0 0
177 0 warn "caching options for $module at $other_cache_path...\n";
178 0 my $src = Data::Dumper::Dumper($c);
179 0 $src =~ s/^\$VAR1/\$${class}::OPTS_SPEC/;
180 #print STDERR ">> $src\n";
181 0 $fh->print($src);
182 #require Data::Dumper;
183 #my $src = Data::Dumper::Dumper($c);
184 }
185 }
186 }
187 }
188 }
189 }
190 };
191 0 ▶ print STDERR ">>>> $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
192 }
193  
194 11 1;
195  
196 =pod
197  
198 =head1 NAME
199  
200 Getopt::Complete::Cache - cache options next-to the command they apply-to
201  
202 =head1 VERSION
203  
204 This document describes Getopt::Complete::Cache 0.25.
205  
206 =head1 SYNOPSIS
207  
208 Presuming MyApp.pm isa Command, and "myapp" is an executable like:
209  
210 use MyApp;
211 MyApp->execute_with_shell_params_and_exit();
212  
213 Add this BEFORE using the MyApp module:
214  
215 use Getopt::Complete::Cache class => 'MyApp';
216 use MyApp;
217 MyApp->execute_with_shell_params_and_exit();
218  
219 Now the shell will look for MyApp.pm.opts during completion
220 and will never actually load the MyApp.pm module during tab-completion.
221  
222 The .opts file is autogenerated upon the first attempt to find it.
223  
224 =head1 DESCRIPTION
225  
226 This module is for the obscure case in which:
227 1. the compile time on an executable is sluggish, and we don't want to have sluggish tab-completion
228 2. the command-line should be cached relative to a given module name
229  
230 This is most useful with classes implementing the Command.pm API. Since these modules may form a
231 large command tree, the caching occurs at individual levels in the tree separately.
232  
233 =cut
234  
235  
↑ Summary blib/lib/Getopt/Complete/Args.pm »