Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg: use new type instead of string for versions #9060

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/installed_libraries.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()))
Expand Down
3 changes: 2 additions & 1 deletion bin/pkg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/dune_pkg/dune_pkg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions src/dune_pkg/lock_dir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,15 @@ 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
}

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
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/dune_pkg/lock_dir.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/dune_pkg/opam_solver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 } =
Expand Down
18 changes: 18 additions & 0 deletions src/dune_pkg/package_version.ml
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions src/dune_pkg/package_version.mli
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions src/dune_pkg_outdated/dune_pkg_outdated.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand All @@ -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
})
;;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/dune_pkg_outdated/dune_pkg_outdated.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/dune_pkg_outdated/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/dune_rules/dune_package.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:[]
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/dune_rules/dune_package.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 7 additions & 4 deletions src/dune_rules/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/expander.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/dune_rules/findlib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/dune_rules/gen_meta.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) ->
Expand Down
6 changes: 3 additions & 3 deletions src/dune_rules/lib_info.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading
Loading