File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -48,3 +48,29 @@ let read_file f =
4848 Bytes. unsafe_to_string s
4949 with e ->
5050 failwith (Printf. sprintf " Cannot read content of %s.\n %s" f (Printexc. to_string e))
51+
52+ let write_file ~name ~contents =
53+ let ch = open_out_bin name in
54+ output_string ch contents;
55+ close_out ch
56+
57+ let remove_file file = try Sys. remove file with Sys_error _ -> ()
58+
59+ let gen_file file f =
60+ let f_tmp =
61+ Filename. temp_file_name
62+ ~temp_dir: (Filename. dirname file)
63+ (Filename. basename file)
64+ " .tmp"
65+ in
66+ try
67+ let res = f f_tmp in
68+ remove_file file;
69+ Sys. rename f_tmp file;
70+ res
71+ with exc ->
72+ remove_file f_tmp;
73+ raise exc
74+
75+ let with_intermediate_file name f =
76+ Fun. protect ~finally: (fun () -> remove_file name) (fun () -> f name)
Original file line number Diff line number Diff line change @@ -21,3 +21,9 @@ val find_in_path : string list -> string -> string option
2121val absolute_path : string -> string
2222
2323val read_file : string -> string
24+
25+ val write_file : name :string -> contents :string -> unit
26+
27+ val gen_file : string -> (string -> 'a ) -> 'a
28+
29+ val with_intermediate_file : string -> (string -> 'a ) -> 'a
You can’t perform that action at this time.
0 commit comments