gen_licenses: use String.starts_with from the Stdlib

This commit is contained in:
Matthieu Dubuget committed 2026-11-22 19:22:45 +01:00
1 parent b9e82711ec
commit 31d500a955
1 file changed
+11 -12
+11 -12
View File
@@ -21,6 +21,7 @@ let rec map_result f = function
Ok (y :: ys) Ok (y :: ys)
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args) let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
let lines cmd = let lines cmd =
let* l = OS.Cmd.(run_out cmd |> to_lines) in let* l = OS.Cmd.(run_out cmd |> to_lines) in
Ok (List.map String.trim l) Ok (List.map String.trim l)
@@ -34,14 +35,7 @@ let line cmd =
(* Windows writes paths with backslashes (doubled in the .changes files, where (* Windows writes paths with backslashes (doubled in the .changes files, where
they are escaped), and drive letters in either case. *) they are escaped), and drive letters in either case. *)
let slashes = Re.compile (Re.rep1 (Re.set "/\\")) let slashes = Re.compile (Re.rep1 (Re.set "/\\"))
let normalize s = String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
let normalize s =
String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
let starts_with ~prefix s =
String.length s >= String.length prefix
&& String.sub s 0 (String.length prefix) = prefix
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)" let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
(* The libraries of the first [(libraries ...)] field of a dune file, minus (* The libraries of the first [(libraries ...)] field of a dune file, minus
@@ -77,7 +71,8 @@ let load_changes switch =
let owner ~stdlib ~changes lib = let owner ~stdlib ~changes lib =
let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in let* dir = line (ocamlfind [ "query"; "-format"; "%d"; lib ]) in
if starts_with ~prefix:(normalize stdlib) (normalize dir) then Ok "ocaml" if String.starts_with ~prefix:(normalize stdlib) (normalize dir) then
Ok "ocaml"
else else
let top = List.hd (String.split_on_char '.' lib) in let top = List.hd (String.split_on_char '.' lib) in
let meta = let meta =
@@ -98,7 +93,9 @@ let package_info ~switch pkg =
let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in
let prefix = pkg ^ "." in let prefix = pkg ^ "." in
match match
List.find_opt (fun e -> starts_with ~prefix (Fpath.basename e)) entries List.find_opt
(fun e -> String.starts_with ~prefix (Fpath.basename e))
entries
with with
| None -> msg "no opam metadata for %s" pkg | None -> msg "no opam metadata for %s" pkg
| Some e -> | Some e ->
@@ -125,7 +122,8 @@ let license_file ~switch pkg =
(List.find_opt (List.find_opt
(fun e -> (fun e ->
let n = String.uppercase_ascii (Fpath.basename e) in let n = String.uppercase_ascii (Fpath.basename e) in
starts_with ~prefix:"LICENSE" n || starts_with ~prefix:"COPYING" n) String.starts_with ~prefix:"LICENSE" n
|| String.starts_with ~prefix:"COPYING" n)
entries) entries)
let run file = let run file =
@@ -139,7 +137,8 @@ let run file =
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs) map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
in in
let packages = List.sort_uniq compare owners in let packages = List.sort_uniq compare owners in
if packages = [] then msg "no library found in %s" file else if packages = [] then msg "no library found in %s" file
else
let* infos = let* infos =
map_result map_result
(fun p -> (fun p ->