diff --git a/bin/installed_libraries.ml b/bin/installed_libraries.ml index f789b5a350b..31d85678da4 100644 --- a/bin/installed_libraries.ml +++ b/bin/installed_libraries.ml @@ -58,16 +58,16 @@ let term = Lib_name.to_string (Dune_package.Entry.name e)) in List.iter pkgs ~f:(fun e -> - let ver = + let ver_string = match Dune_package.Entry.version e with - | Some v -> v + | Some v -> Dune_pkg.Package_version.to_string v | _ -> "n/a" in Printf.printf "%-*s (version: %s)\n" max_len (Lib_name.to_string (Dune_package.Entry.name e)) - ver); + ver_string); Memo.return ()) in fun () -> Memo.run (run ())) diff --git a/bin/pkg.ml b/bin/pkg.ml index 34cb893e93d..2e912138ea9 100644 --- a/bin/pkg.ml +++ b/bin/pkg.ml @@ -3,6 +3,7 @@ module Lock_dir = Dune_pkg.Lock_dir module Fetch = Dune_pkg.Fetch module Opam_repo = Dune_pkg.Opam_repo module Repository_id = Dune_pkg.Repository_id +module Package_version = Dune_pkg.Package_version let context_term ~doc = Arg.(value & opt (some Arg.context_name) None & info [ "context" ] ~docv:"CONTEXT" ~doc) @@ -374,7 +375,7 @@ module Lock = struct Pp.enumerate packages ~f:(fun { Lock_dir.Pkg.info = { Lock_dir.Pkg_info.name; version; _ }; _ } -> - Pp.verbatim (Package_name.to_string name ^ "." ^ version)) + Pp.verbatim (Package_name.to_string name ^ "." ^ Package_version.to_string version)) ;; let solve diff --git a/src/dune_pkg/dune_pkg.ml b/src/dune_pkg/dune_pkg.ml index c114c03a8e0..cae6e71bee6 100644 --- a/src/dune_pkg/dune_pkg.ml +++ b/src/dune_pkg/dune_pkg.ml @@ -11,4 +11,5 @@ module Solver_stats = Solver_stats module Substs = Substs module Sys_poll = Sys_poll module Version_preference = Version_preference +module Package_version = Package_version module Pkg_workspace = Workspace diff --git a/src/dune_pkg/lock_dir.ml b/src/dune_pkg/lock_dir.ml index 78677918e96..bdd6c7094a3 100644 --- a/src/dune_pkg/lock_dir.ml +++ b/src/dune_pkg/lock_dir.ml @@ -103,7 +103,7 @@ end module Pkg_info = struct type t = { name : Package_name.t - ; version : string + ; version : Package_version.t ; dev : bool ; source : Source.t option ; extra_sources : (Path.Local.t * Source.t) list @@ -111,7 +111,7 @@ module Pkg_info = struct let equal { name; version; dev; source; extra_sources } t = Package_name.equal name t.name - && String.equal version t.version + && Package_version.equal version t.version && Bool.equal dev t.dev && Option.equal Source.equal source t.source && List.equal @@ -132,14 +132,14 @@ module Pkg_info = struct let to_dyn { name; version; dev; source; extra_sources } = Dyn.record [ "name", Package_name.to_dyn name - ; "version", Dyn.string version + ; "version", Package_version.to_dyn version ; "dev", Dyn.bool dev ; "source", Dyn.option Source.to_dyn source ; "extra_sources", Dyn.list (Dyn.pair Path.Local.to_dyn Source.to_dyn) extra_sources ] ;; - let default_version = "dev" + let default_version = Package_version.of_string "dev" end module Pkg = struct @@ -197,7 +197,8 @@ module Pkg = struct let open Decoder in enter @@ fields - @@ let+ version = field ~default:Pkg_info.default_version Fields.version string + @@ let+ version = + field ~default:Pkg_info.default_version Fields.version Package_version.decode and+ install_command = field_o Fields.install Action.decode_pkg and+ build_command = field_o Fields.build Action.decode_pkg and+ deps = field ~default:[] Fields.deps (repeat (located Package_name.decode)) @@ -245,7 +246,7 @@ module Pkg = struct = let open Encoder in record_fields - [ field Fields.version string version + [ field Fields.version Package_version.encode version ; field_o Fields.install Action.encode install_command ; field_o Fields.build Action.encode build_command ; field_l Fields.deps Package_name.encode (List.map deps ~f:snd) diff --git a/src/dune_pkg/lock_dir.mli b/src/dune_pkg/lock_dir.mli index 96d7fa51b86..f86bf17f2b9 100644 --- a/src/dune_pkg/lock_dir.mli +++ b/src/dune_pkg/lock_dir.mli @@ -16,13 +16,13 @@ end module Pkg_info : sig type t = { name : Package_name.t - ; version : string + ; version : Package_version.t ; dev : bool ; source : Source.t option ; extra_sources : (Path.Local.t * Source.t) list } - val default_version : string + val default_version : Package_version.t end module Pkg : sig diff --git a/src/dune_pkg/opam_solver.ml b/src/dune_pkg/opam_solver.ml index cadc24709cf..9f85a97631c 100644 --- a/src/dune_pkg/opam_solver.ml +++ b/src/dune_pkg/opam_solver.ml @@ -27,7 +27,7 @@ module Context_for_dune = struct type rejection = Unavailable let local_package_default_version = - OpamPackage.Version.of_string Lock_dir.Pkg_info.default_version + Package_version.to_opam Lock_dir.Pkg_info.default_version ;; type t = @@ -455,7 +455,7 @@ let opam_package_to_lock_file_pkg opam_package = let name = OpamPackage.name opam_package in - let version = OpamPackage.version opam_package |> OpamPackage.Version.to_string in + let version = OpamPackage.version opam_package |> Package_version.of_opam in let dev = OpamPackage.Name.Map.mem name local_packages in let+ opam_file, loc = let+ { Opam_repo.With_file.opam_file = opam_file_with_filters; file } = diff --git a/src/dune_pkg/package_version.ml b/src/dune_pkg/package_version.ml new file mode 100644 index 00000000000..bac9473c68d --- /dev/null +++ b/src/dune_pkg/package_version.ml @@ -0,0 +1,18 @@ +open Stdune +include String + +include ( + Dune_util.Stringlike.Make (struct + type t = string + + let to_string x = x + let module_ = "Package_version.Name" + let description = "package version name" + let description_of_valid_string = None + let hint_valid = None + let of_string_opt s = if s = "" then None else Some s + end) : + Dune_util.Stringlike with type t := t) + +let of_opam = OpamPackage.Version.to_string +let to_opam = OpamPackage.Version.of_string diff --git a/src/dune_pkg/package_version.mli b/src/dune_pkg/package_version.mli new file mode 100644 index 00000000000..f57c1685892 --- /dev/null +++ b/src/dune_pkg/package_version.mli @@ -0,0 +1,12 @@ +open! Stdune + +type t + +val of_string : string -> t +val to_string : t -> string +val equal : t -> t -> bool +val to_dyn : t -> Dyn.t +val encode : t Dune_lang.Encoder.t +val decode : t Dune_lang.Decoder.t +val of_opam : OpamPackage.Version.t -> t +val to_opam : t -> OpamPackage.Version.t diff --git a/src/dune_pkg_outdated/dune_pkg_outdated.ml b/src/dune_pkg_outdated/dune_pkg_outdated.ml index af992e8579b..3b4aaade244 100644 --- a/src/dune_pkg_outdated/dune_pkg_outdated.ml +++ b/src/dune_pkg_outdated/dune_pkg_outdated.ml @@ -3,8 +3,8 @@ open Import type candidate = { is_immediate_dep_of_local_package : bool ; name : Package_name.t - ; outdated_version : string - ; newer_version : string + ; outdated_version : Package_version.t + ; newer_version : Package_version.t } type result = @@ -111,7 +111,7 @@ let better_candidate | Some newest_opam_file -> let version = OpamFile.OPAM.version newest_opam_file in (match - OpamPackage.Version.of_string pkg.info.version + Package_version.to_opam pkg.info.version |> OpamPackage.Version.compare version |> Ordering.of_int with @@ -120,7 +120,7 @@ let better_candidate Better_candidate { is_immediate_dep_of_local_package ; name = pkg.info.name - ; newer_version = version |> OpamPackage.Version.to_string + ; newer_version = version |> Package_version.of_opam ; outdated_version = pkg.info.version }) ;; @@ -147,11 +147,11 @@ let pp results ~transitive ~lock_dir_path = ; Pp.space ; Pp.tag (User_message.Style.Ansi_styles [ `Fg_bright_red ]) - (Pp.verbatim outdated_version) + (Pp.verbatim (Package_version.to_string outdated_version)) ; Pp.text " < " ; Pp.tag (User_message.Style.Ansi_styles [ `Fg_bright_green ]) - (Pp.verbatim newer_version) + (Pp.verbatim (Package_version.to_string newer_version)) ]) else None) with diff --git a/src/dune_pkg_outdated/dune_pkg_outdated.mli b/src/dune_pkg_outdated/dune_pkg_outdated.mli index 6841053a04b..55be9e6ffdc 100644 --- a/src/dune_pkg_outdated/dune_pkg_outdated.mli +++ b/src/dune_pkg_outdated/dune_pkg_outdated.mli @@ -36,8 +36,8 @@ module For_tests : sig val better_candidate : is_immediate_dep_of_local_package:bool -> name:string - -> newer_version:string - -> outdated_version:string + -> newer_version:Package_version.t + -> outdated_version:Package_version.t -> result val explain_results diff --git a/src/dune_pkg_outdated/import.ml b/src/dune_pkg_outdated/import.ml index 3bbbbbf0c02..40f8580a543 100644 --- a/src/dune_pkg_outdated/import.ml +++ b/src/dune_pkg_outdated/import.ml @@ -4,6 +4,7 @@ include struct open Dune_pkg module Lock_dir = Lock_dir module Opam_repo = Opam_repo + module Package_version = Package_version end include struct diff --git a/src/dune_rules/dune_package.ml b/src/dune_rules/dune_package.ml index 78b8ac2371f..fa91a3bad93 100644 --- a/src/dune_rules/dune_package.ml +++ b/src/dune_rules/dune_package.ml @@ -385,7 +385,7 @@ let path_to_dyn = function type t = { name : Package.Name.t ; entries : Entry.t Lib_name.Map.t - ; version : string option + ; version : Dune_pkg.Package_version.t option ; sections : Path.t Section.Map.t ; sites : Section.t Site.Map.t ; dir : Path.t @@ -395,7 +395,7 @@ type t = let decode ~lang ~dir = let open Dune_lang.Decoder in let+ name = field "name" Package.Name.decode - and+ version = field_o "version" string + and+ version = field_o "version" Dune_pkg.Package_version.decode and+ sections = field ~default:[] @@ -458,7 +458,7 @@ let encode ~dune_version { entries; name; version; dir; sections; sites; files } let sexp = record_fields [ field "name" Package.Name.encode name - ; field_o "version" string version + ; field_o "version" Dune_pkg.Package_version.encode version ; field_l "sections" (pair Section.encode (Dune_lang.Path.Local.encode ~dir)) @@ -489,7 +489,7 @@ let to_dyn { entries; name; version; dir; sections; sites; files } = record [ "entries", list Entry.to_dyn (Lib_name.Map.values entries) ; "name", Package.Name.to_dyn name - ; "version", option string version + ; "version", option Dune_pkg.Package_version.to_dyn version ; "dir", Path.to_dyn dir ; "sections", Section.Map.to_dyn Path.to_dyn sections ; "sites", Site.Map.to_dyn Section.to_dyn sites diff --git a/src/dune_rules/dune_package.mli b/src/dune_rules/dune_package.mli index a1cd77f544e..f2692e3e9c8 100644 --- a/src/dune_rules/dune_package.mli +++ b/src/dune_rules/dune_package.mli @@ -41,7 +41,7 @@ module Entry : sig Dune itself never produces hidden libraries. *) val name : t -> Lib_name.t - val version : t -> string option + val version : t -> Dune_pkg.Package_version.t option val loc : t -> Loc.t val to_dyn : t Dyn.builder end @@ -51,7 +51,7 @@ type path = [ `File | `Dir ] * Install.Entry.Dst.t type t = { name : Package.Name.t ; entries : Entry.t Lib_name.Map.t - ; version : string option + ; version : Dune_pkg.Package_version.t option ; sections : Path.t Section.Map.t ; sites : Section.t Site.Map.t ; dir : Path.t diff --git a/src/dune_rules/dune_project.ml b/src/dune_rules/dune_project.ml index 877c7f8ae1d..8734212c8c0 100644 --- a/src/dune_rules/dune_project.ml +++ b/src/dune_rules/dune_project.ml @@ -129,7 +129,7 @@ end type t = { name : Name.t ; root : Path.Source.t - ; version : string option + ; version : Dune_pkg.Package_version.t option ; dune_version : Dune_lang.Syntax.Version.t ; info : Package.Info.t ; packages : Package.t Package.Name.Map.t @@ -209,7 +209,7 @@ let to_dyn record [ "name", Name.to_dyn name ; "root", Path.Source.to_dyn root - ; "version", (option string) version + ; "version", (option Dune_pkg.Package_version.to_dyn) version ; "dune_version", Dune_lang.Syntax.Version.to_dyn dune_version ; "info", Package.Info.to_dyn info ; "project_file", Path.Source.to_dyn project_file @@ -708,7 +708,10 @@ let encode : t -> Dune_lang.t list = |> Option.to_list in let name = constr "name" Name.encode name in - let version = Option.map ~f:(constr "version" string) version |> Option.to_list in + let version = + Option.map ~f:(constr "version" Dune_pkg.Package_version.encode) version + |> Option.to_list + in [ lang_stanza; name ] @ flags @ version @@ -853,7 +856,7 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = String_with_vars.set_decoding_env (Pform.Env.initial lang.version) @@ fields @@ let+ name = field_o "name" Name.decode - and+ version = field_o "version" string + and+ version = field_o "version" Dune_pkg.Package_version.decode and+ info = Package.Info.decode () and+ packages = multi_field "package" (Package.decode ~dir) and+ explicit_extensions = diff --git a/src/dune_rules/expander.ml b/src/dune_rules/expander.ml index be1bb5e26a2..cc109d3aab3 100644 --- a/src/dune_rules/expander.ml +++ b/src/dune_rules/expander.ml @@ -114,7 +114,7 @@ let dep p = let expand_version { scope; _ } ~(source : Dune_lang.Template.Pform.t) s = let value_from_version = function | None -> [ Value.String "" ] - | Some s -> [ String s ] + | Some s -> [ String (Dune_pkg.Package_version.to_string s) ] in let project = Scope.project scope in match diff --git a/src/dune_rules/findlib.ml b/src/dune_rules/findlib.ml index ba8746e79ba..c07efe041d4 100644 --- a/src/dune_rules/findlib.ml +++ b/src/dune_rules/findlib.ml @@ -124,7 +124,9 @@ let to_dune_library (t : Findlib.Package.t) ~dir_contents ~ext_lib = | Public (_, _) -> Lib_info.Status.Installed in let src_dir = Obj_dir.dir obj_dir in - let version = Findlib.Package.version t in + let version = + Findlib.Package.version t |> Option.map ~f:Dune_pkg.Package_version.of_string + in let dune_version = None in let virtual_deps = [] in let implements = None in diff --git a/src/dune_rules/gen_meta.ml b/src/dune_rules/gen_meta.ml index c6c46aefcca..8bd777629a7 100644 --- a/src/dune_rules/gen_meta.ml +++ b/src/dune_rules/gen_meta.ml @@ -158,7 +158,7 @@ let gen ~(package : Package.t) ~add_directory_entry entries = let version = match package.version with | None -> [] - | Some s -> [ rule "version" [] Set s ] + | Some s -> [ rule "version" [] Set (Dune_pkg.Package_version.to_string s) ] in let+ pkgs = Memo.parallel_map entries ~f:(fun (e : Scope.DB.Lib_entry.t) -> diff --git a/src/dune_rules/lib_info.ml b/src/dune_rules/lib_info.ml index 0876323c770..d45c82f6529 100644 --- a/src/dune_rules/lib_info.ml +++ b/src/dune_rules/lib_info.ml @@ -356,7 +356,7 @@ type 'path t = ; src_dir : 'path ; orig_src_dir : 'path option ; obj_dir : 'path Obj_dir.t - ; version : string option + ; version : Dune_pkg.Package_version.t option ; synopsis : string option ; archives : 'path list Mode.Dict.t ; plugins : 'path list Mode.Dict.t @@ -442,7 +442,7 @@ let equal && path_equal src_dir t.src_dir && Option.equal path_equal orig_src_dir t.orig_src_dir && Obj_dir.equal obj_dir t.obj_dir - && Option.equal String.equal version t.version + && Option.equal Dune_pkg.Package_version.equal version t.version && Option.equal String.equal synopsis t.synopsis && Mode.Dict.equal (List.equal path_equal) archives t.archives && Mode.Dict.equal (List.equal path_equal) plugins t.plugins @@ -769,7 +769,7 @@ let to_dyn ; "src_dir", path src_dir ; "orig_src_dir", option path orig_src_dir ; "obj_dir", Obj_dir.to_dyn obj_dir - ; "version", option string version + ; "version", option Dune_pkg.Package_version.to_dyn version ; "synopsis", option string synopsis ; "archives", Mode.Dict.to_dyn (list path) archives ; "plugins", Mode.Dict.to_dyn (list path) plugins diff --git a/src/dune_rules/lib_info.mli b/src/dune_rules/lib_info.mli index acfab3a2776..2aa611117ec 100644 --- a/src/dune_rules/lib_info.mli +++ b/src/dune_rules/lib_info.mli @@ -151,7 +151,7 @@ val preprocess : _ t -> Preprocess.With_instrumentation.t Preprocess.Per_module. val sub_systems : _ t -> Sub_system_info.t Sub_system_name.Map.t val enabled : _ t -> Enabled_status.t val orig_src_dir : 'path t -> 'path option -val version : _ t -> string option +val version : _ t -> Dune_pkg.Package_version.t option val dune_version : _ t -> Dune_lang.Syntax.Version.t option (** Directory where the source files for the library are located. Returns the @@ -164,7 +164,7 @@ type local = Path.Build.t t val user_written_deps : _ t -> Lib_dep.t list val of_local : local -> external_ val as_local_exn : external_ -> local -val set_version : 'a t -> string option -> 'a t +val set_version : 'a t -> Dune_pkg.Package_version.t option -> 'a t val for_dune_package : Path.t t @@ -196,7 +196,7 @@ val create -> src_dir:'a -> orig_src_dir:'a option -> obj_dir:'a Obj_dir.t - -> version:string option + -> version:Dune_pkg.Package_version.t option -> synopsis:string option -> main_module_name:Main_module_name.t -> sub_systems:Sub_system_info.t Sub_system_name.Map.t diff --git a/src/dune_rules/link_time_code_gen.ml b/src/dune_rules/link_time_code_gen.ml index edf680d4b3d..74595079d12 100644 --- a/src/dune_rules/link_time_code_gen.ml +++ b/src/dune_rules/link_time_code_gen.ml @@ -122,7 +122,8 @@ let build_info_code cctx ~libs ~api_version = in let version_of_package placeholders (p : Package.t) = match p.version with - | Some v -> Memo.return (sprintf "Some %S" v, placeholders) + | Some v -> + Memo.return (sprintf "Some %S" (Dune_pkg.Package_version.to_string v), placeholders) | None -> placeholder placeholders (Package.dir p) in let* version, placeholders = @@ -137,7 +138,9 @@ let build_info_code cctx ~libs ~api_version = Memo.List.fold_left ~init:([], placeholders) libs ~f:(fun (libs, placeholders) lib -> let+ v, placeholders = match Lib_info.version (Lib.info lib) with - | Some v -> Memo.return (sprintf "Some %S" v, placeholders) + | Some v -> + Memo.return + (sprintf "Some %S" (Dune_pkg.Package_version.to_string v), placeholders) | None -> (match Lib_info.status (Lib.info lib) with | Installed_private | Installed -> Memo.return ("None", placeholders) diff --git a/src/dune_rules/odoc.ml b/src/dune_rules/odoc.ml index fd7e3fe34e9..854ce107896 100644 --- a/src/dune_rules/odoc.ml +++ b/src/dune_rules/odoc.ml @@ -458,7 +458,7 @@ let sp = Printf.sprintf module Toplevel_index = struct type item = { name : string - ; version : string option + ; version : Dune_pkg.Package_version.t option ; link : string } @@ -474,7 +474,8 @@ module Toplevel_index = struct let version_suffix = match version with | None -> "" - | Some v -> sp {| %s|} v + | Some v -> + sp {| %s|} (Dune_pkg.Package_version.to_string v) in sp "