Skip to content

Commit 373b03b

Browse files
committed
Use the logic in pkg_enabled to detect package management
Re-use the function in pkg_enabled, and move it to pkg_common to avoid repitition. There is already a similar function `check_pkg_management_enabled` in pkg_common, but we're not using it here because its purpose is to just see if dune-workspace has (pkg enabled), and it doesn't cover all the cases we want to check, to know whether package management is enabled. Signed-off-by: Sudha Parimala <sudharg247@gmail.com>
1 parent d4efea8 commit 373b03b

4 files changed

Lines changed: 33 additions & 26 deletions

File tree

bin/install_uninstall.ml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -517,21 +517,21 @@ let run
517517
User_error.raise
518518
[ Pp.textf "Context %S not found!" (Dune_engine.Context_name.to_string name) ])
519519
in
520-
(match
521-
List.find contexts ~f:(fun ctx ->
522-
match Context.kind ctx with
523-
| Context.Kind.Lock _ -> true
524-
| _ -> false)
525-
with
526-
| Some _ ->
527-
User_error.raise
528-
[ Pp.text "dune install is not supported with Dune package management." ]
529-
~hints:
530-
[ Pp.concat
531-
~sep:Pp.space
532-
[ Pp.text "Use"; User_message.command "opam"; Pp.text "instead." ]
533-
]
534-
| None -> ());
520+
let* source_workspace = Memo.run (Source.Workspace.workspace ()) in
521+
let lock_dir_paths =
522+
Pkg.Pkg_common.Lock_dirs_arg.lock_dirs_of_workspace
523+
Pkg.Pkg_common.Lock_dirs_arg.all
524+
source_workspace
525+
in
526+
if Pkg.Pkg_common.pkg_enabled ~workspace:source_workspace ~lock_dir_paths
527+
then
528+
User_error.raise
529+
[ Pp.text "dune install is not supported with Dune package management." ]
530+
~hints:
531+
[ Pp.concat
532+
~sep:Pp.space
533+
[ Pp.text "Use"; User_message.command "opam"; Pp.text "instead." ]
534+
];
535535
let* pkgs =
536536
match pkgs with
537537
| _ :: _ -> Fiber.return pkgs

bin/pkg/pkg_common.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ module Lock_dirs_arg = struct
207207
;;
208208
end
209209

210+
let pkg_enabled ~(workspace : Workspace.t) ~lock_dir_paths =
211+
(* CR-Leonidas-from-XIV: change this logic when we stop detecting lock
212+
directories in the source tree *)
213+
let any_lockdir_exists =
214+
List.exists lock_dir_paths ~f:(fun p -> Fpath.exists (Path.Source.to_string p))
215+
in
216+
match workspace.config.pkg_enabled with
217+
| Set (_, `Enabled) -> true
218+
| Set (_, `Disabled) -> false
219+
| Unset -> any_lockdir_exists
220+
;;
221+
210222
let check_pkg_management_enabled () =
211223
Memo.run
212224
@@

bin/pkg/pkg_common.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ val pp_packages : Dune_pkg.Lock_dir.Pkg.t list -> User_message.Style.t Pp.t
8484

8585
(** [check_pkg_management_enabled ()] checks if package management is enabled in the
8686
workspace configuration. Raises a user error if it is explicitly disabled. *)
87+
val pkg_enabled
88+
: workspace:Workspace.t
89+
-> lock_dir_paths:Path.Source.t list
90+
-> bool
91+
8792
val check_pkg_management_enabled : unit -> unit Fiber.t

bin/pkg/pkg_enabled.ml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@ let term =
1414
Pkg_common.Lock_dirs_arg.all
1515
workspace
1616
in
17-
let any_lockdir_exists =
18-
List.exists lock_dir_paths ~f:(fun p -> Fpath.exists (Path.Source.to_string p))
19-
in
20-
(* CR-Leonidas-from-XIV: change this logic when we stop detecting lock
21-
directories in the source tree *)
22-
let enabled =
23-
match workspace.config.pkg_enabled with
24-
| Set (_, `Enabled) -> true
25-
| Set (_, `Disabled) -> false
26-
| Unset -> any_lockdir_exists
27-
in
17+
let enabled = Pkg_common.pkg_enabled ~workspace ~lock_dir_paths in
2818
match enabled with
2919
| true -> ()
3020
| false -> exit 1)

0 commit comments

Comments
 (0)