Port tmp files to use Path.t
This commit is contained in:
parent
e310f17237
commit
729e85716c
|
@ -49,22 +49,21 @@ type purpose =
|
||||||
| Build_job of Path.t list
|
| Build_job of Path.t list
|
||||||
|
|
||||||
module Temp = struct
|
module Temp = struct
|
||||||
let tmp_files = ref String.Set.empty
|
let tmp_files = ref Path.Set.empty
|
||||||
let () =
|
let () =
|
||||||
at_exit (fun () ->
|
at_exit (fun () ->
|
||||||
let fns = !tmp_files in
|
let fns = !tmp_files in
|
||||||
tmp_files := String.Set.empty;
|
tmp_files := Path.Set.empty;
|
||||||
String.Set.iter fns ~f:(fun fn ->
|
Path.Set.iter fns ~f:Path.unlink_no_err)
|
||||||
try Sys.force_remove fn with _ -> ()))
|
|
||||||
|
|
||||||
let create prefix suffix =
|
let create prefix suffix =
|
||||||
let fn = Filename.temp_file prefix suffix in
|
let fn = Path.of_string (Filename.temp_file prefix suffix) in
|
||||||
tmp_files := String.Set.add !tmp_files fn;
|
tmp_files := Path.Set.add !tmp_files fn;
|
||||||
fn
|
fn
|
||||||
|
|
||||||
let destroy fn =
|
let destroy fn =
|
||||||
(try Sys.force_remove fn with Sys_error _ -> ());
|
Path.unlink_no_err fn;
|
||||||
tmp_files := String.Set.remove !tmp_files fn
|
tmp_files := Path.Set.remove !tmp_files fn
|
||||||
end
|
end
|
||||||
|
|
||||||
module Fancy = struct
|
module Fancy = struct
|
||||||
|
@ -234,7 +233,7 @@ let run_internal ?dir ?(stdout_to=Terminal) ?(stderr_to=Terminal) ~env ~purpose
|
||||||
match stdout_to, stderr_to with
|
match stdout_to, stderr_to with
|
||||||
| (Terminal, _ | _, Terminal) when !Clflags.capture_outputs ->
|
| (Terminal, _ | _, Terminal) when !Clflags.capture_outputs ->
|
||||||
let fn = Temp.create "jbuilder" ".output" in
|
let fn = Temp.create "jbuilder" ".output" in
|
||||||
let fd = Unix.openfile fn [O_WRONLY; O_SHARE_DELETE] 0 in
|
let fd = Unix.openfile (Path.to_string fn) [O_WRONLY; O_SHARE_DELETE] 0 in
|
||||||
(Some fn, fd, fd, Some fd)
|
(Some fn, fd, fd, Some fd)
|
||||||
| _ ->
|
| _ ->
|
||||||
(None, Unix.stdout, Unix.stderr, None)
|
(None, Unix.stdout, Unix.stderr, None)
|
||||||
|
@ -259,7 +258,7 @@ let run_internal ?dir ?(stdout_to=Terminal) ?(stderr_to=Terminal) ~env ~purpose
|
||||||
match output_filename with
|
match output_filename with
|
||||||
| None -> ""
|
| None -> ""
|
||||||
| Some fn ->
|
| Some fn ->
|
||||||
let s = Io.read_file (Path.of_string fn) in
|
let s = Io.read_file fn in
|
||||||
Temp.destroy fn;
|
Temp.destroy fn;
|
||||||
let len = String.length s in
|
let len = String.length s in
|
||||||
if len > 0 && s.[len - 1] <> '\n' then
|
if len > 0 && s.[len - 1] <> '\n' then
|
||||||
|
@ -327,20 +326,18 @@ let run ?dir ?stdout_to ?stderr_to ~env ?(purpose=Internal_job) fail_mode
|
||||||
let run_capture_gen ?dir ~env ?(purpose=Internal_job) fail_mode prog args ~f =
|
let run_capture_gen ?dir ~env ?(purpose=Internal_job) fail_mode prog args ~f =
|
||||||
let fn = Temp.create "jbuild" ".output" in
|
let fn = Temp.create "jbuild" ".output" in
|
||||||
map_result fail_mode
|
map_result fail_mode
|
||||||
(run_internal ?dir ~stdout_to:(File fn) ~env ~purpose fail_mode prog args)
|
(run_internal ?dir ~stdout_to:(File (Path.to_string fn))
|
||||||
|
~env ~purpose fail_mode prog args)
|
||||||
~f:(fun () ->
|
~f:(fun () ->
|
||||||
let x = f fn in
|
let x = f fn in
|
||||||
Temp.destroy fn;
|
Temp.destroy fn;
|
||||||
x)
|
x)
|
||||||
|
|
||||||
let run_capture =
|
let run_capture = run_capture_gen ~f:Io.read_file
|
||||||
run_capture_gen ~f:(fun p -> Io.read_file (Path.of_string p))
|
let run_capture_lines = run_capture_gen ~f:Io.lines_of_file
|
||||||
let run_capture_lines =
|
|
||||||
run_capture_gen ~f:(fun p -> Io.lines_of_file (Path.of_string p))
|
|
||||||
|
|
||||||
let run_capture_line ?dir ~env ?(purpose=Internal_job) fail_mode prog args =
|
let run_capture_line ?dir ~env ?(purpose=Internal_job) fail_mode prog args =
|
||||||
run_capture_gen ?dir ~env ~purpose fail_mode prog args ~f:(fun fn ->
|
run_capture_gen ?dir ~env ~purpose fail_mode prog args ~f:(fun fn ->
|
||||||
let fn = Path.of_string fn in
|
|
||||||
match Io.lines_of_file fn with
|
match Io.lines_of_file fn with
|
||||||
| [x] -> x
|
| [x] -> x
|
||||||
| l ->
|
| l ->
|
||||||
|
|
Loading…
Reference in New Issue