From 3c53a3b6efb99e48c6f2332d4973499bc62c15d6 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 6 May 2019 18:52:09 +0800 Subject: [PATCH] Put template fields at the bottom of opam file Signed-off-by: Rudi Grinberg --- dune.opam | 14 +++++++------- src/opam_create.ml | 18 +++++++++++------- src/opam_file.ml | 30 +++++++++++++++--------------- src/opam_file.mli | 4 +++- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/dune.opam b/dune.opam index d97e516daacf..be303afc74de 100644 --- a/dune.opam +++ b/dune.opam @@ -1,10 +1,3 @@ -build: [ - # opam 2 sets OPAM_SWITCH_PREFIX, so we don't need a hardcoded path - ["ocaml" "configure.ml" "--libdir" lib] {opam-version < "2"} - ["ocaml" "bootstrap.ml"] - ["./boot.exe" "--release" "--subst"] {pinned} - ["./boot.exe" "--release" "-j" jobs] -] opam-version: "2.0" maintainer: ["Jane Street Group, LLC "] authors: ["Jane Street Group, LLC "] @@ -41,3 +34,10 @@ conflicts: [ "jbuilder" {!= "transition"} "odoc" {< "1.3.0"} ] +build: [ + # opam 2 sets OPAM_SWITCH_PREFIX, so we don't need a hardcoded path + ["ocaml" "configure.ml" "--libdir" lib] {opam-version < "2"} + ["ocaml" "bootstrap.ml"] + ["./boot.exe" "--release" "--subst"] {pinned} + ["./boot.exe" "--release" "-j" jobs] +] diff --git a/src/opam_create.ml b/src/opam_create.ml index 648504192695..6d50accf5c35 100644 --- a/src/opam_create.ml +++ b/src/opam_create.ml @@ -108,15 +108,19 @@ let add_rule sctx ~project ~pkg = | Some p -> Build.contents p | None -> Build.return "") >>> - Build.arr (fun contents -> - let opamfile = Opam_file.of_string ~path:opam_path contents in - let generated = + Build.arr (fun template -> + let opamfile = Opam_file.of_string ~path:opam_path template in + let existing_vars_template = Opam_file.existing_variables opamfile in + let generated_fields = let package = Local_package.package pkg in - let more_fields = opam_fields project package in - Opam_file.Create.add_missing_bindings opamfile more_fields + opam_fields project package + |> List.filter ~f:(fun (v, _) -> + not (String.Set.mem existing_vars_template v)) + |> Opam_file.Create.of_bindings ~file:opam_path in - OpamPrinter.Preserved.items contents opamfile.file_contents - generated.file_contents) + sprintf "%s\n%s" + (OpamPrinter.opamfile generated_fields) + template) >>> Build.write_file_dyn opam_path in let dir = Local_package.build_dir pkg in diff --git a/src/opam_file.ml b/src/opam_file.ml index db243f81df15..09128bc00e62 100644 --- a/src/opam_file.ml +++ b/src/opam_file.ml @@ -80,25 +80,25 @@ let absolutify_positions ~file_contents t = let nopos : OpamParserTypes.pos = ("",0,0) (* Null position *) +let existing_variables t = + List.fold_left ~init:String.Set.empty t.file_contents + ~f:(fun acc l -> + match l with + | Section (_, _) -> acc + | Variable (_, var, _) -> String.Set.add acc var) + module Create = struct let string s = String (nopos, s) let list f xs = List (nopos, List.map ~f xs) let string_list xs = list string xs - let add_missing_bindings (t : t) vars = - let existing_variables = - List.fold_left ~init:String.Set.empty t.file_contents - ~f:(fun acc l -> - match l with - | Section (_, _) -> acc - | Variable (_, var, _) -> String.Set.add acc var) - in - let vars = - List.filter_map vars ~f:(fun (var, value) -> - if String.Set.mem existing_variables var then - None - else - Some (Variable (nopos, var, value))) + let of_bindings vars ~file = + let file_contents = + List.map vars ~f:(fun (var, value) -> + Variable (nopos, var, value)) in - { t with file_contents = vars @ t.file_contents } + let file_name = Path.to_string file in + { file_contents + ; file_name + } end diff --git a/src/opam_file.mli b/src/opam_file.mli index 5bad5ae8a46f..994580169516 100644 --- a/src/opam_file.mli +++ b/src/opam_file.mli @@ -24,6 +24,8 @@ val absolutify_positions : file_contents:string -> opamfile -> opamfile val nopos : OpamParserTypes.pos +val existing_variables : t -> String.Set.t + module Create : sig open OpamParserTypes @@ -33,5 +35,5 @@ module Create : sig val string_list : string list -> value - val add_missing_bindings : t -> (string * value) list -> t + val of_bindings : (string * value) list -> file:Path.t -> t end