Improve opam parsing errors & use opam-file-format to extract the version
This commit is contained in:
parent
f2a857f406
commit
60ad83c522
|
@ -281,14 +281,18 @@ module Jbuilder_opam_file_format = struct
|
||||||
; file_name : string
|
; file_name : string
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
module OpamParser = struct
|
module OpamBaseParser = struct
|
||||||
open OpamParserTypes
|
open OpamParserTypes
|
||||||
let file fn =
|
let main _lex _lexbuf fn =
|
||||||
assert (fn = "jbuilder.opam");
|
assert (fn = "jbuilder.opam");
|
||||||
{ file_contents = []
|
{ file_contents = []
|
||||||
; file_name = fn
|
; file_name = fn
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
module OpamLexer = struct
|
||||||
|
exception Error of string
|
||||||
|
let token _ = assert false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Glob_lexer = struct
|
module Glob_lexer = struct
|
||||||
|
|
|
@ -407,14 +407,14 @@ let with_file_out ?(binary=true)fn ~f =
|
||||||
|
|
||||||
let with_lexbuf_from_file fn ~f =
|
let with_lexbuf_from_file fn ~f =
|
||||||
with_file_in fn ~f:(fun ic ->
|
with_file_in fn ~f:(fun ic ->
|
||||||
let lb = Lexing.from_channel ic in
|
let lb = Lexing.from_channel ic in
|
||||||
lb.lex_curr_p <-
|
lb.lex_curr_p <-
|
||||||
{ pos_fname = fn
|
{ pos_fname = fn
|
||||||
; pos_lnum = 1
|
; pos_lnum = 1
|
||||||
; pos_bol = 0
|
; pos_bol = 0
|
||||||
; pos_cnum = 0
|
; pos_cnum = 0
|
||||||
};
|
};
|
||||||
f lb)
|
f lb)
|
||||||
|
|
||||||
let input_lines =
|
let input_lines =
|
||||||
let rec loop ic acc =
|
let rec loop ic acc =
|
||||||
|
|
|
@ -149,18 +149,16 @@ let load ?(extra_ignored_subtrees=Path.Set.empty) () =
|
||||||
match Filename.split_extension fn with
|
match Filename.split_extension fn with
|
||||||
| (pkg, ".opam") when pkg <> "" ->
|
| (pkg, ".opam") when pkg <> "" ->
|
||||||
let version_from_opam_file =
|
let version_from_opam_file =
|
||||||
let lines = lines_of_file (Path.relative path fn |> Path.to_string) in
|
let opam = Opam_file.load (Path.relative path fn |> Path.to_string) in
|
||||||
List.find_map lines ~f:(fun s ->
|
match Opam_file.get_field opam "version" with
|
||||||
try
|
| Some (String (_, s)) -> Some s
|
||||||
Scanf.sscanf s "version: %S" (fun x -> Some x)
|
| _ -> None
|
||||||
with _ ->
|
in
|
||||||
None)
|
(pkg,
|
||||||
in
|
{ Package. name = pkg
|
||||||
(pkg,
|
; path
|
||||||
{ Package. name = pkg
|
; version_from_opam_file
|
||||||
; path
|
}) :: acc
|
||||||
; version_from_opam_file
|
|
||||||
}) :: acc
|
|
||||||
| _ -> acc)
|
| _ -> acc)
|
||||||
in
|
in
|
||||||
if String_set.mem "jbuild-ignore" files then
|
if String_set.mem "jbuild-ignore" files then
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
open Import
|
||||||
|
open Jbuilder_opam_file_format
|
||||||
|
open OpamParserTypes
|
||||||
|
|
||||||
|
type t = opamfile
|
||||||
|
|
||||||
|
let load fn =
|
||||||
|
with_lexbuf_from_file fn ~f:(fun lb ->
|
||||||
|
try
|
||||||
|
OpamBaseParser.main OpamLexer.token lb fn
|
||||||
|
with
|
||||||
|
| OpamLexer.Error msg ->
|
||||||
|
Loc.fail_lex lb "%s" msg
|
||||||
|
| Parsing.Parse_error ->
|
||||||
|
Loc.fail_lex lb "Parse error")
|
||||||
|
|
||||||
|
let get_field t name =
|
||||||
|
List.find_map t.file_contents
|
||||||
|
~f:(function
|
||||||
|
| Variable (_, var, value) when name = var ->
|
||||||
|
Some value
|
||||||
|
| _ -> None)
|
|
@ -0,0 +1,12 @@
|
||||||
|
(** Parsing and interpretation of opam files *)
|
||||||
|
|
||||||
|
open Jbuilder_opam_file_format.OpamParserTypes
|
||||||
|
|
||||||
|
(** Type of opam files *)
|
||||||
|
type t = opamfile
|
||||||
|
|
||||||
|
(** Load a file *)
|
||||||
|
val load : string -> t
|
||||||
|
|
||||||
|
(** Extracts a field *)
|
||||||
|
val get_field : t -> string -> value option
|
|
@ -21,7 +21,7 @@ let is_a_source_file fn =
|
||||||
| _ -> true
|
| _ -> true
|
||||||
|
|
||||||
let make_watermark_map ~name ~version ~commit =
|
let make_watermark_map ~name ~version ~commit =
|
||||||
let opam_file = OpamParser.file (name ^ ".opam") in
|
let opam_file = Opam_file.load (name ^ ".opam") in
|
||||||
let version_num =
|
let version_num =
|
||||||
if String.is_prefix version ~prefix:"v" then
|
if String.is_prefix version ~prefix:"v" then
|
||||||
String.sub version ~pos:1 ~len:(String.length version - 1)
|
String.sub version ~pos:1 ~len:(String.length version - 1)
|
||||||
|
@ -29,13 +29,7 @@ let make_watermark_map ~name ~version ~commit =
|
||||||
version
|
version
|
||||||
in
|
in
|
||||||
let opam_var name sep =
|
let opam_var name sep =
|
||||||
match
|
match Opam_file.get_field opam_file name with
|
||||||
List.find_map opam_file.file_contents
|
|
||||||
~f:(function
|
|
||||||
| Variable (_, var, value) when name = var ->
|
|
||||||
Some value
|
|
||||||
| _ -> None)
|
|
||||||
with
|
|
||||||
| None -> Error (sprintf "variable %S not found in opam file" name)
|
| None -> Error (sprintf "variable %S not found in opam file" name)
|
||||||
| Some value ->
|
| Some value ->
|
||||||
let err = Error (sprintf "invalid value for variable %S in opam file" name) in
|
let err = Error (sprintf "invalid value for variable %S in opam file" name) in
|
||||||
|
|
Loading…
Reference in New Issue