Skip to content

Commit

Permalink
Put template fields at the bottom of opam file
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
  • Loading branch information
rgrinberg committed May 6, 2019
1 parent c974a9c commit 3c53a3b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
14 changes: 7 additions & 7 deletions dune.opam
Original file line number Diff line number Diff line change
@@ -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 <opensource@janestreet.com>"]
authors: ["Jane Street Group, LLC <opensource@janestreet.com>"]
Expand Down Expand Up @@ -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]
]
18 changes: 11 additions & 7 deletions src/opam_create.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 15 additions & 15 deletions src/opam_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/opam_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit 3c53a3b

Please sign in to comment.