Skip to content

Commit

Permalink
Add command to build and run ocamllsp
Browse files Browse the repository at this point in the history
This command will exit with an error message unless it's run within a
dune project with a dune.lock directory containing a lockfile for the
package "ocaml" so it's not ready for use by editors. Support for
falling back to a system or opam-managed installation of ocamllsp will
come in a later change.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
  • Loading branch information
gridbugs committed Sep 23, 2024
1 parent d92b74e commit 5d674c4
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/lock_dev_tool.ml
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,4 @@ let lock_ocamlformat () =
;;

let lock_odoc () = lock_dev_tool Odoc None
let lock_ocamllsp () = lock_dev_tool Ocamllsp None
1 change: 1 addition & 0 deletions bin/lock_dev_tool.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ open! Import
val is_enabled : bool Lazy.t
val lock_ocamlformat : unit -> unit Memo.t
val lock_odoc : unit -> unit Memo.t
val lock_ocamllsp : unit -> unit Memo.t
1 change: 1 addition & 0 deletions bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let all : _ Cmdliner.Cmd.t list =
; Promotion.group
; Pkg.group
; Pkg.Alias.group
; Tools.group
]
in
terms @ groups
Expand Down
62 changes: 62 additions & 0 deletions bin/tools/ocamllsp.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
open! Import
module Pkg_dev_tool = Dune_rules.Pkg_dev_tool

let ocamllsp_exe_path = Path.build @@ Pkg_dev_tool.exe_path Ocamllsp
let ocamllsp_exe_name = Pkg_dev_tool.exe_name Ocamllsp

(* Replace the current dune process with ocamllsp. *)
let run_ocamllsp common ~args =
let exe_path_string = Path.to_string ocamllsp_exe_path in
Console.print_user_message
(Dune_rules.Pkg_build_progress.format_user_message
~verb:"Running"
~object_:
(User_message.command (String.concat ~sep:" " (ocamllsp_exe_name :: args))));
Console.finish ();
restore_cwd_and_execve common exe_path_string (exe_path_string :: args) Env.initial
;;

let build_ocamllsp common =
let open Fiber.O in
let+ result =
Build_cmd.run_build_system ~common ~request:(fun _build_system ->
Action_builder.path ocamllsp_exe_path)
in
match result with
| Error `Already_reported -> raise Dune_util.Report_error.Already_reported
| Ok () -> ()
;;

let is_in_dune_project builder =
Workspace_root.create
~default_is_cwd:(Common.Builder.default_root_is_cwd builder)
~specified_by_user:(Common.Builder.root builder)
|> Result.is_ok
;;

let term =
let+ builder = Common.Builder.term
and+ args = Arg.(value & pos_all string [] (info [] ~docv:"ARGS")) in
match is_in_dune_project builder with
| false ->
User_error.raise
[ Pp.textf
"Unable to run %s as a dev-tool because you don't appear to be inside a dune \
project."
ocamllsp_exe_name
]
| true ->
let common, config = Common.init builder in
Scheduler.go ~common ~config (fun () ->
let open Fiber.O in
let* () = Lock_dev_tool.lock_ocamllsp () |> Memo.run in
let+ () = build_ocamllsp common in
run_ocamllsp common ~args)
;;

let info =
let doc = "Run ocamllsp, installing it as a dev tool if necessary." in
Cmd.info "ocamllsp" ~doc
;;

let command = Cmd.v info term
4 changes: 4 additions & 0 deletions bin/tools/ocamllsp.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
open! Import

(** Command to run ocamllsp, installing it if necessary *)
val command : unit Cmd.t
11 changes: 11 additions & 0 deletions bin/tools/tools.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open! Import

module Exec = struct
let doc = "Command group for running wrapped tools."
let info = Cmd.info ~doc "exec"
let group = Cmd.group info [ Ocamllsp.command ]
end

let doc = "Command group for wrapped tools."
let info = Cmd.info ~doc "tools"
let group = Cmd.group info [ Exec.group ]
3 changes: 3 additions & 0 deletions bin/tools/tools.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
open Import

val group : unit Cmd.t
2 changes: 2 additions & 0 deletions src/dune_rules/dune_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ module Executables = Executables
module Tests = Tests
module Stanzas = Stanzas
module Lock_dir = Lock_dir
module Pkg_dev_tool = Pkg_dev_tool
module Pkg_build_progress = Pkg_build_progress

module Install_rules = struct
let install_file = Install_rules.install_file
Expand Down

0 comments on commit 5d674c4

Please sign in to comment.