Skip to content

Commit

Permalink
do opam file generation in the same order as opam lint
Browse files Browse the repository at this point in the history
The logic for the field order used by `opam lint --normalise`
is in opam-file and not opam-file-format. This is too much to
vendor, so this commit hardcodes the field order based on
the opam-file logic.

It won't change very often upstream so this shouldn't impose
too much of a maintenance overhead.

Closes ocaml#2290

Signed-off-by: Anil Madhavapeddy <[email protected]>
  • Loading branch information
avsm committed Jul 8, 2019
1 parent 5666b0b commit 818535c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/opam_create.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ let opam_fields project (package : Package.t) =
; "build", default_build_command project
]
in
List.concat
let sort_fields l =
if Dune_project.dune_version project < (1, 11) then l
else Opam_file.Create.normalise_field_order l
in
sort_fields @@ List.concat
[ fields
; list_fields
; optional_fields
Expand Down
45 changes: 45 additions & 0 deletions src/opam_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,51 @@ module Create = struct
let list f xs = List (nopos, List.map ~f xs)
let string_list xs = list string xs

let normalise_field_order vars =
let normal_field_order = [
(* Extracted from opam/src/format/opamFile.ml *)
"opam-version";
"name";
"version";
"synopsis";
"description";
"maintainer";
"authors";
"author";
"license";
"tags";
"homepage";
"doc";
"bug-reports";
"depends";
"depopts";
"conflicts";
"conflict-class";
"available";
"flags";
"setenv";
"build";
"run-test";
"install";
"remove";
"substs";
"patches";
"build-env";
"features";
"messages";
"post-messages";
"depexts";
"libraries";
"syntax";
"dev-repo";
"pin-depends";
"extra-files" ]
in
List.filter_map normal_field_order
~f:(fun field ->
Option.map (List.assoc vars field)
~f:(fun v -> field, v))

let of_bindings vars ~file =
let file_contents =
List.map vars ~f:(fun (var, value) ->
Expand Down
2 changes: 2 additions & 0 deletions src/opam_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ module Create : sig

val string_list : string list -> value

val normalise_field_order : (string * value) list -> (string * value) list

val of_bindings : (string * value) list -> file:Path.t -> t
end

0 comments on commit 818535c

Please sign in to comment.