Skip to content

Commit d49a7c5

Browse files
panglesdjonludlam
authored andcommitted
Compute missing output path from input name in odoc source-tree
To be more consistent with other compile-like commands. In case no output path is provided, use the same as input, changing the extension to `.odoc` and adding a `src-` if necessary. Signed-off-by: Paul-Elliot <[email protected]>
1 parent ede2bf3 commit d49a7c5

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/odoc/bin/main.ml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,26 @@ end = struct
290290
end
291291

292292
module Source_tree = struct
293+
let has_src_prefix input =
294+
input |> Fs.File.basename |> Fs.File.to_string
295+
|> Astring.String.is_prefix ~affix:"src-"
296+
297+
let output_file ~output ~input =
298+
match output with
299+
| Some output -> output
300+
| None ->
301+
let output =
302+
if not (has_src_prefix input) then
303+
let directory = Fs.File.dirname input in
304+
let name = input |> Fs.File.basename |> Fs.File.to_string in
305+
let name = "src-" ^ name in
306+
Fs.File.create ~directory ~name
307+
else input
308+
in
309+
Fs.File.(set_ext ".odoc" output)
310+
293311
let compile_source_tree directories output parent input warnings_options =
312+
let output = output_file ~output ~input in
294313
let resolver =
295314
Resolver.create ~important_digests:true ~directories ~open_modules:[]
296315
in
@@ -304,11 +323,9 @@ module Source_tree = struct
304323
let f = Fs.File.of_string s in
305324
if not (Fs.File.has_ext ".odoc" f) then
306325
Error (`Msg "Output file must have '.odoc' extension.")
307-
else
308-
let basename = Fs.File.to_string (Fs.File.basename f) in
309-
if not (Astring.String.is_prefix ~affix:"src-" basename) then
310-
Error (`Msg "Output file must be prefixed with 'src-'.")
311-
else Ok f
326+
else if not (has_src_prefix f) then
327+
Error (`Msg "Output file must be prefixed with 'src-'.")
328+
else Ok f
312329
| Error _ as e -> e
313330
and print = Fpath.pp in
314331
Arg.conv (parse, print)
@@ -327,7 +344,7 @@ module Source_tree = struct
327344
The basename must start with the prefix 'src-' and extension '.odoc'."
328345
in
329346
Arg.(
330-
required
347+
value
331348
& opt (some arg_page_output) None
332349
& info ~docs ~docv:"PATH" ~doc [ "o" ])
333350
in

test/sources/source_hierarchy.t/run.t

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ A page can have source children.
22

33
$ odoc compile -c module-a -c module-b -c src-source root.mld
44

5-
$ printf "lib/main.ml\nlib/b/b.ml\nlib/a/a.ml\n" > source_tree.map
6-
$ odoc source-tree -I . --parent page-root -o src-source.odoc source_tree.map
5+
$ printf "lib/main.ml\nlib/b/b.ml\nlib/a/a.ml\n" > source.map
6+
$ odoc source-tree -I . --parent page-root source.map
77

88
Compile the modules:
99

0 commit comments

Comments
 (0)