blib/lib/Devel/ebug/HTTP.pm

filter: Statementstmt 64.1% 93 / 145 Branchbran 42.8% 18 / 42 Conditioncond 53.8% 7 / 13 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 2 totaltotal 59.0% 134 / 227 CC 27 SCAR 26.5
CC
27
Cov
59%
_tell_ebug
37.3
main
12.0
_codelines
11.7
CRAP 14.2
↑ Summary
1 package Devel::ebug::HTTP;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Mojolicious::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use File::ShareDir::Dist qw( dist_share );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use PPI::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: A web front end to a simple, extensible Perl debugger
13 1 our $VERSION = '0.37'; # VERSION
14  
15 # global for now, sigh
16 1 my $codelines_cache;
17 my $ebug;
18 1 my $lines_visible_above_count = 10;
19 1 my $sequence = 1;
20 1 my $vars;
21  
22 {
23 1 my $share = dist_share('Devel-ebug-HTTP');
24  
25 1 ▶ unless(defined $share)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $share) 0 1 1
26 {
27 0 ▶ $share = -f "share/root/index.html.tt" # TODO do relative to ebug?
Branch: 1 branch
BranchTrueFalseTotal
-f 'share/root/index.html.tt' ? : 0 0 0
28 ? "share"
29 : die "unable to find home or root";
30 }
31  
32 1 push @{ app->static->paths }, "$share/root";
33 1 push @{ app->renderer->paths }, "$share/root";
34 }
35  
36 1 plugin 'TtRenderer';
37 1 app->renderer->default_handler('tt');
38  
39 1 app->log->level('fatal');
40  
41 1 any '/ajax_variable/:variable' => \&_ajax_variable;
42 1 any '/ajax_eval' => \&_ajax_eval;
43 1 any '/' => \&_do_the_request;
44 1 any '/*whatever' => \&_do_the_request;
45  
46 2 ▶sub _ajax_variable {
Subroutine _ajax_variable: called CC=4 SCAR=14.1
47 2 my($c) = @_;
48 2 my $variable = $c->stash('variable');
49 2 ▶ $variable = '\\' . $variable if $variable =~ /^[%@]/;
Branch: 1 branch
BranchTrueFalseTotal
if $variable =~ /^[%\@]/u 0 2 2
50 2 my $value = $ebug->yaml($variable);
51 2 ▶ $value =~ s/^--- // unless $variable =~ /^[%@]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $variable =~ /^[%\@]/u 2 0 2
52 2 ▶ $value = "Not defined" if $value =~ /^Global symbol/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^Global symbol/u 2 0 2
53 2 $value =~ s{\n}{<br/>}g;
54 2 my $xml = qq{<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
55 <response>
56 <variable>$variable</variable>
57 <value><![CDATA[$value]]></value>
58 </response>
59 };
60 2 $c->res->headers->content_type("text/xml");
61 2 $c->render(data => $xml);
62 }
63  
64 1 ▶sub _ajax_eval {
Subroutine _ajax_eval: called CC=2 SCAR=7.0
65 1 my($c) = @_;
66 1 my $eval = $c->req->params->to_hash->{eval};
67 1 ▶ my $result = $ebug->eval($eval) || "No output";
Condition50%1/2
$ebug->eval($eval) || 'No output'
l!l
10
MC/DC0%0/1
$ebug->eval($eval) || 'No output'
$ebug->eval($eval)
Truth table50%1/2
A
A= $ebug->eval($eval)
Aresult
11
00
68 1 $result =~ s/ at \(eval .+$//;
69 1 $c->res->headers->content_type("text/html");
70 1 $c->render(data => $result);
71 }
72  
73 2 ▶sub _do_the_request {
Subroutine _do_the_request: called CC=4 SCAR=14.6
74 2 my($c) = @_;
75 2 my $params = $c->req->params->to_hash;
76  
77 # clear out template variables
78 2 $vars = {};
79  
80 # pass commands we've been passed to the ebug
81 2 ▶ my $action = lc($params->{myaction} || '');
Condition100%2/2
$params->{'myaction'} || ''
l!l
11
MC/DC100%1/1
$params->{'myaction'} || ''
$params->{'myaction'}
Truth table100%2/2
A
A= $params->{'myaction'}
Aresult
11
00
82 2 _tell_ebug($c, $action);
83  
84 # check we're doing things in the right order
85 2 my $cgi_sequence = $params->{sequence};
86 2 ▶ if (defined $cgi_sequence && $cgi_sequence < $sequence) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cgi_sequence and $cgi_sequence < $sequence) 0 2 2
Condition66%2/3
defined $cgi_sequence and $cgi_sequence < $sequence
!ll&&!rl&&r
110
MC/DC0%0/2
defined $cgi_sequence and $cgi_sequence < $sequence
defined $cgi_sequence $cgi_sequence < $sequence
Truth table66%2/3
A and B
A= defined $cgi_sequenceB= $cgi_sequence < $sequence
ABresult
0X0
100
111
87 0 $ebug->undo($sequence - $cgi_sequence);
88 0 $sequence = $cgi_sequence;
89 }
90 2 $sequence++;
91  
92 2 _set_up_stash($c);
93  
94 0 $c->render(template => 'index');
95 }
96  
97 2 ▶sub _tell_ebug {
Subroutine _tell_ebug: called CC=13 SCAR=36.2
98 2 my ($c, $action) = @_;
99 2 my $params = $c->req->params->to_hash;
100  
101 2 ▶ if ($ebug->finished &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ebug->finished and $action ne "restart" and $action ne "undo") 0 2 2
Condition33%2/6
$ebug->finished and $action ne "restart" and $action ne "undo"
!ll&&!rl&&r
200
$ebug->finished and $action ne "restart"
!ll&&!rl&&r
200
MC/DC0%0/3
$ebug->finished and $action ne "restart" and $action ne "undo"
$ebug->finished $action ne "restart" $action ne "undo"
Truth table25%1/4
A and B and C
A= $ebug->finishedB= $action ne "restart"C= $action ne "undo"
ABCresult
0XX0
10X0
1100
1111
102 ($action ne "restart") &&
103 ($action ne "undo")) {
104 0 return;
105 }
106  
107 2 ▶ if ($action eq 'break point:') {
Branch: 3 branches
BranchTrueFalseTotal
if ($action eq 'break point:') { } 0 2 2
elsif ($action eq 'break_point') { } 0 2 2
elsif ($action eq 'break_point_delete') { } 0 2 2
108 0 $ebug->break_point($params->{'break_point'});
109 } elsif ($action eq 'break_point') {
110 0 $ebug->break_point($params->{line});
111 } elsif ($action eq 'break_point_delete') {
112 0 $ebug->break_point_delete($params->{line});
113 2 ▶ } if ($action eq 'next') {
Branch: 4 branches
BranchTrueFalseTotal
if ($action eq 'next') { } 0 2 2
elsif ($action eq 'restart') { } 0 2 2
elsif ($action eq 'return') { } 0 2 2
elsif ($action eq 'run') { } 0 2 2
114 0 $ebug->next;
115 } elsif ($action eq 'restart') {
116 0 $ebug->load;
117 } elsif ($action eq 'return') {
118 0 $ebug->return;
119 } elsif ($action eq 'run') {
120 0 $ebug->run;
121 2 ▶ } if ($action eq 'step') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'step') { } 1 1 2
elsif ($action eq 'undo') { } 0 1 1
122 1 $ebug->step;
123 } elsif ($action eq 'undo') {
124 0 $ebug->undo;
125 }
126 }
127  
128 2 ▶sub _set_up_stash {
Subroutine _set_up_stash: called CC=2 SCAR=7.1
129 2 my($c) = @_;
130  
131 2 my $break_points;
132 2 $break_points->{$_}++ foreach $ebug->break_points;
133  
134 2 my $url = $c->req->url->base->clone;
135 2 $url->path('/');
136 2 $url = $url->to_string;
137  
138 2 my($stdout, $stderr) = $ebug->output;
139  
140 2 my $codelines = _codelines($c);
141  
142 0 $vars = {
143 %$vars,
144 break_points => $break_points,
145 codelines => $codelines,
146 ebug => $ebug,
147 sequence => $sequence,
148 stack_trace_human => [$ebug->stack_trace_human],
149 stdout => $stdout,
150 stderr => $stderr,
151 subroutine => $ebug->subroutine,
152 top_visible_line => max(1, $ebug->line - $lines_visible_above_count + 1),
153 url => $url,
154 };
155  
156 0 $c->stash(%$vars);
157 }
158  
159 2 ▶sub _codelines {
Subroutine _codelines: called CC=4 SCAR=24.6
160 2 my($c) = @_;
161 2 my $filename = $ebug->filename;
162 2 ▶ return $codelines_cache->{$filename} if exists $codelines_cache->{$filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $codelines_cache->{$filename} 0 2 2
163  
164 2 my $url = $c->req->url->base->clone;
165 2 $url->path('/');
166 2 $url = $url->to_string;
167 2 my $code = join "\n", $ebug->codelines;
168 0 my $document = PPI::Document->new(\$code);
169 0 my $highlight = PPI::HTML->new(line_numbers => 1);
170 0 my $pretty = $highlight->html($document);
171  
172 0 my $split = '<span class="line_number">';
173  
174 # turn significant whitespace into &nbsp;
175 my @lines = map {
176 0 $_ =~ s{</span>( +)}{"</span>" . ("&nbsp;" x length($1))}e;
177 0 "$split$_";
178 } split /$split/, $pretty;
179  
180 # right-justify the line number
181 @lines = map {
182 0 s{<span class="line_number"> ?(\d+) ?:}{
183 0 my $line = $1;
184 0 my $size = 4 - (length($1));
185 0 ▶ $size = 0 if $size < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 0 0 0 0
186 0 $line = line_html($url, $line);
187 0 '<span class="line_number">' . ("&nbsp;" x $size) . "$line:"}e;
188 0 $_;
189 } @lines;
190  
191 # add the dynamic tooltips
192 @lines = map {
193 0 s{<span class="symbol">(.+?)</span>}{
194 0 '<span class="symbol">' . variable_html($url, $1) . "</span>"
195 }eg;
196 0 $_;
197 } @lines;
198  
199 # make us slightly more XHTML
200 0 $_ =~ s{<br>}{<br/>} foreach @lines;
201  
202 # link module names to metacpan
203 @lines = map {
204 0 $_ =~ s{<span class="word">([^<]+?::[^<]+?)</span>}{<span class="word"><a href="https://metacpan.org/pod/$1">$1</a></span>};
205 0 $_;
206 } @lines;
207  
208 0 $codelines_cache->{$filename} = \@lines;
209 0 return \@lines;
210 }
211  
212 0 ▶sub variable_html {
Subroutine variable_html: not called CC=1 SCAR=6.9
Pod undocumented
213 0 my($url, $variable) = @_;
214 0 return qq{<a href="#" style="text-decoration: none" onmouseover="highlight_variable('$variable'); return tooltip('$variable')" onmouseout="unhighlight_variable('$variable'); return nd();">$variable</a>};
215 }
216  
217 0 ▶sub line_html {
Subroutine line_html: not called CC=1 SCAR=6.9
Pod undocumented
218 0 my($url, $line) = @_;
219 0 return qq{<a href="#" style="text-decoration: none" onClick="return break_point($line)">$line</a>};
220 }
221  
222 package Devel::ebug::HTTP::App;
223  
224 0 ▶sub main {
Subroutine main: not called CC=3 SCAR=24.8
225 0 my $filename = shift @ARGV;
226 0 ▶ die "Usage: ebug_http filename\n" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 0 0
227  
228 0 require Devel::ebug;
229 0 $ebug = Devel::ebug->new;
230 0 $ebug->program($filename);
231 0 $ebug->load;
232  
233 0 ▶ Devel::ebug::HTTP::app->start(@ARGV ? @ARGV : 'daemon');
Branch: 1 branch
BranchTrueFalseTotal
@ARGV ? : 0 0 0
234 }
235  
236 1 ▶sub ebug {
Subroutine ebug: called CC=2 SCAR=7.1
237 1 my(undef, $new) = @_;
238 1 ▶ $ebug = $new if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 1 0 1
239 1 return $ebug;
240 }
241  
242 1 1;
243  
244 __END__
↑ Summary