blib/lib/App/DuckPAN.pm

filter: Statementstmt 40.0% 135 / 337 Branchbran 9.0% 9 / 100 Conditioncond 2.5% 2 / 78 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 53.6% 37 / 69 Podpod 0.0% 0 / 23 totaltotal 28.3% 183 / 645 CC 80 SCAR 49.0
CC
80
Cov
28%
execute
643.5
check_requirements
90.0
check_ddg
90.0
CRAP 134.2
« blib/lib/App/DuckPAN/Cmd/Server.pm ↑ Summary blib/lib/App/DuckPAN/Cmd/Roadrunner.pm »
1 package App::DuckPAN;
2 5 our $AUTHORITY = 'cpan:DDG';
3 # ABSTRACT: The DuckDuckGo DuckPAN client
4 5 $App::DuckPAN::VERSION = '1021';
5 5 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use MooX::Cmd;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use MooX::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use App::DuckPAN::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Class::Load ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use HTTP::Request::Common qw( GET POST );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use List::Util qw( first max );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Parse::CPAN::Packages::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use File::Temp qw/ :POSIX /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Term::UI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Term::ReadLine;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Perl::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use open qw/:std :utf8/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use App::DuckPAN::Cmd::Help;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use DDG::Meta::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 5 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 option dev_version => (
34 is => 'ro',
35 default => 9.999,
36 doc => 'Version used when using unreleased code, e.g. git repos'
37 );
38  
39 option check => (
40 is => 'ro',
41 lazy => 1,
42 negativable => 1,
43 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 5 doc => 'perform requirements checks. turn off with --no-check',
45 );
46  
47 option empty => (
48 is => 'ro',
49 lazy => 1,
50 short => 'e',
51 3 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 5 doc => 'empty duckpan cache at start-up',
53 );
54  
55 has cachesec => (
56 is => 'ro',
57 lazy => 1,
58 0 ▶ default => sub { 60 * 60 * 4 }, # 4 hours by default
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 5 );
60  
61 option colors => (
62 is => 'ro',
63 lazy => 1,
64 negativable => 1,
65 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 5 doc => 'use color output. turn off with --no-colors',
67 );
68  
69 option verbose => (
70 is => 'ro',
71 lazy => 1,
72 short => 'v|debug',
73 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 5 doc => 'provide expanded output during operation',
75 );
76  
77 5 has duckpan_packages => (
78 is => 'ro',
79 lazy => 1,
80 builder => 1,
81 );
82  
83 0 ▶sub _build_duckpan_packages {
Subroutine _build_duckpan_packages: not called CC=2 SCAR=17.9
84 0 my $self = shift;
85  
86 0 my $gz = '02packages.details.txt.gz';
87 0 my $package_url = join('/', $self->duckpan, 'modules', $gz);
88 0 my $mirror_to = $self->cfg->cache_path->child($gz);
89  
90 0 ▶ if (is_error(mirror($package_url, $mirror_to))) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_error(scalar &mirror($package_url, $mirror_to))) 0 0 0
91 0 $self->emit_and_exit(1, "Cannot download $package_url");
92 }
93  
94 0 return Parse::CPAN::Packages::Fast->new($mirror_to->stringify);
95 }
96  
97 option duckpan => (
98 is => 'ro',
99 lazy => 1,
100 0 ▶ default => sub { 'http://duckpan.org/' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 5 doc => 'URI for the duckpan package server. defaults to "https://duckpan.org/"',
102 );
103  
104 1 ▶sub _ua_string {
Subroutine _ua_string: called CC=3 SCAR=12.7
105 1 my ($self) = @_;
106 1 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
107 1 ▶ my $version = $class->VERSION || $self->dev_version;
Condition33%1/3
$class->VERSION || $self->dev_version
l!l&&r!l&&!r
100
MC/DC0%0/2
$class->VERSION || $self->dev_version
$class->VERSION $self->dev_version
Truth table33%1/3
A || B
A= $class->VERSIONB= $self->dev_version
ABresult
1X1
011
000
108 1 return "$class/$version";
109 }
110  
111 5 option http_proxy => (
112 is => 'ro',
113 predicate => 1,
114 doc => 'proxy to use for outbound HTTP requests',
115 );
116  
117 5 option config => (
118 is => 'ro',
119 predicate => 1,
120 doc => 'path to config directory. defaults to "~/.duckpan/config"',
121 );
122  
123 5 option cache => (
124 is => 'ro',
125 predicate => 1,
126 doc => 'path to cache directory. defaults to "~/.duckpan/cache"',
127 );
128  
129 5 has term => (
130 is => 'ro',
131 lazy => 1,
132 builder => 1,
133 );
134  
135 0 ▶sub _build_term { Term::ReadLine->new('duckpan') }
Subroutine _build_term: not called CC=1 SCAR=6.9
136  
137 5 has ia_types => (
138 is => 'ro',
139 lazy => 1,
140 builder => 1,
141 );
142  
143 0 ▶sub _build_ia_types {
Subroutine _build_ia_types: not called CC=1 SCAR=6.9
144 0 my $ddg_path = path('lib', 'DDG');
145 0 my $t_dir = path('t');
146 return [{
147 0 name => 'Goodie',
148 dir => $ddg_path->child('Goodie'),
149 supported => 1,
150 path_basename => 'zeroclickinfo-goodies',
151 templates => {
152 code => {
153 in => path('template', 'lib', 'DDG', 'Goodie', 'Example.pm'),
154 out => $ddg_path->child('Goodie')
155 },
156 test => {
157 in => path('template', 't', 'Example.t'),
158 out => $t_dir
159 },
160 },
161 },
162 {
163 name => 'Spice',
164 dir => $ddg_path->child('Spice'),
165 supported => 1,
166 path_basename => 'zeroclickinfo-spice',
167 templates => {
168 code => {
169 in => path('template', 'lib', 'DDG', 'Spice', 'Example.pm'),
170 out => $ddg_path->child('Spice')
171 },
172 test => {
173 in => path('template', 't', 'Example.t'),
174 out => $t_dir
175 },
176 handlebars => {
177 in => path('template', 'share', 'spice', 'example', 'example.handlebars'),
178 out => path('share', 'spice')
179 },
180 js => {
181 in => path('template', 'share', 'spice', 'example', 'example.js'),
182 out => path('share', 'spice')
183 },
184 },
185 },
186 {
187 name => 'Fathead',
188 dir => $ddg_path->child('Fathead'),
189 supported => 1,
190 path_basename => 'zeroclickinfo-fathead',
191 },
192 {
193 name => 'Longtail',
194 dir => $ddg_path->child('Longtail'),
195 supported => 0,
196 path_basename => 'zeroclickinfo-longtail',
197 },
198 ];
199 }
200  
201 0 ▶sub get_reply {
Subroutine get_reply: not called CC=1 SCAR=6.9
Pod undocumented
202 0 my ( $self, $prompt, %params ) = @_;
203 0 my $return = $self->term->get_reply( prompt => $prompt, %params );
204 0 Encode::_utf8_on($return);
205 0 return $return;
206 }
207  
208 0 ▶sub ask_yn {
Subroutine ask_yn: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my ( $self, $prompt, %params ) = @_;
210  
211 0 return $self->term->ask_yn( prompt => $prompt, %params );
212 }
213  
214 5 has http => (
215 is => 'ro',
216 builder => 1,
217 lazy => 1,
218 );
219  
220 1 ▶sub _build_http {
Subroutine _build_http: called CC=2 SCAR=7.0
221 1 my ( $self ) = @_;
222 1 my $agent = LWP::UserAgent->new;
223 1 $agent->agent($self->_ua_string);
224 1 $agent->env_proxy;
225 1 ▶ $agent->proxy( http => $self->http_proxy ) if $self->has_http_proxy;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_http_proxy 0 1 1
226 1 return $agent;
227 }
228  
229 5 has server_hostname => (
230 is => 'ro',
231 lazy => 1,
232 builder => 1,
233 );
234  
235 1 ▶sub _build_server_hostname { defined $ENV{APP_DUCKPAN_SERVER_HOSTNAME} ? $ENV{APP_DUCKPAN_SERVER_HOSTNAME} : 'duckduckgo.com' }
Subroutine _build_server_hostname: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'APP_DUCKPAN_SERVER_HOSTNAME'} ? : 0 1 1
236  
237 5 has cfg => (
238 is => 'ro',
239 lazy => 1,
240 builder => 1,
241 handles => [qw(
242 config_path
243 config_file
244 set_config
245 get_config
246 )]
247 );
248  
249 3 ▶sub _build_cfg {
Subroutine _build_cfg: called CC=3 SCAR=11.1
250 3 my ( $self ) = @_;
251 3 ▶ App::DuckPAN::Config->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_config ? : 1 2 3
$self->has_cache ? : 0 3 3
252 $self->has_config ? ( config_path => $self->config ) : (),
253 $self->has_cache ? ( cache_path => $self->cache ) : (),
254 );
255 }
256  
257 5 has perl => (
258 is => 'ro',
259 builder => 1,
260 lazy => 1,
261 );
262  
263 1 ▶sub _build_perl {
Subroutine _build_perl: called CC=1 SCAR=0.0
264 1 load_class('App::DuckPAN::Perl');
265 1 App::DuckPAN::Perl->new( app => shift );
266 }
267  
268 5 has ddg => (
269 is => 'ro',
270 builder => 1,
271 lazy => 1,
272 );
273  
274 1 ▶sub _build_ddg {
Subroutine _build_ddg: called CC=1 SCAR=0.0
275 1 load_class('App::DuckPAN::DDG');
276 1 App::DuckPAN::DDG->new( app => shift );
277 }
278  
279 5 has fathead => (
280 is => 'ro',
281 builder => 1,
282 lazy => 1,
283 );
284  
285 0 ▶sub _build_fathead {
Subroutine _build_fathead: not called CC=1 SCAR=6.9
286 0 load_class('App::DuckPAN::Fathead');
287 0 App::DuckPAN::Fathead->new( app => shift );
288 }
289  
290 1 ▶sub execute {
Subroutine execute: called CC=27 SCAR=64.7
Pod undocumented
291 1 my ( $self, $args, $chain ) = @_;
292 0 my @arr_args = grep { $_ !~ /^-/} @{$args}; # Command line switches make it here, so we try to remove
293 1 ▶ App::DuckPAN::Cmd::Help->run(1) if scalar @arr_args == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @arr_args == 0 1 0 1
294 0 ▶ if (@arr_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@arr_args) 0 0 0
295 0 my (@modules, @left_args, $ddg);
296 0 for (@arr_args) {
297 0 ▶ if (/^www/i ||
Branch: 2 branches
BranchTrueFalseTotal
if (/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i or /^(longtail)$/i or /^ddgc?::/i or /^app/i) { } 0 0 0
elsif (/^duckpan|update|(upgrade|(reinstall|latest))$/i) { } 0 0 0
Condition0%0/27
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i or /^(longtail)$/i or /^ddgc?::/i or /^app/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i or /^(longtail)$/i or /^ddgc?::/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i or /^(longtail)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i or /^(ddg)$/i
l!l&&r!l&&!r
000
/^www/i or /^dist/i
l!l&&r!l&&!r
000
MC/DC0%0/10
/^www/i or /^dist/i or /^(ddg)$/i or /(opensourceduckduckgo)$/i or /^(goodie)$/i or /^(spice)$/i or /^(fathead)$/i or /^(longtail)$/i or /^ddgc?::/i or /^app/i
/^www/i /^dist/i /^(ddg)$/i /(opensourceduckduckgo)$/i /^(goodie)$/i /^(spice)$/i /^(fathead)$/i /^(longtail)$/i /^ddgc?::/i /^app/i
Truth table0%0/11
A or B or C or D or E or F or G or H or I or J
A= /^www/iB= /^dist/iC= /^(ddg)$/iD= /(opensourceduckduckgo)$/iE= /^(goodie)$/iF= /^(spice)$/iG= /^(fathead)$/iH= /^(longtail)$/iI= /^ddgc?::/iJ= /^app/i
ABCDEFGHIJresult
1XXXXXXXXX1
01XXXXXXXX1
001XXXXXXX1
0001XXXXXX1
00001XXXXX1
000001XXXX1
0000001XXX1
00000001XX1
000000001X1
00000000011
00000000000
298 /^dist/i ||
299 /^(ddg)$/i ||
300 /(opensourceduckduckgo)$/i ||
301 /^(goodie)$/i || /^(spice)$/i || /^(fathead)$/i || /^(longtail)$/i ||
302 /^ddgc?::/i ||
303 /^app/i) {
304 0 my $m = lc $1;
305  
306 0 ▶ if($m eq 'goodie' or $m eq 'spice' or $m eq 'fathead' or $m eq 'longtail'){
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq "goodie" or $m eq "spice" or $m eq "fathead" or $m eq "longtail") 0 0 0
Condition0%0/9
$m eq "goodie" or $m eq "spice" or $m eq "fathead" or $m eq "longtail"
l!l&&r!l&&!r
000
$m eq "goodie" or $m eq "spice" or $m eq "fathead"
l!l&&r!l&&!r
000
$m eq "goodie" or $m eq "spice"
l!l&&r!l&&!r
000
MC/DC0%0/4
$m eq "goodie" or $m eq "spice" or $m eq "fathead" or $m eq "longtail"
$m eq "goodie" $m eq "spice" $m eq "fathead" $m eq "longtail"
Truth table0%0/5
A or B or C or D
A= $m eq "goodie"B= $m eq "spice"C= $m eq "fathead"D= $m eq "longtail"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
307 0 $_ = 'DDG::' . ucfirst($m) . 'Bundle::OpenSourceDuckDuckGo';
308 0 $m = 'opensourceduckduckgo';
309 }
310  
311 0 ▶ if($m eq 'opensourceduckduckgo' && !$ddg){
Branch: 2 branches
BranchTrueFalseTotal
if ($m eq 'opensourceduckduckgo' and not $ddg) { } 0 0 0
elsif ($m eq 'ddg' and $ddg) { } 0 0 0
Condition0%0/3
$m eq "opensourceduckduckgo" and not $ddg
!ll&&!rl&&r
000
MC/DC0%0/2
$m eq "opensourceduckduckgo" and not $ddg
$m eq "opensourceduckduckgo" not $ddg
Truth table0%0/3
A and not(B)
A= $m eq "opensourceduckduckgo"B= not $ddg
ABresult
0X0
100
111
312 0 unshift @modules, 'DDG';
313 0 $ddg = 1;
314 }
315 0 ▶ elsif($m eq 'ddg' && $ddg){ next }
Condition0%0/3
$m eq "ddg" and $ddg
!ll&&!rl&&r
000
MC/DC0%0/2
$m eq "ddg" and $ddg
$m eq "ddg" $ddg
Truth table0%0/3
A and B
A= $m eq "ddg"B= $ddg
ABresult
0X0
100
111
316 0 push @modules, $_;
317 }
318 elsif (m/^duckpan|update|(upgrade|(reinstall|latest))$/i) {
319 0 my ($all_modules, $reinstall_latest) = map { lc } ($1, $2);
320 0 ▶ $self->empty_cache unless $self->empty;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->empty 0 0 0
321 0 push @modules, 'App::DuckPAN';
322 0 ▶ if($all_modules){
Branch: 1 branch
BranchTrueFalseTotal
if ($all_modules) 0 0 0
323 0 push @modules, 'DDG';
324 0 ▶ unshift @modules, $reinstall_latest if $reinstall_latest;
Branch: 1 branch
BranchTrueFalseTotal
if $reinstall_latest 0 0 0
325 }
326 }
327 else {
328 0 push @left_args, $_;
329 }
330 }
331 0 ▶ exit $self->perl->duckpan_install(@modules) unless @left_args;
Branch: 1 branch
BranchTrueFalseTotal
unless @left_args 0 0 0
332 }
333 0 $self->emit_and_exit(0, "Unknown command. Use `duckpan help` to see the list of available DuckPAN commands.");
334 }
335  
336 has standard_prefix_width => (
337 is => 'ro',
338 3 ▶ default => sub { 9 },
Subroutine __ANON__: called CC=1 SCAR=0.0
339 5 );
340  
341 0 ▶sub _output_prefix {
Subroutine _output_prefix: not called CC=2 SCAR=17.9
342 0 my ($self, $word, $color) = @_;
343  
344 0 my $extra_spaces = max(0, $self->standard_prefix_width - length($word) - 2 ); # 2 []s to be added.
345  
346 0 my $full_prefix = '[' . uc $word . ']' . (' ' x $extra_spaces);
347  
348 0 ▶ return ($self->colors) ? colored($full_prefix, $color) : $full_prefix;
Branch: 1 branch
BranchTrueFalseTotal
$self->colors ? : 0 0 0
349 }
350  
351 0 ▶sub emit_info {
Subroutine emit_info: not called CC=1 SCAR=6.9
Pod undocumented
352 0 my ($self, @msg) = @_;
353  
354 0 $self->_print_msg(*STDOUT, '', @msg);
355 }
356  
357 0 ▶sub emit_error {
Subroutine emit_error: not called CC=1 SCAR=6.9
Pod undocumented
358 0 my ($self, @msg) = @_;
359  
360 0 state $prefix = $self->_output_prefix('ERROR', 'red bold');
361  
362 0 $self->_print_msg(*STDERR, $prefix, @msg);
363 }
364  
365 0 ▶sub emit_and_exit {
Subroutine emit_and_exit: not called CC=2 SCAR=17.9
Pod undocumented
366 0 my ($self, $exit_code, @msg) = @_;
367  
368 0 state $prefix = $self->_output_prefix('FATAL', 'bright_red bold');
369  
370 0 ▶ if ($exit_code == 0) { # This is just an info message.
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0) { } 0 0 0
371 0 $self->emit_info(@msg);
372 }
373 else { # But if it's an unhappy exit
374 0 $self->_print_msg(*STDERR, $prefix, @msg);
375 }
376  
377 0 exit $exit_code;
378 }
379  
380 0 ▶sub emit_debug {
Subroutine emit_debug: not called CC=2 SCAR=17.9
Pod undocumented
381 0 my ($self, @msg) = @_;
382  
383 0 ▶ return unless $self->verbose; # only show messages in verbose mode.
Branch: 1 branch
BranchTrueFalseTotal
unless $self->verbose 0 0 0
384  
385 0 return $self->_print_msg(*STDOUT, '', @msg);
386 }
387  
388 0 ▶sub emit_notice {
Subroutine emit_notice: not called CC=1 SCAR=6.9
Pod undocumented
389 0 my ($self, @msg) = @_;
390  
391 0 state $prefix = $self->_output_prefix('NOTICE', 'yellow bold');
392  
393 0 $self->_print_msg(*STDOUT, $prefix, @msg);
394 }
395  
396 0 ▶sub _print_msg {
Subroutine _print_msg: not called CC=2 SCAR=17.9
397 0 my ($self, $fh, $prefix, @lines) = @_;
398  
399 0 foreach my $line (map { $prefix . $_ } grep { $_ } @lines) {
400 0 print $fh $line . "\n";
401 }
402 }
403  
404 0 ▶sub camel_to_underscore {
Subroutine camel_to_underscore: not called CC=1 SCAR=6.9
Pod undocumented
405 0 my ($self, $name) = @_;
406 # Replace first capital by lowercase
407 # if followed my lowercase.
408 0 $name =~ s/^([A-Z])([a-z])/lc($1).$2/ge;
409 # Substitute camelCase to camel_case
410 0 $name =~ s/([a-z])([A-Z])/$1.'_'.lc($2)/ge;
411 0 return lc $name;
412 }
413  
414 7 ▶sub phrase_to_camel {
Subroutine phrase_to_camel: called CC=1 SCAR=0.0
Pod undocumented
415 7 my ($self, $phrase) = @_;
416 7 my $camel = $phrase;
417  
418 7 $camel =~ s/
419 (?: # if a character:
420 \s+ # - follows spaces
421 | (?<=::) # - or follows ::
422 | ^ # - or is the first character
423 )(.) # (the character)
424 /\U$1/gx; # then uppercase it (preceding spaces are removed)
425  
426 # remove trailing spaces
427 7 $camel =~ s/\s+$//;
428  
429 7 return $camel;
430 }
431  
432 # Normalize an Instant Answer name to a standard form.
433 # Returns undef if an IA matching the given name cannot be found.
434 0 ▶sub get_ia_by_name {
Subroutine get_ia_by_name: not called CC=4 SCAR=30.0
Pod undocumented
435 0 my ($self, $name) = @_;
436 0 my $ia;
437  
438 0 ▶ if ($name =~ /^DDG::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^DDG::/) { } 0 0 0
439 0 $ia = DDG::Meta::Data->get_ia(module => $name);
440 0 ▶ $ia = $ia->[0] if $ia;
Branch: 1 branch
BranchTrueFalseTotal
if $ia 0 0 0
441 }
442 else {
443 0 ▶ $ia = $name =~ /_/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /_/ ? : 0 0 0
444 ? DDG::Meta::Data->get_ia(id => $name)
445 : DDG::Meta::Data->get_ia(id => $self->camel_to_underscore($name));
446 }
447  
448 0 return $ia;
449 }
450  
451 0 ▶sub check_requirements {
Subroutine check_requirements: not called CC=9 SCAR=45.0
Pod undocumented
452 0 my ($self) = @_;
453  
454 0 ▶ if (!$self->check) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->check) 0 0 0
455 0 $self->emit_notice("Requirements checking was disabled...");
456 0 return 1;
457 }
458 0 my $signal_file = $self->cfg->cache_path->child('perl_checked');
459 0 ▶ my $last_checked_perl = ($signal_file->exists) ? $signal_file->stat->mtime : 0;
Branch: 1 branch
BranchTrueFalseTotal
$signal_file->exists ? : 0 0 0
460 0 ▶ if ((time - $last_checked_perl) <= $self->cachesec) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $last_checked_perl <= $self->cachesec) { } 0 0 0
461 0 $self->emit_debug("Perl module versions recently checked, skipping requirements check...");
462 }
463 else {
464 0 $self->emit_info("Checking for DuckPAN requirements...");
465  
466 0 ▶ $self->emit_and_exit(1, 'Requirements check failed')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_perl and $self->check_app_duckpan and $self->check_ddg and $self->check_ssh and $self->check_git 0 0 0
Condition0%0/12
$self->check_perl and $self->check_app_duckpan and $self->check_ddg and $self->check_ssh and $self->check_git
!ll&&!rl&&r
000
$self->check_perl and $self->check_app_duckpan and $self->check_ddg and $self->check_ssh
!ll&&!rl&&r
000
$self->check_perl and $self->check_app_duckpan and $self->check_ddg
!ll&&!rl&&r
000
$self->check_perl and $self->check_app_duckpan
!ll&&!rl&&r
000
MC/DC0%0/5
$self->check_perl and $self->check_app_duckpan and $self->check_ddg and $self->check_ssh and $self->check_git
$self->check_perl $self->check_app_duckpan $self->check_ddg $self->check_ssh $self->check_git
Truth table0%0/6
A and B and C and D and E
A= $self->check_perlB= $self->check_app_duckpanC= $self->check_ddgD= $self->check_sshE= $self->check_git
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
467 unless (
468 $self->check_perl &&
469 $self->check_app_duckpan &&
470 $self->check_ddg &&
471 $self->check_ssh &&
472 $self->check_git);
473 }
474 0 $signal_file->touch;
475  
476 0 return 1;
477 }
478  
479 1 ▶sub get_local_ddg_version {
Subroutine get_local_ddg_version: called CC=1 SCAR=0.0
Pod undocumented
480 1 my ( $self ) = @_;
481 1 return $self->perl->get_local_version('DDG');
482 }
483  
484 0 ▶sub get_local_app_duckpan_version {
Subroutine get_local_app_duckpan_version: not called CC=1 SCAR=6.9
Pod undocumented
485 0 my ( $self ) = @_;
486 0 return $self->perl->get_local_version('App::DuckPAN');
487 }
488  
489  
490 0 ▶sub check_git {
Subroutine check_git: not called CC=5 SCAR=34.0
Pod undocumented
491 0 my ( $self ) = @_;
492 0 my $ok = 0;
493 0 $self->emit_info("Checking for git...");
494 0 ▶ if (my $git = which('git')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $git = which('git')) { } 0 0 0
495 0 my $version_string = `$git --version`;
496 0 ▶ if ($version_string =~ m/git version (\d+)\.(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version_string =~ /git version (\d+)\.(\d+)/) { } 0 0 0
497 0 ▶ if ($1 <= 1 && $2 < 7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 <= 1 and $2 < 7) { } 0 0 0
Condition0%0/3
$1 <= 1 and $2 < 7
!ll&&!rl&&r
000
MC/DC0%0/2
$1 <= 1 and $2 < 7
$1 <= 1 $2 < 7
Truth table0%0/3
A and B
A= $1 <= 1B= $2 < 7
ABresult
0X0
100
111
498 0 $self->emit_error("require minimum git 1.7");
499 }
500 else {
501 0 $self->emit_debug($git);
502 0 $ok = 1;
503 }
504 }
505 else {
506 0 $self->emit_error("Unknown git version!");
507 }
508 }
509 else {
510 0 $self->emit_error("git not found");
511 }
512 0 return $ok;
513 }
514  
515 0 ▶sub check_ssh {
Subroutine check_ssh: not called CC=2 SCAR=17.9
Pod undocumented
516 0 my ( $self ) = @_;
517 0 my $ok = 0;
518 0 $self->emit_info("Checking for ssh...");
519 0 ▶ if (my $ssh = which('ssh')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ssh = which('ssh')) { } 0 0 0
520 0 $self->emit_debug($ssh);
521 0 $ok = 1;
522 }
523 else {
524 0 $self->emit_error('ssh not found');
525 }
526 0 return $ok;
527 }
528  
529 5 my %perl_versions = (
530 required => Perl::Version->new('v5.14'),
531 recommended => Perl::Version->new('v5.16'),
532 );
533  
534 0 ▶sub check_perl {
Subroutine check_perl: not called CC=3 SCAR=24.8
Pod undocumented
535 0 my ($self) = @_;
536  
537 0 $self->emit_info("Checking perl version... ");
538 0 my $installed_version = Perl::Version->new($]);
539  
540 0 my $ok = 1;
541 0 ▶ if ($installed_version->vcmp($perl_versions{required}) < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($installed_version->vcmp($perl_versions{'required'}) < 0) { } 0 0 0
elsif ($installed_version->vcmp($perl_versions{'recommended'}) < 0) { } 0 0 0
542 0 $self->emit_error('perl ' . $perl_versions{required}->normal . ' or higher is required. ', $installed_version->normal . ' is installed.');
543 0 $ok = 0;
544 }
545 elsif ($installed_version->vcmp($perl_versions{recommended}) < 0) {
546 0 $self->emit_notice('perl ' . $perl_versions{recommended}->normal . ' or higher is recommended. ',
547 $installed_version->normal . " is installed.");
548 }
549 else {
550 0 $self->emit_debug($installed_version->normal);
551 }
552  
553 0 return $ok;
554 }
555  
556 0 ▶sub check_app_duckpan {
Subroutine check_app_duckpan: not called CC=6 SCAR=37.4
Pod undocumented
557 0 my ($self) = @_;
558 0 my $ok = 1;
559 0 ▶ my $pin_version = $ENV{"DuckPAN"} || undef;
Condition0%0/2
$ENV{'DuckPAN'} || undef
l!l
00
MC/DC0%0/1
$ENV{'DuckPAN'} || undef
$ENV{'DuckPAN'}
Truth table0%0/2
A
A= $ENV{'DuckPAN'}
Aresult
11
00
560 0 my $installed_version = $self->get_local_app_duckpan_version;
561 0 ▶ return $ok if $installed_version && $installed_version == $self->dev_version;
Branch: 1 branch
BranchTrueFalseTotal
if $installed_version and $installed_version == $self->dev_version 0 0 0
Condition0%0/3
$installed_version and $installed_version == $self->dev_version
!ll&&!rl&&r
000
MC/DC0%0/2
$installed_version and $installed_version == $self->dev_version
$installed_version $installed_version == $self->dev_version
Truth table0%0/3
A and B
A= $installed_versionB= $installed_version == $self->dev_version
ABresult
0X0
100
111
562 0 $self->emit_info("Checking for latest App::DuckPAN... ");
563 0 my $packages = $self->duckpan_packages;
564 0 my $module = $packages->package('App::DuckPAN');
565 0 my $latest = $self->duckpan . 'authors/id/' . $module->distribution->pathname;
566 0 my $latest_version = version->parse($module->version);
567  
568 0 ▶ if ($installed_version >= $latest_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed_version >= $latest_version) { } 0 0 0
569 0 my $msg = "App::DuckPAN version: $installed_version";
570 0 ▶ $msg .= " (duckpan has " . $module->version . ")" if $installed_version ne $module->version;
Branch: 1 branch
BranchTrueFalseTotal
if $installed_version ne $module->version 0 0 0
571 0 $self->emit_debug($msg);
572 }
573 else {
574 0 my @msg = (
575 "You have version $installed_version, latest is " . $module->version . "!",
576 "Please install the latest App::DuckPAN package with: duckpan upgrade"
577 );
578 0 $self->emit_notice(@msg);
579 }
580 0 return $ok;
581 }
582  
583 0 ▶sub check_ddg {
Subroutine check_ddg: not called CC=9 SCAR=45.0
Pod undocumented
584 0 my ($self) = @_;
585 0 my $ok = 1;
586 0 ▶ my $pin_version = $ENV{"DDG"} || undef;
Condition0%0/2
$ENV{'DDG'} || undef
l!l
00
MC/DC0%0/1
$ENV{'DDG'} || undef
$ENV{'DDG'}
Truth table0%0/2
A
A= $ENV{'DDG'}
Aresult
11
00
587 0 my $installed_version = $self->get_local_ddg_version;
588 0 ▶ return $ok if $installed_version && $installed_version == $self->dev_version;
Branch: 1 branch
BranchTrueFalseTotal
if $installed_version and $installed_version == $self->dev_version 0 0 0
Condition0%0/3
$installed_version and $installed_version == $self->dev_version
!ll&&!rl&&r
000
MC/DC0%0/2
$installed_version and $installed_version == $self->dev_version
$installed_version $installed_version == $self->dev_version
Truth table0%0/3
A and B
A= $installed_versionB= $installed_version == $self->dev_version
ABresult
0X0
100
111
589 0 warn "installing DDG";
590 0 $self->emit_info("Checking for latest DDG Perl package...");
591 0 my $packages = $self->duckpan_packages;
592 0 my $module = $packages->package('DDG');
593 0 my $latest = $self->duckpan . 'authors/id/' . $module->distribution->pathname;
594 0 my $latest_version = version->parse($module->version);
595  
596 0 ▶ if ($installed_version >= $latest_version) {
Branch: 2 branches
BranchTrueFalseTotal
if ($installed_version >= $latest_version) { } 0 0 0
elsif ($pin_version and $pin_version < $latest_version) { } 0 0 0
597 0 my $msg = "DDG version: $installed_version";
598 0 ▶ $msg .= " (duckpan has $latest_version )" if $installed_version ne $latest_version;
Branch: 1 branch
BranchTrueFalseTotal
if $installed_version ne $latest_version 0 0 0
599 0 $self->emit_debug($msg);
600 }
601 ▶ elsif ($pin_version && $pin_version < $latest_version){
Condition0%0/3
$pin_version and $pin_version < $latest_version
!ll&&!rl&&r
000
MC/DC0%0/2
$pin_version and $pin_version < $latest_version
$pin_version $pin_version < $latest_version
Truth table0%0/3
A and B
A= $pin_versionB= $pin_version < $latest_version
ABresult
0X0
100
111
602 0 my @msg = (
603 "A newer version of DDG exists: $latest_version.",
604 "You have the version pinned to: $pin_version. Please update your version pin!"
605 );
606 0 $self->emit_notice(@msg);
607 }
608 else {
609 0 ▶ if ($installed_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($installed_version) { } 0 0 0
610 0 my @msg = (
611 "You have version $installed_version, latest is " . $module->version . "!",
612 "Please install the latest DDG package with: duckpan DDG"
613 );
614 0 $self->emit_notice(@msg);
615 }
616 else {
617 0 $self->perl->duckpan_install('DDG');
618 }
619 }
620 0 return $ok;
621 }
622  
623 0 ▶sub get_ia_type {
Subroutine get_ia_type: not called CC=2 SCAR=17.9
Pod undocumented
624 0 my ($self) = @_;
625  
626 0 my $repo = $self->repository;
627 0 ▶ unless ($repo) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($repo) 0 0 0
628 0 $self->emit_and_exit(-1,
629 'Must be run from within an Instant Answer repository'
630 );
631 }
632  
633 0 return $repo;
634 }
635  
636 0 ▶sub empty_cache {
Subroutine empty_cache: not called CC=1 SCAR=6.9
Pod undocumented
637 0 my ($self) = @_;
638 # Clear cache so share files are written into cache
639 0 my $cache = $self->cfg->cache_path;
640 0 $self->emit_info("Emptying DuckPAN cache...");
641 0 $cache->remove_tree({keep_root => 1});
642 0 $self->emit_info("DuckPAN cache emptied");
643 }
644  
645 5 has repository => (
646 is => 'rwp',
647 doc => 'Instant Answer repository from which DuckPAN was run.',
648 trigger => \&_check_repository,
649 );
650  
651 0 ▶sub _get_repository_config {
Subroutine _get_repository_config: not called CC=4 SCAR=30.0
652 0 my ($self, $by, $lookup, $single) = @_;
653 0 ▶ $single //= 0;
Condition0%0/2
$single //= 0
l!l
00
MC/DC0%0/1
$single //= 0
$single
Truth table0%0/2
A
A= $single
Aresult
11
00
654 0 my @repos = grep { $_->{$by} eq $lookup } @{$self->ia_types};
655 0 ▶ $single ? (@repos > 1 ? undef : $repos[0]) : @repos;
Branch: 2 branches
BranchTrueFalseTotal
$single ? : 0 0 0
@repos > 1 ? : 0 0 0
656 }
657  
658 0 ▶sub _check_repository {
Subroutine _check_repository: not called CC=2 SCAR=17.9
659 0 my ($self, $repo) = @_;
660 0 my $path_basename = $repo->{path_basename};
661 $self->emit_and_exit(-1,
662 "'$path_basename' is currently not supported by DuckPAN."
663 0 ▶ ) unless $repo->{supported};
Branch: 1 branch
BranchTrueFalseTotal
unless $repo->{"supported"} 0 0 0
664 }
665  
666 # Ensure further commands are run from the 'root' of the Instant
667 # Answer repository.
668 0 ▶sub initialize_working_directory {
Subroutine initialize_working_directory: not called CC=3 SCAR=24.8
Pod undocumented
669 0 my $self = shift;
670 0 my $check_path = Path::Tiny::cwd;
671 0 ▶ while (!$check_path->is_rootdir()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $check_path->is_rootdir) 0 0 0
672 0 ▶ if (my $repo = $self->_get_repository_config(
Branch: 1 branch
BranchTrueFalseTotal
if (my $repo = $self->_get_repository_config("path_basename", $check_path->basename, 1)) 0 0 0
673 path_basename => $check_path->basename, 1
674 )) {
675 0 $self->_set_repository($repo);
676 0 chdir $check_path->stringify;
677 0 last;
678 }
679 }
680 continue {
681 0 $check_path = $check_path->parent;
682 }
683 }
684  
685 3 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=21.6
Pod undocumented
686 3 my ($self) = @_;
687  
688 3 ▶ $self->emit_and_exit(1, 'We dont support Win32') if ($^O eq 'MSWin32');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 3 3
689 3 my $env_config = $self->cfg->config_path->child('env.ini');
690 3 ▶ $self->empty_cache if $self->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->empty 0 3 3
691 3 ▶ if ($env_config->exists) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env_config->exists) 0 3 3
692 0 my $env = Config::INI::Reader->read_file($env_config);
693 0 ▶ map { $ENV{$_} = $env->{'_'}{$_}; } keys %{$env->{'_'}} if $env->{'_'};
Branch: 1 branch
BranchTrueFalseTotal
if $env->{"_"} 0 0 0
694 }
695 }
696  
697 5 1;
698  
699 __END__
« blib/lib/App/DuckPAN/Cmd/Server.pm ↑ Summary blib/lib/App/DuckPAN/Cmd/Roadrunner.pm »