Skip to content

Commit d8a73c3

Browse files
committed
Add JSON toplevel index
Signed-off-by: Etienne Millon <me@emillon.org>
1 parent dc61593 commit d8a73c3

File tree

5 files changed

+58
-13
lines changed

5 files changed

+58
-13
lines changed

otherlibs/chrome-trace/src/chrome_trace.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Json = struct
99
| `List of t list
1010
| `Bool of bool
1111
| `Assoc of (string * t) list
12+
| `Null
1213
]
1314
end
1415

otherlibs/chrome-trace/src/chrome_trace.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Json : sig
1919
| `List of t list
2020
| `Bool of bool
2121
| `Assoc of (string * t) list
22+
| `Null
2223
]
2324
end
2425

src/dune_rules/odoc.ml

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ module Output_format = struct
141141
| Html -> Alias.make Alias0.doc ~dir
142142
| Json -> Alias.make Alias0.doc_json ~dir
143143
;;
144+
145+
let toplevel_index_path format ctx =
146+
let base = Paths.toplevel_index ctx in
147+
match format with
148+
| Html -> base
149+
| Json -> Path.Build.extend_basename base ~suffix:".json"
150+
;;
144151
end
145152

146153
module Dep : sig
@@ -501,14 +508,46 @@ module Toplevel_index = struct
501508
Paths.odoc_support_dirname
502509
(html_list_items t)
503510
;;
511+
512+
let string_to_json s = `String s
513+
let list_to_json ~f l = `List (List.map ~f l)
514+
515+
let option_to_json ~f = function
516+
| None -> `Null
517+
| Some x -> f x
518+
;;
519+
520+
let item_to_json { name; version; link } =
521+
`Assoc
522+
[ "name", string_to_json name
523+
; "version", option_to_json ~f:string_to_json version
524+
; "link", string_to_json link
525+
]
526+
;;
527+
528+
(** This format is public API. *)
529+
let to_json items = `Assoc [ "packages", list_to_json items ~f:item_to_json ]
530+
531+
let json t = Dune_stats.Json.to_string (to_json t)
532+
533+
let content (output : Output_format.t) t =
534+
match output with
535+
| Html -> html t
536+
| Json -> json t
537+
;;
504538
end
505539

506-
let setup_toplevel_index_rule sctx =
540+
let setup_toplevel_index_rule sctx output =
507541
let* packages = Only_packages.get () in
508542
let index = Toplevel_index.of_packages packages in
509-
let html = Toplevel_index.html index in
543+
let content = Toplevel_index.content output index in
510544
let ctx = Super_context.context sctx in
511-
add_rule sctx (Action_builder.write_file (Paths.toplevel_index ctx) html)
545+
let path = Output_format.toplevel_index_path output ctx in
546+
add_rule sctx (Action_builder.write_file path content)
547+
;;
548+
549+
let setup_toplevel_index_rules sctx =
550+
Output_format.iter ~f:(setup_toplevel_index_rule sctx)
512551
;;
513552

514553
let libs_of_pkg ctx ~pkg =
@@ -568,11 +607,6 @@ let create_odoc ctx ~target odoc_file =
568607
{ odoc_file; odocl_file; html_file = file Html; json_file = file Json }
569608
;;
570609

571-
let static_html ctx =
572-
let open Paths in
573-
[ odoc_support ctx; toplevel_index ctx ]
574-
;;
575-
576610
let check_mlds_no_dupes ~pkg ~mlds =
577611
match
578612
List.rev_map mlds ~f:(fun mld ->
@@ -702,12 +736,13 @@ let out_file (output : Output_format.t) odoc =
702736
let out_files ctx (output : Output_format.t) odocs =
703737
let extra_files =
704738
match output with
705-
| Html -> List.map ~f:Path.build (static_html ctx)
739+
| Html -> [ Path.build (Paths.odoc_support ctx) ]
706740
| Json -> []
707741
in
708-
List.rev_append
709-
extra_files
710-
(List.map odocs ~f:(fun odoc -> Path.build (out_file output odoc)))
742+
Path.build (Output_format.toplevel_index_path output ctx)
743+
:: List.rev_append
744+
extra_files
745+
(List.map odocs ~f:(fun odoc -> Path.build (out_file output odoc)))
711746
;;
712747

713748
let setup_lib_html_rules_def =
@@ -911,7 +946,7 @@ let gen_rules sctx ~dir rest =
911946
| [ "_html" ] ->
912947
let ctx = Super_context.context sctx in
913948
let directory_targets = Path.Build.Map.singleton (Paths.odoc_support ctx) Loc.none in
914-
has_rules ~directory_targets (setup_css_rule sctx >>> setup_toplevel_index_rule sctx)
949+
has_rules ~directory_targets (setup_css_rule sctx >>> setup_toplevel_index_rules sctx)
915950
| [ "_mlds"; pkg ] ->
916951
with_package pkg ~f:(fun pkg ->
917952
let* _mlds, rules = package_mlds sctx ~pkg in

src/dune_stats/dune_stats.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ module Json = struct
6868
Buffer.add_char buf '{';
6969
object_body_to_buf o buf;
7070
Buffer.add_char buf '}'
71+
| `Null -> Buffer.add_string buf "null"
7172

7273
and array_body_to_buf t buf =
7374
match t with

test/blackbox-tests/test-cases/odoc/doc-json.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,22 @@
2222

2323
$ dune build @doc-json
2424
$ list_docs
25+
_build/default/_doc/_html/index.html.json
2526
_build/default/_doc/_html/l/L/M/index.html.json
2627
_build/default/_doc/_html/l/L/index.html.json
2728
_build/default/_doc/_html/l/index.html.json
2829

30+
The toplevel index is generated by dune itself:
31+
32+
$ cat _build/default/_doc/_html/index.html.json
33+
{"packages":[{"name":"l","version":null,"link":"l/index.html"}]}
34+
2935
@doc will continue generating doc as usual:
3036

3137
$ dune build @doc
3238
$ list_docs
3339
_build/default/_doc/_html/index.html
40+
_build/default/_doc/_html/index.html.json
3441
_build/default/_doc/_html/l/L/M/index.html
3542
_build/default/_doc/_html/l/L/M/index.html.json
3643
_build/default/_doc/_html/l/L/index.html

0 commit comments

Comments
 (0)