Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch Inotify to irmin-watcher #65

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/bin/dune
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
(name serve)
(virtual_modules serve)
(modules serve)
(libraries fpath slipshow))
(libraries fpath slipshow lwt))
12 changes: 3 additions & 9 deletions compiler/src/bin/native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
(implements serve)
(preprocess
(pps ppx_blob))
(preprocessor_deps client/client.bc.js)
(libraries
slipshow
fpath
lwt
inotify.lwt
dream
; bos
))
(preprocessor_deps
(file client/client.bc.js))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the dune manual, (file <filename>) and <filename> is the same. What is the reason to prefer on to the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that was from testing the ppx_blob issue I mentioned above

(libraries slipshow fpath lwt irmin-watcher dream))
79 changes: 37 additions & 42 deletions compiler/src/bin/native/serve.ml
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
open Lwt.Syntax

(* A promise that never returns and consumes a file
unwatcher *)
let wait_forever (_unwatch : unit -> unit Lwt.t) =
let forever, _ = Lwt.wait () in
forever

let do_watch input f =
match input with
| `Stdin -> Error (`Msg "--watch is incompatible with stdin input")
| `File input ->
let parent = Fpath.parent input in
let parent = Fpath.to_string parent in
let input_filename = Fpath.filename input in
let inotify = Inotify.create () in
let _watch_descriptor =
Inotify.add_watch inotify parent [ Inotify.S_Close_write ]
let callback filename =
if String.equal filename input_filename then (
Logs.app (fun m -> m "Recompiling");
match f () with
| Ok _ -> Lwt.return_unit
| Error (`Msg s) ->
Logs.warn (fun m -> m "%s" s);
Lwt.return_unit)
else Lwt.return_unit
in
let rec loop () =
let events = Inotify.read inotify in
List.iter
(function
| _, _, _, Some filename ->
if String.equal filename input_filename then (
Logs.app (fun m -> m "Recompiling");
match f () with
| Ok _ -> ()
| Error (`Msg s) -> Logs.warn (fun m -> m "%s" s))
else ()
| _ -> ())
events;
loop ()
let main =
let* unwatch = Irmin_watcher.hook 0 parent callback in
wait_forever unwatch
in
loop ()
Lwt_main.run main

let html_source =
Format.sprintf
Expand All @@ -51,7 +54,7 @@ let html_source =
</body>
</html>
|html}
[%blob "compiler/src/bin/native/client/client.bc.js"]
[%blob "client/client.bc.js"]
Comment on lines -54 to +57
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm it seems to work in both situation, is it some blob ppx magic? Anyway, the short form is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The long form doesn't work when you vendor (in the dune sense) slipshow ^^"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes! I understand. Thanks for the explanation :)


let do_serve input f =
let cond = Lwt_condition.create () in
Expand All @@ -64,10 +67,6 @@ let do_serve input f =
let parent = Fpath.parent input in
let parent = Fpath.to_string parent in
let input_filename = Fpath.filename input in
let* inotify = Lwt_inotify.create () in
let _watch_descriptor =
Lwt_inotify.add_watch inotify parent [ Inotify.S_Close_write ]
in
let content = ref "" in
let new_content =
match f () with
Expand All @@ -92,26 +91,22 @@ let do_serve input f =
Dream.respond !content);
]
in
let rec loop () =
let* _descriptor, _event_kinds, _, filename =
Lwt_inotify.read inotify
in
match filename with
| Some filename when String.equal filename input_filename ->
Logs.app (fun m -> m "Recompiling");
let new_content =
match f () with
| Ok s -> Slipshow.delayed_to_string s
| Error (`Msg s) ->
Logs.warn (fun m -> m "%s" s);
s
in
content := new_content;
Lwt_condition.broadcast cond ();
loop ()
| _ -> loop ()
let callback filename =
if String.equal filename input_filename then (
Logs.app (fun m -> m "Recompiling");
let new_content =
match f () with
| Ok s -> Slipshow.delayed_to_string s
| Error (`Msg s) ->
Logs.warn (fun m -> m "%s" s);
s
in
content := new_content;
Lwt_condition.broadcast cond ());
Lwt.return_unit
in
loop ()
let* unwatch = Irmin_watcher.hook 0 parent callback in
wait_forever unwatch
in
Logs.app (fun m ->
m
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
base64
bos
lwt
inotify
irmin-watcher
js_of_ocaml-compiler
js_of_ocaml-lwt
magic-mime
Expand Down
2 changes: 1 addition & 1 deletion slipshow.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ depends: [
"base64"
"bos"
"lwt"
"inotify"
"irmin-watcher"
"js_of_ocaml-compiler"
"js_of_ocaml-lwt"
"magic-mime"
Expand Down
Loading