Skip to content

Commit

Permalink
add variant for dune pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
PizieDust committed Feb 20, 2025
1 parent 33e79ef commit 207ce17
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
53 changes: 44 additions & 9 deletions src/sandbox.ml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ type t =
| Esy of Esy.t * Esy.Manifest.t
| Global
| Custom of string
| Dune_pkg of string

let equal t1 t2 =
match t1, t2 with
| Global, Global -> true
| Esy (e1, p1), Esy (e2, p2) -> Esy.Manifest.equal p1 p2 && Esy.equal e1 e2
| Opam (o1, s1), Opam (o2, s2) -> Opam.Switch.equal s1 s2 && Opam.equal o1 o2
| Custom s1, Custom s2 -> String.equal s1 s2
| Dune_pkg s1, Dune_pkg s2 -> String.equal s1 s2
| _, _ -> false
;;

Expand All @@ -76,6 +78,7 @@ let to_string = function
| Opam (_, switch) -> Printf.sprintf "opam(%s)" (Opam.Switch.name switch)
| Global -> "global"
| Custom _ -> "custom"
| Dune_pkg _ -> "Dune Package Manager"
;;

let to_pretty_string t =
Expand All @@ -91,6 +94,7 @@ let to_pretty_string t =
print_opam project_name
| Global -> "Global OCaml"
| Custom _ -> "Custom OCaml"
| Dune_pkg _ -> "Dune Package Manager"
;;

module Kind = struct
Expand All @@ -99,12 +103,14 @@ module Kind = struct
| Esy
| Global
| Custom
| Dune_pkg

let of_string = function
| "opam" -> Some Opam
| "esy" -> Some Esy
| "global" -> Some Global
| "custom" -> Some Custom
| "Dune Package Manager" -> Some Dune_pkg
| _ -> None
;;

Expand All @@ -121,6 +127,7 @@ module Kind = struct
| Esy -> "esy"
| Global -> "global"
| Custom -> "custom"
| Dune_pkg -> "Dune Package Manager"
;;

let to_json s = Jsonoo.Encode.string (to_string s)
Expand All @@ -132,12 +139,14 @@ module Setting = struct
| Esy of Esy.Manifest.t
| Global
| Custom of string
| Dune_pkg of string

let kind : t -> Kind.t = function
| Opam _ -> Opam
| Esy _ -> Esy
| Global -> Global
| Custom _ -> Custom
| Dune_pkg _ -> Dune_pkg
;;

let of_json json =
Expand All @@ -161,6 +170,9 @@ module Setting = struct
| Custom ->
let template = Jsonoo.Decode.field "template" decode_vars json in
Custom template
| Dune_pkg ->
let template = Jsonoo.Decode.field "template" decode_vars json in
Dune_pkg template
;;

let to_json (t : t) =
Expand All @@ -174,6 +186,7 @@ module Setting = struct
[ kind; "root", encode_vars @@ (manifest |> Esy.Manifest.path |> Path.to_string) ]
| Opam switch -> object_ [ kind; "switch", encode_vars @@ Opam.Switch.name switch ]
| Custom template -> object_ [ kind; "template", string template ]
| Dune_pkg template -> object_ [ kind; "template", string template ]
;;

let t = Settings.create_setting ~scope:Workspace ~key:"sandbox" ~of_json ~to_json
Expand Down Expand Up @@ -231,6 +244,7 @@ let of_settings () : t option Promise.t =
None))
| Some Global -> Promise.return (Some Global)
| Some (Custom template) -> Promise.return (Some (Custom template))
| Some (Dune_pkg template) -> Promise.return (Some (Dune_pkg template))
;;

(** If [Workspace.workspaceFolders()] returns a list with a single element,
Expand Down Expand Up @@ -289,10 +303,8 @@ let detect_dune_pkg ~project_root () =
let+ exists = Fs.exists (Path.to_string dune_lock_path) in
if exists
then (
show_message
`Info
"Dune Pkg detected. Configuring a custom sandbox for the workspace.";
Some (Custom "$prog $args"))
show_message `Info "Dune Package Manager detected.";
Some (Dune_pkg "$prog $args"))
else None
;;

Expand Down Expand Up @@ -323,6 +335,7 @@ let save_to_settings sandbox =
| Opam (_, switch) -> Setting.Opam switch
| Global -> Setting.Global
| Custom template -> Setting.Custom template
| Dune_pkg template -> Setting.Dune_pkg template
in
Settings.set ~section:"ocaml" Setting.t (to_setting sandbox)
;;
Expand Down Expand Up @@ -350,7 +363,7 @@ module Candidate = struct
then Some (switch_kind_s ^ " | Currently active switch in project root")
else Some switch_kind_s
| Esy (_, _) -> Some "Esy"
| Global | Custom _ -> None)
| Global | Custom _ | Dune_pkg _ -> None)
in
match sandbox with
| Opam (_, Named name) -> create ~label:name ?description ()
Expand All @@ -375,6 +388,12 @@ module Candidate = struct
~label:"Custom"
~detail:"Custom sandbox using a command template"
()
| Dune_pkg _ ->
create
?description
~label:"Dune Package Manager"
~detail:"Use Dune Package Management for this project"
()
;;

let ok sandbox = { sandbox; status = Ok () }
Expand Down Expand Up @@ -458,8 +477,9 @@ let sandbox_candidates ~workspace_folders =
(* doesn't matter what the custom fields are set to here user will input
custom commands in [select] *)
in
let dune_pkg = Candidate.ok (Dune_pkg "$prog $args") in
let+ esy, (opam, current_switch) = Promise.all2 (esy, opam) in
let cs = (global :: custom :: esy) @ opam in
let cs = (dune_pkg :: global :: custom :: esy) @ opam in
Option.value_map current_switch ~default:cs ~f:(fun current_switch ->
current_switch :: cs)
;;
Expand Down Expand Up @@ -510,7 +530,7 @@ let get_command sandbox bin args : Cmd.t =
| Opam (opam, switch) -> Opam.exec opam switch ~args:(bin :: args)
| Esy (esy, manifest) -> Esy.exec esy manifest ~args:(bin :: args)
| Global -> Spawn { bin = Path.of_string bin; args }
| Custom template ->
| Custom template | Dune_pkg template ->
let command =
template
|> String.substr_replace_all ~pattern:"$prog" ~with_:(Cmd.quote bin)
Expand Down Expand Up @@ -546,7 +566,7 @@ let packages t =
let open Promise.Result.Syntax in
match t with
| Global -> Promise.Result.return []
| Custom _ -> Promise.Result.return []
| Custom _ | Dune_pkg _ -> Promise.Result.return []
| Esy (esy, manifest) ->
let+ r = Esy.packages esy manifest in
List.map r ~f:Package.of_esy
Expand All @@ -559,7 +579,7 @@ let root_packages t =
let open Promise.Result.Syntax in
match t with
| Global -> Promise.Result.return []
| Custom _ -> Promise.Result.return []
| Custom _ | Dune_pkg _ -> Promise.Result.return []
| Esy (esy, manifest) ->
let+ r = Esy.root_packages esy manifest in
List.map r ~f:Package.of_esy
Expand All @@ -583,6 +603,11 @@ let uninstall_packages t packages =
| Custom _ ->
show_message `Error "Uninstalling packages is not supported for Custom sandboxes";
Promise.return ()
| Dune_pkg _ ->
show_message
`Error
"Uninstalling packages is not yet supported for this Dune Package Manager";
Promise.return ()
| Esy (_esy, _manifest) ->
(* TODO: Implement Esy sandbox inspection *)
show_message `Error "Uninstalling packages is not supported for Esy sandboxes";
Expand Down Expand Up @@ -628,6 +653,11 @@ let install_packages t packages =
| Custom _ ->
show_message `Error "Installing packages is not supported for Custom sandboxes";
Promise.return ()
| Dune_pkg _ ->
show_message
`Error
"Installing packages is not yet supported for this Dune Package Manager";
Promise.return ()
| Esy (_esy, _manifest) ->
(* TODO: Implement Esy sandbox inspection *)
show_message `Error "Installing packages is not supported for Esy sandboxes";
Expand Down Expand Up @@ -667,6 +697,11 @@ let upgrade_packages t =
| Custom _ ->
show_message `Error "Upgrading packages is not supported for Custom sandboxes";
Promise.return ()
| Dune_pkg _ ->
show_message
`Error
"Upgrading packages is not yet supported for this Dune Package Manager";
Promise.return ()
| Esy (_esy, _manifest) ->
(* TODO: Implement Esy sandbox inspection *)
show_message `Error "Upgrading packages is not supported for Esy sandboxes";
Expand Down
1 change: 1 addition & 0 deletions src/sandbox.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type t =
| Esy of Esy.t * Esy.Manifest.t
| Global
| Custom of string
| Dune_pkg of string

val equal : t -> t -> bool
val to_string : t -> string
Expand Down

0 comments on commit 207ce17

Please sign in to comment.