From 1f9dd2ea94d3a77a38ba72c6279d5a3ee9d0ddb4 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Sun, 11 Aug 2024 14:07:37 +0800 Subject: [PATCH] refactor: remove [Dune_rules.Package] (#10818) Signed-off-by: Rudi Grinberg --- bin/arg.ml | 2 +- bin/arg.mli | 2 +- bin/common.ml | 3 +- bin/import.ml | 2 +- src/dune_pkg/opam_file.ml | 84 ++++++++++++++++++++++ src/dune_pkg/opam_file.mli | 7 ++ src/dune_rules/dune_project.ml | 2 +- src/dune_rules/dune_rules.ml | 1 - src/dune_rules/import.ml | 1 + src/dune_rules/install_rules.ml | 2 + src/dune_rules/only_packages.mli | 2 + src/dune_rules/package.ml | 87 ----------------------- src/dune_rules/package.mli | 7 -- src/dune_rules/package_map_traversals.ml | 1 - src/dune_rules/package_map_traversals.mli | 5 -- test/expect-tests/findlib_tests.ml | 8 ++- 16 files changed, 109 insertions(+), 107 deletions(-) delete mode 100644 src/dune_rules/package.ml delete mode 100644 src/dune_rules/package.mli delete mode 100644 src/dune_rules/package_map_traversals.ml delete mode 100644 src/dune_rules/package_map_traversals.mli diff --git a/bin/arg.ml b/bin/arg.ml index e4ff7f76526..7cffc60593a 100644 --- a/bin/arg.ml +++ b/bin/arg.ml @@ -11,7 +11,7 @@ include struct module Dep_conf = Dep_conf end -module Package = Dune_rules.Package +module Package = Dune_lang.Package module Context_name = Dune_engine.Context_name let package_name = conv Package.Name.conv diff --git a/bin/arg.mli b/bin/arg.mli index acad8fee152..c1f7b3f5a23 100644 --- a/bin/arg.mli +++ b/bin/arg.mli @@ -33,7 +33,7 @@ val dep : Dep.t conv val graph_format : Dune_graph.Graph.File_format.t conv val path : Path.t conv val external_path : Path.External.t conv -val package_name : Dune_rules.Package.Name.t conv +val package_name : Dune_lang.Package.Name.t conv val profile : Dune_lang.Profile.t conv val lib_name : Dune_lang.Lib_name.t conv val version : Dune_lang.Syntax.Version.t conv diff --git a/bin/common.ml b/bin/common.ml index da78905b5af..49811f2977a 100644 --- a/bin/common.ml +++ b/bin/common.ml @@ -14,7 +14,6 @@ end open struct open Dune_rules - module Package = Package module Colors = Colors module Only_packages = Only_packages end @@ -26,6 +25,8 @@ open struct module Manpage = Manpage end +module Package = Dune_lang.Package + module Let_syntax = struct let ( let+ ) t f = Term.(const f $ t) let ( and+ ) a b = Term.(const (fun x y -> x, y) $ a $ b) diff --git a/bin/import.ml b/bin/import.ml index 452bfaaa5c4..86679de7434 100644 --- a/bin/import.ml +++ b/bin/import.ml @@ -28,7 +28,6 @@ include struct module Super_context = Super_context module Context = Context module Workspace = Workspace - module Package = Package module Dune_project = Dune_project module Dune_package = Dune_package module Resolve = Resolve @@ -67,6 +66,7 @@ include struct module Profile = Profile module Lib_name = Lib_name module Package_name = Package_name + module Package = Package module Package_version = Package_version module Source_kind = Source_kind module Package_info = Package_info diff --git a/src/dune_pkg/opam_file.ml b/src/dune_pkg/opam_file.ml index bc36835de0c..cec17d64e9c 100644 --- a/src/dune_pkg/opam_file.ml +++ b/src/dune_pkg/opam_file.ml @@ -200,3 +200,87 @@ module Create = struct { file_contents; file_name } ;; end + +let load_opam_file_with_contents ~contents:opam_file_string file name = + let loc = Loc.in_file (Path.source file) in + let opam = + let opam = + let lexbuf = + Lexbuf.from_string opam_file_string ~fname:(Path.Source.to_string file) + in + try Ok (parse lexbuf) with + | User_error.E _ as exn -> Error exn + in + match opam with + | Ok s -> Some s + | Error exn -> + (* CR-rgrinberg: make it possible to disable this warning *) + User_warning.emit + ~loc + [ Pp.text + "Unable to read opam file. Some information about this package such as its \ + version will be ignored." + ; Pp.textf "Reason: %s" (Printexc.to_string exn) + ]; + None + in + let open Option.O in + let get_one name = + let* value = + let* opam = opam in + get_field opam name + in + match value.pelem with + | String s -> Some s + | _ -> None + in + let get_many name = + let* value = + let* opam = opam in + get_field opam name + in + match value.pelem with + | String s -> Some [ s ] + | List l -> + List.fold_left + l.pelem + ~init:(Some []) + ~f:(fun acc (v : OpamParserTypes.FullPos.value) -> + let* acc = acc in + match v.pelem with + | String s -> Some (s :: acc) + | _ -> None) + >>| List.rev + | _ -> None + in + let dir = Path.Source.parent_exn file in + let info = + Dune_lang.Package_info.create + ~maintainers:(get_many "maintainer") + ~authors:(get_many "authors") + ~homepage:(get_one "homepage") + ~bug_reports:(get_one "bug-reports") + ~documentation:(get_one "doc") + ~license:(get_many "license") + ~source: + (let+ url = get_one "dev-repo" in + Dune_lang.Source_kind.Url url) + in + Dune_lang.Package.create + ~name + ~dir + ~loc + ~version:(get_one "version" |> Option.map ~f:Package_version.of_string) + ~conflicts:[] + ~depends:[] + ~depopts:[] + ~info + ~synopsis:(get_one "synopsis") + ~description:(get_one "description") + ~has_opam_file:(Exists true) + ~tags:(Option.value (get_many "tags") ~default:[]) + ~deprecated_package_names:Package_name.Map.empty + ~sites:Dune_lang.Site.Map.empty + ~allow_empty:true + ~original_opam_file:(Some { file; contents = opam_file_string }) +;; diff --git a/src/dune_pkg/opam_file.mli b/src/dune_pkg/opam_file.mli index b070c9b24ce..6667e2ea628 100644 --- a/src/dune_pkg/opam_file.mli +++ b/src/dune_pkg/opam_file.mli @@ -31,3 +31,10 @@ module Create : sig val normalise_field_order : (string * value) list -> (string * value) list val of_bindings : (string * value) list -> file:Path.t -> t end + +(** Construct a package description from an opam file and its contents *) +val load_opam_file_with_contents + : contents:string + -> Path.Source.t + -> Package_name.t + -> Dune_lang.Package.t diff --git a/src/dune_rules/dune_project.ml b/src/dune_rules/dune_project.ml index 998365afc49..58b055f9389 100644 --- a/src/dune_rules/dune_project.ml +++ b/src/dune_rules/dune_project.ml @@ -933,7 +933,7 @@ let gen_load ~read ~dir ~files ~infer_from_opam_files : t option Memo.t = let loc = Loc.in_file (Path.source opam_file) in let pkg = let+ contents = read opam_file in - Package.load_opam_file_with_contents ~contents opam_file name + Dune_pkg.Opam_file.load_opam_file_with_contents ~contents opam_file name in (name, (loc, pkg)) :: acc) |> Package.Name.Map.of_list_exn diff --git a/src/dune_rules/dune_rules.ml b/src/dune_rules/dune_rules.ml index 58a068790ab..039a824791a 100644 --- a/src/dune_rules/dune_rules.ml +++ b/src/dune_rules/dune_rules.ml @@ -61,7 +61,6 @@ module Dune_project = Dune_project module Source_tree = Source_tree module Source_dir_status = Source_dir_status module Dune_file0 = Dune_file0 -module Package = Package module Dialect = Dialect module Private_context = Private_context module Odoc = Odoc diff --git a/src/dune_rules/import.ml b/src/dune_rules/import.ml index 3fc6ac7df5d..b8438000cab 100644 --- a/src/dune_rules/import.ml +++ b/src/dune_rules/import.ml @@ -100,6 +100,7 @@ include struct module Package_dependency = Package_dependency module Package_constraint = Package_constraint module Dune_project_name = Dune_project_name + module Package = Package end include Dune_engine.No_io diff --git a/src/dune_rules/install_rules.ml b/src/dune_rules/install_rules.ml index 13230ce5cd9..32cda7c9387 100644 --- a/src/dune_rules/install_rules.ml +++ b/src/dune_rules/install_rules.ml @@ -499,6 +499,8 @@ end = struct Some (name, entries) ;; + module Package_map_traversals = Memo.Make_parallel_map (Package.Name.Map) + let stanzas_to_entries sctx = let ctx = Context.build_context (Super_context.context sctx) in let* stanzas = Dune_load.dune_files ctx.name in diff --git a/src/dune_rules/only_packages.mli b/src/dune_rules/only_packages.mli index 53ec37f60b5..6c6b8759157 100644 --- a/src/dune_rules/only_packages.mli +++ b/src/dune_rules/only_packages.mli @@ -1,3 +1,5 @@ +open Import + (** Restrict the set of visible packages *) module Clflags : sig diff --git a/src/dune_rules/package.ml b/src/dune_rules/package.ml deleted file mode 100644 index 6968f20cc61..00000000000 --- a/src/dune_rules/package.ml +++ /dev/null @@ -1,87 +0,0 @@ -open! Import -include Dune_lang.Package -module Opam_file = Dune_pkg.Opam_file - -let load_opam_file_with_contents ~contents:opam_file_string file name = - let loc = Loc.in_file (Path.source file) in - let opam = - let opam = - let lexbuf = - Lexbuf.from_string opam_file_string ~fname:(Path.Source.to_string file) - in - try Ok (Opam_file.parse lexbuf) with - | User_error.E _ as exn -> Error exn - in - match opam with - | Ok s -> Some s - | Error exn -> - (* CR-rgrinberg: make it possible to disable this warning *) - User_warning.emit - ~loc - [ Pp.text - "Unable to read opam file. Some information about this package such as its \ - version will be ignored." - ; Pp.textf "Reason: %s" (Printexc.to_string exn) - ]; - None - in - let open Option.O in - let get_one name = - let* value = - let* opam = opam in - Opam_file.get_field opam name - in - match value.pelem with - | String s -> Some s - | _ -> None - in - let get_many name = - let* value = - let* opam = opam in - Opam_file.get_field opam name - in - match value.pelem with - | String s -> Some [ s ] - | List l -> - List.fold_left - l.pelem - ~init:(Some []) - ~f:(fun acc (v : OpamParserTypes.FullPos.value) -> - let* acc = acc in - match v.pelem with - | String s -> Some (s :: acc) - | _ -> None) - >>| List.rev - | _ -> None - in - let dir = Path.Source.parent_exn file in - let info = - Package_info.create - ~maintainers:(get_many "maintainer") - ~authors:(get_many "authors") - ~homepage:(get_one "homepage") - ~bug_reports:(get_one "bug-reports") - ~documentation:(get_one "doc") - ~license:(get_many "license") - ~source: - (let+ url = get_one "dev-repo" in - Source_kind.Url url) - in - create - ~name - ~dir - ~loc - ~version:(get_one "version" |> Option.map ~f:Package_version.of_string) - ~conflicts:[] - ~depends:[] - ~depopts:[] - ~info - ~synopsis:(get_one "synopsis") - ~description:(get_one "description") - ~has_opam_file:(Exists true) - ~tags:(Option.value (get_many "tags") ~default:[]) - ~deprecated_package_names:Name.Map.empty - ~sites:Site.Map.empty - ~allow_empty:true - ~original_opam_file:(Some { file; contents = opam_file_string }) -;; diff --git a/src/dune_rules/package.mli b/src/dune_rules/package.mli deleted file mode 100644 index 0e41c0c334f..00000000000 --- a/src/dune_rules/package.mli +++ /dev/null @@ -1,7 +0,0 @@ -(** Information about a package defined in the workspace *) - -open Import -include module type of Dune_lang.Package with type t = Dune_lang.Package.t - -(** Construct a package description from an opam file and its contents *) -val load_opam_file_with_contents : contents:string -> Path.Source.t -> Name.t -> t diff --git a/src/dune_rules/package_map_traversals.ml b/src/dune_rules/package_map_traversals.ml deleted file mode 100644 index bb9ac52304b..00000000000 --- a/src/dune_rules/package_map_traversals.ml +++ /dev/null @@ -1 +0,0 @@ -include Memo.Make_parallel_map (Package.Name.Map) diff --git a/src/dune_rules/package_map_traversals.mli b/src/dune_rules/package_map_traversals.mli deleted file mode 100644 index e647a619c81..00000000000 --- a/src/dune_rules/package_map_traversals.mli +++ /dev/null @@ -1,5 +0,0 @@ -module Map := Package.Name.Map - -type t := Package.Name.t - -val parallel_map : 'a Map.t -> f:(t -> 'a -> 'b Memo.t) -> 'b Map.t Memo.t diff --git a/test/expect-tests/findlib_tests.ml b/test/expect-tests/findlib_tests.ml index 47020c05093..c0d7494726d 100644 --- a/test/expect-tests/findlib_tests.ml +++ b/test/expect-tests/findlib_tests.ml @@ -2,7 +2,13 @@ open Stdune module Lib_name = Dune_lang.Lib_name module Meta = Dune_findlib.Findlib.Meta module Findlib_config = Dune_findlib.Findlib.Config -module Lib_dep = Dune_lang.Lib_dep + +include struct + open Dune_lang + module Lib_dep = Lib_dep + module Package = Package +end + open Dune_rules open Dune_rules.For_tests open Dune_tests_common