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)
let ocamlfind args = Cmd.(v "ocamlfind" %% of_list args)
let lines cmd =
let* l = OS.Cmd.(run_out cmd |> to_lines) in
Ok (List.map String.trim l)
@@ -34,14 +35,7 @@ let line cmd =
(* Windows writes paths with backslashes (doubled in the .changes files, where
they are escaped), and drive letters in either case. *)
let slashes = Re.compile (Re.rep1 (Re.set "/\\"))
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 normalize s = String.lowercase_ascii (Re.replace_string slashes ~by:"/" s)
let libraries_re = Re.Perl.compile_pat "\\(libraries([^)]*)\\)"
(* 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* 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
let top = List.hd (String.split_on_char '.' lib) in
let meta =
@@ -98,7 +93,9 @@ let package_info ~switch pkg =
let* entries = sorted_entries Fpath.(switch / ".opam-switch" / "packages") in
let prefix = pkg ^ "." in
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
| None -> msg "no opam metadata for %s" pkg
| Some e ->
@@ -125,7 +122,8 @@ let license_file ~switch pkg =
(List.find_opt
(fun e ->
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)
let run file =
@@ -139,7 +137,8 @@ let run file =
map_result (owner ~stdlib ~changes) (List.sort_uniq compare libs)
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 =
map_result
(fun p ->