lib/Lingy/ClojureREPL.pm

filter: Statementstmt 39.1% 36 / 92 Branchbran 3.1% 1 / 32 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 2 totaltotal 32.1% 46 / 143 CC 19 SCAR 41.7
CC
19
Cov
29%
rep
132.0
start
56.0
END
7.6
CRAP 64.6
« lib/Lingy/Reader.pm ↑ Summary lib/Lingy/Env.pm »
1 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Lingy::ClojureREPL;
3  
4 1 ▶use File::Which;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Symbol 'gensym';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes 'usleep';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XXX;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 my $done = '3416ebc19a42578b8ebc3f59ea1806266cea4290';
12 1 my $pid;
13 1 my ($in, $out, $err);
14 1 my ($select_out, $select_err);
15  
16 1 my $Y = "\e[0;33m";
17 1 my $Z = "\e[0m";
18  
19 1 my $clojure_jar;
20 1 my $already_searched = 0;
21 1 my $main_repl = <<'...';
22 (require '[clojure.main :as main])
23 (use 'clojure.repl
24 ' clojure.pprint)
25 (apply main/repl [
26 :prompt #()
27 :caught (fn [e]
28 (let [
29 e-via (binding [ *data-readers* {'error identity} ] (let [ err-data (read-string (pr-str e))] (:via err-data)))
30 [m1 m2 m3] e-via ]
31 (if m1 (println (:message m1)))
32 (if m2 (println (:message m2)))))])
33 ...
34 # ...
35 1 my $server_opt =
36 '-Dclojure.server.repl=' .
37 '{:port 5555 :accept clojure.core.server/repl}';
38  
39 sub start {
40 0 ▶ my ($class, $newline) = @_;
Subroutine start: not called CC=7 SCAR=40.3
Pod undocumented
41 0 ▶ if (not $clojure_jar) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($clojure_jar) 0 0 0
42 0 ▶ return if $already_searched++;
Branch: 1 branch
BranchTrueFalseTotal
if $already_searched++ 0 0 0
43 0 ▶ my $file = which('clojure') or do {
Branch: 1 branch
BranchTrueFalseTotal
my $file = which("clojure") or do { print "${Y}Can't find 'clojure' on this system$Z\n"; return } 0 0 0
44 0 print "${Y}Can't find 'clojure' on this system$Z\n";
45 0 return;
46 };
47 0 ▶ open my $fh, $file or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "Can't open '${file}' for input: $!" 0 0 0
48 die "Can't open '$file' for input: $!";
49 0 my $text = do {local $/; <$fh>};
50 0 ▶ $text =~ /java -cp +(.+?\.jar)/ or do {
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /java -cp +(.+?\.jar)/ or do { print "${Y}Can't find 'clojure' on this system$Z\n"; return } 0 0 0
51 0 print "${Y}Can't find 'clojure' on this system$Z\n";
52 0 return;
53 };
54 0 $clojure_jar = $1;
55 }
56  
57 0 $pid = open3(
58 $in,
59 $out,
60 $err = gensym,
61 (
62 'java', '-cp', $clojure_jar,
63 $server_opt,
64 'clojure.main',
65 '-e', $main_repl,
66 )
67 );
68  
69 0 ▶ print "\n" if $newline;
Branch: 1 branch
BranchTrueFalseTotal
if $newline 0 0 0
70 0 print "$Y*** Started Clojure REPL server ($pid)$Z\n\n";
71  
72 0 $select_out = new IO::Select();
73 0 $select_out->add($out);
74 0 $select_err = new IO::Select();
75 0 $select_err->add($err);
76 }
77  
78 sub rep {
79 0 ▶ my ($class, $input) = @_;
Subroutine rep: not called CC=11 SCAR=48.8
Pod undocumented
80 0 ▶ $class->start(1) unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
81 0 ▶ return unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 0 0
82  
83 0 ▶ return if $input =~ /^\s*\(\s*clojure-repl-on\s*\)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $input =~ /^\s*\(\s*clojure-repl-on\s*\)\s*$/ 0 0 0
84  
85 0 print $in qq<$input\n"$done"\n>;
86  
87 0 my $output = '';
88 0 my $string = '';
89 0 my $count = 0;
90 0 my $rc = 0;
91  
92 0 usleep 500_000;
93 0 while (1) {
94 0 ▶ if ($select_out->can_read(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_out->can_read(0)) 0 0 0
95 0 sysread($out, $string, 4096);
96 0 ▶ $output .= $string // '';
Condition0%0/2
$string // ""
l!l
00
MC/DC0%0/1
$string // ""
$string
Truth table0%0/2
A
A= $string
Aresult
11
00
97 0 ▶ last if $output =~ s/"$done"\n+//;
Branch: 1 branch
BranchTrueFalseTotal
if $output =~ s/"$done"\n+// 0 0 0
98 }
99 0 ▶ if ($select_err->can_read(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select_err->can_read(0)) 0 0 0
100 0 sysread($err, $string, 4096);
101 0 ▶ $output .= $string // '';
Condition0%0/2
$string // ""
l!l
00
MC/DC0%0/1
$string // ""
$string
Truth table0%0/2
A
A= $string
Aresult
11
00
102 0 last;
103 }
104  
105 0 ▶ if (++$count >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$count >= 3) 0 0 0
106 0 $output = 'timeout';
107 0 kill -9, $pid;
108 0 print "$Y*** Killed Clojure REPL server ($pid)$Z\n";
109 0 undef $pid;
110 0 $rc = 255;
111 0 last;
112 }
113  
114 0 usleep 500_000;
115 }
116  
117 0 $output =~ s/\n*\z//;
118 0 ▶ $output .= "\n" if length $output;
Branch: 1 branch
BranchTrueFalseTotal
if length $output 0 0 0
119  
120 0 print STDOUT "${Y}Clojure:$Z\n$output\n";
121 }
122  
123 END {
124 1 ▶ if (defined $pid) {
Subroutine END: called CC=3 SCAR=20.3
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pid) 0 1 1
125 0 print $in '(java.lang.System/exit 0)', "\n";
126 0 waitpid( $pid, 0 );
127 0 my $rc = $? >> 8;
128 0 print "$Y*** Stopped Clojure REPL server ($pid)$Z\n";
129 0 ▶ exit $rc unless $rc == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc == 0 0 0 0
130 }
131 }
132  
133 1 1;
« lib/Lingy/Reader.pm ↑ Summary lib/Lingy/Env.pm »