Skip to content

Commit e01fe4b

Browse files
committed
feat: Run "rm -fr ./_learn-ocaml-build/exercises" before the build
Update CLI doc accordingly
1 parent d8e9f1b commit e01fe4b

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

src/main/learnocaml_main.ml

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ let absolute_filename path =
3030
then Filename.concat (Sys.getcwd ()) path
3131
else path
3232

33+
let dflt_build_dir = "_learn-ocaml-build"
34+
3335
module Args = struct
3436
open Arg
3537

@@ -51,9 +53,14 @@ module Args = struct
5153
tutorials."
5254

5355
let build_dir =
54-
value & opt dir "./_learn-ocaml-build" & info ["build-dir"] ~docs ~docv:"DIR" ~doc:
55-
"Directory where the repository exercises should be copied and precompiled. \
56-
Giving the same path as `--repo` is a valid value for `--build-dir`."
56+
value & opt dir ("./" ^ dflt_build_dir) & info ["build-dir"] ~docs ~docv:"DIR" ~doc:
57+
(Printf.sprintf
58+
"Directory where the repo exercises are copied and precompiled. \
59+
When $(docv) takes its default value (e.g. when it is omitted in CLI), \
60+
'$(b,learn-ocaml build)' first erases the '$(docv)/exercises' subfolder. \
61+
Note that the default value for $(docv), './%s', is generally a sensible choice. \
62+
But passing the same argument as the one for $(i,--repo) is also a valid value for $(docv)."
63+
dflt_build_dir)
5764

5865
let app_dir =
5966
value & opt string "./www" & info ["app-dir"; "o"] ~docs ~docv:"DIR" ~doc:
@@ -344,20 +351,39 @@ let main o =
344351
let copy_build_exercises o =
345352
(* NOTE: if `--build` = `--repo`, then no copy is needed.
346353
Before checking path equality, we need to get canonical paths *)
347-
let repo_dir = readlink o.repo_dir / "exercises" in
348-
let build_dir = readlink o.build_dir / "exercises" in
349-
if repo_dir <> build_dir then begin
354+
let repo_exos_dir = readlink o.repo_dir / "exercises" in
355+
let build_exos_dir = readlink o.build_dir / "exercises" in
356+
if repo_exos_dir <> build_exos_dir then begin
357+
(* NOTE: if the CLI arg is "./_learn-ocaml-build" or "_learn-ocaml-build"
358+
then the exercises subdirectory is erased beforehand *)
359+
begin
360+
if (o.build_dir = dflt_build_dir || o.build_dir = "./" ^ dflt_build_dir)
361+
&& Sys.file_exists build_exos_dir then
362+
Lwt.catch (fun () ->
363+
Lwt_process.exec ("rm",[|"rm";"-rf"; build_exos_dir|]) >>= fun r ->
364+
if r <> Unix.WEXITED 0 then
365+
Lwt.fail_with "Removing previous build-dir failed"
366+
else Lwt.return_unit)
367+
(fun ex ->
368+
Printf.eprintf
369+
"Error: while removing previous build-dir \
370+
%s:\n %s\n%!"
371+
build_exos_dir (Printexc.to_string ex);
372+
exit 1)
373+
else
374+
Lwt.return_unit
375+
end >>= fun () ->
350376
Printf.printf "Populating %s\n%!" (o.build_dir / "exercises");
351377
(* NOTE: we choose to reuse Lwt_utils.copy_tree,
352378
even if we could use "rsync" (upside: "--delete-delay",
353379
but downside: would require the availability of rsync). *)
354380
Lwt.catch
355-
(fun () -> Lwt_utils.copy_tree repo_dir build_dir)
381+
(fun () -> Lwt_utils.copy_tree repo_exos_dir build_exos_dir)
356382
(function
357383
| Failure _ ->
358384
Lwt.fail_with @@ Printf.sprintf
359385
"Failed to copy repo exercises to %s"
360-
(build_dir)
386+
(build_exos_dir)
361387
| e -> Lwt.fail e)
362388
(* NOTE: no chown is needed,
363389
but we may want to run "chmod -R u+w exercises"

0 commit comments

Comments
 (0)