Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
moyodiallo committed Apr 20, 2023
1 parent bcb5b46 commit ed3ea10
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
49 changes: 26 additions & 23 deletions deps.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ let copy_rules =
(fun d_item ->
d_item
|> Describe_external_lib.get_item
|> (fun (item:Describe_external_lib.item) -> item.source_dir ^ "/dune")
|> (fun (item:Describe_external_lib.item) -> String.cat item.source_dir "/dune")
|> (Dune_rules.Copy_rules.get_copy_rules))
|> Dune_rules.Copy_rules.copy_rules_map

Expand All @@ -54,7 +54,7 @@ let find_exe_item_package (item:Describe_external_lib.item) =
(* Only allow for private executables to find the package *)
item.extensions
|> List.find_map (fun extension ->
let bin_name = Dune_rules.Copy_rules.find_dest_name ~name:(item.name ^ extension) copy_rules in
let bin_name = Dune_rules.Copy_rules.find_dest_name ~name:(String.cat item.name extension) copy_rules in
Option.map (fun (item:Describe_entries.item) -> item.package) (Item_map.find_opt bin_name bin_of_entries))

let get_dune_items dir_types ~pkg ~target =
Expand All @@ -65,18 +65,21 @@ let get_dune_items dir_types ~pkg ~target =
* because it will be resolve with separate request*)
let open Describe_external_lib in
let get_name = function
| Lib item -> item.name ^ ".lib"
| Exe item -> item.name ^ ".exe"
| Test item -> item.name ^ ".test"
| Lib item -> String.cat item.name ".lib"
| Exe item -> String.cat item.name ".exe"
| Test item -> String.cat item.name ".test"
in
let d_items_lib =
d_items
|> List.filter is_lib_item
|> List.map (fun d_item ->
d_item
|> get_item
|> (fun (item:Describe_external_lib.item) ->
(item.name ^ ".lib", Lib item)))
|> List.filter_map (fun d_item ->
match is_lib_item d_item with
| true ->
d_item
|> get_item
|> (fun (item:Describe_external_lib.item) ->
(String.cat item.name ".lib", Lib item))
|> Option.some
| false -> None)
|> List.to_seq |> Hashtbl.of_seq
in
let rec add_internal acc = function
Expand All @@ -87,12 +90,12 @@ let get_dune_items dir_types ~pkg ~target =
else begin
Hashtbl.add acc (get_name item) item;
(get_item item).internal_deps
|> List.filter (fun (_, k) -> Kind.is_required k)
|> List.filter_map (fun (name, _) ->
match Hashtbl.find_opt d_items_lib (name ^ ".lib") with
|> List.filter_map (fun (name, k) ->
match Hashtbl.find_opt d_items_lib (String.cat name ".lib") with
| None -> None
| Some d_item_lib ->
if Option.is_some (get_item d_item_lib).package then None else Some d_item_lib)
if Kind.is_required k && Option.is_some (get_item d_item_lib).package then None
else Some d_item_lib)
|> fun internals -> add_internal acc (tl @ internals)
end
in
Expand Down Expand Up @@ -129,13 +132,13 @@ let get_dune_items dir_types ~pkg ~target =
let lib_deps ~pkg ~target =
get_dune_items (Hashtbl.create 10) ~pkg ~target
|> List.map Describe_external_lib.get_item
|> List.fold_left (fun acc (item:Describe_external_lib.item) ->
List.map (fun dep -> (fst dep, item.source_dir)) item.external_deps @ acc) []
|> List.fold_left (fun acc (lib,path) ->
if Astring.String.take ~sat:((<>) '.') lib <> pkg then
let dirs = Libraries.find_opt lib acc |> Option.value ~default:Dir_set.empty in
Libraries.add lib (Dir_set.add path dirs) acc
else
acc) Libraries.empty
|> List.fold_left (fun libs (item:Describe_external_lib.item) ->
List.map (fun dep -> fst dep, item.source_dir) item.external_deps
|> List.fold_left (fun acc (lib,path) ->
if Astring.String.take ~sat:((<>) '.') lib <> pkg then
let dirs = Libraries.find_opt lib acc |> Option.value ~default:Dir_set.empty in
Libraries.add lib (Dir_set.add path dirs) acc
else
acc) libs) Libraries.empty

let get_external_lib_deps ~pkg ~target : t = lib_deps ~pkg ~target
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(sexplib (>= v0.14.0))
(cmdliner (>= 1.1.0))
(stdune (>= 3.0))
(ocaml (>= 4.10.0))
(ocaml (>= 4.13.0))
(bos (>= 0.2.0))
(fmt (>= 0.8.9))
(opam-state (>= 2.0.9))
Expand Down
2 changes: 1 addition & 1 deletion dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ let update (changes:(_ * Change.t list) Paths.t) (t:t) =
match package_name items with
| None -> failwith "Missing 'name' in (package)!"
| Some name ->
match Paths.find_opt (name ^ ".opam") changes with
match Paths.find_opt (String.cat name ".opam") changes with
| None -> items
| Some (_opam, changes) -> update_or_create "depends" (apply_changes ~changes) items
in
Expand Down
2 changes: 1 addition & 1 deletion opam-dune-lint.opam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ depends: [
"sexplib" {>= "v0.14.0"}
"cmdliner" {>= "1.1.0"}
"stdune" {>= "3.0"}
"ocaml" {>= "4.10.0"}
"ocaml" {>= "4.13.0"}
"bos" {>= "0.2.0"}
"fmt" {>= "0.8.9"}
"opam-state" {>= "2.0.9"}
Expand Down

0 comments on commit ed3ea10

Please sign in to comment.