forked from
anil.recoil.org/monopam-myspace
My aggregated monorepo of OCaml code, automaintained
1(** Read package data from day10's html directory *)
2
3let list_package_names ~html_dir =
4 let p_dir = Filename.concat html_dir "p" in
5 if Sys.file_exists p_dir && Sys.is_directory p_dir then
6 Sys.readdir p_dir
7 |> Array.to_list
8 |> List.filter (fun name ->
9 let path = Filename.concat p_dir name in
10 Sys.is_directory path)
11 |> List.sort String.compare
12 else
13 []
14
15let compare_versions v1 v2 =
16 (* Simple version comparison - compare segments numerically where possible *)
17 let parse v =
18 String.split_on_char '.' v
19 |> List.map (fun s -> try `Int (int_of_string s) with _ -> `Str s)
20 in
21 let rec cmp l1 l2 = match l1, l2 with
22 | [], [] -> 0
23 | [], _ -> -1
24 | _, [] -> 1
25 | `Int a :: t1, `Int b :: t2 ->
26 let c = Int.compare a b in if c <> 0 then c else cmp t1 t2
27 | `Str a :: t1, `Str b :: t2 ->
28 let c = String.compare a b in if c <> 0 then c else cmp t1 t2
29 | `Int _ :: _, `Str _ :: _ -> -1
30 | `Str _ :: _, `Int _ :: _ -> 1
31 in
32 cmp (parse v2) (parse v1) (* Descending order *)
33
34let list_package_versions ~html_dir ~name =
35 let pkg_dir = Filename.concat (Filename.concat html_dir "p") name in
36 if Sys.file_exists pkg_dir && Sys.is_directory pkg_dir then
37 Sys.readdir pkg_dir
38 |> Array.to_list
39 |> List.filter (fun version ->
40 let path = Filename.concat pkg_dir version in
41 Sys.is_directory path)
42 |> List.sort compare_versions
43 else
44 []
45
46let list_packages ~html_dir =
47 list_package_names ~html_dir
48 |> List.concat_map (fun name ->
49 list_package_versions ~html_dir ~name
50 |> List.map (fun version -> (name, version)))
51
52let package_has_docs ~html_dir ~name ~version =
53 let path = Filename.concat html_dir
54 (Filename.concat "p" (Filename.concat name version)) in
55 Sys.file_exists path && Sys.is_directory path
56
57let docs_path ~name ~version =
58 Printf.sprintf "/docs/p/%s/%s/" name version