feat(utils): add File.ocamlformat_file_opt

This commit is contained in:
Emile Rolley 2022-07-21 15:51:29 +02:00
parent 7ad81158eb
commit 677ff92ae6
3 changed files with 13 additions and 1 deletions

View File

@ -338,7 +338,8 @@ let driver source_file (options : Cli.options) : int =
Cli.debug_print "Compiling program into OCaml...";
Cli.debug_print "Writing to %s..."
(Option.value ~default:"stdout" output_file);
Lcalc.To_ocaml.format_program fmt prgm type_ordering
Lcalc.To_ocaml.format_program fmt prgm type_ordering;
File.ocamlformat_file_opt output_file
| `Plugin (Plugin.Lcalc p) ->
let output_file, _ = get_output ~ext:p.Plugin.extension () in
Cli.debug_print "Compiling program through backend \"%s\"..."

View File

@ -45,3 +45,10 @@ let with_formatter_of_opt_file filename_opt f =
match filename_opt with
| None -> finally (fun () -> flush stdout) (fun () -> f Format.std_formatter)
| Some filename -> with_formatter_of_file filename f
let ocamlformat_file_opt = function
| Some f ->
Cli.debug_print "Formatting %s..." f;
if Sys.command (Printf.sprintf "ocamlformat %s -i" f) <> 0 then
failwith ("Internal error: ocamlformat failed on " ^ f)
| None -> ()

View File

@ -40,3 +40,7 @@ val with_formatter_of_opt_file : string option -> (Format.formatter -> 'a) -> 'a
(** [with_formatter_of_opt_file filename_opt f] manages the formatter created
from the file [filename_opt] if there is some (see
{!with_formatter_of_file}), otherwise, uses the [Format.std_formatter]. *)
(** {2 Utility functions on files} *)
val ocamlformat_file_opt : string option -> unit