package odoc-driver

  1. Overview
  2. Docs

Source file odoc.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
open Bos

module Id : sig
  type t
  val to_fpath : t -> Fpath.t
  val of_fpath : Fpath.t -> t
  val to_string : t -> string
end = struct
  type t = Fpath.t

  let to_fpath id = id

  let of_fpath id = id |> Fpath.normalize |> Fpath.rem_empty_seg
  (* If an odoc path ends with a [/] everything breaks *)

  let to_string id = match Fpath.to_string id with "." -> "" | v -> v
end

let index_filename = "index.odoc-index"

let sidebar_filename = "sidebar.odoc-sidebar"

type compile_deps = { digest : Digest.t; deps : (string * Digest.t) list }

let odoc = ref (Cmd.v "odoc")

let odoc_md = ref (Cmd.v "odoc-md")

let compile_deps f =
  let cmd = Cmd.(!odoc % "compile-deps" % Fpath.to_string f) in
  let desc = Printf.sprintf "Compile deps for %s" (Fpath.to_string f) in
  let deps = Cmd_outputs.submit None desc cmd None in
  let l = List.filter_map (Astring.String.cut ~sep:" ") deps in
  let basename = Fpath.(basename (f |> rem_ext)) |> String.capitalize_ascii in
  match List.partition (fun (n, _) -> basename = n) l with
  | [ (_, digest) ], deps -> Ok { digest; deps }
  | _ -> Error (`Msg "odd")

let compile ~output_dir ~input_file:file ~includes ~warnings_tag ~parent_id
    ~ignore_output =
  let open Cmd in
  let includes =
    Fpath.Set.fold
      (fun path acc -> Cmd.(acc % "-I" % p path))
      includes Cmd.empty
  in

  let output_file =
    let _, f = Fpath.split_base file in
    Some Fpath.(output_dir // Id.to_fpath parent_id // set_ext "odoc" f)
  in
  let cmd =
    !odoc % "compile" % Fpath.to_string file % "--output-dir" % p output_dir
    %% includes % "--enable-missing-root-warning"
  in
  let cmd = cmd % "--parent-id" % Id.to_string parent_id in
  let cmd =
    match warnings_tag with
    | None -> cmd
    | Some tag -> cmd % "--warnings-tag" % tag
  in
  let desc = Printf.sprintf "Compiling %s" (Fpath.to_string file) in
  let log =
    if ignore_output then None else Some (`Compile, Fpath.to_string file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd output_file

let compile_md ~output_dir ~input_file:file ~parent_id =
  let open Cmd in
  let output_file =
    let _, f = Fpath.split_base file in
    Some Fpath.(output_dir // Id.to_fpath parent_id // set_ext "odoc" f)
  in
  let cmd = !odoc_md % p file % "--output-dir" % p output_dir in
  let cmd =
    match Id.to_string parent_id with "" -> cmd | x -> cmd % "--parent-id" % x
  in
  let desc = Printf.sprintf "Compiling Markdown %s" (Fpath.to_string file) in
  let _lines =
    Cmd_outputs.submit
      (Some (`Compile, Fpath.to_string file))
      desc cmd output_file
  in
  ()

let compile_asset ~output_dir ~name ~parent_id =
  let open Cmd in
  let output_file =
    Some
      Fpath.(output_dir // Id.to_fpath parent_id / ("asset-" ^ name ^ ".odoc"))
  in
  let cmd =
    !odoc % "compile-asset" % "--name" % name % "--output-dir" % p output_dir
  in

  let cmd = cmd % "--parent-id" % Id.to_string parent_id in
  let desc = Printf.sprintf "Compiling %s" name in
  ignore @@ Cmd_outputs.submit (Some (`Compile, name)) desc cmd output_file

let compile_impl ~output_dir ~input_file:file ~includes ~parent_id ~source_id =
  let open Cmd in
  let includes =
    Fpath.Set.fold
      (fun path acc -> Cmd.(acc % "-I" % p path))
      includes Cmd.empty
  in
  let cmd =
    !odoc % "compile-impl" % Fpath.to_string file % "--output-dir"
    % p output_dir %% includes % "--enable-missing-root-warning"
  in
  let output_file =
    let _, f = Fpath.split_base file in
    Some
      Fpath.(
        output_dir // Id.to_fpath parent_id
        / ("impl-" ^ to_string (set_ext "odoc" f)))
  in
  let cmd = cmd % "--parent-id" % Id.to_string parent_id in
  let cmd = cmd % "--source-id" % Id.to_string source_id in
  let desc =
    Printf.sprintf "Compiling implementation %s" (Fpath.to_string file)
  in
  ignore
  @@ Cmd_outputs.submit
       (Some (`Compile, Fpath.to_string file))
       desc cmd output_file

let doc_args docs =
  let open Cmd in
  List.fold_left
    (fun acc (pkg_name, path) ->
      let s = Format.asprintf "%s:%a" pkg_name Fpath.pp path in
      v "-P" % s %% acc)
    Cmd.empty docs

let lib_args libs =
  let open Cmd in
  List.fold_left
    (fun acc (libname, path) ->
      let s = Format.asprintf "%s:%a" libname Fpath.pp path in
      v "-L" % s %% acc)
    Cmd.empty libs

let link ?(ignore_output = false) ~custom_layout ~input_file:file ?output_file
    ~docs ~libs ~includes ~warnings_tags ?current_package () =
  let open Cmd in
  let output_file =
    match output_file with Some f -> f | None -> Fpath.set_ext "odocl" file
  in
  let docs = doc_args docs in
  let libs = lib_args libs in
  let includes =
    List.fold_left (fun acc i -> acc % "-I" % p i) empty includes
  in
  let current_package =
    match current_package with
    | None -> Cmd.empty
    | Some c -> Cmd.(v "--current-package" % c)
  in
  let cmd =
    !odoc % "link" % p file % "-o" % p output_file %% docs %% libs %% includes
    %% current_package % "--enable-missing-root-warning"
  in
  let cmd =
    if Fpath.to_string file = "stdlib.odoc" then cmd % "--open=\"\"" else cmd
  in
  let cmd =
    List.fold_left (fun acc k -> acc % "--warnings-tags" % k) cmd warnings_tags
  in
  let desc = Printf.sprintf "Linking %s" (Fpath.to_string file) in
  let cmd = if custom_layout then cmd % "--custom-layout" else cmd in
  let log =
    if ignore_output then None else Some (`Link, Fpath.to_string file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd (Some output_file)

let compile_index ?(ignore_output = false) ~output_file ?occurrence_file ~json
    ~roots ~simplified ~wrap () =
  let roots =
    List.fold_left (fun c r -> Cmd.(c % "--root" % p r)) Cmd.empty roots
  in
  let json = if json then Cmd.v "--json" else Cmd.empty in
  let simplified =
    if simplified then Cmd.v "--simplified-json" else Cmd.empty
  in
  let wrap = if wrap then Cmd.v "--wrap-json" else Cmd.empty in
  let occ =
    match occurrence_file with
    | None -> Cmd.empty
    | Some f -> Cmd.(v "--occurrences" % p f)
  in
  let cmd =
    Cmd.(
      !odoc % "compile-index" %% json %% simplified %% wrap %% v "-o"
      % p output_file %% roots %% occ)
  in
  let desc =
    Printf.sprintf "Generating index for %s" (Fpath.to_string output_file)
  in
  let log =
    if ignore_output then None else Some (`Index, Fpath.to_string output_file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd (Some output_file)

let sidebar_generate ?(ignore_output = false) ~output_file ~json input_file () =
  let json = if json then Cmd.v "--json" else Cmd.empty in
  let cmd =
    Cmd.(
      !odoc % "sidebar-generate" %% json %% v "-o" % p output_file
      % p input_file)
  in
  let desc =
    Printf.sprintf "Generating sidebar for %s" (Fpath.to_string output_file)
  in
  let log =
    if ignore_output then None else Some (`Generate, Fpath.to_string output_file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd (Some output_file)

let html_generate ~output_dir ?sidebar ?(ignore_output = false)
    ?(search_uris = []) ?remap ?(as_json = false) ?home_breadcrumb
    ~input_file:file () =
  let open Cmd in
  let index =
    match sidebar with None -> empty | Some idx -> v "--sidebar" % p idx
  in
  let home_breadcrumb =
    match home_breadcrumb with
    | None -> empty
    | Some name -> v "--home-breadcrumb" % name
  in
  let search_uris =
    List.fold_left
      (fun acc filename -> acc % "--search-uri" % p filename)
      empty search_uris
  in
  let cmd =
    !odoc % "html-generate" % p file %% index %% search_uris % "-o" % output_dir
    %% home_breadcrumb
  in
  let cmd =
    match remap with None -> cmd | Some f -> cmd % "--remap-file" % p f
  in
  let cmd = if as_json then cmd % "--as-json" else cmd in
  let desc = Printf.sprintf "Generating HTML for %s" (Fpath.to_string file) in
  let log =
    if ignore_output then None else Some (`Generate, Fpath.to_string file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd None

let html_generate_asset ~output_dir ?(ignore_output = false) ?home_breadcrumb
    ~input_file:file ~asset_path () =
  let open Cmd in
  let home_breadcrumb =
    match home_breadcrumb with
    | None -> empty
    | Some name -> v "--home-breadcrumb" % name
  in
  let cmd =
    !odoc % "html-generate-asset" % "-o" % output_dir % "--asset-unit" % p file
    % p asset_path %% home_breadcrumb
  in
  let desc = Printf.sprintf "Copying asset %s" (Fpath.to_string file) in
  let log =
    if ignore_output then None else Some (`Generate, Fpath.to_string file)
  in
  ignore @@ Cmd_outputs.submit log desc cmd None

let html_generate_source ~output_dir ?(ignore_output = false) ~source ?sidebar
    ?(search_uris = []) ?(as_json = false) ?home_breadcrumb ~input_file:file ()
    =
  let open Cmd in
  let file = v "--impl" % p file in
  let sidebar =
    match sidebar with None -> empty | Some idx -> v "--sidebar" % p idx
  in
  let home_breadcrumb =
    match home_breadcrumb with
    | None -> empty
    | Some name -> v "--home-breadcrumb" % name
  in
  let search_uris =
    List.fold_left
      (fun acc filename -> acc % "--search-uri" % p filename)
      empty search_uris
  in
  let cmd =
    !odoc % "html-generate-source" %% file %% sidebar % p source %% search_uris
    % "-o" % output_dir %% home_breadcrumb
  in
  let cmd = if as_json then cmd % "--as-json" else cmd in

  let desc = Printf.sprintf "Generating HTML for %s" (Fpath.to_string source) in
  let log =
    if ignore_output then None else Some (`Generate, Fpath.to_string source)
  in
  ignore @@ Cmd_outputs.submit log desc cmd None

let support_files path =
  let open Cmd in
  let cmd = !odoc % "support-files" % "-o" % Fpath.to_string path in
  let desc = "Generating support files" in
  Cmd_outputs.submit None desc cmd None

let count_occurrences ~input ~output =
  let open Cmd in
  let input = Cmd.of_values Fpath.to_string input in
  let output_c = v "-o" % p output in
  let cmd = !odoc % "count-occurrences" %% input %% output_c in
  let desc = "Counting occurrences" in
  let log = Some (`Count_occurrences, Fpath.to_string output) in
  ignore @@ Cmd_outputs.submit log desc cmd None

let classify dirs =
  let open Cmd in
  let cmd = List.fold_left (fun cmd d -> cmd % p d) (!odoc % "classify") dirs in
  let desc =
    Format.asprintf "Classifying [%a]" (Fmt.(list ~sep:sp) Fpath.pp) dirs
  in
  let log =
    Some (`Classify, String.concat "," (List.map Fpath.to_string dirs))
  in
  let lines =
    Cmd_outputs.submit log desc cmd None |> List.filter (fun l -> l <> "")
  in
  List.map
    (fun line ->
      match String.split_on_char ' ' line with
      | name :: modules -> (name, modules)
      | _ -> failwith "bad classify output")
    lines
OCaml

Innovation. Community. Security.