Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Alpha DIALLO <[email protected]>
  • Loading branch information
moyodiallo committed Sep 30, 2024
1 parent 0f47762 commit 69ea9cf
Showing 1 changed file with 55 additions and 68 deletions.
123 changes: 55 additions & 68 deletions bin/build_cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,40 +82,11 @@ let run_build_system ~common ~request =
Fiber.return ())
;;

let lock_ocamlformat () =
if Lazy.force Lock_dev_tool.is_enabled
then
(* Note that generating the ocamlformat lockdir here means
that it will be created when a user runs `dune fmt` but not
when a user runs `dune build @fmt`. It's important that
this logic remain outside of `dune build`, as `dune
build` is intended to only build targets, and generating
a lockdir is not building a target. *)
Lock_dev_tool.lock_ocamlformat () |> Memo.run
else Fiber.return ()
let run_build_command_poll_eager ~(common : Common.t) ~request =
Scheduler.Run.poll (run_build_system ~common ~request)
;;

let run_build_command_poll_eager
~with_lock_ocamlformat
~(common : Common.t)
~config
~request
: unit
=
let open Fiber.O in
Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config (fun () ->
Scheduler.Run.poll
(let* () = if with_lock_ocamlformat then lock_ocamlformat () else Fiber.return () in
run_build_system ~common ~request))
;;

let run_build_command_poll_passive
~with_lock_ocamlformat
~(common : Common.t)
~config
~request:_
: unit
=
let run_build_command_poll_passive ~(common : Common.t) ~config ~request:_ =
(* CR-someday aalekseyev: It would've been better to complain if [request] is
non-empty, but we can't check that here because [request] is a function.*)
let open Fiber.O in
Expand All @@ -124,51 +95,67 @@ let run_build_command_poll_passive
| `Allow server -> server
| `Forbid_builds -> Code_error.raise "rpc server must be allowed in passive mode" []
in
Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config (fun () ->
Scheduler.Run.poll_passive
~get_build_request:
(let* () =
if with_lock_ocamlformat then lock_ocamlformat () else Fiber.return ()
in
let+ (Build (targets, ivar)) = Dune_rpc_impl.Server.pending_build_action rpc in
let request setup =
Target.interpret_targets (Common.root common) config setup targets
in
run_build_system ~common ~request, ivar))
Scheduler.Run.poll_passive
~get_build_request:
(let+ (Build (targets, ivar)) = Dune_rpc_impl.Server.pending_build_action rpc in
let request setup =
Target.interpret_targets (Common.root common) config setup targets
in
run_build_system ~common ~request, ivar)
;;

let run_build_command_once ~with_lock_ocamlformat ~(common : Common.t) ~config ~request =
let run_build_command_once ~(common : Common.t) ~request =
let open Fiber.O in
let once () =
let* () = if with_lock_ocamlformat then lock_ocamlformat () else Fiber.return () in
let+ res = run_build_system ~common ~request in
match res with
| Error `Already_reported -> raise Dune_util.Report_error.Already_reported
| Ok () -> ()
in
Scheduler.go ~common ~config once
let+ res = run_build_system ~common ~request in
match res with
| Error `Already_reported -> raise Dune_util.Report_error.Already_reported
| Ok () -> ()
;;

let run_build_command ~(common : Common.t) ~config ~request =
(match Common.watch common with
| Yes Eager -> run_build_command_poll_eager
| Yes Passive -> run_build_command_poll_passive
| No -> run_build_command_once)
~with_lock_ocamlformat:false
~common
~config
~request
match Common.watch common with
| Yes Eager ->
(fun () -> run_build_command_poll_eager ~common ~request)
|> Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config
| Yes Passive ->
(fun () -> run_build_command_poll_passive ~common ~config ~request)
|> Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config
| No ->
(fun () -> run_build_command_once ~common ~request) |> Scheduler.go ~common ~config
;;

let run_build_command_fmt ~(common : Common.t) ~config ~request =
(match Common.watch common with
| Yes Eager -> run_build_command_poll_eager
| Yes Passive -> run_build_command_poll_passive
| No -> run_build_command_once)
~with_lock_ocamlformat:true
~common
~config
~request
let lock_ocamlformat () =
if Lazy.force Lock_dev_tool.is_enabled
then
(* Note that generating the ocamlformat lockdir here means
that it will be created when a user runs `dune fmt` but not
when a user runs `dune build @fmt`. It's important that
this logic remain outside of `dune build`, as `dune
build` is intended to only build targets, and generating
a lockdir is not building a target. *)
Lock_dev_tool.lock_ocamlformat () |> Memo.run
else Fiber.return ()
in
match Common.watch common with
| Yes Eager ->
(fun () ->
let open Fiber.O in
let* () = lock_ocamlformat () in
run_build_command_poll_eager ~common ~request)
|> Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config
| Yes Passive ->
(fun () ->
let open Fiber.O in
let* () = lock_ocamlformat () in
run_build_command_poll_passive ~common ~config ~request)
|> Scheduler.go_with_rpc_server_and_console_status_reporting ~common ~config
| No ->
(fun () ->
let open Fiber.O in
let* () = lock_ocamlformat () in
run_build_command_once ~common ~request)
|> Scheduler.go ~common ~config
;;

let runtest_info =
Expand Down

0 comments on commit 69ea9cf

Please sign in to comment.