From b826340d7536c2777fdcafb3bfcc6ebc8d8053ee Mon Sep 17 00:00:00 2001 From: Marc Coquand Date: Tue, 26 Dec 2023 11:15:33 -0600 Subject: Support output file --- bin/main.ml | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'bin/main.ml') diff --git a/bin/main.ml b/bin/main.ml index d644133..43ce56b 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -7,7 +7,26 @@ let parse (s : string) = ast -let fixgen content output_type () = +let print_stdout = function + | [ (_, content) ] -> print_endline content + | list -> + list + |> List.iter (fun (name, content) -> + print_endline ("FILE NAME: " ^ name); + print_endline content) + + +let file_name name file_format = name ^ "." ^ file_format + +let save_files file_format output_folder list = + list + |> List.iter (fun (name, content) -> + let file = open_out (output_folder ^ "/" ^ file_name name file_format) in + output_string file content; + close_out file) + + +let fixgen content output_type output_folder = let result = parse content in let generate_output = match output_type with @@ -15,13 +34,18 @@ let fixgen content output_type () = | "json" -> Fixture.json_of_generated_fixtures | _ -> failwith "Unsupported output type, supported types are: json, csv" in + let save_output = + match output_folder with + | "STDOUT" -> print_stdout + | s -> save_files output_type s + in match result with | Some ast -> Ast_types.compile ast - |> List.iter (fun (name, file) -> + |> List.map (fun (name, file) -> let result = generate_output file in - print_endline ("FILE NAME: " ^ name); - print_endline result) + name, result) + |> save_output | None -> print_endline "error" @@ -31,12 +55,17 @@ let file = (* TODO: Support option to set output folder *) +let output_folder = + let doc = "Set output folder" in + Arg.( + value & opt string "STDOUT" & info [ "o"; "output-folder" ] ~docv:"OUTPUT FOLDER" ~doc) + let output_type = let doc = "Set output file type, supported=csv" in Arg.(value & opt string "csv" & info [ "f"; "file-type" ] ~docv:"FILE TYPE" ~doc) -let fixgen_t = Term.(const fixgen $ file $ output_type $ const ()) +let fixgen_t = Term.(const fixgen $ file $ output_type $ output_folder) let cmd = Cmd.v (Cmd.info "fixgen") fixgen_t let () = exit (Cmd.eval cmd) -- cgit v1.2.3