| 1 |
open Dune_rpc.V1 |
| 2 |
open Lwt.Syntax |
| 3 |
|
| 4 |
module V1 = struct |
| 5 |
module Fiber = struct |
| 6 |
include Lwt |
| 7 |
|
| 8 |
let collect_errors f = |
| 9 |
Lwt.catch |
| 10 |
(fun () -> f () |> Lwt.map (fun x -> Ok x)) |
| 11 |
(fun exn -> Lwt.return_error [ exn ]) |
| 12 |
;; |
| 13 |
|
| 14 |
let fork_and_join_unit (x : unit -> unit Lwt.t) y = |
| 15 |
let open Lwt in |
| 16 |
Lwt.both (x ()) (y ()) >|= snd |
| 17 |
;; |
| 18 |
|
| 19 |
let finalize f ~finally = Lwt.finalize f finally |
| 20 |
|
| 21 |
let parallel_iter ls ~f = |
| 22 |
let stream = Lwt_stream.from ls in |
| 23 |
Lwt_stream.iter_p f stream |
| 24 |
;; |
| 25 |
|
| 26 |
module Ivar = struct |
| 27 |
type 'a t = 'a Lwt.t * 'a Lwt.u |
| 28 |
|
| 29 |
let create () = Lwt.task () |
| 30 |
|
| 31 |
let fill (_, u) x = |
| 32 |
Lwt.wakeup u x; |
| 33 |
Lwt.return_unit |
| 34 |
;; |
| 35 |
|
| 36 |
let read (x, _) = x |
| 37 |
end |
| 38 |
|
| 39 |
module O = Syntax |
| 40 |
end |
| 41 |
|
| 42 |
module Client = |
| 43 |
Client.Make |
| 44 |
(Fiber) |
| 45 |
(struct |
| 46 |
type t = Lwt_io.input_channel * Lwt_io.output_channel |
| 47 |
|
| 48 |
let read (i, o) = |
| 49 |
(* The input and output channels share the same file descriptor. If |
| 50 |
the output channel has been closed, reading from the input channel |
| 51 |
will result in an error. *) |
| 52 |
let is_channel_closed () = Lwt_io.is_closed o in |
| 53 |
let open Csexp.Parser in |
| 54 |
let lexer = Lexer.create () in |
| 55 |
let rec loop depth stack = |
| 56 |
if is_channel_closed () |
| 57 |
then ( |
| 58 |
Lexer.feed_eoi lexer; |
| 59 |
Lwt.return_none) |
| 60 |
else |
| 61 |
let* res = Lwt_io.read_char_opt i in |
| 62 |
match res with |
| 63 |
| None -> |
| 64 |
Lexer.feed_eoi lexer; |
| 65 |
Lwt.return_none |
| 66 |
| Some c -> |
| 67 |
(match Lexer.feed lexer c with |
| 68 |
| Await -> loop depth stack |
| 69 |
| Lparen -> loop (depth + 1) (Stack.open_paren stack) |
| 70 |
| Rparen -> |
| 71 |
let stack = Stack.close_paren stack in |
| 72 |
let depth = depth - 1 in |
| 73 |
if depth = 0 |
| 74 |
then ( |
| 75 |
let sexps = Stack.to_list stack in |
| 76 |
sexps |> List.hd |> Lwt.return_some) |
| 77 |
else loop depth stack |
| 78 |
| Atom count -> |
| 79 |
if is_channel_closed () |
| 80 |
then ( |
| 81 |
Lexer.feed_eoi lexer; |
| 82 |
Lwt.return_none) |
| 83 |
else |
| 84 |
let* atom = |
| 85 |
let bytes = Bytes.create count in |
| 86 |
let+ () = Lwt_io.read_into_exactly i bytes 0 count in |
| 87 |
Bytes.to_string bytes |
| 88 |
in |
| 89 |
loop depth (Stack.add_atom atom stack)) |
| 90 |
in |
| 91 |
loop 0 Stack.Empty |
| 92 |
;; |
| 93 |
|
| 94 |
let close (_, o) = Lwt_io.close o |
| 95 |
|
| 96 |
let write (_, o) csexps = |
| 97 |
Lwt_list.iter_s (fun sexp -> Lwt_io.write o (Csexp.to_string sexp)) csexps |
| 98 |
;; |
| 99 |
end) |
| 100 |
|
| 101 |
module Where = |
| 102 |
Where.Make |
| 103 |
(Fiber) |
| 104 |
(struct |
| 105 |
let read_file s : (string, exn) result Lwt.t = |
| 106 |
Lwt.catch |
| 107 |
(fun () -> Lwt_result.ok (Lwt_io.with_file ~mode:Input s Lwt_io.read)) |
| 108 |
Lwt_result.fail |
| 109 |
;; |
| 110 |
|
| 111 |
let analyze_path s = |
| 112 |
Lwt.try_bind |
| 113 |
(fun () -> Lwt_unix.stat s) |
| 114 |
(fun stat -> |
| 115 |
Lwt.return |
| 116 |
(match stat.st_kind with |
| 117 |
| Unix.S_SOCK -> Ok `Unix_socket |
| 118 |
| S_REG -> Ok `Normal_file |
| 119 |
| _ -> Ok `Other)) |
| 120 |
(fun e -> Lwt.return (Error e)) |
| 121 |
;; |
| 122 |
end) |
| 123 |
|
| 124 |
let connect_chan where = |
| 125 |
let+ fd = |
| 126 |
let domain, sockaddr = |
| 127 |
match where with |
| 128 |
| `Unix socket -> Unix.PF_UNIX, Unix.ADDR_UNIX socket |
| 129 |
| `Ip (`Host host, `Port port) -> |
| 130 |
let addr = Unix.inet_addr_of_string host in |
| 131 |
Unix.PF_INET, Unix.ADDR_INET (addr, port) |
| 132 |
in |
| 133 |
let fd = Lwt_unix.socket domain Unix.SOCK_STREAM 0 in |
| 134 |
let+ () = Lwt_unix.connect fd sockaddr in |
| 135 |
fd |
| 136 |
in |
| 137 |
let fd mode = Lwt_io.of_fd fd ~mode in |
| 138 |
fd Input, fd Output |
| 139 |
;; |
| 140 |
end |