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

Vendor variable in lockfile #237

Merged
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

### Changed

- Mark packages to be pulled by opam-monorepo with the `vendor` variable so
using OPAM with `opam install --deps-only --locked .` will not install
packages that will be installed with `opam-monorepo pull` (#237,
@Leonidas-from-XIV)

### Deprecated

### Fixed
Expand Down
29 changes: 15 additions & 14 deletions cli/list_cmd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ open Import
open Duniverse

type t = {
name : string;
version : string;
name : OpamPackage.Name.t;
version : OpamPackage.Version.t;
loc : string;
pinned : bool;
descr : string option;
Expand All @@ -14,6 +14,7 @@ type t = {
let pad s max_len = Printf.sprintf "%-*s" max_len s

let guess_pin ~version ~loc =
let version = OpamPackage.Version.to_string version in
(* opam-overlays *)
String.is_suffix ~suffix:"+dune" version
|| String.is_prefix ~prefix:"https://github.com/dune-universe" loc
Expand All @@ -29,13 +30,15 @@ let pp_pin_version = Fmt.(styled `Blue string)
let pp_pin_loc ppf s =
Fmt.pf ppf "pinned at %a" Fmt.(styled `Underline string) s

let compare_pkg x y = String.compare x.name y.name
let compare_pkg x y = OpamPackage.Name.compare x.name y.name

let pp ~max_name ~max_version ~short ppf t =
if short then Fmt.string ppf t.name
if short then Duniverse_lib.Opam.Pp.package_name ppf t.name
else
let padded_name = pad t.name max_name in
let padded_version = pad t.version max_version in
let padded_name = pad (OpamPackage.Name.to_string t.name) max_name in
let padded_version =
pad (OpamPackage.Version.to_string t.version) max_version
in
if t.pinned then
Fmt.pf ppf "%a %a %a" pp_name padded_name pp_pin_version padded_version
pp_pin_loc t.loc
Expand All @@ -45,7 +48,7 @@ let pp ~max_name ~max_version ~short ppf t =

let pkgs_of_repo (t : resolved Repo.t) =
List.map
~f:(fun (pkg : Opam.t) ->
~f:(fun (pkg : OpamPackage.t) ->
let name = pkg.name in
let version = pkg.version in
let loc = Repo.Url.to_string t.url in
Expand All @@ -66,11 +69,7 @@ let with_descr pkgs =
OpamSwitchState.with_ `Lock_none global_state (fun switch_state ->
List.map
~f:(fun pkg ->
let opam =
OpamPackage.create
(OpamPackage.Name.of_string pkg.name)
(OpamPackage.Version.of_string pkg.version)
in
let opam = OpamPackage.create pkg.name pkg.version in
match OpamSwitchState.opam switch_state opam with
| opam -> { pkg with descr = OpamFile.OPAM.synopsis opam }
| exception Not_found ->
Expand All @@ -88,8 +87,10 @@ let run (`Root root) (`Lockfile explicit_lockfile) short () =
let max_name, max_version =
List.fold_left
~f:(fun (max_name, max_version) t ->
( max (String.length t.name) max_name,
max (String.length t.version) max_version ))
( max (String.length (OpamPackage.Name.to_string t.name)) max_name,
max
(String.length (OpamPackage.Version.to_string t.version))
max_version ))
~init:(0, 0) pkgs
in
let pp = pp ~max_name ~max_version ~short in
Expand Down
6 changes: 6 additions & 0 deletions lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)
open Import

let base_packages =
[
Expand All @@ -26,6 +27,8 @@ let base_packages =
"ocaml-base-compiler";
"ocaml-variants";
]
|> List.map ~f:OpamPackage.Name.of_string
|> OpamPackage.Name.Set.of_list

let duniverse_opam_repo =
"git+https://github.com/dune-universe/opam-overlays.git"
Expand All @@ -49,3 +52,6 @@ let dune_src_dir = Fpath.(bootstrap_src_dir / "dune")
let dune_latest_tag = "2.6.0" (* TODO get from opam metadata *)

let lockfile_ext = ".opam.locked"

(* variable to use for vendoring *)
let vendor_variable = OpamVariable.of_string "vendor"
70 changes: 20 additions & 50 deletions lib/duniverse.ml
Original file line number Diff line number Diff line change
@@ -1,34 +1,9 @@
open Import
module O = Opam

type unresolved = Git.Ref.t

type resolved = Git.Ref.resolved

module Opam = struct
type t = { name : string; version : string }

let equal t t' =
let { name; version } = t in
let { name = name'; version = version' } = t' in
String.equal name name' && String.equal version version'

let pp fmt { name; version } = Format.fprintf fmt "%s.%s" name version

let raw_pp fmt { name; version } =
let open Pp_combinators.Ocaml in
Format.fprintf fmt "@[<hov 2>{ name = %a;@ version = %a }@]" string name
string version

let to_opam { name = n; version = v } =
OpamPackage.(create (Name.of_string n) (Version.of_string v))

let from_opam pkg =
let name = OpamPackage.name_to_string pkg in
let version = OpamPackage.version_to_string pkg in
{ name; version }
end

module Repo = struct
module Url = struct
type 'ref t = Git of { repo : string; ref : 'ref } | Other of string
Expand Down Expand Up @@ -71,38 +46,38 @@ module Repo = struct
let to_opam_url t = opam_url_from_string (to_string t)

let from_opam_url opam_url =
match O.Url.from_opam opam_url with
| O.Url.Other s -> Ok (Other s)
| O.Url.Git { repo; ref = Some commit } ->
match Opam.Url.from_opam opam_url with
| Opam.Url.Other s -> Ok (Other s)
| Opam.Url.Git { repo; ref = Some commit } ->
Ok (Git { repo; ref = { Git.Ref.t = commit; commit } })
| _ -> Error (`Msg "Git URL must be resolved to a commit hash")
end

module Package = struct
type t = {
opam : Opam.t;
opam : OpamPackage.t;
dev_repo : string;
url : unresolved Url.t;
hashes : OpamHash.t list;
}

let equal t t' =
Opam.equal t.opam t'.opam
OpamPackage.equal t.opam t'.opam
&& String.equal t.dev_repo t'.dev_repo
&& Url.equal Git.Ref.equal t.url t'.url

let pp fmt { opam; dev_repo; url; hashes } =
let open Pp_combinators.Ocaml in
Format.fprintf fmt
"@[<hov 2>{ opam = %a;@ dev_repo = %a;@ url = %a;@ hashes = %a }@]"
Opam.raw_pp opam string dev_repo (Url.pp Git.Ref.pp) url
(list O.Pp.hash) hashes
Opam.Pp.raw_package opam string dev_repo (Url.pp Git.Ref.pp) url
(list Opam.Pp.hash) hashes

let from_package_summary ~get_default_branch ps =
let open O.Package_summary in
let open Opam.Package_summary in
let open Result.O in
let url ourl =
match (ourl : O.Url.t) with
match (ourl : Opam.Url.t) with
| Other s -> Ok (Url.Other s)
| Git { repo; ref = Some ref } -> Ok (Url.Git { repo; ref })
| Git { repo; ref = None } ->
Expand All @@ -111,23 +86,17 @@ module Repo = struct
match ps with
| _ when is_base_package ps -> Ok None
| { url_src = None; _ } | { dev_repo = None; _ } -> Ok None
| {
url_src = Some url_src;
name;
version;
dev_repo = Some dev_repo;
hashes;
_;
} ->
| { url_src = Some url_src; package; dev_repo = Some dev_repo; hashes; _ }
->
url url_src >>= fun url ->
Ok (Some { opam = { name; version }; dev_repo; url; hashes })
Ok (Some { opam = package; dev_repo; url; hashes })
end

type 'ref t = {
dir : string;
url : 'ref Url.t;
hashes : OpamHash.t list;
provided_packages : Opam.t list;
provided_packages : OpamPackage.t list;
}

let log_url_selection ~dev_repo ~packages ~highest_version_package =
Expand All @@ -136,7 +105,8 @@ module Repo = struct
| Other s -> s
in
let pp_package fmt { Package.opam = { name; version }; url; _ } =
Format.fprintf fmt "%s.%s: %s" name version (url_to_string url)
Format.fprintf fmt "%a.%a: %s" Opam.Pp.package_name name Opam.Pp.version
version (url_to_string url)
in
let sep fmt () = Format.fprintf fmt "\n" in
Logs.info (fun l ->
Expand Down Expand Up @@ -179,7 +149,7 @@ module Repo = struct
need dune to provide that feature. *)
let highest_version_package =
List.max_exn packages ~compare:(fun p p' ->
OpamVersionCompare.compare p.Package.opam.version p'.opam.version)
OpamPackage.Version.compare p.Package.opam.version p'.opam.version)
in
log_url_selection ~dev_repo ~packages ~highest_version_package;
let url = highest_version_package.url in
Expand All @@ -198,16 +168,16 @@ module Repo = struct
in
String.equal dir dir'
&& Url.equal equal_ref url url'
&& List.equal O.Hash.equal hashes hashes'
&& List.equal Opam.equal provided_packages provided_packages'
&& List.equal Opam.Hash.equal hashes hashes'
&& List.equal OpamPackage.equal provided_packages provided_packages'

let pp pp_ref fmt { dir; url; hashes; provided_packages } =
let open Pp_combinators.Ocaml in
Format.fprintf fmt
"@[<hov 2>{ dir = %a;@ url = %a;@ hashes = %a;@ provided_packages = %a \
}@]"
string dir (Url.pp pp_ref) url (list O.Pp.hash) hashes (list Opam.raw_pp)
provided_packages
string dir (Url.pp pp_ref) url (list Opam.Pp.hash) hashes
(list Opam.Pp.raw_package) provided_packages

let resolve ~resolve_ref ({ url; _ } as t) =
let open Result.O in
Expand Down
23 changes: 4 additions & 19 deletions lib/duniverse.mli
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
module O = Opam

type unresolved = Git.Ref.t

type resolved = Git.Ref.resolved

module Opam : sig
type t = { name : string; version : string }
(** Type of dependencies to install through opam *)

val equal : t -> t -> bool

val pp : t Fmt.t

val to_opam : t -> OpamPackage.t

val from_opam : OpamPackage.t -> t
end

module Repo : sig
module Url : sig
type 'ref t = Git of { repo : string; ref : 'ref } | Other of string
Expand All @@ -38,7 +23,7 @@ module Repo : sig
dir : string;
url : 'ref Url.t;
hashes : OpamHash.t list;
provided_packages : Opam.t list;
provided_packages : OpamPackage.t list;
}
(** Type of dependencies to clone in the duniverse *)

Expand All @@ -52,7 +37,7 @@ module Repo : sig

module Package : sig
type t = {
opam : Opam.t;
opam : OpamPackage.t;
dev_repo : string;
url : unresolved Url.t;
hashes : OpamHash.t list;
Expand All @@ -64,7 +49,7 @@ module Repo : sig

val from_package_summary :
get_default_branch:(string -> (string, Rresult.R.msg) result) ->
O.Package_summary.t ->
Opam.Package_summary.t ->
(t option, [ `Msg of string ]) result
end

Expand All @@ -80,7 +65,7 @@ val equal : t -> t -> bool

val from_package_summaries :
get_default_branch:(string -> (string, Rresult.R.msg) result) ->
O.Package_summary.t list ->
Opam.Package_summary.t list ->
(unresolved Repo.t list, [ `Msg of string ]) result
(** Build opamverse and duniverse from a list of [Types.Opam.entry] values.
It filters out virtual packages and packages with unknown dev-repo. *)
Expand Down
Loading