| 1 | use strict; use warnings; | ||
| 2 | package Lingy::ClojureREPL; | ||
| 3 | |||
| 4 | use File::Which; | ||
| 5 | use IO::Select; | ||
| 6 | use IPC::Open3; | ||
| 7 | use Symbol 'gensym'; | ||
| 8 | use Time::HiRes 'usleep'; | ||
| 9 | use XXX; | ||
| 10 | |||
| 11 | my $done = '3416ebc19a42578b8ebc3f59ea1806266cea4290'; | ||
| 12 | my $pid; | ||
| 13 | my ($in, $out, $err); | ||
| 14 | my ($select_out, $select_err); | ||
| 15 | |||
| 16 | my $Y = "\e[0;33m"; | ||
| 17 | my $Z = "\e[0m"; | ||
| 18 | |||
| 19 | my $clojure_jar = $ENV{LINGY_CLOJURE_JAR}; | ||
| 20 | my $already_searched = 0; | ||
| 21 | 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 | ||
| 30 | [ *data-readers* {'error identity} ] | ||
| 31 | (let [ err-data (read-string (pr-str e))] | ||
| 32 | (:via err-data))) | ||
| 33 | [m1 m2 m3] e-via ] | ||
| 34 | (if m1 (println (:message m1))) | ||
| 35 | (if m2 (println (:message m2)))))]) | ||
| 36 | ... | ||
| 37 | # ... | ||
| 38 | my $server_opt = | ||
| 39 | '-Dclojure.server.repl=' . | ||
| 40 | '{:port 5555 :accept clojure.core.server/repl}'; | ||
| 41 | |||
| 42 | sub start { | ||
| 43 | my ($class, $newline) = @_; | ||
| 44 | if (not $clojure_jar) { | ||
| 45 | return if $already_searched++; | ||
| 46 | my $file = which('clojure') or do { | ||
| 47 | print "${Y}Can't find 'clojure' on this system$Z\n"; | ||
| 48 | return; | ||
| 49 | }; | ||
| 50 | open my $fh, $file or | ||
| 51 | die "Can't open '$file' for input: $!"; | ||
| 52 | my $text = do {local $/; <$fh>}; | ||
| 53 | $text =~ /java -cp +(.+?\.jar)/ or do { | ||
| 54 | print "${Y}Can't find the Clojure jar file on this system.$Z\n"; | ||
| 55 | print "${Y}Try setting the LINGY_CLOJURE_JAR env variable.$Z\n"; | ||
| 56 | return; | ||
| 57 | }; | ||
| 58 | $clojure_jar = $1; | ||
| 59 | } | ||
| 60 | |||
| 61 | $pid = open3( | ||
| 62 | $in, | ||
| 63 | $out, | ||
| 64 | $err = gensym, | ||
| 65 | ( | ||
| 66 | 'java', '-cp', $clojure_jar, | ||
| 67 | $server_opt, | ||
| 68 | 'clojure.main', | ||
| 69 | '-e', $main_repl, | ||
| 70 | ) | ||
| 71 | ); | ||
| 72 | |||
| 73 | print "\n" if $newline; | ||
| 74 | print "$Y*** Started Clojure REPL server ($pid)$Z\n\n"; | ||
| 75 | |||
| 76 | $select_out = new IO::Select(); | ||
| 77 | $select_out->add($out); | ||
| 78 | $select_err = new IO::Select(); | ||
| 79 | $select_err->add($err); | ||
| 80 | } | ||
| 81 | |||
| 82 | sub rep { | ||
| 83 | my ($class, $input) = @_; | ||
| 84 | $class->start(1) unless $pid; | ||
| 85 | return unless $pid; | ||
| 86 | |||
| 87 | return if $input =~ /^\s*\(\s*clojure-repl-on\s*\)\s*$/; | ||
| 88 | |||
| 89 | print $in qq<$input\n"$done"\n>; | ||
| 90 | |||
| 91 | my $output = ''; | ||
| 92 | my $string = ''; | ||
| 93 | my $count = 0; | ||
| 94 | my $rc = 0; | ||
| 95 | |||
| 96 | usleep 500_000; | ||
| 97 | while (1) { | ||
| 98 | if ($select_out->can_read(0)) { | ||
| 99 | sysread($out, $string, 4096); | ||
| 100 | $output .= $string // ''; | ||
| 101 | last if $output =~ s/"$done"\n+//; | ||
| 102 | } | ||
| 103 | if ($select_err->can_read(0)) { | ||
| 104 | sysread($err, $string, 4096); | ||
| 105 | $output .= $string // ''; | ||
| 106 | last; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (++$count >= 3) { | ||
| 110 | $output = 'timeout'; | ||
| 111 | kill -9, $pid; | ||
| 112 | print "$Y*** Killed Clojure REPL server ($pid)$Z\n"; | ||
| 113 | undef $pid; | ||
| 114 | $rc = 255; | ||
| 115 | last; | ||
| 116 | } | ||
| 117 | |||
| 118 | usleep 500_000; | ||
| 119 | } | ||
| 120 | |||
| 121 | $output =~ s/\n*\z//; | ||
| 122 | $output .= "\n" if length $output; | ||
| 123 | |||
| 124 | print STDOUT "${Y}Clojure:$Z\n$output\n"; | ||
| 125 | } | ||
| 126 | |||
| 127 | END { | ||
| 128 | if (defined $pid) { | ||
| 129 | print $in '(java.lang.System/exit 0)', "\n"; | ||
| 130 | waitpid( $pid, 0 ); | ||
| 131 | my $rc = $? >> 8; | ||
| 132 | print "$Y*** Stopped Clojure REPL server ($pid)$Z\n"; | ||
| 133 | exit $rc unless $rc == 0; | ||
| 134 | } | ||
| 135 | } | ||
| 136 | |||
| 137 | 1; |